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/SSLServerIOEventDispatch.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.HttpRequestFactory;
040: import org.apache.http.impl.DefaultHttpRequestFactory;
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.NHttpServerIOTarget;
045: import org.apache.http.nio.NHttpServiceHandler;
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 SSLServerIOEventDispatch 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 NHttpServiceHandler handler;
059: protected final SSLContext sslcontext;
060: protected final SSLIOSessionHandler sslHandler;
061: protected final HttpParams params;
062:
063: public SSLServerIOEventDispatch(final NHttpServiceHandler 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 service 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 SSLServerIOEventDispatch(final NHttpServiceHandler 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 HttpRequestFactory createHttpRequestFactory() {
097: return new DefaultHttpRequestFactory();
098: }
099:
100: protected NHttpServerIOTarget createConnection(
101: final IOSession session) {
102: return new DefaultNHttpServerConnection(session,
103: createHttpRequestFactory(), this .allocator, this .params);
104: }
105:
106: public void connected(final IOSession session) {
107:
108: SSLIOSession sslSession = new SSLIOSession(session,
109: this .sslcontext, this .sslHandler);
110:
111: NHttpServerIOTarget conn = createConnection(sslSession);
112:
113: session.setAttribute(NHTTP_CONN, conn);
114: session.setAttribute(SSL_SESSION, sslSession);
115:
116: this .handler.connected(conn);
117:
118: try {
119: sslSession.bind(SSLMode.SERVER, this .params);
120: } catch (SSLException ex) {
121: this .handler.exception(conn, ex);
122: sslSession.shutdown();
123: }
124: }
125:
126: public void disconnected(final IOSession session) {
127: NHttpServerIOTarget conn = (NHttpServerIOTarget) session
128: .getAttribute(NHTTP_CONN);
129:
130: this .handler.closed(conn);
131: }
132:
133: public void inputReady(final IOSession session) {
134: NHttpServerIOTarget conn = (NHttpServerIOTarget) session
135: .getAttribute(NHTTP_CONN);
136: SSLIOSession sslSession = (SSLIOSession) session
137: .getAttribute(SSL_SESSION);
138:
139: try {
140: if (sslSession.isAppInputReady()) {
141: conn.consumeInput(this .handler);
142: }
143: sslSession.inboundTransport();
144: } catch (IOException ex) {
145: this .handler.exception(conn, ex);
146: sslSession.shutdown();
147: }
148: }
149:
150: public void outputReady(final IOSession session) {
151: NHttpServerIOTarget conn = (NHttpServerIOTarget) session
152: .getAttribute(NHTTP_CONN);
153: SSLIOSession sslSession = (SSLIOSession) session
154: .getAttribute(SSL_SESSION);
155:
156: try {
157: if (sslSession.isAppOutputReady()) {
158: conn.produceOutput(this .handler);
159: }
160: sslSession.outboundTransport();
161: } catch (IOException ex) {
162: this .handler.exception(conn, ex);
163: sslSession.shutdown();
164: }
165: }
166:
167: public void timeout(final IOSession session) {
168: NHttpServerIOTarget conn = (NHttpServerIOTarget) session
169: .getAttribute(NHTTP_CONN);
170: SSLIOSession sslSession = (SSLIOSession) session
171: .getAttribute(SSL_SESSION);
172:
173: this .handler.timeout(conn);
174: synchronized (sslSession) {
175: if (sslSession.isOutboundDone()
176: && !sslSession.isInboundDone()) {
177: // The session failed to cleanly terminate
178: sslSession.shutdown();
179: }
180: }
181: }
182:
183: }
|