001: /*
002: * $HeadURL:https://svn.apache.org/repos/asf/jakarta/httpcomponents/httpcore/trunk/module-niossl/src/test/java/org/apache/http/mockup/TestHttpSSLClient.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.SSLContext;
040: import javax.net.ssl.TrustManager;
041: import javax.net.ssl.TrustManagerFactory;
042:
043: import org.apache.http.impl.nio.SSLClientIOEventDispatch;
044: import org.apache.http.impl.nio.reactor.DefaultConnectingIOReactor;
045: import org.apache.http.nio.NHttpClientHandler;
046: import org.apache.http.nio.reactor.ConnectingIOReactor;
047: import org.apache.http.nio.reactor.IOEventDispatch;
048: import org.apache.http.params.HttpParams;
049:
050: public class TestHttpSSLClient {
051:
052: private final SSLContext sslcontext;
053: private final ConnectingIOReactor ioReactor;
054: private final HttpParams params;
055:
056: private volatile IOReactorThread thread;
057:
058: public TestHttpSSLClient(final HttpParams params) throws Exception {
059: super ();
060: this .params = params;
061: this .ioReactor = new DefaultConnectingIOReactor(2, this .params);
062:
063: ClassLoader cl = getClass().getClassLoader();
064: URL url = cl.getResource("test.keystore");
065: KeyStore keystore = KeyStore.getInstance("jks");
066: keystore.load(url.openStream(), "nopassword".toCharArray());
067: TrustManagerFactory tmfactory = TrustManagerFactory
068: .getInstance(TrustManagerFactory.getDefaultAlgorithm());
069: tmfactory.init(keystore);
070: TrustManager[] trustmanagers = tmfactory.getTrustManagers();
071: this .sslcontext = SSLContext.getInstance("TLS");
072: this .sslcontext.init(null, trustmanagers, null);
073: }
074:
075: public HttpParams getParams() {
076: return this .params;
077: }
078:
079: private void execute(final NHttpClientHandler clientHandler)
080: throws IOException {
081: IOEventDispatch ioEventDispatch = new SSLClientIOEventDispatch(
082: clientHandler, this .sslcontext, this .params);
083:
084: this .ioReactor.execute(ioEventDispatch);
085: }
086:
087: public void openConnection(final InetSocketAddress address,
088: final Object attachment) {
089: this .ioReactor.connect(address, null, attachment, null);
090: }
091:
092: public void start(final NHttpClientHandler clientHandler) {
093: this .thread = new IOReactorThread(clientHandler);
094: this .thread.start();
095: }
096:
097: public void shutdown() throws IOException {
098: this .ioReactor.shutdown();
099: try {
100: if (this .thread != null) {
101: this .thread.join(500);
102: }
103: } catch (InterruptedException ignore) {
104: }
105: }
106:
107: private class IOReactorThread extends Thread {
108:
109: private final NHttpClientHandler clientHandler;
110:
111: public IOReactorThread(final NHttpClientHandler clientHandler) {
112: super ();
113: this .clientHandler = clientHandler;
114: }
115:
116: @Override
117: public void run() {
118: try {
119: execute(this .clientHandler);
120: } catch (IOException ex) {
121: ex.printStackTrace();
122: }
123: }
124:
125: }
126:
127: }
|