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.DefaultHttpRequestFactory;
027: import org.apache.http.impl.nio.DefaultNHttpServerConnection;
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.NHttpServiceHandler;
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 SSLServerIOEventDispatch 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 NHttpServiceHandler handler;
043: private final SSLContext sslcontext;
044: private final SSLIOSessionHandler sslHandler;
045: private final HttpParams params;
046:
047: public SSLServerIOEventDispatch(final NHttpServiceHandler 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 service 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 LoggingNHttpServiceHandler(handler);
065: this .params = params;
066: this .sslcontext = sslcontext;
067: this .sslHandler = sslHandler;
068: }
069:
070: public SSLServerIOEventDispatch(final NHttpServiceHandler 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: LoggingNHttpServerConnection conn = new LoggingNHttpServerConnection(
081: new LoggingIOSession(sslSession),
082: new DefaultHttpRequestFactory(),
083: new HeapByteBufferAllocator(), this .params);
084:
085: session.setAttribute(NHTTP_CONN, conn);
086: session.setAttribute(SSL_SESSION, sslSession);
087:
088: this .handler.connected(conn);
089:
090: try {
091: sslSession.bind(SSLMode.SERVER, this .params);
092: } catch (SSLException ex) {
093: this .handler.exception(conn, ex);
094: sslSession.shutdown();
095: }
096: }
097:
098: public void disconnected(final IOSession session) {
099: DefaultNHttpServerConnection conn = (DefaultNHttpServerConnection) session
100: .getAttribute(NHTTP_CONN);
101: this .handler.closed(conn);
102: }
103:
104: public void inputReady(final IOSession session) {
105: DefaultNHttpServerConnection conn = (DefaultNHttpServerConnection) session
106: .getAttribute(NHTTP_CONN);
107: SSLIOSession sslSession = (SSLIOSession) session
108: .getAttribute(SSL_SESSION);
109: try {
110: synchronized (sslSession) {
111: if (sslSession.isAppInputReady()) {
112: conn.consumeInput(this .handler);
113: }
114: sslSession.inboundTransport();
115: }
116: } catch (IOException ex) {
117: this .handler.exception(conn, ex);
118: sslSession.shutdown();
119: }
120: }
121:
122: public void outputReady(final IOSession session) {
123: DefaultNHttpServerConnection conn = (DefaultNHttpServerConnection) session
124: .getAttribute(NHTTP_CONN);
125: SSLIOSession sslSession = (SSLIOSession) session
126: .getAttribute(SSL_SESSION);
127: try {
128: synchronized (sslSession) {
129: if (sslSession.isAppOutputReady()) {
130: conn.produceOutput(this .handler);
131: }
132: sslSession.outboundTransport();
133: }
134: } catch (IOException ex) {
135: this .handler.exception(conn, ex);
136: sslSession.shutdown();
137: }
138: }
139:
140: public void timeout(final IOSession session) {
141: DefaultNHttpServerConnection conn = (DefaultNHttpServerConnection) session
142: .getAttribute(NHTTP_CONN);
143: SSLIOSession sslSession = (SSLIOSession) session
144: .getAttribute(SSL_SESSION);
145: this .handler.timeout(conn);
146: synchronized (sslSession) {
147: if (sslSession.isOutboundDone()
148: && !sslSession.isInboundDone()) {
149: // The session failed to terminate cleanly
150: sslSession.shutdown();
151: }
152: }
153: }
154:
155: }
|