1 | /**
|
---|
2 | * ProxyAuth.java
|
---|
3 | * Copyright (C) 2010 New Zealand Digital Library, http://expeditee.org
|
---|
4 | *
|
---|
5 | * This program is free software: you can redistribute it and/or modify
|
---|
6 | * it under the terms of the GNU General Public License as published by
|
---|
7 | * the Free Software Foundation, either version 3 of the License, or
|
---|
8 | * (at your option) any later version.
|
---|
9 | *
|
---|
10 | * This program is distributed in the hope that it will be useful,
|
---|
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
13 | * GNU General Public License for more details.
|
---|
14 | *
|
---|
15 | * You should have received a copy of the GNU General Public License
|
---|
16 | * along with this program. If not, see <http://www.gnu.org/licenses/>.
|
---|
17 | */
|
---|
18 |
|
---|
19 | package org.expeditee.io;
|
---|
20 |
|
---|
21 | import java.net.Authenticator;
|
---|
22 | import java.net.PasswordAuthentication;
|
---|
23 |
|
---|
24 | public class ProxyAuth extends Authenticator {
|
---|
25 |
|
---|
26 | private String httpUser = null, httpPass = null, httpsUser = null, httpsPass = null;
|
---|
27 | private int attempts = 0;
|
---|
28 |
|
---|
29 | public ProxyAuth() {
|
---|
30 | }
|
---|
31 |
|
---|
32 | @Override
|
---|
33 | public PasswordAuthentication getPasswordAuthentication() {
|
---|
34 | // TODO: differentiate between HTTP and HTTPS proxies somehow
|
---|
35 | // currently just chooses whichever one is set, preferentially http
|
---|
36 | System.out.println("Authenticating");
|
---|
37 | // stop it from breaking from a redirect loop
|
---|
38 | attempts++;
|
---|
39 | if(attempts > 5) {
|
---|
40 | return null;
|
---|
41 | }
|
---|
42 | String user = httpUser != null ? httpUser : httpsUser;
|
---|
43 | char[] pass = httpPass != null ? httpPass.toCharArray() : (httpsPass != null ? httpsPass.toCharArray() : null);
|
---|
44 | if(user == null || user.length() == 0 || pass == null || pass.length == 0) {
|
---|
45 | return null;
|
---|
46 | }
|
---|
47 | return new PasswordAuthentication(user, pass);
|
---|
48 | }
|
---|
49 |
|
---|
50 | public void setup(String user, String pass) {
|
---|
51 | // System.out.println("setup proxy");
|
---|
52 | this.httpUser = user;
|
---|
53 | this.httpPass = pass;
|
---|
54 | this.httpsUser = user;
|
---|
55 | this.httpsPass = pass;
|
---|
56 | attempts = 0;
|
---|
57 | }
|
---|
58 | }
|
---|