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.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  
34  /**
35   * ProxyIoSession.java - Class that contains all informations for the current proxy 
36   * authentication session.
37   * 
38   * @author The Apache MINA Project (dev@mina.apache.org)
39   * @version $Rev: 685703 $, $Date: 2008-08-14 00:14:47 +0200 (Thu, 14 Aug 2008) $
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 then it will be used etc ...
52       */
53      private List<HttpAuthenticationMethods> preferedOrder;
54  
55      /**
56       * The request to send to the proxy.
57       */
58      private ProxyRequest request;
59  
60      /**
61       * The currently selected proxy handler. 
62       */
63      private ProxyLogicHandler handler;
64  
65      /**
66       * Parent {@link ProxyFilter} handling the session.
67       */
68      private ProxyFilter proxyFilter;
69  
70      /**
71       * The session.
72       */
73      private IoSession session;
74  
75      /**
76       * The connector.
77       */
78      private ProxyConnector connector;
79  
80      /**
81       * Address of the proxy server.
82       */
83      private InetSocketAddress proxyAddress = null;
84  
85      /**
86       * A flag that indicates that proxy closed the connection before handshake is done. So
87       * we need to reconnect to the proxy to continue handshaking process.  
88       */
89      private boolean reconnectionNeeded = false;
90  
91      /**
92       * Name of the charset used for string encoding & decoding.
93       */
94      private String charsetName;
95  
96      /**
97       * The session event queue.
98       */
99      private IoSessionEventQueue eventQueue;
100 
101     /**
102      * Set to true when an exception has been thrown.
103      */
104     private boolean authenticationFailed;
105 
106     public IoSessionEventQueue getEventQueue() {
107         return eventQueue;
108     }
109 
110     public ProxyIoSession(InetSocketAddress proxyAddress, ProxyRequest request) {
111         setProxyAddress(proxyAddress);
112         setRequest(request);
113     }
114 
115     public List<HttpAuthenticationMethods> getPreferedOrder() {
116         return preferedOrder;
117     }
118 
119     public void setPreferedOrder(List<HttpAuthenticationMethods> preferedOrder) {
120         this.preferedOrder = preferedOrder;
121     }
122 
123     public ProxyLogicHandler getHandler() {
124         return handler;
125     }
126 
127     public void setHandler(ProxyLogicHandler handler) {
128         this.handler = handler;
129     }
130 
131     public ProxyFilter getProxyFilter() {
132         return proxyFilter;
133     }
134 
135     /**
136      * Note : Please do not call this method from your code it could result in an
137      * unexpected behaviour.
138      */
139     public void setProxyFilter(ProxyFilter proxyFilter) {
140         this.proxyFilter = proxyFilter;
141     }
142 
143     public ProxyRequest getRequest() {
144         return request;
145     }
146 
147     public void setRequest(ProxyRequest request) {
148         if (request == null) {
149             throw new NullPointerException("request cannot be null");
150         }
151 
152         this.request = request;
153     }
154 
155     public IoSession getSession() {
156         return session;
157     }
158 
159     /**
160      * Note : Please do not call this method from your code it could result in an
161      * unexpected behaviour.
162      */
163     public void setSession(IoSession session) {
164         this.session = session;
165         this.eventQueue = new IoSessionEventQueue(this);
166     }
167 
168     public ProxyConnector getConnector() {
169         return connector;
170     }
171 
172     /**
173      * Note : Please do not call this method from your code it could result in an
174      * unexpected behaviour.
175      */
176     public void setConnector(ProxyConnector connector) {
177         this.connector = connector;
178     }
179 
180     public InetSocketAddress getProxyAddress() {
181         return proxyAddress;
182     }
183 
184     public void setProxyAddress(InetSocketAddress proxyAddress) {
185         if (proxyAddress == null) {
186             throw new IllegalArgumentException("proxyAddress cannot be null");
187         }
188 
189         if (!(proxyAddress instanceof InetSocketAddress)) {
190             throw new NullPointerException("Unsupported proxyAddress type "
191                     + proxyAddress.getClass().getName());
192         }
193 
194         this.proxyAddress = proxyAddress;
195     }
196 
197     public boolean isReconnectionNeeded() {
198         return reconnectionNeeded;
199     }
200 
201     /**
202      * Note : Please do not call this method from your code it could result in an
203      * unexpected behaviour.
204      */
205     public void setReconnectionNeeded(boolean reconnectionNeeded) {
206         this.reconnectionNeeded = reconnectionNeeded;
207     }
208 
209     public Charset getCharset() {
210         return Charset.forName(getCharsetName());
211     }
212 
213     public synchronized String getCharsetName() {
214         if (charsetName == null) {
215             charsetName = DEFAULT_ENCODING;
216         }
217 
218         return charsetName;
219     }
220 
221     public void setCharsetName(String charsetName) {
222         this.charsetName = charsetName;
223     }
224 
225     public boolean isAuthenticationFailed() {
226         return authenticationFailed;
227     }
228 
229     public void setAuthenticationFailed(boolean authenticationFailed) {
230         this.authenticationFailed = authenticationFailed;
231     }
232 }