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;
21  
22  import java.util.List;
23  import java.util.Map;
24  
25  import org.apache.mina.core.filterchain.IoFilter.NextFilter;
26  import org.apache.mina.proxy.ProxyAuthException;
27  import org.apache.mina.proxy.handlers.ProxyRequest;
28  import org.apache.mina.proxy.session.ProxyIoSession;
29  import org.apache.mina.proxy.utils.StringUtilities;
30  import org.slf4j.Logger;
31  import org.slf4j.LoggerFactory;
32  
33  /**
34   * AbstractAuthLogicHandler.java - Abstract class that handles an authentication 
35   * mechanism logic.
36   * 
37   * @author <a href="http://mina.apache.org">Apache MINA Project</a>
38   * @since MINA 2.0.0-M3
39   */
40  public abstract class AbstractAuthLogicHandler {
41      private static final Logger LOGGER = LoggerFactory.getLogger(AbstractAuthLogicHandler.class);
42  
43      /**
44       * The request to be handled by the proxy.
45       */
46      protected ProxyRequest request;
47  
48      /**
49       * Object that contains all the proxy authentication session informations.
50       */
51      protected ProxyIoSession proxyIoSession;
52  
53      /**
54       * The current handshake step.
55       */
56      protected int step = 0;
57  
58      /**
59       * Instantiates a handler for the given proxy session.
60       * 
61       * @param proxyIoSession the proxy session object
62       * @throws ProxyAuthException If we get an error during the proxy authentication
63       */
64      protected AbstractAuthLogicHandler(final ProxyIoSession proxyIoSession) throws ProxyAuthException {
65          this.proxyIoSession = proxyIoSession;
66          this.request = proxyIoSession.getRequest();
67  
68          if (this.request == null || !(this.request instanceof HttpProxyRequest)) {
69              throw new IllegalArgumentException("request parameter should be a non null HttpProxyRequest instance");
70          }
71      }
72  
73      /**
74       * Method called at each step of the handshaking process.
75       * 
76       * @param nextFilter the next filter
77       * @throws ProxyAuthException If we get an error during the proxy authentication
78       */
79      public abstract void doHandshake(final NextFilter nextFilter) throws ProxyAuthException;
80  
81      /**
82       * Handles a HTTP response from the proxy server.
83       * 
84       * @param response The HTTP response.
85       * @throws ProxyAuthException If we get an error during the proxy authentication
86       */
87      public abstract void handleResponse(final HttpProxyResponse response) throws ProxyAuthException;
88  
89      /**
90       * Sends an HTTP request.
91       * 
92       * @param nextFilter the next filter
93       * @param request the request to write
94       * @throws ProxyAuthException If we get an error during the proxy authentication
95       */
96      protected void writeRequest(final NextFilter nextFilter, final HttpProxyRequest request) throws ProxyAuthException {
97          if (LOGGER.isDebugEnabled()) {
98              LOGGER.debug("  sending HTTP request");
99          }
100 
101         ((AbstractHttpLogicHandler) proxyIoSession.getHandler()).writeRequest(nextFilter, request);
102     }
103 
104     /**
105      * Try to force proxy connection to be kept alive.
106      * 
107      * @param headers the request headers
108      */
109     public static void addKeepAliveHeaders(Map<String, List<String>> headers) {
110         StringUtilities.addValueToHeader(headers, "Keep-Alive", HttpProxyConstants.DEFAULT_KEEP_ALIVE_TIME, true);
111         StringUtilities.addValueToHeader(headers, "Proxy-Connection", "keep-Alive", true);
112     }
113 
114 }