01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one
03: * or more contributor license agreements. See the NOTICE file
04: * distributed with this work for additional information
05: * regarding copyright ownership. The ASF licenses this file
06: * to you under the Apache License, Version 2.0 (the
07: * "License"); you may not use this file except in compliance
08: * with the License. You may obtain a copy of the License at
09: *
10: * http://www.apache.org/licenses/LICENSE-2.0
11: *
12: * Unless required by applicable law or agreed to in writing,
13: * software distributed under the License is distributed on an
14: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15: * KIND, either express or implied. See the License for the
16: * specific language governing permissions and limitations
17: * under the License.
18: */
19: package org.apache.axis2.transport.nhttp;
20:
21: import org.apache.http.impl.DefaultHttpResponseFactory;
22: import org.apache.http.impl.nio.DefaultNHttpClientConnection;
23: import org.apache.http.nio.NHttpClientHandler;
24: import org.apache.http.nio.reactor.IOEventDispatch;
25: import org.apache.http.nio.reactor.IOSession;
26: import org.apache.http.nio.util.HeapByteBufferAllocator;
27: import org.apache.http.params.HttpParams;
28:
29: public class PlainClientIOEventDispatch implements IOEventDispatch {
30:
31: private static final String NHTTP_CONN = "AXIS2.NHTTP_CONN";
32:
33: private final NHttpClientHandler handler;
34: private final HttpParams params;
35:
36: public PlainClientIOEventDispatch(final NHttpClientHandler handler,
37: final HttpParams params) {
38: super ();
39: if (handler == null) {
40: throw new IllegalArgumentException(
41: "HTTP client handler may not be null");
42: }
43: if (params == null) {
44: throw new IllegalArgumentException(
45: "HTTP parameters may not be null");
46: }
47: // Decorate client handler with logging capabilities
48: this .handler = new LoggingNHttpClientHandler(handler);
49: this .params = params;
50: }
51:
52: public void connected(final IOSession session) {
53: // Decorate I/O session with logging capabilities
54: LoggingNHttpClientConnection conn = new LoggingNHttpClientConnection(
55: new LoggingIOSession(session),
56: new DefaultHttpResponseFactory(),
57: new HeapByteBufferAllocator(), this .params);
58: session.setAttribute(NHTTP_CONN, conn);
59:
60: Object attachment = session
61: .getAttribute(IOSession.ATTACHMENT_KEY);
62: this .handler.connected(conn, attachment);
63: }
64:
65: public void disconnected(final IOSession session) {
66: DefaultNHttpClientConnection conn = (DefaultNHttpClientConnection) session
67: .getAttribute(NHTTP_CONN);
68: this .handler.closed(conn);
69: }
70:
71: public void inputReady(final IOSession session) {
72: DefaultNHttpClientConnection conn = (DefaultNHttpClientConnection) session
73: .getAttribute(NHTTP_CONN);
74: conn.consumeInput(this .handler);
75: }
76:
77: public void outputReady(final IOSession session) {
78: DefaultNHttpClientConnection conn = (DefaultNHttpClientConnection) session
79: .getAttribute(NHTTP_CONN);
80: conn.produceOutput(this .handler);
81: }
82:
83: public void timeout(final IOSession session) {
84: DefaultNHttpClientConnection conn = (DefaultNHttpClientConnection) session
85: .getAttribute(NHTTP_CONN);
86: this.handler.timeout(conn);
87: }
88:
89: }
|