View Javadoc

1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *
10   *    http://www.apache.org/licenses/LICENSE-2.0
11   *
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License.
18   *
19   */
20  package org.apache.mina.proxy.handlers.http.basic;
21  
22  import java.util.HashMap;
23  import java.util.List;
24  import java.util.Map;
25  
26  import org.apache.mina.core.filterchain.IoFilter.NextFilter;
27  import org.apache.mina.proxy.ProxyAuthException;
28  import org.apache.mina.proxy.handlers.http.AbstractAuthLogicHandler;
29  import org.apache.mina.proxy.handlers.http.HttpProxyConstants;
30  import org.apache.mina.proxy.handlers.http.HttpProxyRequest;
31  import org.apache.mina.proxy.handlers.http.HttpProxyResponse;
32  import org.apache.mina.proxy.session.ProxyIoSession;
33  import org.apache.mina.proxy.utils.StringUtilities;
34  import org.apache.mina.util.Base64;
35  import org.slf4j.Logger;
36  import org.slf4j.LoggerFactory;
37  
38  /**
39   * HttpBasicAuthLogicHandler.java - HTTP Basic authentication mechanism logic handler.
40   * 
41   * @author The Apache MINA Project (dev@mina.apache.org)
42   * @version $Rev: 759669 $, $Date: 2009-03-29 13:25:44 +0200 (Sun, 29 Mar 2009) $
43   * @since MINA 2.0.0-M3
44   */
45  public class HttpBasicAuthLogicHandler extends AbstractAuthLogicHandler {
46      private final static Logger logger = LoggerFactory
47              .getLogger(HttpBasicAuthLogicHandler.class);
48  
49      /**
50       * {@inheritDoc}
51       */
52      public HttpBasicAuthLogicHandler(final ProxyIoSession proxyIoSession)
53              throws ProxyAuthException {
54          super(proxyIoSession);
55  
56          if (request == null || !(request instanceof HttpProxyRequest)) {
57              throw new IllegalArgumentException(
58                      "request parameter should be a non null HttpProxyRequest instance");
59          }
60  
61          ((HttpProxyRequest) request).checkRequiredProperties(
62  				HttpProxyConstants.USER_PROPERTY,
63  				HttpProxyConstants.PWD_PROPERTY);
64      }
65  
66      /**
67       * {@inheritDoc}
68       */
69      @Override
70      public void doHandshake(final NextFilter nextFilter)
71              throws ProxyAuthException {
72          logger.debug(" doHandshake()");
73  
74          if (step > 0) {
75              throw new ProxyAuthException("Authentication request already sent");
76          }
77  
78          // Send request
79          HttpProxyRequest req = (HttpProxyRequest) request;
80          Map<String, List<String>> headers = req.getHeaders() != null ? req
81                  .getHeaders() : new HashMap<String, List<String>>();
82  
83          String username = req.getProperties().get(
84                  HttpProxyConstants.USER_PROPERTY);
85          String password = req.getProperties().get(
86                  HttpProxyConstants.PWD_PROPERTY);
87  
88          StringUtilities.addValueToHeader(headers, "Proxy-Authorization",
89                  "Basic " + createAuthorization(username, password), true);
90  
91          StringUtilities.addValueToHeader(headers, "Keep-Alive",
92                  HttpProxyConstants.DEFAULT_KEEP_ALIVE_TIME, true);
93          StringUtilities.addValueToHeader(headers, "Proxy-Connection",
94                  "keep-Alive", true);
95          req.setHeaders(headers);
96  
97          writeRequest(nextFilter, req);
98          step++;
99      }
100 
101     /**
102      * Computes the authorization header value.
103      * 
104      * @param username the user name
105      * @param password the user password
106      * @return the authorization header value as a string
107      */
108     public static String createAuthorization(final String username,
109             final String password) {
110         return new String(Base64.encodeBase64((username + ":" + password)
111                 .getBytes()));
112     }
113 
114     /**
115      * {@inheritDoc}
116      */
117     @Override
118     public void handleResponse(final HttpProxyResponse response)
119             throws ProxyAuthException {
120         if (response.getStatusCode() != 407) {
121             throw new ProxyAuthException("Received error response code ("
122                     + response.getStatusLine() + ").");
123         }
124     }
125 }