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