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;
21  
22  import java.io.UnsupportedEncodingException;
23  import java.util.LinkedList;
24  import java.util.Queue;
25  
26  import org.apache.mina.core.buffer.IoBuffer;
27  import org.apache.mina.core.filterchain.IoFilter.NextFilter;
28  import org.apache.mina.core.future.DefaultWriteFuture;
29  import org.apache.mina.core.future.WriteFuture;
30  import org.apache.mina.core.session.IoSession;
31  import org.apache.mina.core.write.DefaultWriteRequest;
32  import org.apache.mina.core.write.WriteRequest;
33  import org.apache.mina.proxy.filter.ProxyFilter;
34  import org.apache.mina.proxy.filter.ProxyHandshakeIoBuffer;
35  import org.apache.mina.proxy.session.ProxyIoSession;
36  import org.slf4j.Logger;
37  import org.slf4j.LoggerFactory;
38  
39  /**
40   * AbstractProxyLogicHandler.java - Helper class to handle proxy handshaking logic. Derived classes 
41   * implement proxy type specific logic.
42   * <p>
43   * Based upon SSLHandler from mina-filter-ssl.
44   * 
45   * @author The Apache MINA Project (dev@mina.apache.org)
46   * @version $Rev: 759663 $, $Date: 2009-03-29 12:46:40 +0200 (Sun, 29 Mar 2009) $
47   * @since MINA 2.0.0-M3
48   */
49  public abstract class AbstractProxyLogicHandler implements ProxyLogicHandler {
50  
51      private final static Logger logger = LoggerFactory
52              .getLogger(AbstractProxyLogicHandler.class);
53  
54      /**
55       * Object that contains all the proxy authentication session informations.
56       */
57      private ProxyIoSession proxyIoSession;
58  
59      /**
60       * Queue of write events which occurred before the proxy handshake had completed.
61       */
62      private Queue<Event> writeRequestQueue = null;
63  
64      /**
65       * Has the handshake been completed.
66       */
67      private boolean handshakeComplete = false;
68  
69      /**
70       * Creates a new {@link AbstractProxyLogicHandler}.
71       * 
72       * @param proxyIoSession {@link ProxyIoSession} in use.
73       */
74      public AbstractProxyLogicHandler(ProxyIoSession proxyIoSession) {
75          this.proxyIoSession = proxyIoSession;
76      }
77  
78      /**
79       * Returns the proxy filter {@link ProxyFilter}.
80       */
81      protected ProxyFilter getProxyFilter() {
82          return proxyIoSession.getProxyFilter();
83      }
84  
85      /**
86       * Returns the session.
87       */
88      protected IoSession getSession() {
89          return proxyIoSession.getSession();
90      }
91  
92      /**
93       * Returns the {@link ProxyIoSession} object.
94       */
95      public ProxyIoSession getProxyIoSession() {
96          return proxyIoSession;
97      }
98  
99      /**
100      * Writes data to the proxy server.
101      * 
102      * @param nextFilter the next filter
103      * @param data Data buffer to be written.
104      */
105     protected WriteFuture writeData(final NextFilter nextFilter,
106             final IoBuffer data) throws UnsupportedEncodingException {
107         // write net data
108         ProxyHandshakeIoBuffer writeBuffer = new ProxyHandshakeIoBuffer(data);
109 
110         logger.debug("   session write: {}", writeBuffer);
111 
112         WriteFuture writeFuture = new DefaultWriteFuture(getSession());
113         getProxyFilter().writeData(nextFilter, getSession(),
114                 new DefaultWriteRequest(writeBuffer, writeFuture), true);
115 
116         return writeFuture;
117     }
118 
119     /**
120      * Returns <code>true</code> if handshaking is complete and
121      * data can be sent through the proxy.
122      */
123     public boolean isHandshakeComplete() {
124         synchronized (this) {
125             return handshakeComplete;
126         }
127     }
128 
129     /**
130      * Signals that the handshake has finished.
131      */
132     protected final void setHandshakeComplete() {
133         synchronized (this) {
134             handshakeComplete = true;
135         }
136 
137         ProxyIoSession proxyIoSession = getProxyIoSession();
138         proxyIoSession.getConnector()
139                 .fireConnected(proxyIoSession.getSession())
140                 .awaitUninterruptibly();
141 
142         logger.debug("  handshake completed");
143 
144         // Connected OK
145         try {
146             proxyIoSession.getEventQueue().flushPendingSessionEvents();
147             flushPendingWriteRequests();
148         } catch (Exception ex) {
149             logger.error("Unable to flush pending write requests", ex);
150         }
151     }
152 
153     /**
154      * Send any write requests which were queued whilst waiting for handshaking to complete.
155      */
156     protected synchronized void flushPendingWriteRequests() throws Exception {
157         logger.debug(" flushPendingWriteRequests()");
158 
159         if (writeRequestQueue == null) {
160             return;
161         }
162 
163         Event scheduledWrite;
164         while ((scheduledWrite = writeRequestQueue.poll()) != null) {
165             logger.debug(" Flushing buffered write request: {}",
166                     scheduledWrite.data);
167 
168             getProxyFilter().filterWrite(scheduledWrite.nextFilter,
169                     getSession(), (WriteRequest) scheduledWrite.data);
170         }
171 
172         // Free queue
173         writeRequestQueue = null;
174     }
175 
176     /**
177      * Enqueue a message to be written once handshaking is complete.
178      */
179     public synchronized void enqueueWriteRequest(final NextFilter nextFilter,
180             final WriteRequest writeRequest) {
181         if (writeRequestQueue == null) {
182             writeRequestQueue = new LinkedList<Event>();
183         }
184 
185         writeRequestQueue.offer(new Event(nextFilter, writeRequest));
186     }
187 
188     /**
189      * Close the session.
190      */
191     protected void closeSession(final String message, final Throwable t) {
192         if (t != null) {
193             logger.error(message, t);
194             proxyIoSession.setAuthenticationFailed(true);
195         } else {
196             logger.error(message);
197         }
198 
199         getSession().close(true);
200     }
201 
202     /**
203      * Close the session.
204      * 
205      * @param message
206      */
207     protected void closeSession(final String message) {
208         closeSession(message, null);
209     }
210 
211     /**
212      * Event wrapper class for enqueued events.
213      */
214     private final static class Event {
215         private final NextFilter nextFilter;
216 
217         private final Object data;
218 
219         Event(final NextFilter nextFilter, final Object data) {
220             this.nextFilter = nextFilter;
221             this.data = data;
222         }
223 
224         public Object getData() {
225             return data;
226         }
227 
228         public NextFilter getNextFilter() {
229             return nextFilter;
230         }
231     }
232 }