1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28 package org.apache.http.impl.nio;
29
30 import java.io.IOException;
31
32 import javax.net.ssl.SSLContext;
33
34 import org.apache.http.annotation.Immutable;
35 import org.apache.http.config.ConnectionConfig;
36 import org.apache.http.impl.nio.reactor.AbstractIODispatch;
37 import org.apache.http.nio.NHttpConnectionFactory;
38 import org.apache.http.nio.NHttpServerEventHandler;
39 import org.apache.http.nio.reactor.IOEventDispatch;
40 import org.apache.http.nio.reactor.IOSession;
41 import org.apache.http.nio.reactor.ssl.SSLSetupHandler;
42 import org.apache.http.params.HttpParams;
43 import org.apache.http.util.Args;
44
45
46
47
48
49
50
51 @SuppressWarnings("deprecation")
52 @Immutable
53 public class DefaultHttpServerIODispatch
54 extends AbstractIODispatch<DefaultNHttpServerConnection> {
55
56 private final NHttpServerEventHandler handler;
57 private final NHttpConnectionFactory<DefaultNHttpServerConnection> connFactory;
58
59 public DefaultHttpServerIODispatch(
60 final NHttpServerEventHandler handler,
61 final NHttpConnectionFactory<DefaultNHttpServerConnection> connFactory) {
62 super();
63 this.handler = Args.notNull(handler, "HTTP client handler");
64 this.connFactory = Args.notNull(connFactory, "HTTP server connection factory");
65 }
66
67
68
69
70
71 @Deprecated
72 public DefaultHttpServerIODispatch(
73 final NHttpServerEventHandler handler,
74 final HttpParams params) {
75 this(handler, new DefaultNHttpServerConnectionFactory(params));
76 }
77
78
79
80
81
82 @Deprecated
83 public DefaultHttpServerIODispatch(
84 final NHttpServerEventHandler handler,
85 final SSLContext sslcontext,
86 final SSLSetupHandler sslHandler,
87 final HttpParams params) {
88 this(handler, new SSLNHttpServerConnectionFactory(sslcontext, sslHandler, params));
89 }
90
91
92
93
94
95 @Deprecated
96 public DefaultHttpServerIODispatch(
97 final NHttpServerEventHandler handler,
98 final SSLContext sslcontext,
99 final HttpParams params) {
100 this(handler, sslcontext, null, params);
101 }
102
103
104
105
106 public DefaultHttpServerIODispatch(final NHttpServerEventHandler handler, final ConnectionConfig config) {
107 this(handler, new DefaultNHttpServerConnectionFactory(config));
108 }
109
110
111
112
113 public DefaultHttpServerIODispatch(
114 final NHttpServerEventHandler handler,
115 final SSLContext sslcontext,
116 final SSLSetupHandler sslHandler,
117 final ConnectionConfig config) {
118 this(handler, new SSLNHttpServerConnectionFactory(sslcontext, sslHandler, config));
119 }
120
121
122
123
124 public DefaultHttpServerIODispatch(
125 final NHttpServerEventHandler handler,
126 final SSLContext sslcontext,
127 final ConnectionConfig config) {
128 this(handler, new SSLNHttpServerConnectionFactory(sslcontext, null, config));
129 }
130
131 @Override
132 protected DefaultNHttpServerConnection createConnection(final IOSession session) {
133 return this.connFactory.createConnection(session);
134 }
135
136 @Override
137 protected void onConnected(final DefaultNHttpServerConnection conn) {
138 try {
139 this.handler.connected(conn);
140 } catch (final Exception ex) {
141 this.handler.exception(conn, ex);
142 }
143 }
144
145 @Override
146 protected void onClosed(final DefaultNHttpServerConnection conn) {
147 this.handler.closed(conn);
148 }
149
150 @Override
151 protected void onException(final DefaultNHttpServerConnection conn, final IOException ex) {
152 this.handler.exception(conn, ex);
153 }
154
155 @Override
156 protected void onInputReady(final DefaultNHttpServerConnection conn) {
157 conn.consumeInput(this.handler);
158 }
159
160 @Override
161 protected void onOutputReady(final DefaultNHttpServerConnection conn) {
162 conn.produceOutput(this.handler);
163 }
164
165 @Override
166 protected void onTimeout(final DefaultNHttpServerConnection conn) {
167 try {
168 this.handler.timeout(conn);
169 } catch (final Exception ex) {
170 this.handler.exception(conn, ex);
171 }
172 }
173
174 }