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
31
32
33
34
35
36
37
38
39
40
41
42
43 package org.eclipse.jgit.transport.http;
44
45 import java.io.IOException;
46 import java.io.InputStream;
47 import java.io.OutputStream;
48 import java.net.HttpURLConnection;
49 import java.net.MalformedURLException;
50 import java.net.ProtocolException;
51 import java.net.Proxy;
52 import java.net.URL;
53 import java.security.KeyManagementException;
54 import java.security.NoSuchAlgorithmException;
55 import java.security.SecureRandom;
56 import java.util.List;
57 import java.util.Map;
58
59 import javax.net.ssl.HostnameVerifier;
60 import javax.net.ssl.HttpsURLConnection;
61 import javax.net.ssl.KeyManager;
62 import javax.net.ssl.SSLContext;
63 import javax.net.ssl.TrustManager;
64
65
66
67
68
69
70
71
72 public class JDKHttpConnection implements HttpConnection {
73 HttpURLConnection wrappedUrlConnection;
74
75
76
77
78
79
80
81
82
83 protected JDKHttpConnection(URL url)
84 throws MalformedURLException,
85 IOException {
86 this.wrappedUrlConnection = (HttpURLConnection) url.openConnection();
87 }
88
89
90
91
92
93
94
95
96
97
98
99 protected JDKHttpConnection(URL url, Proxy proxy)
100 throws MalformedURLException, IOException {
101 this.wrappedUrlConnection = (HttpURLConnection) url
102 .openConnection(proxy);
103 }
104
105
106 @Override
107 public int getResponseCode() throws IOException {
108 return wrappedUrlConnection.getResponseCode();
109 }
110
111
112 @Override
113 public URL getURL() {
114 return wrappedUrlConnection.getURL();
115 }
116
117
118 @Override
119 public String getResponseMessage() throws IOException {
120 return wrappedUrlConnection.getResponseMessage();
121 }
122
123
124 @Override
125 public Map<String, List<String>> getHeaderFields() {
126 return wrappedUrlConnection.getHeaderFields();
127 }
128
129
130 @Override
131 public void setRequestProperty(String key, String value) {
132 wrappedUrlConnection.setRequestProperty(key, value);
133 }
134
135
136 @Override
137 public void setRequestMethod(String method) throws ProtocolException {
138 wrappedUrlConnection.setRequestMethod(method);
139 }
140
141
142 @Override
143 public void setUseCaches(boolean usecaches) {
144 wrappedUrlConnection.setUseCaches(usecaches);
145 }
146
147
148 @Override
149 public void setConnectTimeout(int timeout) {
150 wrappedUrlConnection.setConnectTimeout(timeout);
151 }
152
153
154 @Override
155 public void setReadTimeout(int timeout) {
156 wrappedUrlConnection.setReadTimeout(timeout);
157 }
158
159
160 @Override
161 public String getContentType() {
162 return wrappedUrlConnection.getContentType();
163 }
164
165
166 @Override
167 public InputStream getInputStream() throws IOException {
168 return wrappedUrlConnection.getInputStream();
169 }
170
171
172 @Override
173 public String getHeaderField(String name) {
174 return wrappedUrlConnection.getHeaderField(name);
175 }
176
177
178 @Override
179 public int getContentLength() {
180 return wrappedUrlConnection.getContentLength();
181 }
182
183
184 @Override
185 public void setInstanceFollowRedirects(boolean followRedirects) {
186 wrappedUrlConnection.setInstanceFollowRedirects(followRedirects);
187 }
188
189
190 @Override
191 public void setDoOutput(boolean dooutput) {
192 wrappedUrlConnection.setDoOutput(dooutput);
193 }
194
195
196 @Override
197 public void setFixedLengthStreamingMode(int contentLength) {
198 wrappedUrlConnection.setFixedLengthStreamingMode(contentLength);
199 }
200
201
202 @Override
203 public OutputStream getOutputStream() throws IOException {
204 return wrappedUrlConnection.getOutputStream();
205 }
206
207
208 @Override
209 public void setChunkedStreamingMode(int chunklen) {
210 wrappedUrlConnection.setChunkedStreamingMode(chunklen);
211 }
212
213
214 @Override
215 public String getRequestMethod() {
216 return wrappedUrlConnection.getRequestMethod();
217 }
218
219
220 @Override
221 public boolean usingProxy() {
222 return wrappedUrlConnection.usingProxy();
223 }
224
225
226 @Override
227 public void connect() throws IOException {
228 wrappedUrlConnection.connect();
229 }
230
231
232 @Override
233 public void setHostnameVerifier(HostnameVerifier hostnameverifier) {
234 ((HttpsURLConnection) wrappedUrlConnection)
235 .setHostnameVerifier(hostnameverifier);
236 }
237
238
239 @Override
240 public void configure(KeyManager[] km, TrustManager[] tm,
241 SecureRandom random) throws NoSuchAlgorithmException,
242 KeyManagementException {
243 SSLContext ctx = SSLContext.getInstance("TLS");
244 ctx.init(km, tm, random);
245 ((HttpsURLConnection) wrappedUrlConnection).setSSLSocketFactory(ctx
246 .getSocketFactory());
247 }
248 }