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.client;
29
30 import java.io.IOException;
31 import java.nio.channels.CancelledKeyException;
32
33 import org.apache.commons.logging.Log;
34 import org.apache.commons.logging.LogFactory;
35 import org.apache.http.impl.nio.DefaultNHttpClientConnection;
36 import org.apache.http.impl.nio.reactor.AbstractIODispatch;
37 import org.apache.http.nio.NHttpClientEventHandler;
38 import org.apache.http.nio.reactor.IOSession;
39
40 class InternalIODispatch extends AbstractIODispatch<DefaultNHttpClientConnection> {
41
42 private final Log log = LogFactory.getLog(InternalIODispatch.class);
43
44 private final NHttpClientEventHandler handler;
45
46 public InternalIODispatch(final NHttpClientEventHandler handler) {
47 super();
48 if (this.log.isDebugEnabled()) {
49 this.handler = new InternalRequestExecutor(this.log, handler);
50 } else {
51 this.handler = handler;
52 }
53 }
54
55 @Override
56 protected DefaultNHttpClientConnection createConnection(final IOSession session) {
57
58
59
60 log.debug("Unexpected invocation of #createConnection");
61 session.close();
62 throw new CancelledKeyException();
63 }
64
65 @Override
66 protected void onConnected(final DefaultNHttpClientConnection conn) {
67 final Object attachment = conn.getContext().getAttribute(IOSession.ATTACHMENT_KEY);
68 try {
69 this.handler.connected(conn, attachment);
70 } catch (final Exception ex) {
71 this.handler.exception(conn, ex);
72 }
73 }
74
75 @Override
76 protected void onClosed(final DefaultNHttpClientConnection conn) {
77 this.handler.closed(conn);
78 }
79
80 @Override
81 protected void onException(final DefaultNHttpClientConnection conn, final IOException ex) {
82 this.handler.exception(conn, ex);
83 }
84
85 @Override
86 protected void onInputReady(final DefaultNHttpClientConnection conn) {
87 conn.consumeInput(this.handler);
88 }
89
90 @Override
91 protected void onOutputReady(final DefaultNHttpClientConnection conn) {
92 conn.produceOutput(this.handler);
93 }
94
95 @Override
96 protected void onTimeout(final DefaultNHttpClientConnection conn) {
97 try {
98 this.handler.timeout(conn);
99 } catch (final Exception ex) {
100 this.handler.exception(conn, ex);
101 }
102 }
103
104 }