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 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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.List;
  56. import java.util.Map;
  57. import javax.net.ssl.HostnameVerifier;
  58. import javax.net.ssl.HttpsURLConnection;
  59. import javax.net.ssl.KeyManager;
  60. import javax.net.ssl.SSLContext;
  61. import javax.net.ssl.TrustManager;
  62. /**
  63. * A {@link HttpConnection} which simply delegates every call to a
  64. * {@link HttpURLConnection}. This is the default implementation used by JGit
  65. *
  66. * @since 3.3
  67. */
  68. public class JDKHttpConnection implements HttpConnection {
  69. HttpURLConnection wrappedUrlConnection;
  70. /**
  71. * @param url
  72. * @throws MalformedURLException
  73. * @throws IOException
  74. */
  75. protected JDKHttpConnection(URL url)
  76. throws MalformedURLException,
  77. IOException {
  78. this.wrappedUrlConnection = (HttpURLConnection) url.openConnection();
  79. }
  80. /**
  81. * @param url
  82. * @param proxy
  83. * @throws MalformedURLException
  84. * @throws IOException
  85. */
  86. protected JDKHttpConnection(URL url, Proxy proxy)
  87. throws MalformedURLException, IOException {
  88. this.wrappedUrlConnection = (HttpURLConnection) url
  89. .openConnection(proxy);
  90. }
  91. public int getResponseCode() throws IOException {
  92. return wrappedUrlConnection.getResponseCode();
  93. }
  94. public URL getURL() {
  95. return wrappedUrlConnection.getURL();
  96. }
  97. public String getResponseMessage() throws IOException {
  98. return wrappedUrlConnection.getResponseMessage();
  99. }
  100. public Map<String, List<String>> getHeaderFields() {
  101. return wrappedUrlConnection.getHeaderFields();
  102. }
  103. public void setRequestProperty(String key, String value) {
  104. wrappedUrlConnection.setRequestProperty(key, value);
  105. }
  106. public void setRequestMethod(String method) throws ProtocolException {
  107. wrappedUrlConnection.setRequestMethod(method);
  108. }
  109. public void setUseCaches(boolean usecaches) {
  110. wrappedUrlConnection.setUseCaches(usecaches);
  111. }
  112. public void setConnectTimeout(int timeout) {
  113. wrappedUrlConnection.setConnectTimeout(timeout);
  114. }
  115. public void setReadTimeout(int timeout) {
  116. wrappedUrlConnection.setReadTimeout(timeout);
  117. }
  118. public String getContentType() {
  119. return wrappedUrlConnection.getContentType();
  120. }
  121. public InputStream getInputStream() throws IOException {
  122. return wrappedUrlConnection.getInputStream();
  123. }
  124. public String getHeaderField(String name) {
  125. return wrappedUrlConnection.getHeaderField(name);
  126. }
  127. public int getContentLength() {
  128. return wrappedUrlConnection.getContentLength();
  129. }
  130. public void setInstanceFollowRedirects(boolean followRedirects) {
  131. wrappedUrlConnection.setInstanceFollowRedirects(followRedirects);
  132. }
  133. public void setDoOutput(boolean dooutput) {
  134. wrappedUrlConnection.setDoOutput(dooutput);
  135. }
  136. public void setFixedLengthStreamingMode(int contentLength) {
  137. wrappedUrlConnection.setFixedLengthStreamingMode(contentLength);
  138. }
  139. public OutputStream getOutputStream() throws IOException {
  140. return wrappedUrlConnection.getOutputStream();
  141. }
  142. public void setChunkedStreamingMode(int chunklen) {
  143. wrappedUrlConnection.setChunkedStreamingMode(chunklen);
  144. }
  145. public String getRequestMethod() {
  146. return wrappedUrlConnection.getRequestMethod();
  147. }
  148. public boolean usingProxy() {
  149. return wrappedUrlConnection.usingProxy();
  150. }
  151. public void connect() throws IOException {
  152. wrappedUrlConnection.connect();
  153. }
  154. public void setHostnameVerifier(HostnameVerifier hostnameverifier) {
  155. ((HttpsURLConnection) wrappedUrlConnection)
  156. .setHostnameVerifier(hostnameverifier);
  157. }
  158. public void configure(KeyManager[] km, TrustManager[] tm,
  159. SecureRandom random) throws NoSuchAlgorithmException,
  160. KeyManagementException {
  161. SSLContext ctx = SSLContext.getInstance("TLS"); //$NON-NLS-1$
  162. ctx.init(km, tm, random);
  163. ((HttpsURLConnection) wrappedUrlConnection).setSSLSocketFactory(ctx
  164. .getSocketFactory());
  165. }
  166. }