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 <a href="http://mina.apache.org">Apache MINA Project</a>
42   * @since MINA 2.0.0-M3
43   */
44  public class HttpBasicAuthLogicHandler extends AbstractAuthLogicHandler {
45      private final static Logger logger = LoggerFactory
46              .getLogger(HttpBasicAuthLogicHandler.class);
47  
48      /**
49       * {@inheritDoc}
50       */
51      public HttpBasicAuthLogicHandler(final ProxyIoSession proxyIoSession)
52              throws ProxyAuthException {
53          super(proxyIoSession);
54  
55          ((HttpProxyRequest) request).checkRequiredProperties(
56                  HttpProxyConstants.USER_PROPERTY,
57                  HttpProxyConstants.PWD_PROPERTY);
58      }
59  
60      /**
61       * {@inheritDoc}
62       */
63      @Override
64      public void doHandshake(final NextFilter nextFilter)
65              throws ProxyAuthException {
66          logger.debug(" doHandshake()");
67  
68          if (step > 0) {
69              throw new ProxyAuthException("Authentication request already sent");
70          }
71  
72          // Send request
73          HttpProxyRequest req = (HttpProxyRequest) request;
74          Map<String, List<String>> headers = req.getHeaders() != null ? req
75                  .getHeaders() : new HashMap<String, List<String>>();
76  
77          String username = req.getProperties().get(
78                  HttpProxyConstants.USER_PROPERTY);
79          String password = req.getProperties().get(
80                  HttpProxyConstants.PWD_PROPERTY);
81  
82          StringUtilities.addValueToHeader(headers, "Proxy-Authorization",
83                  "Basic " + createAuthorization(username, password), true);
84  
85          addKeepAliveHeaders(headers);
86          req.setHeaders(headers);
87  
88          writeRequest(nextFilter, req);
89          step++;
90      }
91  
92      /**
93       * Computes the authorization header value.
94       * 
95       * @param username the user name
96       * @param password the user password
97       * @return the authorization header value as a string
98       */
99      public static String createAuthorization(final String username,
100             final String password) {
101         return new String(Base64.encodeBase64((username + ":" + password)
102                 .getBytes()));
103     }
104 
105     /**
106      * {@inheritDoc}
107      */
108     @Override
109     public void handleResponse(final HttpProxyResponse response)
110             throws ProxyAuthException {
111         if (response.getStatusCode() != 407) {
112             throw new ProxyAuthException("Received error response code ("
113                     + response.getStatusLine() + ").");
114         }
115     }
116 }