01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: *
17: */
18: package org.apache.ivy.util.url;
19:
20: import java.net.Authenticator;
21: import java.net.PasswordAuthentication;
22:
23: import org.apache.ivy.util.Credentials;
24: import org.apache.ivy.util.Message;
25:
26: /**
27: *
28: */
29: public final class IvyAuthenticator extends Authenticator {
30:
31: /**
32: * The sole instance.
33: */
34: public static final IvyAuthenticator INSTANCE = new IvyAuthenticator();
35:
36: /**
37: * Private c'tor to prevent instantiation. Also installs this as the default Authenticator to
38: * use by the JVM.
39: */
40: private IvyAuthenticator() {
41: // Install this as the default Authenticator object.
42: Authenticator.setDefault(this );
43: }
44:
45: // API ******************************************************************
46:
47: // Overriding Authenticator *********************************************
48:
49: protected PasswordAuthentication getPasswordAuthentication() {
50: PasswordAuthentication result = null;
51:
52: String proxyHost = System.getProperty("http.proxyHost");
53: if (getRequestingHost().equals(proxyHost)) {
54: String proxyUser = System.getProperty("http.proxyUser");
55: if ((proxyUser != null) && (proxyUser.trim().length() > 0)) {
56: String proxyPass = System.getProperty(
57: "http.proxyPassword", "");
58: Message
59: .debug("authenicating to proxy server with username ["
60: + proxyUser + "]");
61: result = new PasswordAuthentication(proxyUser,
62: proxyPass.toCharArray());
63: }
64: } else {
65: Credentials c = CredentialsStore.INSTANCE.getCredentials(
66: getRequestingPrompt(), getRequestingHost());
67: Message.debug("authentication: k='"
68: + Credentials.buildKey(getRequestingPrompt(),
69: getRequestingHost()) + "' c='" + c + "'");
70: if (c != null) {
71: result = new PasswordAuthentication(c.getUserName(), c
72: .getPasswd().toCharArray());
73: }
74: }
75:
76: return result;
77: }
78:
79: }
|