001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */
019: package org.apache.axis2.transport.nhttp;
020:
021: import java.io.IOException;
022:
023: import javax.net.ssl.SSLContext;
024: import javax.net.ssl.SSLException;
025:
026: import org.apache.http.impl.DefaultHttpResponseFactory;
027: import org.apache.http.impl.nio.DefaultNHttpClientConnection;
028: import org.apache.http.impl.nio.reactor.SSLIOSession;
029: import org.apache.http.impl.nio.reactor.SSLIOSessionHandler;
030: import org.apache.http.impl.nio.reactor.SSLMode;
031: import org.apache.http.nio.NHttpClientHandler;
032: import org.apache.http.nio.reactor.IOEventDispatch;
033: import org.apache.http.nio.reactor.IOSession;
034: import org.apache.http.nio.util.HeapByteBufferAllocator;
035: import org.apache.http.params.HttpParams;
036:
037: public class SSLClientIOEventDispatch implements IOEventDispatch {
038:
039: private static final String NHTTP_CONN = "AXIS2.NHTTP_CONN";
040: private static final String SSL_SESSION = "AXIS2.SSL_SESSION";
041:
042: private final NHttpClientHandler handler;
043: private final HttpParams params;
044: private final SSLContext sslcontext;
045: private final SSLIOSessionHandler sslHandler;
046:
047: public SSLClientIOEventDispatch(final NHttpClientHandler handler,
048: final SSLContext sslcontext,
049: final SSLIOSessionHandler sslHandler,
050: final HttpParams params) {
051: super ();
052: if (handler == null) {
053: throw new IllegalArgumentException(
054: "HTTP client handler may not be null");
055: }
056: if (sslcontext == null) {
057: throw new IllegalArgumentException(
058: "SSL context may not be null");
059: }
060: if (params == null) {
061: throw new IllegalArgumentException(
062: "HTTP parameters may not be null");
063: }
064: this .handler = new LoggingNHttpClientHandler(handler);
065: this .params = params;
066: this .sslcontext = sslcontext;
067: this .sslHandler = sslHandler;
068: }
069:
070: public SSLClientIOEventDispatch(final NHttpClientHandler handler,
071: final SSLContext sslcontext, final HttpParams params) {
072: this (handler, sslcontext, null, params);
073: }
074:
075: public void connected(final IOSession session) {
076:
077: SSLIOSession sslSession = new SSLIOSession(session,
078: this .sslcontext, this .sslHandler);
079:
080: LoggingNHttpClientConnection conn = new LoggingNHttpClientConnection(
081: new LoggingIOSession(sslSession),
082: new DefaultHttpResponseFactory(),
083: new HeapByteBufferAllocator(), this .params);
084:
085: session.setAttribute(NHTTP_CONN, conn);
086: session.setAttribute(SSL_SESSION, sslSession);
087:
088: Object attachment = session
089: .getAttribute(IOSession.ATTACHMENT_KEY);
090: this .handler.connected(conn, attachment);
091:
092: try {
093: sslSession.bind(SSLMode.CLIENT, this .params);
094: } catch (SSLException ex) {
095: this .handler.exception(conn, ex);
096: sslSession.shutdown();
097: }
098: }
099:
100: public void disconnected(final IOSession session) {
101: DefaultNHttpClientConnection conn = (DefaultNHttpClientConnection) session
102: .getAttribute(NHTTP_CONN);
103:
104: this .handler.closed(conn);
105: }
106:
107: public void inputReady(final IOSession session) {
108: DefaultNHttpClientConnection conn = (DefaultNHttpClientConnection) session
109: .getAttribute(NHTTP_CONN);
110: SSLIOSession sslSession = (SSLIOSession) session
111: .getAttribute(SSL_SESSION);
112: try {
113: synchronized (sslSession) {
114: while (sslSession.isAppInputReady()) {
115: conn.consumeInput(this .handler);
116: }
117: sslSession.inboundTransport();
118: }
119: } catch (IOException ex) {
120: this .handler.exception(conn, ex);
121: sslSession.shutdown();
122: }
123: }
124:
125: public void outputReady(final IOSession session) {
126: DefaultNHttpClientConnection conn = (DefaultNHttpClientConnection) session
127: .getAttribute(NHTTP_CONN);
128: SSLIOSession sslSession = (SSLIOSession) session
129: .getAttribute(SSL_SESSION);
130: try {
131: synchronized (sslSession) {
132: if (sslSession.isAppOutputReady()) {
133: conn.produceOutput(this .handler);
134: }
135: sslSession.outboundTransport();
136: }
137: } catch (IOException ex) {
138: this .handler.exception(conn, ex);
139: sslSession.shutdown();
140: }
141: }
142:
143: public void timeout(final IOSession session) {
144: DefaultNHttpClientConnection conn = (DefaultNHttpClientConnection) session
145: .getAttribute(NHTTP_CONN);
146: SSLIOSession sslSession = (SSLIOSession) session
147: .getAttribute(SSL_SESSION);
148: this .handler.timeout(conn);
149: synchronized (sslSession) {
150: if (sslSession.isOutboundDone()
151: && !sslSession.isInboundDone()) {
152: // The session failed to terminate cleanly
153: sslSession.shutdown();
154: }
155: }
156: }
157:
158: }
|