| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
|
| 20 | |
|
| 21 | |
|
| 22 | |
|
| 23 | |
|
| 24 | |
|
| 25 | |
|
| 26 | |
|
| 27 | |
|
| 28 | |
|
| 29 | |
|
| 30 | |
package com.jcabi.http.wire; |
| 31 | |
|
| 32 | |
import com.jcabi.aspects.Immutable; |
| 33 | |
import com.jcabi.http.Request; |
| 34 | |
import com.jcabi.http.Response; |
| 35 | |
import com.jcabi.http.Wire; |
| 36 | |
import java.io.IOException; |
| 37 | |
import java.io.InputStream; |
| 38 | |
import java.security.KeyManagementException; |
| 39 | |
import java.security.NoSuchAlgorithmException; |
| 40 | |
import java.security.SecureRandom; |
| 41 | |
import java.security.cert.X509Certificate; |
| 42 | |
import java.util.Collection; |
| 43 | |
import java.util.Map; |
| 44 | |
import javax.net.ssl.HttpsURLConnection; |
| 45 | |
import javax.net.ssl.SSLContext; |
| 46 | |
import javax.net.ssl.SSLSocketFactory; |
| 47 | |
import javax.net.ssl.TrustManager; |
| 48 | |
import javax.net.ssl.X509TrustManager; |
| 49 | |
import lombok.EqualsAndHashCode; |
| 50 | |
import lombok.ToString; |
| 51 | |
|
| 52 | |
|
| 53 | |
|
| 54 | |
|
| 55 | |
|
| 56 | |
|
| 57 | |
|
| 58 | |
|
| 59 | |
|
| 60 | |
|
| 61 | |
|
| 62 | |
|
| 63 | |
|
| 64 | |
|
| 65 | |
|
| 66 | |
|
| 67 | |
|
| 68 | |
@Immutable |
| 69 | 0 | @ToString(of = "origin") |
| 70 | 0 | @EqualsAndHashCode(of = "origin") |
| 71 | |
public final class TrustedWire implements Wire { |
| 72 | |
|
| 73 | |
|
| 74 | |
|
| 75 | |
|
| 76 | 1 | private static final TrustManager MANAGER = new X509TrustManager() { |
| 77 | |
@Override |
| 78 | |
public X509Certificate[] getAcceptedIssuers() { |
| 79 | 0 | return new X509Certificate[0]; |
| 80 | |
} |
| 81 | |
@Override |
| 82 | |
public void checkClientTrusted(final X509Certificate[] certs, |
| 83 | |
final String type) { |
| 84 | |
|
| 85 | 0 | } |
| 86 | |
@Override |
| 87 | |
public void checkServerTrusted(final X509Certificate[] certs, |
| 88 | |
final String types) { |
| 89 | |
|
| 90 | 0 | } |
| 91 | |
}; |
| 92 | |
|
| 93 | |
|
| 94 | |
|
| 95 | |
|
| 96 | |
private final transient Wire origin; |
| 97 | |
|
| 98 | |
|
| 99 | |
|
| 100 | |
|
| 101 | |
|
| 102 | 1 | public TrustedWire(final Wire wire) { |
| 103 | 1 | this.origin = wire; |
| 104 | 1 | } |
| 105 | |
|
| 106 | |
|
| 107 | |
@Override |
| 108 | |
public Response send(final Request req, final String home, |
| 109 | |
final String method, |
| 110 | |
final Collection<Map.Entry<String, String>> headers, |
| 111 | |
final InputStream content, |
| 112 | |
final int connect, final int read) throws IOException { |
| 113 | 1 | synchronized (TrustedWire.class) { |
| 114 | 1 | final SSLSocketFactory def = |
| 115 | |
HttpsURLConnection.getDefaultSSLSocketFactory(); |
| 116 | |
try { |
| 117 | 1 | HttpsURLConnection.setDefaultSSLSocketFactory( |
| 118 | |
TrustedWire.context().getSocketFactory() |
| 119 | |
); |
| 120 | 1 | return this.origin.send( |
| 121 | |
req, home, method, headers, content, |
| 122 | |
connect, read |
| 123 | |
); |
| 124 | |
} finally { |
| 125 | 1 | HttpsURLConnection.setDefaultSSLSocketFactory(def); |
| 126 | |
} |
| 127 | 0 | } |
| 128 | |
} |
| 129 | |
|
| 130 | |
|
| 131 | |
|
| 132 | |
|
| 133 | |
|
| 134 | |
private static SSLContext context() { |
| 135 | |
try { |
| 136 | 1 | final SSLContext ctx = SSLContext.getInstance("SSL"); |
| 137 | 1 | ctx.init( |
| 138 | |
null, |
| 139 | |
new TrustManager[] {TrustedWire.MANAGER}, |
| 140 | |
new SecureRandom() |
| 141 | |
); |
| 142 | 1 | return ctx; |
| 143 | 0 | } catch (final KeyManagementException ex) { |
| 144 | 0 | throw new IllegalStateException(ex); |
| 145 | 0 | } catch (final NoSuchAlgorithmException ex) { |
| 146 | 0 | throw new IllegalStateException(ex); |
| 147 | |
} |
| 148 | |
} |
| 149 | |
|
| 150 | |
} |