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.

ConnectionUtils.java 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*
  2. * Copyright 2011 gitblit.com.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.gitblit.utils;
  17. import java.io.IOException;
  18. import java.net.URL;
  19. import java.net.URLConnection;
  20. import java.security.SecureRandom;
  21. import java.security.cert.CertificateException;
  22. import java.security.cert.X509Certificate;
  23. import javax.net.ssl.HostnameVerifier;
  24. import javax.net.ssl.HttpsURLConnection;
  25. import javax.net.ssl.SSLContext;
  26. import javax.net.ssl.SSLSession;
  27. import javax.net.ssl.TrustManager;
  28. import javax.net.ssl.X509TrustManager;
  29. /**
  30. * Utility class for establishing HTTP/HTTPS connections.
  31. *
  32. * @author James Moger
  33. *
  34. */
  35. public class ConnectionUtils {
  36. static final String CHARSET;
  37. private static final SSLContext SSL_CONTEXT;
  38. private static final DummyHostnameVerifier HOSTNAME_VERIFIER;
  39. static {
  40. SSLContext context = null;
  41. try {
  42. context = SSLContext.getInstance("SSL");
  43. context.init(null, new TrustManager[] { new DummyTrustManager() }, new SecureRandom());
  44. } catch (Throwable t) {
  45. t.printStackTrace();
  46. }
  47. SSL_CONTEXT = context;
  48. HOSTNAME_VERIFIER = new DummyHostnameVerifier();
  49. CHARSET = "UTF-8";
  50. }
  51. public static void setAuthorization(URLConnection conn, String username, char[] password) {
  52. if (!StringUtils.isEmpty(username) && (password != null && password.length > 0)) {
  53. conn.setRequestProperty(
  54. "Authorization",
  55. "Basic "
  56. + Base64.encodeBytes((username + ":" + new String(password)).getBytes()));
  57. }
  58. }
  59. public static URLConnection openReadConnection(String url, String username, char[] password)
  60. throws IOException {
  61. URLConnection conn = openConnection(url, username, password);
  62. conn.setRequestProperty("Accept-Charset", ConnectionUtils.CHARSET);
  63. return conn;
  64. }
  65. public static URLConnection openConnection(String url, String username, char[] password)
  66. throws IOException {
  67. URL urlObject = new URL(url);
  68. URLConnection conn = urlObject.openConnection();
  69. setAuthorization(conn, username, password);
  70. conn.setUseCaches(false);
  71. conn.setDoOutput(true);
  72. if (conn instanceof HttpsURLConnection) {
  73. HttpsURLConnection secureConn = (HttpsURLConnection) conn;
  74. secureConn.setSSLSocketFactory(SSL_CONTEXT.getSocketFactory());
  75. secureConn.setHostnameVerifier(HOSTNAME_VERIFIER);
  76. }
  77. return conn;
  78. }
  79. /**
  80. * DummyTrustManager trusts all certificates.
  81. *
  82. * @author James Moger
  83. */
  84. private static class DummyTrustManager implements X509TrustManager {
  85. @Override
  86. public void checkClientTrusted(X509Certificate[] certs, String authType)
  87. throws CertificateException {
  88. }
  89. @Override
  90. public void checkServerTrusted(X509Certificate[] certs, String authType)
  91. throws CertificateException {
  92. }
  93. @Override
  94. public X509Certificate[] getAcceptedIssuers() {
  95. return null;
  96. }
  97. }
  98. /**
  99. * Trusts all hostnames from a certificate, including self-signed certs.
  100. *
  101. * @author James Moger
  102. */
  103. private static class DummyHostnameVerifier implements HostnameVerifier {
  104. @Override
  105. public boolean verify(String hostname, SSLSession session) {
  106. return true;
  107. }
  108. }
  109. }