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 HttpConnection} which simply delegates every call to a
67   * {@link HttpURLConnection}. This is the default implementation used by JGit
68   *
69   * @since 3.3
70   */
71  public class JDKHttpConnection implements HttpConnection {
72  	HttpURLConnection wrappedUrlConnection;
73  
74  	/**
75  	 * @param url
76  	 * @throws MalformedURLException
77  	 * @throws IOException
78  	 */
79  	protected JDKHttpConnection(URL url)
80  			throws MalformedURLException,
81  			IOException {
82  		this.wrappedUrlConnection = (HttpURLConnection) url.openConnection();
83  	}
84  
85  	/**
86  	 * @param url
87  	 * @param proxy
88  	 * @throws MalformedURLException
89  	 * @throws IOException
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"); //$NON-NLS-1$
213 		ctx.init(km, tm, random);
214 		((HttpsURLConnection) wrappedUrlConnection).setSSLSocketFactory(ctx
215 				.getSocketFactory());
216 	}
217 }