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