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