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
42 .getLogger(AbstractAuthLogicHandler.class);
43
44 /**
45 * The request to be handled by the proxy.
46 */
47 protected ProxyRequest request;
48
49 /**
50 * Object that contains all the proxy authentication session informations.
51 */
52 protected ProxyIoSession proxyIoSession;
53
54 /**
55 * The current handshake step.
56 */
57 protected int step = 0;
58
59 /**
60 * Instantiates a handler for the given proxy session.
61 *
62 * @param proxyIoSession the proxy session object
63 * @throws ProxyAuthException
64 */
65 protected AbstractAuthLogicHandler(final ProxyIoSession proxyIoSession)
66 throws ProxyAuthException {
67 this.proxyIoSession = proxyIoSession;
68 this.request = proxyIoSession.getRequest();
69
70 if (this.request == null || !(this.request instanceof HttpProxyRequest)) {
71 throw new IllegalArgumentException(
72 "request parameter should be a non null HttpProxyRequest instance");
73 }
74 }
75
76 /**
77 * Method called at each step of the handshaking process.
78 *
79 * @param nextFilter the next filter
80 * @throws ProxyAuthException
81 */
82 public abstract void doHandshake(final NextFilter nextFilter)
83 throws ProxyAuthException;
84
85 /**
86 * Handles a HTTP response from the proxy server.
87 *
88 * @param response The HTTP response.
89 * @throws ProxyAuthException
90 */
91 public abstract void handleResponse(final HttpProxyResponse response)
92 throws ProxyAuthException;
93
94 /**
95 * Sends an HTTP request.
96 *
97 * @param nextFilter the next filter
98 * @param request the request to write
99 * @throws ProxyAuthException
100 */
101 protected void writeRequest(final NextFilter nextFilter,
102 final HttpProxyRequest request) throws ProxyAuthException {
103 logger.debug(" sending HTTP request");
104
105 ((AbstractHttpLogicHandler) proxyIoSession.getHandler()).writeRequest(
106 nextFilter, request);
107 }
108
109 /**
110 * Try to force proxy connection to be kept alive.
111 *
112 * @param headers the request headers
113 */
114 public static void addKeepAliveHeaders(Map<String, List<String>> headers) {
115 StringUtilities.addValueToHeader(headers, "Keep-Alive",
116 HttpProxyConstants.DEFAULT_KEEP_ALIVE_TIME, true);
117 StringUtilities.addValueToHeader(headers, "Proxy-Connection",
118 "keep-Alive", true);
119 }
120
121 }