View Javadoc
1   /*
2    * Copyright (C) 2013 Christian Halstrick <christian.halstrick@sap.com>
3    * and other copyright owners as documented in the project's IP log.
4    *
5    * This program and the accompanying materials are made available
6    * under the terms of the Eclipse Distribution License v1.0 which
7    * accompanies this distribution, is reproduced below, and is
8    * available at http://www.eclipse.org/org/documents/edl-v10.php
9    *
10   * All rights reserved.
11   *
12   * Redistribution and use in source and binary forms, with or
13   * without modification, are permitted provided that the following
14   * conditions are met:
15   *
16   * - Redistributions of source code must retain the above copyright
17   *   notice, this list of conditions and the following disclaimer.
18   *
19   * - Redistributions in binary form must reproduce the above
20   *   copyright notice, this list of conditions and the following
21   *   disclaimer in the documentation and/or other materials provided
22   *   with the distribution.
23   *
24   * - Neither the name of the Eclipse Foundation, Inc. nor the
25   *   names of its contributors may be used to endorse or promote
26   *   products derived from this software without specific prior
27   *   written permission.
28   *
29   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
30   * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
31   * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
32   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
34   * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
35   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
36   * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
37   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
38   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
40   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
41   * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
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   * A {@link org.eclipse.jgit.transport.http.HttpConnection} which simply
67   * delegates every call to a {@link java.net.HttpURLConnection}. This is the
68   * default implementation used by JGit
69   *
70   * @since 3.3
71   */
72  public class JDKHttpConnection implements HttpConnection {
73  	HttpURLConnection wrappedUrlConnection;
74  
75  	/**
76  	 * Constructor for JDKHttpConnection.
77  	 *
78  	 * @param url
79  	 *            a {@link java.net.URL} object.
80  	 * @throws java.net.MalformedURLException
81  	 * @throws java.io.IOException
82  	 */
83  	protected JDKHttpConnection(URL url)
84  			throws MalformedURLException,
85  			IOException {
86  		this.wrappedUrlConnection = (HttpURLConnection) url.openConnection();
87  	}
88  
89  	/**
90  	 * Constructor for JDKHttpConnection.
91  	 *
92  	 * @param url
93  	 *            a {@link java.net.URL} object.
94  	 * @param proxy
95  	 *            a {@link java.net.Proxy} object.
96  	 * @throws java.net.MalformedURLException
97  	 * @throws java.io.IOException
98  	 */
99  	protected JDKHttpConnection(URL url, Proxy proxy)
100 			throws MalformedURLException, IOException {
101 		this.wrappedUrlConnection = (HttpURLConnection) url
102 				.openConnection(proxy);
103 	}
104 
105 	/** {@inheritDoc} */
106 	@Override
107 	public int getResponseCode() throws IOException {
108 		return wrappedUrlConnection.getResponseCode();
109 	}
110 
111 	/** {@inheritDoc} */
112 	@Override
113 	public URL getURL() {
114 		return wrappedUrlConnection.getURL();
115 	}
116 
117 	/** {@inheritDoc} */
118 	@Override
119 	public String getResponseMessage() throws IOException {
120 		return wrappedUrlConnection.getResponseMessage();
121 	}
122 
123 	/** {@inheritDoc} */
124 	@Override
125 	public Map<String, List<String>> getHeaderFields() {
126 		return wrappedUrlConnection.getHeaderFields();
127 	}
128 
129 	/** {@inheritDoc} */
130 	@Override
131 	public void setRequestProperty(String key, String value) {
132 		wrappedUrlConnection.setRequestProperty(key, value);
133 	}
134 
135 	/** {@inheritDoc} */
136 	@Override
137 	public void setRequestMethod(String method) throws ProtocolException {
138 		wrappedUrlConnection.setRequestMethod(method);
139 	}
140 
141 	/** {@inheritDoc} */
142 	@Override
143 	public void setUseCaches(boolean usecaches) {
144 		wrappedUrlConnection.setUseCaches(usecaches);
145 	}
146 
147 	/** {@inheritDoc} */
148 	@Override
149 	public void setConnectTimeout(int timeout) {
150 		wrappedUrlConnection.setConnectTimeout(timeout);
151 	}
152 
153 	/** {@inheritDoc} */
154 	@Override
155 	public void setReadTimeout(int timeout) {
156 		wrappedUrlConnection.setReadTimeout(timeout);
157 	}
158 
159 	/** {@inheritDoc} */
160 	@Override
161 	public String getContentType() {
162 		return wrappedUrlConnection.getContentType();
163 	}
164 
165 	/** {@inheritDoc} */
166 	@Override
167 	public InputStream getInputStream() throws IOException {
168 		return wrappedUrlConnection.getInputStream();
169 	}
170 
171 	/** {@inheritDoc} */
172 	@Override
173 	public String getHeaderField(String name) {
174 		return wrappedUrlConnection.getHeaderField(name);
175 	}
176 
177 	/** {@inheritDoc} */
178 	@Override
179 	public int getContentLength() {
180 		return wrappedUrlConnection.getContentLength();
181 	}
182 
183 	/** {@inheritDoc} */
184 	@Override
185 	public void setInstanceFollowRedirects(boolean followRedirects) {
186 		wrappedUrlConnection.setInstanceFollowRedirects(followRedirects);
187 	}
188 
189 	/** {@inheritDoc} */
190 	@Override
191 	public void setDoOutput(boolean dooutput) {
192 		wrappedUrlConnection.setDoOutput(dooutput);
193 	}
194 
195 	/** {@inheritDoc} */
196 	@Override
197 	public void setFixedLengthStreamingMode(int contentLength) {
198 		wrappedUrlConnection.setFixedLengthStreamingMode(contentLength);
199 	}
200 
201 	/** {@inheritDoc} */
202 	@Override
203 	public OutputStream getOutputStream() throws IOException {
204 		return wrappedUrlConnection.getOutputStream();
205 	}
206 
207 	/** {@inheritDoc} */
208 	@Override
209 	public void setChunkedStreamingMode(int chunklen) {
210 		wrappedUrlConnection.setChunkedStreamingMode(chunklen);
211 	}
212 
213 	/** {@inheritDoc} */
214 	@Override
215 	public String getRequestMethod() {
216 		return wrappedUrlConnection.getRequestMethod();
217 	}
218 
219 	/** {@inheritDoc} */
220 	@Override
221 	public boolean usingProxy() {
222 		return wrappedUrlConnection.usingProxy();
223 	}
224 
225 	/** {@inheritDoc} */
226 	@Override
227 	public void connect() throws IOException {
228 		wrappedUrlConnection.connect();
229 	}
230 
231 	/** {@inheritDoc} */
232 	@Override
233 	public void setHostnameVerifier(HostnameVerifier hostnameverifier) {
234 		((HttpsURLConnection) wrappedUrlConnection)
235 				.setHostnameVerifier(hostnameverifier);
236 	}
237 
238 	/** {@inheritDoc} */
239 	@Override
240 	public void configure(KeyManager[] km, TrustManager[] tm,
241 			SecureRandom random) throws NoSuchAlgorithmException,
242 			KeyManagementException {
243 		SSLContext ctx = SSLContext.getInstance("TLS"); //$NON-NLS-1$
244 		ctx.init(km, tm, random);
245 		((HttpsURLConnection) wrappedUrlConnection).setSSLSocketFactory(ctx
246 				.getSocketFactory());
247 	}
248 }