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              logger.debug("Event queue CLEARED");
67          }
68      }
69  
70      /**
71       * Event is enqueued only if necessary : 
72       * - socks proxies do not need the reconnection feature so events are always 
73       * forwarded for these.
74       * - http proxies events will be enqueued while handshake has not been completed
75       * or until connection was closed.
76       * If connection was prematurely closed previous events are discarded and only the
77       * session closed is delivered.  
78       * 
79       * @param evt the event to enqueue
80       */
81      public void enqueueEventIfNecessary(final IoSessionEvent evt) {
82          logger.debug("??? >> Enqueue {}", evt);
83  
84          if (proxyIoSession.getRequest() instanceof SocksProxyRequest) {
85              // No reconnection used
86              evt.deliverEvent();
87              
88              return;
89          }
90  
91          if (proxyIoSession.getHandler().isHandshakeComplete()) {
92              evt.deliverEvent();
93          } else {
94              if (evt.getType() == IoSessionEventType.CLOSED) {
95                  if (proxyIoSession.isAuthenticationFailed()) {
96                      proxyIoSession.getConnector().cancelConnectFuture();
97                      discardSessionQueueEvents();
98                      evt.deliverEvent();
99                  } else {
100                     discardSessionQueueEvents();
101                 }
102             } else if (evt.getType() == IoSessionEventType.OPENED) {
103                 // Enqueue event cause it will not reach IoHandler but deliver it to enable 
104                 // session creation.
105                 enqueueSessionEvent(evt);
106                 evt.deliverEvent();
107             } else {
108                 enqueueSessionEvent(evt);
109             }
110         }
111     }
112 
113     /**
114      * Send any session event which were queued while waiting for handshaking to complete.
115      * 
116      * Please note this is an internal method. DO NOT USE it in your code.
117      * 
118      * @throws Exception If something went wrong while flushing the pending events
119      */
120     public void flushPendingSessionEvents() throws Exception {
121         synchronized (sessionEventsQueue) {
122             IoSessionEvent evt;
123 
124             while ((evt = sessionEventsQueue.poll()) != null) {
125                 logger.debug(" Flushing buffered event: {}", evt);
126                 evt.deliverEvent();
127             }
128         }
129     }
130 
131     /**
132      * Enqueue an event to be delivered once handshaking is complete.
133      * 
134      * @param evt the session event to enqueue
135      */
136     private void enqueueSessionEvent(final IoSessionEvent evt) {
137         synchronized (sessionEventsQueue) {
138             logger.debug("Enqueuing event: {}", evt);
139             sessionEventsQueue.offer(evt);
140         }
141     }
142 }