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 final static 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
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
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
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
95 */
96 protected void writeRequest(final NextFilter nextFilter, final HttpProxyRequest request) throws ProxyAuthException {
97 logger.debug(" sending HTTP request");
98
99 ((AbstractHttpLogicHandler) proxyIoSession.getHandler()).writeRequest(nextFilter, request);
100 }
101
102 /**
103 * Try to force proxy connection to be kept alive.
104 *
105 * @param headers the request headers
106 */
107 public static void addKeepAliveHeaders(Map<String, List<String>> headers) {
108 StringUtilities.addValueToHeader(headers, "Keep-Alive", HttpProxyConstants.DEFAULT_KEEP_ALIVE_TIME, true);
109 StringUtilities.addValueToHeader(headers, "Proxy-Connection", "keep-Alive", true);
110 }
111
112 }