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