001: /*
002: * $HeadURL:https://svn.apache.org/repos/asf/jakarta/httpcomponents/httpcore/trunk/module-niossl/src/test/java/org/apache/http/mockup/TestHttpSSLServer.java $
003: * $Revision:575703 $
004: * $Date:2007-09-14 16:40:15 +0200 (Fri, 14 Sep 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.mockup;
033:
034: import java.io.IOException;
035: import java.net.InetSocketAddress;
036: import java.net.URL;
037: import java.security.KeyStore;
038:
039: import javax.net.ssl.KeyManager;
040: import javax.net.ssl.KeyManagerFactory;
041: import javax.net.ssl.SSLContext;
042:
043: import org.apache.http.impl.nio.SSLServerIOEventDispatch;
044: import org.apache.http.impl.nio.reactor.DefaultListeningIOReactor;
045: import org.apache.http.nio.NHttpServiceHandler;
046: import org.apache.http.nio.reactor.IOEventDispatch;
047: import org.apache.http.nio.reactor.ListenerEndpoint;
048: import org.apache.http.nio.reactor.ListeningIOReactor;
049: import org.apache.http.params.HttpParams;
050:
051: /**
052: * Trivial test server based on HttpCore NIO SSL
053: *
054: * @author Oleg Kalnichevski
055: */
056: public class TestHttpSSLServer {
057:
058: private final SSLContext sslcontext;
059: private final ListeningIOReactor ioReactor;
060: private final HttpParams params;
061:
062: private volatile IOReactorThread thread;
063: private ListenerEndpoint endpoint;
064:
065: public TestHttpSSLServer(final HttpParams params) throws Exception {
066: super ();
067: this .params = params;
068: this .ioReactor = new DefaultListeningIOReactor(2, this .params);
069:
070: ClassLoader cl = getClass().getClassLoader();
071: URL url = cl.getResource("test.keystore");
072: KeyStore keystore = KeyStore.getInstance("jks");
073: keystore.load(url.openStream(), "nopassword".toCharArray());
074: KeyManagerFactory kmfactory = KeyManagerFactory
075: .getInstance(KeyManagerFactory.getDefaultAlgorithm());
076: kmfactory.init(keystore, "nopassword".toCharArray());
077: KeyManager[] keymanagers = kmfactory.getKeyManagers();
078: this .sslcontext = SSLContext.getInstance("TLS");
079: this .sslcontext.init(keymanagers, null, null);
080: }
081:
082: public HttpParams getParams() {
083: return this .params;
084: }
085:
086: private void execute(final NHttpServiceHandler serviceHandler)
087: throws IOException {
088: IOEventDispatch ioEventDispatch = new SSLServerIOEventDispatch(
089: serviceHandler, this .sslcontext, this .params);
090:
091: this .ioReactor.execute(ioEventDispatch);
092: }
093:
094: public ListenerEndpoint getListenerEndpoint() {
095: return this .endpoint;
096: }
097:
098: public void start(final NHttpServiceHandler serviceHandler) {
099: this .endpoint = this .ioReactor.listen(new InetSocketAddress(0));
100: this .thread = new IOReactorThread(serviceHandler);
101: this .thread.start();
102: }
103:
104: public void shutdown() throws IOException {
105: this .ioReactor.shutdown();
106: try {
107: if (this .thread != null) {
108: this .thread.join(500);
109: }
110: } catch (InterruptedException ignore) {
111: }
112: }
113:
114: private class IOReactorThread extends Thread {
115:
116: private final NHttpServiceHandler serviceHandler;
117:
118: public IOReactorThread(final NHttpServiceHandler serviceHandler) {
119: super ();
120: this .serviceHandler = serviceHandler;
121: }
122:
123: @Override
124: public void run() {
125: try {
126: execute(this .serviceHandler);
127: } catch (IOException ex) {
128: ex.printStackTrace();
129: }
130: }
131:
132: }
133:
134: }
|