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 ProxyHandshakeIoBuffer writeBuffer = new ProxyHandshakeIoBuffer(data);
106
107 LOGGER.debug(" session write: {}", writeBuffer);
108
109 WriteFuture writeFuture = new DefaultWriteFuture(getSession());
110 getProxyFilter().writeData(nextFilter, getSession(), new DefaultWriteRequest(writeBuffer, writeFuture), true);
111
112 return writeFuture;
113 }
114
115
116
117
118
119 public boolean isHandshakeComplete() {
120 synchronized (this) {
121 return handshakeComplete;
122 }
123 }
124
125
126
127
128 protected final void setHandshakeComplete() {
129 synchronized (this) {
130 handshakeComplete = true;
131 }
132
133 ProxyIoSession proxyIoSession = getProxyIoSession();
134 proxyIoSession.getConnector().fireConnected(proxyIoSession.getSession()).awaitUninterruptibly();
135
136 LOGGER.debug(" handshake completed");
137
138
139 try {
140 proxyIoSession.getEventQueue().flushPendingSessionEvents();
141 flushPendingWriteRequests();
142 } catch (Exception ex) {
143 LOGGER.error("Unable to flush pending write requests", ex);
144 }
145 }
146
147
148
149
150
151
152 protected synchronized void flushPendingWriteRequests() throws Exception {
153 LOGGER.debug(" flushPendingWriteRequests()");
154
155 if (writeRequestQueue == null) {
156 return;
157 }
158
159 Event scheduledWrite;
160 while ((scheduledWrite = writeRequestQueue.poll()) != null) {
161 LOGGER.debug(" Flushing buffered write request: {}", scheduledWrite.data);
162
163 getProxyFilter().filterWrite(scheduledWrite.nextFilter, getSession(), (WriteRequest) scheduledWrite.data);
164 }
165
166
167 writeRequestQueue = null;
168 }
169
170
171
172
173 public synchronized void enqueueWriteRequest(final NextFilter nextFilter, final WriteRequest writeRequest) {
174 if (writeRequestQueue == null) {
175 writeRequestQueue = new LinkedList<Event>();
176 }
177
178 writeRequestQueue.offer(new Event(nextFilter, writeRequest));
179 }
180
181
182
183
184
185
186
187 protected void closeSession(final String message, final Throwable t) {
188 if (t != null) {
189 LOGGER.error(message, t);
190 proxyIoSession.setAuthenticationFailed(true);
191 } else {
192 LOGGER.error(message);
193 }
194
195 getSession().closeNow();
196 }
197
198
199
200
201
202
203 protected void closeSession(final String message) {
204 closeSession(message, null);
205 }
206
207
208
209
210 private final static class Event {
211 private final NextFilter nextFilter;
212
213 private final Object data;
214
215 Event(final NextFilter nextFilter, final Object data) {
216 this.nextFilter = nextFilter;
217 this.data = data;
218 }
219 }
220 }