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