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    package org.apache.servicemix.http;
018    
019    import javax.jbi.messaging.MessageExchange;
020    import javax.jbi.messaging.MessagingException;
021    import javax.jbi.messaging.NormalizedMessage;
022    
023    import org.apache.commons.httpclient.Credentials;
024    import org.apache.commons.httpclient.HttpClient;
025    import org.apache.commons.httpclient.UsernamePasswordCredentials;
026    import org.apache.commons.httpclient.auth.AuthScope;
027    import org.apache.servicemix.expression.Expression;
028    
029    /**
030     * @author roehl.sioson
031     * @org.apache.xbean.XBean element="basicAuthCredentials" description=
032     *                         "This class contains parameters needed to send basic authentication credentials"
033     */
034    public class BasicAuthCredentials {
035    
036        protected Expression username;
037        protected Expression password;
038    
039        public BasicAuthCredentials() {
040        }
041    
042        /**
043         * Returns the username for authentication.
044         * 
045         * @return an <code>Expression</code> containing the username
046         */
047        public Expression getUsername() {
048            return username;
049        }
050    
051        /**
052         * Sets the username to use for authentocation.
053         * 
054         * @param username am <code>Expression</code> containing the username.
055         * @org.apache.xbean.Property description="the username to use for authentication"
056         */
057        public void setUsername(Expression username) {
058            this.username = username;
059        }
060    
061        /**
062         * Returns the password for authentication.
063         * 
064         * @return an <code>Expression</code> containing the password
065         */
066        public Expression getPassword() {
067            return password;
068        }
069    
070        /**
071         * Sets the password to use for authentocation.
072         * 
073         * @param password am <code>Expression</code> containing the password.
074         * @org.apache.xbean.Property description="the password to use for authentication"
075         */
076        public void setPassword(Expression password) {
077            this.password = password;
078        }
079    
080        /**
081         * Applies this authentication to the given method.
082         * 
083         * @param client the client on which to set the authentication information
084         * @param exchange the message exchange to be used for evaluating the expression
085         * @param message the normalized message to be used for evaluating the expression
086         * @throws MessagingException if the correct value for username/password cannot be determined when using an expression
087         */
088        public void applyCredentials(HttpClient client, MessageExchange exchange, NormalizedMessage message)
089            throws MessagingException {
090            AuthScope scope = new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT);
091            Credentials credentials = new UsernamePasswordCredentials((String)this.username.evaluate(exchange,
092                                                                                                     message),
093                                                                      (String)this.password.evaluate(exchange,
094                                                                                                     message));
095            client.getState().setCredentials(scope, credentials);
096        }
097    
098        /**
099         * Applies this authentication to the given method.
100         * 
101         * @param client the client on which to set the authentication information
102         * @param exchange the message exchange to be used for evaluating the expression
103         * @param message the normalized message to be used for evaluating the expression
104         * @throws MessagingException if the correct value for user name/password cannot be determined when using an expression
105         */
106        public void applyProxyCredentials(HttpClient client, MessageExchange exchange, NormalizedMessage message)
107            throws MessagingException {
108            AuthScope scope = new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT);
109            Credentials credentials = new UsernamePasswordCredentials((String)this.username.evaluate(exchange,
110                                                                                                     message),
111                                                                      (String)this.password.evaluate(exchange,
112                                                                                                     message));
113            client.getState().setProxyCredentials(scope, credentials);
114        }
115    
116    }