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