You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

JDKHttpConnection.java 7.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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. import java.io.IOException;
  45. import java.io.InputStream;
  46. import java.io.OutputStream;
  47. import java.net.HttpURLConnection;
  48. import java.net.MalformedURLException;
  49. import java.net.ProtocolException;
  50. import java.net.Proxy;
  51. import java.net.URL;
  52. import java.security.KeyManagementException;
  53. import java.security.NoSuchAlgorithmException;
  54. import java.security.SecureRandom;
  55. import java.util.LinkedList;
  56. import java.util.List;
  57. import java.util.Map;
  58. import javax.net.ssl.HostnameVerifier;
  59. import javax.net.ssl.HttpsURLConnection;
  60. import javax.net.ssl.KeyManager;
  61. import javax.net.ssl.SSLContext;
  62. import javax.net.ssl.TrustManager;
  63. import org.eclipse.jgit.annotations.NonNull;
  64. /**
  65. * A {@link org.eclipse.jgit.transport.http.HttpConnection} which simply
  66. * delegates every call to a {@link java.net.HttpURLConnection}. This is the
  67. * default implementation used by JGit
  68. *
  69. * @since 3.3
  70. */
  71. public class JDKHttpConnection implements HttpConnection {
  72. HttpURLConnection wrappedUrlConnection;
  73. // used for mock testing
  74. JDKHttpConnection(HttpURLConnection urlConnection) {
  75. this.wrappedUrlConnection = urlConnection;
  76. }
  77. /**
  78. * Constructor for JDKHttpConnection.
  79. *
  80. * @param url
  81. * a {@link java.net.URL} object.
  82. * @throws java.net.MalformedURLException
  83. * @throws java.io.IOException
  84. */
  85. protected JDKHttpConnection(URL url)
  86. throws MalformedURLException,
  87. IOException {
  88. this.wrappedUrlConnection = (HttpURLConnection) url.openConnection();
  89. }
  90. /**
  91. * Constructor for JDKHttpConnection.
  92. *
  93. * @param url
  94. * a {@link java.net.URL} object.
  95. * @param proxy
  96. * a {@link java.net.Proxy} object.
  97. * @throws java.net.MalformedURLException
  98. * @throws java.io.IOException
  99. */
  100. protected JDKHttpConnection(URL url, Proxy proxy)
  101. throws MalformedURLException, IOException {
  102. this.wrappedUrlConnection = (HttpURLConnection) url
  103. .openConnection(proxy);
  104. }
  105. /** {@inheritDoc} */
  106. @Override
  107. public int getResponseCode() throws IOException {
  108. return wrappedUrlConnection.getResponseCode();
  109. }
  110. /** {@inheritDoc} */
  111. @Override
  112. public URL getURL() {
  113. return wrappedUrlConnection.getURL();
  114. }
  115. /** {@inheritDoc} */
  116. @Override
  117. public String getResponseMessage() throws IOException {
  118. return wrappedUrlConnection.getResponseMessage();
  119. }
  120. /** {@inheritDoc} */
  121. @Override
  122. public Map<String, List<String>> getHeaderFields() {
  123. return wrappedUrlConnection.getHeaderFields();
  124. }
  125. /** {@inheritDoc} */
  126. @Override
  127. public void setRequestProperty(String key, String value) {
  128. wrappedUrlConnection.setRequestProperty(key, value);
  129. }
  130. /** {@inheritDoc} */
  131. @Override
  132. public void setRequestMethod(String method) throws ProtocolException {
  133. wrappedUrlConnection.setRequestMethod(method);
  134. }
  135. /** {@inheritDoc} */
  136. @Override
  137. public void setUseCaches(boolean usecaches) {
  138. wrappedUrlConnection.setUseCaches(usecaches);
  139. }
  140. /** {@inheritDoc} */
  141. @Override
  142. public void setConnectTimeout(int timeout) {
  143. wrappedUrlConnection.setConnectTimeout(timeout);
  144. }
  145. /** {@inheritDoc} */
  146. @Override
  147. public void setReadTimeout(int timeout) {
  148. wrappedUrlConnection.setReadTimeout(timeout);
  149. }
  150. /** {@inheritDoc} */
  151. @Override
  152. public String getContentType() {
  153. return wrappedUrlConnection.getContentType();
  154. }
  155. /** {@inheritDoc} */
  156. @Override
  157. public InputStream getInputStream() throws IOException {
  158. return wrappedUrlConnection.getInputStream();
  159. }
  160. /** {@inheritDoc} */
  161. @Override
  162. public String getHeaderField(@NonNull String name) {
  163. return wrappedUrlConnection.getHeaderField(name);
  164. }
  165. @Override
  166. public List<String> getHeaderFields(@NonNull String name) {
  167. Map<String, List<String>> m = wrappedUrlConnection.getHeaderFields();
  168. List<String> fields = mapValuesToListIgnoreCase(name, m);
  169. return fields;
  170. }
  171. private static List<String> mapValuesToListIgnoreCase(String keyName,
  172. Map<String, List<String>> m) {
  173. List<String> fields = new LinkedList<>();
  174. m.entrySet().stream().filter(e -> keyName.equalsIgnoreCase(e.getKey()))
  175. .filter(e -> e.getValue() != null)
  176. .forEach(e -> fields.addAll(e.getValue()));
  177. return fields;
  178. }
  179. /** {@inheritDoc} */
  180. @Override
  181. public int getContentLength() {
  182. return wrappedUrlConnection.getContentLength();
  183. }
  184. /** {@inheritDoc} */
  185. @Override
  186. public void setInstanceFollowRedirects(boolean followRedirects) {
  187. wrappedUrlConnection.setInstanceFollowRedirects(followRedirects);
  188. }
  189. /** {@inheritDoc} */
  190. @Override
  191. public void setDoOutput(boolean dooutput) {
  192. wrappedUrlConnection.setDoOutput(dooutput);
  193. }
  194. /** {@inheritDoc} */
  195. @Override
  196. public void setFixedLengthStreamingMode(int contentLength) {
  197. wrappedUrlConnection.setFixedLengthStreamingMode(contentLength);
  198. }
  199. /** {@inheritDoc} */
  200. @Override
  201. public OutputStream getOutputStream() throws IOException {
  202. return wrappedUrlConnection.getOutputStream();
  203. }
  204. /** {@inheritDoc} */
  205. @Override
  206. public void setChunkedStreamingMode(int chunklen) {
  207. wrappedUrlConnection.setChunkedStreamingMode(chunklen);
  208. }
  209. /** {@inheritDoc} */
  210. @Override
  211. public String getRequestMethod() {
  212. return wrappedUrlConnection.getRequestMethod();
  213. }
  214. /** {@inheritDoc} */
  215. @Override
  216. public boolean usingProxy() {
  217. return wrappedUrlConnection.usingProxy();
  218. }
  219. /** {@inheritDoc} */
  220. @Override
  221. public void connect() throws IOException {
  222. wrappedUrlConnection.connect();
  223. }
  224. /** {@inheritDoc} */
  225. @Override
  226. public void setHostnameVerifier(HostnameVerifier hostnameverifier) {
  227. ((HttpsURLConnection) wrappedUrlConnection)
  228. .setHostnameVerifier(hostnameverifier);
  229. }
  230. /** {@inheritDoc} */
  231. @Override
  232. public void configure(KeyManager[] km, TrustManager[] tm,
  233. SecureRandom random) throws NoSuchAlgorithmException,
  234. KeyManagementException {
  235. SSLContext ctx = SSLContext.getInstance("TLS"); //$NON-NLS-1$
  236. ctx.init(km, tm, random);
  237. ((HttpsURLConnection) wrappedUrlConnection).setSSLSocketFactory(ctx
  238. .getSocketFactory());
  239. }
  240. }