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.event;
21  
22  import java.util.LinkedList;
23  import java.util.Queue;
24  
25  import org.apache.mina.proxy.handlers.socks.SocksProxyRequest;
26  import org.apache.mina.proxy.session.ProxyIoSession;
27  import org.slf4j.Logger;
28  import org.slf4j.LoggerFactory;
29  
30  /**
31   * IoSessionEventQueue.java - Queue that contains filtered session events 
32   * while handshake isn't done.
33   * 
34   * @author <a href="http://mina.apache.org">Apache MINA Project</a>
35   * @since MINA 2.0.0-M3
36   */
37  public class IoSessionEventQueue {
38      private final static Logger logger = LoggerFactory
39              .getLogger(IoSessionEventQueue.class);
40  
41      /**
42       * The proxy session object.
43       */
44      private ProxyIoSession proxyIoSession;
45  
46      /**
47       * Queue of session events which occurred before the proxy handshake had completed.
48       */
49      private Queue<IoSessionEvent> sessionEventsQueue = new LinkedList<IoSessionEvent>();
50  
51      public IoSessionEventQueue(ProxyIoSession proxyIoSession) {
52          this.proxyIoSession = proxyIoSession;
53      }
54  
55      /**
56       * Discard all events from the queue.
57       */
58      private void discardSessionQueueEvents() {
59          synchronized (sessionEventsQueue) {
60              // Free queue
61              sessionEventsQueue.clear();
62              logger.debug("Event queue CLEARED");
63          }
64      }
65  
66      /**
67       * Event is enqueued only if necessary : 
68       * - socks proxies do not need the reconnection feature so events are always 
69       * forwarded for these.
70       * - http proxies events will be enqueued while handshake has not been completed
71       * or until connection was closed.
72       * If connection was prematurely closed previous events are discarded and only the
73       * session closed is delivered.  
74       * 
75       * @param evt the event to enqueue
76       */
77      public void enqueueEventIfNecessary(final IoSessionEvent evt) {
78          logger.debug("??? >> Enqueue {}", evt);
79  
80          if (proxyIoSession.getRequest() instanceof SocksProxyRequest) {
81              // No reconnection used
82              evt.deliverEvent();
83              return;
84          }
85  
86          if (proxyIoSession.getHandler().isHandshakeComplete()) {
87              evt.deliverEvent();
88          } else {
89              if (evt.getType() == IoSessionEventType.CLOSED) {
90                  if (proxyIoSession.isAuthenticationFailed()) {
91                      proxyIoSession.getConnector().cancelConnectFuture();
92                      discardSessionQueueEvents();
93                      evt.deliverEvent();
94                  } else {
95                      discardSessionQueueEvents();
96                  }
97              } else if (evt.getType() == IoSessionEventType.OPENED) {
98                  // Enqueue event cause it will not reach IoHandler but deliver it to enable 
99                  // session creation.
100                 enqueueSessionEvent(evt);
101                 evt.deliverEvent();
102             } else {
103                 enqueueSessionEvent(evt);
104             }
105         }
106     }
107 
108     /**
109      * Send any session event which were queued while waiting for handshaking to complete.
110      * 
111      * Please note this is an internal method. DO NOT USE it in your code.
112      */
113     public void flushPendingSessionEvents() throws Exception {
114         synchronized (sessionEventsQueue) {
115             IoSessionEvent evt;
116             
117             while ((evt = sessionEventsQueue.poll()) != null) {
118                 logger.debug(" Flushing buffered event: {}", evt);    
119                 evt.deliverEvent();
120             }
121         }
122     }
123 
124     /**
125      * Enqueue an event to be delivered once handshaking is complete.
126      * 
127      * @param evt the session event to enqueue
128      */
129     private void enqueueSessionEvent(final IoSessionEvent evt) {
130         synchronized (sessionEventsQueue) {
131             logger.debug("Enqueuing event: {}", evt);
132             sessionEventsQueue.offer(evt);
133         }    
134     }
135 }