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.impl.nio.reactor.AbstractIODispatch;
36 import org.apache.http.nio.NHttpConnectionFactory;
37 import org.apache.http.nio.NHttpServerEventHandler;
38 import org.apache.http.nio.reactor.IOEventDispatch;
39 import org.apache.http.nio.reactor.IOSession;
40 import org.apache.http.nio.reactor.ssl.SSLSetupHandler;
41 import org.apache.http.params.HttpParams;
42
43
44
45
46
47
48
49 @Immutable
50 public class DefaultHttpServerIODispatch
51 extends AbstractIODispatch<DefaultNHttpServerConnection> {
52
53 private final NHttpServerEventHandler handler;
54 private final NHttpConnectionFactory<DefaultNHttpServerConnection> connFactory;
55
56 public DefaultHttpServerIODispatch(
57 final NHttpServerEventHandler handler,
58 final NHttpConnectionFactory<DefaultNHttpServerConnection> connFactory) {
59 super();
60 if (handler == null) {
61 throw new IllegalArgumentException("HTTP client handler may not be null");
62 }
63 if (connFactory == null) {
64 throw new IllegalArgumentException("HTTP server connection factory is null");
65 }
66 this.handler = handler;
67 this.connFactory = connFactory;
68 }
69
70 public DefaultHttpServerIODispatch(
71 final NHttpServerEventHandler handler,
72 final HttpParams params) {
73 this(handler, new DefaultNHttpServerConnectionFactory(params));
74 }
75
76 public DefaultHttpServerIODispatch(
77 final NHttpServerEventHandler handler,
78 final SSLContext sslcontext,
79 final SSLSetupHandler sslHandler,
80 final HttpParams params) {
81 this(handler, new SSLNHttpServerConnectionFactory(sslcontext, sslHandler, params));
82 }
83
84 public DefaultHttpServerIODispatch(
85 final NHttpServerEventHandler handler,
86 final SSLContext sslcontext,
87 final HttpParams params) {
88 this(handler, sslcontext, null, params);
89 }
90
91 @Override
92 protected DefaultNHttpServerConnection createConnection(final IOSession session) {
93 return this.connFactory.createConnection(session);
94 }
95
96 @Override
97 protected void onConnected(final DefaultNHttpServerConnection conn) {
98 try {
99 this.handler.connected(conn);
100 } catch (Exception ex) {
101 this.handler.exception(conn, ex);
102 }
103 }
104
105 @Override
106 protected void onClosed(final DefaultNHttpServerConnection conn) {
107 this.handler.closed(conn);
108 }
109
110 @Override
111 protected void onException(final DefaultNHttpServerConnection conn, IOException ex) {
112 this.handler.exception(conn, ex);
113 }
114
115 @Override
116 protected void onInputReady(final DefaultNHttpServerConnection conn) {
117 conn.consumeInput(this.handler);
118 }
119
120 @Override
121 protected void onOutputReady(final DefaultNHttpServerConnection conn) {
122 conn.produceOutput(this.handler);
123 }
124
125 @Override
126 protected void onTimeout(final DefaultNHttpServerConnection conn) {
127 try {
128 this.handler.timeout(conn);
129 } catch (Exception ex) {
130 this.handler.exception(conn, ex);
131 }
132 }
133
134 }