[919] | 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 |
|
---|
[535] | 19 | package org.expeditee.io;
|
---|
| 20 |
|
---|
| 21 | import java.net.Authenticator;
|
---|
| 22 | import java.net.PasswordAuthentication;
|
---|
| 23 |
|
---|
| 24 | public class ProxyAuth extends Authenticator {
|
---|
| 25 |
|
---|
[1102] | 26 | public static final int MAX_ATTEMPTS = 5;
|
---|
| 27 |
|
---|
[536] | 28 | private String httpUser = null, httpPass = null, httpsUser = null, httpsPass = null;
|
---|
[538] | 29 | private int attempts = 0;
|
---|
[535] | 30 |
|
---|
| 31 | public ProxyAuth() {
|
---|
| 32 | }
|
---|
| 33 |
|
---|
| 34 | @Override
|
---|
| 35 | public PasswordAuthentication getPasswordAuthentication() {
|
---|
| 36 | // TODO: differentiate between HTTP and HTTPS proxies somehow
|
---|
| 37 | // currently just chooses whichever one is set, preferentially http
|
---|
| 38 | System.out.println("Authenticating");
|
---|
[538] | 39 | // stop it from breaking from a redirect loop
|
---|
| 40 | attempts++;
|
---|
[1102] | 41 | if(attempts > MAX_ATTEMPTS) {
|
---|
[538] | 42 | return null;
|
---|
| 43 | }
|
---|
| 44 | String user = httpUser != null ? httpUser : httpsUser;
|
---|
| 45 | char[] pass = httpPass != null ? httpPass.toCharArray() : (httpsPass != null ? httpsPass.toCharArray() : null);
|
---|
| 46 | if(user == null || user.length() == 0 || pass == null || pass.length == 0) {
|
---|
| 47 | return null;
|
---|
| 48 | }
|
---|
| 49 | return new PasswordAuthentication(user, pass);
|
---|
[535] | 50 | }
|
---|
| 51 |
|
---|
[542] | 52 | public void setup(String user, String pass) {
|
---|
[570] | 53 | // System.out.println("setup proxy");
|
---|
[535] | 54 | this.httpUser = user;
|
---|
| 55 | this.httpPass = pass;
|
---|
| 56 | this.httpsUser = user;
|
---|
| 57 | this.httpsPass = pass;
|
---|
[538] | 58 | attempts = 0;
|
---|
[535] | 59 | }
|
---|
| 60 | }
|
---|