~ubuntu-branches/ubuntu/trusty/httpcomponents-core/trusty

« back to all changes in this revision

Viewing changes to httpcore-nio/src/test/java/org/apache/http/mockup/TestHttpSSLClient.java

  • Committer: Bazaar Package Importer
  • Author(s): David Paleino
  • Date: 2010-06-12 08:37:34 UTC
  • Revision ID: james.westby@ubuntu.com-20100612083734-1y8kp6qm4sjk60az
Tags: upstream-4.0.1
ImportĀ upstreamĀ versionĀ 4.0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * $HeadURL:https://svn.apache.org/repos/asf/jakarta/httpcomponents/httpcore/trunk/module-niossl/src/test/java/org/apache/http/mockup/TestHttpSSLClient.java $
 
3
 * $Revision:575703 $
 
4
 * $Date:2007-09-14 16:40:15 +0200 (Fri, 14 Sep 2007) $
 
5
 *
 
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
 
14
 *
 
15
 *   http://www.apache.org/licenses/LICENSE-2.0
 
16
 *
 
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
 
22
 * under the License.
 
23
 * ====================================================================
 
24
 *
 
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/>.
 
29
 *
 
30
 */
 
31
 
 
32
package org.apache.http.mockup;
 
33
 
 
34
import java.io.IOException;
 
35
import java.net.InetSocketAddress;
 
36
import java.net.URL;
 
37
import java.security.KeyStore;
 
38
import java.util.List;
 
39
 
 
40
import javax.net.ssl.SSLContext;
 
41
import javax.net.ssl.TrustManager;
 
42
import javax.net.ssl.TrustManagerFactory;
 
43
 
 
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;
 
51
 
 
52
public class TestHttpSSLClient {
 
53
 
 
54
    private final SSLContext sslcontext;
 
55
    private final DefaultConnectingIOReactor ioReactor;
 
56
    private final HttpParams params;
 
57
    
 
58
    private volatile IOReactorThread thread;
 
59
 
 
60
    private volatile RequestCount requestCount;
 
61
    
 
62
    public TestHttpSSLClient(final HttpParams params) throws Exception {
 
63
        super();
 
64
        this.params = params;
 
65
        this.ioReactor = new DefaultConnectingIOReactor(2, this.params);
 
66
        
 
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);
 
77
    }
 
78
    
 
79
    public HttpParams getParams() {
 
80
        return this.params;
 
81
    }
 
82
    
 
83
    public IOReactorStatus getStatus() {
 
84
        return this.ioReactor.getStatus();
 
85
    }
 
86
    
 
87
    public List<ExceptionEvent> getAuditLog() {
 
88
        return this.ioReactor.getAuditLog();
 
89
    }
 
90
    
 
91
    public void setRequestCount(final RequestCount requestCount) {
 
92
        this.requestCount = requestCount;
 
93
    }
 
94
 
 
95
    private void execute(final NHttpClientHandler clientHandler) throws IOException {
 
96
        IOEventDispatch ioEventDispatch = new SSLClientIOEventDispatch(
 
97
                clientHandler, 
 
98
                this.sslcontext,
 
99
                this.params);
 
100
        
 
101
        this.ioReactor.execute(ioEventDispatch);
 
102
    }
 
103
    
 
104
    public void openConnection(final InetSocketAddress address, final Object attachment) {
 
105
        this.ioReactor.connect(address, null, attachment, null);
 
106
    }
 
107
 
 
108
    public void start(final NHttpClientHandler clientHandler) {
 
109
        this.thread = new IOReactorThread(clientHandler);
 
110
        this.thread.start();
 
111
    }
 
112
    
 
113
    public Exception getException() {
 
114
        if (this.thread != null) {
 
115
            return this.thread.getException();
 
116
        } else {
 
117
            return null;
 
118
        }
 
119
    }
 
120
    
 
121
    public void shutdown() throws IOException {
 
122
        this.ioReactor.shutdown();
 
123
        try {
 
124
            if (this.thread != null) {
 
125
                this.thread.join(500);
 
126
            }
 
127
        } catch (InterruptedException ignore) {
 
128
        }
 
129
    }
 
130
    
 
131
    private class IOReactorThread extends Thread {
 
132
 
 
133
        private final NHttpClientHandler clientHandler;
 
134
        
 
135
        private volatile Exception ex;
 
136
        
 
137
        public IOReactorThread(final NHttpClientHandler clientHandler) {
 
138
            super();
 
139
            this.clientHandler = clientHandler;
 
140
        }
 
141
        
 
142
        @Override
 
143
        public void run() {
 
144
            try {
 
145
                execute(this.clientHandler);
 
146
            } catch (Exception ex) {
 
147
                this.ex = ex;
 
148
                if (requestCount != null) {
 
149
                    requestCount.failure(ex);
 
150
                }
 
151
            }
 
152
        }
 
153
        
 
154
        public Exception getException() {
 
155
            return this.ex;
 
156
        }
 
157
 
 
158
    }    
 
159
    
 
160
}