2
* $HeadURL:https://svn.apache.org/repos/asf/jakarta/httpcomponents/httpcore/trunk/module-niossl/src/test/java/org/apache/http/mockup/TestHttpSSLClient.java $
4
* $Date:2007-09-14 16:40:15 +0200 (Fri, 14 Sep 2007) $
6
* ====================================================================
7
* Licensed to the Apache Software Foundation (ASF) under one
8
* or more contributor license agreements. See the NOTICE file
9
* distributed with this work for additional information
10
* regarding copyright ownership. The ASF licenses this file
11
* to you under the Apache License, Version 2.0 (the
12
* "License"); you may not use this file except in compliance
13
* with the License. You may obtain a copy of the License at
15
* http://www.apache.org/licenses/LICENSE-2.0
17
* Unless required by applicable law or agreed to in writing,
18
* software distributed under the License is distributed on an
19
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
20
* KIND, either express or implied. See the License for the
21
* specific language governing permissions and limitations
23
* ====================================================================
25
* This software consists of voluntary contributions made by many
26
* individuals on behalf of the Apache Software Foundation. For more
27
* information on the Apache Software Foundation, please see
28
* <http://www.apache.org/>.
32
package org.apache.http.mockup;
34
import java.io.IOException;
35
import java.net.InetSocketAddress;
37
import java.security.KeyStore;
38
import java.util.List;
40
import javax.net.ssl.SSLContext;
41
import javax.net.ssl.TrustManager;
42
import javax.net.ssl.TrustManagerFactory;
44
import org.apache.http.impl.nio.SSLClientIOEventDispatch;
45
import org.apache.http.impl.nio.reactor.DefaultConnectingIOReactor;
46
import org.apache.http.impl.nio.reactor.ExceptionEvent;
47
import org.apache.http.nio.NHttpClientHandler;
48
import org.apache.http.nio.reactor.IOEventDispatch;
49
import org.apache.http.nio.reactor.IOReactorStatus;
50
import org.apache.http.params.HttpParams;
52
public class TestHttpSSLClient {
54
private final SSLContext sslcontext;
55
private final DefaultConnectingIOReactor ioReactor;
56
private final HttpParams params;
58
private volatile IOReactorThread thread;
60
private volatile RequestCount requestCount;
62
public TestHttpSSLClient(final HttpParams params) throws Exception {
65
this.ioReactor = new DefaultConnectingIOReactor(2, this.params);
67
ClassLoader cl = getClass().getClassLoader();
68
URL url = cl.getResource("test.keystore");
69
KeyStore keystore = KeyStore.getInstance("jks");
70
keystore.load(url.openStream(), "nopassword".toCharArray());
71
TrustManagerFactory tmfactory = TrustManagerFactory.getInstance(
72
TrustManagerFactory.getDefaultAlgorithm());
73
tmfactory.init(keystore);
74
TrustManager[] trustmanagers = tmfactory.getTrustManagers();
75
this.sslcontext = SSLContext.getInstance("TLS");
76
this.sslcontext.init(null, trustmanagers, null);
79
public HttpParams getParams() {
83
public IOReactorStatus getStatus() {
84
return this.ioReactor.getStatus();
87
public List<ExceptionEvent> getAuditLog() {
88
return this.ioReactor.getAuditLog();
91
public void setRequestCount(final RequestCount requestCount) {
92
this.requestCount = requestCount;
95
private void execute(final NHttpClientHandler clientHandler) throws IOException {
96
IOEventDispatch ioEventDispatch = new SSLClientIOEventDispatch(
101
this.ioReactor.execute(ioEventDispatch);
104
public void openConnection(final InetSocketAddress address, final Object attachment) {
105
this.ioReactor.connect(address, null, attachment, null);
108
public void start(final NHttpClientHandler clientHandler) {
109
this.thread = new IOReactorThread(clientHandler);
113
public Exception getException() {
114
if (this.thread != null) {
115
return this.thread.getException();
121
public void shutdown() throws IOException {
122
this.ioReactor.shutdown();
124
if (this.thread != null) {
125
this.thread.join(500);
127
} catch (InterruptedException ignore) {
131
private class IOReactorThread extends Thread {
133
private final NHttpClientHandler clientHandler;
135
private volatile Exception ex;
137
public IOReactorThread(final NHttpClientHandler clientHandler) {
139
this.clientHandler = clientHandler;
145
execute(this.clientHandler);
146
} catch (Exception ex) {
148
if (requestCount != null) {
149
requestCount.failure(ex);
154
public Exception getException() {