1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.mina.proxy;
21
22 import java.util.LinkedList;
23 import java.util.Queue;
24
25 import org.apache.mina.core.buffer.IoBuffer;
26 import org.apache.mina.core.filterchain.IoFilter.NextFilter;
27 import org.apache.mina.core.future.DefaultWriteFuture;
28 import org.apache.mina.core.future.WriteFuture;
29 import org.apache.mina.core.session.IoSession;
30 import org.apache.mina.core.write.DefaultWriteRequest;
31 import org.apache.mina.core.write.WriteRequest;
32 import org.apache.mina.proxy.filter.ProxyFilter;
33 import org.apache.mina.proxy.filter.ProxyHandshakeIoBuffer;
34 import org.apache.mina.proxy.session.ProxyIoSession;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37
38
39
40
41
42
43
44
45
46
47 public abstract class AbstractProxyLogicHandler implements ProxyLogicHandler {
48
49 private final static Logger LOGGER = LoggerFactory.getLogger(AbstractProxyLogicHandler.class);
50
51
52
53
54 private ProxyIoSession proxyIoSession;
55
56
57
58
59 private Queue<Event> writeRequestQueue = null;
60
61
62
63
64 private boolean handshakeComplete = false;
65
66
67
68
69
70
71 public AbstractProxyLogicHandler(ProxyIoSession proxyIoSession) {
72 this.proxyIoSession = proxyIoSession;
73 }
74
75
76
77
78 protected ProxyFilter getProxyFilter() {
79 return proxyIoSession.getProxyFilter();
80 }
81
82
83
84
85 protected IoSession getSession() {
86 return proxyIoSession.getSession();
87 }
88
89
90
91
92 public ProxyIoSession getProxyIoSession() {
93 return proxyIoSession;
94 }
95
96
97
98
99
100
101
102
103 protected WriteFuture writeData(final NextFilter nextFilter, final IoBuffer data) {
104
105 ProxyHandshakeIoBufferer.html#ProxyHandshakeIoBuffer">ProxyHandshakeIoBuffer writeBuffer = new ProxyHandshakeIoBuffer(data);
106
107 if (LOGGER.isDebugEnabled()) {
108 LOGGER.debug(" session write: {}", writeBuffer);
109 }
110
111 WriteFuture writeFuture = new DefaultWriteFuture(getSession());
112 getProxyFilter().writeData(nextFilter, getSession(), new DefaultWriteRequest(writeBuffer, writeFuture), true);
113
114 return writeFuture;
115 }
116
117
118
119
120
121 public boolean isHandshakeComplete() {
122 synchronized (this) {
123 return handshakeComplete;
124 }
125 }
126
127
128
129
130 protected final void setHandshakeComplete() {
131 synchronized (this) {
132 handshakeComplete = true;
133 }
134
135 ProxyIoSession proxyIoSession = getProxyIoSession();
136 proxyIoSession.getConnector().fireConnected(proxyIoSession.getSession()).awaitUninterruptibly();
137
138 if (LOGGER.isDebugEnabled()) {
139 LOGGER.debug(" handshake completed");
140 }
141
142
143 try {
144 proxyIoSession.getEventQueue().flushPendingSessionEvents();
145 flushPendingWriteRequests();
146 } catch (Exception ex) {
147 LOGGER.error("Unable to flush pending write requests", ex);
148 }
149 }
150
151
152
153
154
155
156 protected synchronized void flushPendingWriteRequests() throws Exception {
157 if (LOGGER.isDebugEnabled()) {
158 LOGGER.debug(" flushPendingWriteRequests()");
159 }
160
161 if (writeRequestQueue == null) {
162 return;
163 }
164
165 Event scheduledWrite;
166 while ((scheduledWrite = writeRequestQueue.poll()) != null) {
167 if (LOGGER.isDebugEnabled()) {
168 LOGGER.debug(" Flushing buffered write request: {}", scheduledWrite.data);
169 }
170
171 getProxyFilter().filterWrite(scheduledWrite.nextFilter, getSession(), (WriteRequest) scheduledWrite.data);
172 }
173
174
175 writeRequestQueue = null;
176 }
177
178
179
180
181 public synchronized void enqueueWriteRequest(final NextFilter nextFilter, final WriteRequest writeRequest) {
182 if (writeRequestQueue == null) {
183 writeRequestQueue = new LinkedList<Event>();
184 }
185
186 writeRequestQueue.offer(new Event(nextFilter, writeRequest));
187 }
188
189
190
191
192
193
194
195 protected void closeSession(final String message, final Throwable t) {
196 if (t != null) {
197 LOGGER.error(message, t);
198 proxyIoSession.setAuthenticationFailed(true);
199 } else {
200 LOGGER.error(message);
201 }
202
203 getSession().closeNow();
204 }
205
206
207
208
209
210
211 protected void closeSession(final String message) {
212 closeSession(message, null);
213 }
214
215
216
217
218 private final static class Event {
219 private final NextFilter nextFilter;
220
221 private final Object data;
222
223 Event(final NextFilter nextFilter, final Object data) {
224 this.nextFilter = nextFilter;
225 this.data = data;
226 }
227 }
228 }