001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */package org.apache.cxf.transport.http;
019:
020: import java.net.URL;
021:
022: import org.apache.cxf.message.Message;
023:
024: /**
025: * This abstract class is extended by developers who need HTTP Basic Auth
026: * functionality on the client side. It supplies userid and password
027: * combinations to an HTTPConduit.
028: * <p>
029: * The HTTPConduit will make a call to getPreemptiveUserPass before
030: * an HTTP request is made. The HTTPConduit will call on
031: * getUserPassForRealm upon getting a 401 HTTP Response with a
032: * "WWW-Authenticate: Basic realm=????" header.
033: * <p>
034: * A HTTPConduit keeps a reference to this HttpBasicAuthSupplier for the life
035: * of the HTTPConduit, unless changed out by dynamic configuration.
036: * Therefore, an implementation of this HttpBasicAuthSupplier may maintain
037: * state for subsequent calls.
038: * <p>
039: * For instance, an implemenation may not provide a UserPass preemptively for
040: * a particular URL and decide to get the realm information from
041: * a 401 response in which the HTTPConduit will call getUserPassForReam for
042: * that URL. Then this implementation may provide the UserPass for this
043: * particular URL preemptively for subsequent calls to getPreemptiveUserPass.
044: */
045: public abstract class HttpBasicAuthSupplier {
046:
047: /**
048: * This field contains the logical name of this HttpBasicAuthSuppler.
049: * This field is not assigned to be final, since an extension may be
050: * Spring initialized as a bean, have an appropriate setLogicalName
051: * method, and set this field.
052: */
053: protected String logicalName;
054:
055: /**
056: * The default constructor assigns the class name as the LogicalName.
057: *
058: */
059: protected HttpBasicAuthSupplier() {
060: logicalName = this .getClass().getName();
061: }
062:
063: /**
064: * This constructor assigns the LogicalName of this HttpBasicAuthSupplier.
065: *
066: * @param name The Logical Name.
067: */
068: protected HttpBasicAuthSupplier(String name) {
069: logicalName = name;
070: }
071:
072: /**
073: * This method returns the LogicalName of this HttpBasicAuthSupplier.
074: */
075: public String getLogicalName() {
076: return logicalName;
077: }
078:
079: /**
080: * This class is used to return the values of the
081: * userid and password used in the HTTP Authorization
082: * Header.
083: */
084: public static final class UserPass {
085: private final String userid;
086: private final String password;
087:
088: /**
089: * This constructor forms the userid and password pair for
090: * the HTTP Authorization header.
091: *
092: * @param user The userid that will be returned from getUserid().
093: * This argument must not contain a colon (":"). If
094: * it does, it will throw an IllegalArgumentException.
095: *
096: * @param pass The password that will be returned from getPassword().
097: */
098: UserPass(String user, String pass) {
099: if (user.contains(":")) {
100: throw new IllegalArgumentException(
101: "The argument \"user\" cannot contain ':'.");
102: }
103: userid = user;
104: password = pass;
105: }
106:
107: /**
108: * This method returns the userid.
109: */
110: public String getUserid() {
111: return userid;
112: }
113:
114: /**
115: * This method returns the password.
116: */
117: public String getPassword() {
118: return password;
119: }
120: }
121:
122: /**
123: * This method is used by extensions of this class to create
124: * a UserPass to return.
125: * @param userid The userid that will be returned from getUserid().
126: * This argument must not contain a colon (":"). If
127: * it does, it will throw an IllegalArgumentException.
128: * @param password The password that will be returned from getPassword().
129: * @return
130: */
131: protected UserPass createUserPass(final String userid,
132: final String password) {
133: return new UserPass(userid, password);
134: }
135:
136: /**
137: * The HTTPConduit makes a call to this method before connecting
138: * to the server behind a particular URL. If this implementation does not
139: * have a UserPass for this URL, it should return null.
140: *
141: * @param conduitName The HTTPConduit making the call.
142: * @param currentURL The URL to which the request is to be made.
143: * @param message The CXF Message.
144: *
145: * @return This method returns null if no UserPass is available.
146: */
147: public abstract UserPass getPreemptiveUserPass(String conduitName,
148: URL currentURL, Message message);
149:
150: /**
151: * The HTTPConduit makes a call to this method if it
152: * receives a 401 response to a particular URL for
153: * a given message. The realm information is taken
154: * from the "WWW-Authenticate: Basic realm=?????"
155: * header. The current message may be retransmitted
156: * if this call returns a UserPass. The current message will
157: * fail with a 401 if null is returned. If no UserPass is available
158: * for this particular URL, realm, and message, then null
159: * should be returned.
160: *
161: * @param conduitName The name of the conduit making the call.
162: * @param currentURL The current URL from which the reponse came.
163: * @param message The CXF Message.
164: * @param realm The realm extraced from the basic auth header.
165: * @return
166: */
167: public abstract UserPass getUserPassForRealm(String conduitName,
168: URL currentURL, Message message, String realm);
169: }
|