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 public class JDKHttpConnection implements HttpConnection {
72 HttpURLConnection wrappedUrlConnection;
73
74
75
76
77
78
79 protected JDKHttpConnection(URL url)
80 throws MalformedURLException,
81 IOException {
82 this.wrappedUrlConnection = (HttpURLConnection) url.openConnection();
83 }
84
85
86
87
88
89
90
91 protected JDKHttpConnection(URL url, Proxy proxy)
92 throws MalformedURLException, IOException {
93 this.wrappedUrlConnection = (HttpURLConnection) url
94 .openConnection(proxy);
95 }
96
97 @Override
98 public int getResponseCode() throws IOException {
99 return wrappedUrlConnection.getResponseCode();
100 }
101
102 @Override
103 public URL getURL() {
104 return wrappedUrlConnection.getURL();
105 }
106
107 @Override
108 public String getResponseMessage() throws IOException {
109 return wrappedUrlConnection.getResponseMessage();
110 }
111
112 @Override
113 public Map<String, List<String>> getHeaderFields() {
114 return wrappedUrlConnection.getHeaderFields();
115 }
116
117 @Override
118 public void setRequestProperty(String key, String value) {
119 wrappedUrlConnection.setRequestProperty(key, value);
120 }
121
122 @Override
123 public void setRequestMethod(String method) throws ProtocolException {
124 wrappedUrlConnection.setRequestMethod(method);
125 }
126
127 @Override
128 public void setUseCaches(boolean usecaches) {
129 wrappedUrlConnection.setUseCaches(usecaches);
130 }
131
132 @Override
133 public void setConnectTimeout(int timeout) {
134 wrappedUrlConnection.setConnectTimeout(timeout);
135 }
136
137 @Override
138 public void setReadTimeout(int timeout) {
139 wrappedUrlConnection.setReadTimeout(timeout);
140 }
141
142 @Override
143 public String getContentType() {
144 return wrappedUrlConnection.getContentType();
145 }
146
147 @Override
148 public InputStream getInputStream() throws IOException {
149 return wrappedUrlConnection.getInputStream();
150 }
151
152 @Override
153 public String getHeaderField(String name) {
154 return wrappedUrlConnection.getHeaderField(name);
155 }
156
157 @Override
158 public int getContentLength() {
159 return wrappedUrlConnection.getContentLength();
160 }
161
162 @Override
163 public void setInstanceFollowRedirects(boolean followRedirects) {
164 wrappedUrlConnection.setInstanceFollowRedirects(followRedirects);
165 }
166
167 @Override
168 public void setDoOutput(boolean dooutput) {
169 wrappedUrlConnection.setDoOutput(dooutput);
170 }
171
172 @Override
173 public void setFixedLengthStreamingMode(int contentLength) {
174 wrappedUrlConnection.setFixedLengthStreamingMode(contentLength);
175 }
176
177 @Override
178 public OutputStream getOutputStream() throws IOException {
179 return wrappedUrlConnection.getOutputStream();
180 }
181
182 @Override
183 public void setChunkedStreamingMode(int chunklen) {
184 wrappedUrlConnection.setChunkedStreamingMode(chunklen);
185 }
186
187 @Override
188 public String getRequestMethod() {
189 return wrappedUrlConnection.getRequestMethod();
190 }
191
192 @Override
193 public boolean usingProxy() {
194 return wrappedUrlConnection.usingProxy();
195 }
196
197 @Override
198 public void connect() throws IOException {
199 wrappedUrlConnection.connect();
200 }
201
202 @Override
203 public void setHostnameVerifier(HostnameVerifier hostnameverifier) {
204 ((HttpsURLConnection) wrappedUrlConnection)
205 .setHostnameVerifier(hostnameverifier);
206 }
207
208 @Override
209 public void configure(KeyManager[] km, TrustManager[] tm,
210 SecureRandom random) throws NoSuchAlgorithmException,
211 KeyManagementException {
212 SSLContext ctx = SSLContext.getInstance("TLS");
213 ctx.init(km, tm, random);
214 ((HttpsURLConnection) wrappedUrlConnection).setSSLSocketFactory(ctx
215 .getSocketFactory());
216 }
217 }