001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package java.net;
019:
020: /**
021: * This class is able to obtain authentication info for a connection, usually
022: * from user. First the application has to set the default authenticator which
023: * extends <code>Authenticator</code> by
024: * <code>setDefault(Authenticator a)</code>.
025: * <p>
026: * It should override <code>getPasswordAuthentication()</code> which dictates
027: * how the authentication info should be obtained.
028: *
029: * @see Authenticator#setDefault(Authenticator)
030: * @see Authenticator#getPasswordAuthentication()
031: */
032: public abstract class Authenticator {
033:
034: // the default authenticator that needs to be set
035: private static Authenticator this Authenticator;
036:
037: private static final NetPermission requestPasswordAuthenticationPermission = new NetPermission(
038: "requestPasswordAuthentication"); //$NON-NLS-1$
039:
040: private static final NetPermission setDefaultAuthenticatorPermission = new NetPermission(
041: "setDefaultAuthenticator"); //$NON-NLS-1$
042:
043: // the requester connection info
044: private String host;
045:
046: private InetAddress addr;
047:
048: private int port;
049:
050: private String protocol;
051:
052: private String prompt;
053:
054: private String scheme;
055:
056: private URL url;
057:
058: private RequestorType rt;
059:
060: /**
061: * This method is responsible for retrieving the username and password for
062: * the sender. The implementation varies. The subclass has to overwrite
063: * this.
064: * <p>
065: * It answers null by default.
066: *
067: * @return java.net.PasswordAuthentication The password authentication that
068: * it obtains
069: */
070: protected PasswordAuthentication getPasswordAuthentication() {
071: return null;
072: }
073:
074: /**
075: * Answers the port of the connection that requests authorization.
076: *
077: * @return int the port of the connection
078: */
079: protected final int getRequestingPort() {
080: return this .port;
081: }
082:
083: /**
084: * Answers the address of the connection that requests authorization or null
085: * if unknown.
086: *
087: * @return InetAddress the address of the connection
088: */
089: protected final InetAddress getRequestingSite() {
090: return this .addr;
091: }
092:
093: /**
094: * Answers the realm (prompt string) of the connection that requires
095: * authorization.
096: *
097: * @return java.lang.String the prompt string of the connection
098: */
099: protected final String getRequestingPrompt() {
100: return this .prompt;
101: }
102:
103: /**
104: * Answers the protocol of the connection that requests authorization.
105: *
106: * @return java.lang.String the protocol of connection
107: */
108: protected final String getRequestingProtocol() {
109: return this .protocol;
110: }
111:
112: /**
113: * Answers the scheme of the connection that requires authorization. Eg.
114: * Basic
115: *
116: * @return java.lang.String the scheme of the connection
117: */
118: protected final String getRequestingScheme() {
119: return this .scheme;
120: }
121:
122: /**
123: * If the permission check of the security manager does not result in a
124: * security exception, this method invokes the methods of the registered
125: * authenticator to get the authentication info.
126: *
127: * @return java.net.PasswordAuthentication the authentication info
128: *
129: * @param rAddr
130: * java.net.InetAddress the address of the connection that
131: * requests authentication
132: * @param rPort
133: * int the port of the connection that requests authentication
134: * @param rProtocol
135: * java.lang.String the protocol of the connection that requests
136: * authentication
137: * @param rPrompt
138: * java.lang.String the realm of the connection that requests
139: * authentication
140: * @param rScheme
141: * java.lang.String the scheme of the connection that requests
142: * authentication
143: * @throws SecurityException
144: * if requestPasswordAuthenticationPermission is denied
145: */
146: public static synchronized PasswordAuthentication requestPasswordAuthentication(
147: InetAddress rAddr, int rPort, String rProtocol,
148: String rPrompt, String rScheme) {
149: SecurityManager sm = System.getSecurityManager();
150: if (sm != null) {
151: sm.checkPermission(requestPasswordAuthenticationPermission);
152: }
153: if (this Authenticator == null) {
154: return null;
155: }
156: // set the requester info so it knows what it is requesting
157: // authentication for
158: this Authenticator.addr = rAddr;
159: this Authenticator.port = rPort;
160: this Authenticator.protocol = rProtocol;
161: this Authenticator.prompt = rPrompt;
162: this Authenticator.scheme = rScheme;
163: this Authenticator.rt = RequestorType.SERVER;
164:
165: // returns the authentication info obtained by the registered
166: // Authenticator
167: return this Authenticator.getPasswordAuthentication();
168: }
169:
170: /**
171: * This method sets <code>a</code> to be the default authenticator. It
172: * will be called whenever the realm that the URL is pointing to requires
173: * authorization. If there is a security manager set then the caller must
174: * have the NetPermission "setDefaultAuthenticator".
175: *
176: * @param a
177: * java.net.Authenticator The authenticator to be set.
178: * @throws SecurityException
179: * if requestPasswordAuthenticationPermission is denied
180: */
181: public static void setDefault(Authenticator a) {
182: SecurityManager sm = System.getSecurityManager();
183: if (sm != null) {
184: sm.checkPermission(setDefaultAuthenticatorPermission);
185: }
186: this Authenticator = a;
187: }
188:
189: /**
190: * If the permission check of the security manager does not result in a
191: * security exception, this method invokes the methods of the registered
192: * authenticator to get the authentication info.
193: *
194: * @return java.net.PasswordAuthentication the authentication info
195: *
196: * @param rHost
197: * java.lang.String the host name of the connection that requests
198: * authentication
199: * @param rAddr
200: * java.net.InetAddress the address of the connection that
201: * requests authentication
202: * @param rPort
203: * int the port of the connection that requests authentication
204: * @param rProtocol
205: * java.lang.String the protocol of the connection that requests
206: * authentication
207: * @param rPrompt
208: * java.lang.String the realm of the connection that requests
209: * authentication
210: * @param rScheme
211: * java.lang.String the scheme of the connection that requests
212: * authentication
213: * @throws SecurityException
214: * if requestPasswordAuthenticationPermission is denied
215: */
216: public static synchronized PasswordAuthentication requestPasswordAuthentication(
217: String rHost, InetAddress rAddr, int rPort,
218: String rProtocol, String rPrompt, String rScheme) {
219: SecurityManager sm = System.getSecurityManager();
220: if (sm != null) {
221: sm.checkPermission(requestPasswordAuthenticationPermission);
222: }
223: if (this Authenticator == null) {
224: return null;
225: }
226: // set the requester info so it knows what it is requesting
227: // authentication for
228: this Authenticator.host = rHost;
229: this Authenticator.addr = rAddr;
230: this Authenticator.port = rPort;
231: this Authenticator.protocol = rProtocol;
232: this Authenticator.prompt = rPrompt;
233: this Authenticator.scheme = rScheme;
234: this Authenticator.rt = RequestorType.SERVER;
235:
236: // returns the authentication info obtained by the registered
237: // Authenticator
238: return this Authenticator.getPasswordAuthentication();
239: }
240:
241: /**
242: * Return the host name of the connection that requests authentication, or
243: * null if unknown.
244: */
245: protected final String getRequestingHost() {
246: return host;
247: }
248:
249: /**
250: * If the permission check of the security manager does not result in a
251: * security exception, this method invokes the methods of the registered
252: * authenticator to get the authentication info.
253: *
254: * @return java.net.PasswordAuthentication the authentication info
255: *
256: * @param rHost
257: * java.lang.String the host name of the connection that requests
258: * authentication
259: * @param rAddr
260: * java.net.InetAddress the address of the connection that
261: * requests authentication
262: * @param rPort
263: * int the port of the connection that requests authentication
264: * @param rProtocol
265: * java.lang.String the protocol of the connection that requests
266: * authentication
267: * @param rPrompt
268: * java.lang.String the realm of the connection that requests
269: * authentication
270: * @param rScheme
271: * java.lang.String the scheme of the connection that requests
272: * authentication
273: * @param rURL
274: * java.net.URL the url of the connection that requests
275: * authentication
276: * @param reqType
277: * java.net.Authenticator.RequestorType the RequestorType of the
278: * connection that requests authentication
279: * @throws SecurityException
280: * if requestPasswordAuthenticationPermission is denied
281: */
282: public static PasswordAuthentication requestPasswordAuthentication(
283: String rHost, InetAddress rAddr, int rPort,
284: String rProtocol, String rPrompt, String rScheme, URL rURL,
285: Authenticator.RequestorType reqType) {
286: SecurityManager sm = System.getSecurityManager();
287: if (null != sm) {
288: sm.checkPermission(requestPasswordAuthenticationPermission);
289: }
290: if (null == this Authenticator) {
291: return null;
292: }
293: // sets the requester info so it knows what it is requesting
294: // authentication for
295: this Authenticator.host = rHost;
296: this Authenticator.addr = rAddr;
297: this Authenticator.port = rPort;
298: this Authenticator.protocol = rProtocol;
299: this Authenticator.prompt = rPrompt;
300: this Authenticator.scheme = rScheme;
301: this Authenticator.url = rURL;
302: this Authenticator.rt = reqType;
303:
304: // returns the authentication info obtained by the registered
305: // Authenticator
306: return this Authenticator.getPasswordAuthentication();
307:
308: }
309:
310: /**
311: * returns the URL of the authentication resulted in this request.
312: *
313: * @return the url of request
314: */
315: protected URL getRequestingURL() {
316: return url;
317: }
318:
319: /**
320: * returns the type of this request, it can be proxy or server
321: *
322: * @return RequestorType of request
323: */
324: protected Authenticator.RequestorType getRequestorType() {
325: return rt;
326: }
327:
328: /**
329: * an enum class of requestor type
330: */
331: public enum RequestorType {
332:
333: /**
334: * type of proxy server
335: */
336: PROXY,
337:
338: /**
339: * type of origin server
340: */
341: SERVER
342: }
343: }
|