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.session;
21
22 import java.net.InetSocketAddress;
23 import java.nio.charset.Charset;
24 import java.util.List;
25
26 import org.apache.mina.core.session.IoSession;
27 import org.apache.mina.proxy.ProxyConnector;
28 import org.apache.mina.proxy.ProxyLogicHandler;
29 import org.apache.mina.proxy.event.IoSessionEventQueue;
30 import org.apache.mina.proxy.filter.ProxyFilter;
31 import org.apache.mina.proxy.handlers.ProxyRequest;
32 import org.apache.mina.proxy.handlers.http.HttpAuthenticationMethods;
33 import org.apache.mina.proxy.handlers.http.HttpSmartProxyHandler;
34
35 /**
36 * ProxyIoSession.java - Class that contains all informations for the current proxy
37 * authentication session.
38 *
39 * @author <a href="http://mina.apache.org">Apache MINA Project</a>
40 * @since MINA 2.0.0-M3
41 */
42 public class ProxyIoSession {
43
44 public final static String PROXY_SESSION = ProxyConnector.class.getName()
45 + ".ProxySession";
46
47 private final static String DEFAULT_ENCODING = "ISO-8859-1";
48
49 /**
50 * The list contains the authentication methods to use.
51 * The order in the list is revelant : if first method is available
52 * then it will be used etc ...
53 */
54 private List<HttpAuthenticationMethods> preferedOrder;
55
56 /**
57 * The request to send to the proxy.
58 */
59 private ProxyRequest request;
60
61 /**
62 * The currently selected proxy handler.
63 */
64 private ProxyLogicHandler handler;
65
66 /**
67 * Parent {@link ProxyFilter} handling the session.
68 */
69 private ProxyFilter proxyFilter;
70
71 /**
72 * The session.
73 */
74 private IoSession session;
75
76 /**
77 * The proxy connector.
78 */
79 private ProxyConnector connector;
80
81 /**
82 * Address of the proxy server.
83 */
84 private InetSocketAddress proxyAddress = null;
85
86 /**
87 * A flag that indicates that the proxy closed the connection before handshake
88 * is done. So we need to reconnect to the proxy to continue the handshaking
89 * process.
90 */
91 private boolean reconnectionNeeded = false;
92
93 /**
94 * Name of the charset used for string encoding & decoding.
95 */
96 private String charsetName;
97
98 /**
99 * The session event queue.
100 */
101 private IoSessionEventQueue eventQueue = new IoSessionEventQueue(this);
102
103 /**
104 * Set to true when an exception has been thrown or if authentication failed.
105 */
106 private boolean authenticationFailed;
107
108 /**
109 * Constructor.
110 *
111 * @param proxyAddress the IP address of the proxy server
112 * @param request the proxy request
113 */
114 public ProxyIoSession(InetSocketAddress proxyAddress, ProxyRequest request) {
115 setProxyAddress(proxyAddress);
116 setRequest(request);
117 }
118
119 /**
120 * Returns the pending event queue.
121 */
122 public IoSessionEventQueue getEventQueue() {
123 return eventQueue;
124 }
125
126 /**
127 * Returns the list of the prefered order for the authentication methods.
128 * This list is used by the {@link HttpSmartProxyHandler} to determine
129 * which authentication mechanism to use first between those accepted by the
130 * proxy server. This list is only used when connecting to an http proxy.
131 */
132 public List<HttpAuthenticationMethods> getPreferedOrder() {
133 return preferedOrder;
134 }
135
136 /**
137 * Sets the ordered list of prefered authentication mechanisms.
138 *
139 * @param preferedOrder the ordered list
140 */
141 public void setPreferedOrder(List<HttpAuthenticationMethods> preferedOrder) {
142 this.preferedOrder = preferedOrder;
143 }
144
145 /**
146 * Returns the {@link ProxyLogicHandler} currently in use.
147 */
148 public ProxyLogicHandler getHandler() {
149 return handler;
150 }
151
152 /**
153 * Sets the {@link ProxyLogicHandler} to use.
154 *
155 * @param handler the {@link ProxyLogicHandler} instance
156 */
157 public void setHandler(ProxyLogicHandler handler) {
158 this.handler = handler;
159 }
160
161 /**
162 * Returns the {@link ProxyFilter}.
163 */
164 public ProxyFilter getProxyFilter() {
165 return proxyFilter;
166 }
167
168 /**
169 * Sets the {@link ProxyFilter}.
170 * Note : Please do not call this method from your code it could result
171 * in an unexpected behaviour.
172 *
173 * @param proxyFilter the filter
174 */
175 public void setProxyFilter(ProxyFilter proxyFilter) {
176 this.proxyFilter = proxyFilter;
177 }
178
179 /**
180 * Returns the proxy request.
181 */
182 public ProxyRequest getRequest() {
183 return request;
184 }
185
186 /**
187 * Sets the proxy request.
188 *
189 * @param request the proxy request
190 */
191 private void setRequest(ProxyRequest request) {
192 if (request == null) {
193 throw new NullPointerException("request cannot be null");
194 }
195
196 this.request = request;
197 }
198
199 /**
200 * Returns the current {@link IoSession}.
201 */
202 public IoSession getSession() {
203 return session;
204 }
205
206 /**
207 * Sets the {@link IoSession} in use.
208 * Note : Please do not call this method from your code it could result in an
209 * unexpected behaviour.
210 *
211 * @param session the current io session
212 */
213 public void setSession(IoSession session) {
214 this.session = session;
215 }
216
217 /**
218 * Returns the proxy connector.
219 */
220 public ProxyConnector getConnector() {
221 return connector;
222 }
223
224 /**
225 * Sets the connector reference of this proxy session.
226 * Note : Please do not call this method from your code it could result in an
227 * unexpected behaviour.
228 *
229 * @param connector the proxy connector
230 */
231 public void setConnector(ProxyConnector connector) {
232 this.connector = connector;
233 }
234
235 /**
236 * Returns the IP address of the proxy server.
237 */
238 public InetSocketAddress getProxyAddress() {
239 return proxyAddress;
240 }
241
242 /**
243 * Sets the IP address of the proxy server.
244 *
245 * @param proxyAddress the IP address of the proxy server
246 */
247 private void setProxyAddress(InetSocketAddress proxyAddress) {
248 if (proxyAddress == null) {
249 throw new IllegalArgumentException("proxyAddress object cannot be null");
250 }
251
252 this.proxyAddress = proxyAddress;
253 }
254
255 /**
256 * Returns true if the current authentication process is not finished
257 * but the server has closed the connection.
258 */
259 public boolean isReconnectionNeeded() {
260 return reconnectionNeeded;
261 }
262
263 /**
264 * Sets the reconnection needed flag. If set to true, it means that an
265 * authentication process is currently running but the proxy server did not
266 * kept the connection alive. So we need to reconnect to the server to complete
267 * the process.
268 * Note : Please do not call this method from your code it could result in an
269 * unexpected behaviour.
270 *
271 * @param reconnectionNeeded the value to set the flag to
272 */
273 public void setReconnectionNeeded(boolean reconnectionNeeded) {
274 this.reconnectionNeeded = reconnectionNeeded;
275 }
276
277 /**
278 * Returns a charset instance of the in use charset name.
279 */
280 public Charset getCharset() {
281 return Charset.forName(getCharsetName());
282 }
283
284 /**
285 * Returns the used charset name or {@link #DEFAULT_ENCODING} if null.
286 */
287 public String getCharsetName() {
288 if (charsetName == null) {
289 charsetName = DEFAULT_ENCODING;
290 }
291
292 return charsetName;
293 }
294
295 /**
296 * Sets the charset to use.
297 *
298 * @param charsetName the charset name
299 */
300 public void setCharsetName(String charsetName) {
301 this.charsetName = charsetName;
302 }
303
304 /**
305 * Returns true if authentication failed.
306 */
307 public boolean isAuthenticationFailed() {
308 return authenticationFailed;
309 }
310
311 /**
312 * Sets the authentication failed flag.
313 *
314 * @param authenticationFailed the value to set the flag to
315 */
316 public void setAuthenticationFailed(boolean authenticationFailed) {
317 this.authenticationFailed = authenticationFailed;
318 }
319 }