PageRenderTime 71ms CodeModel.GetById 11ms RepoModel.GetById 1ms app.codeStats 0ms

/spring-social-core/src/main/java/org/springframework/social/support/ClientHttpRequestFactorySelector.java

http://github.com/SpringSource/spring-social
Java | 155 lines | 102 code | 22 blank | 31 comment | 6 complexity | f696f21fba42a405f9482f50ab8206e0 MD5 | raw file
Possible License(s): Apache-2.0
  1. /*
  2. * Copyright 2015 the original author or authors.
  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. * https://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 org.springframework.social.support;
  17. import java.net.InetSocketAddress;
  18. import java.net.Proxy;
  19. import java.net.URI;
  20. import java.security.KeyManagementException;
  21. import java.security.KeyStore;
  22. import java.security.KeyStoreException;
  23. import java.security.NoSuchAlgorithmException;
  24. import java.security.cert.CertificateException;
  25. import java.security.cert.X509Certificate;
  26. import java.util.Properties;
  27. import javax.net.ssl.SSLContext;
  28. import org.apache.http.HttpHost;
  29. import org.apache.http.client.protocol.HttpClientContext;
  30. import org.apache.http.conn.ssl.NoopHostnameVerifier;
  31. import org.apache.http.conn.ssl.TrustStrategy;
  32. import org.apache.http.impl.client.CloseableHttpClient;
  33. import org.apache.http.impl.client.HttpClients;
  34. import org.apache.http.protocol.HttpContext;
  35. import org.apache.http.ssl.SSLContexts;
  36. import org.springframework.http.HttpMethod;
  37. import org.springframework.http.client.BufferingClientHttpRequestFactory;
  38. import org.springframework.http.client.ClientHttpRequestFactory;
  39. import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
  40. import org.springframework.http.client.SimpleClientHttpRequestFactory;
  41. import org.springframework.util.ClassUtils;
  42. /**
  43. * Chooses a request factory. Picks a HttpComponentsClientRequestFactory factory if Apache HttpComponents HttpClient is in the classpath.
  44. * If not, falls back to SimpleClientHttpRequestFactory.
  45. * @author Craig Walls
  46. * @author Roy Clarkson
  47. */
  48. public class ClientHttpRequestFactorySelector {
  49. public static ClientHttpRequestFactory foo() {
  50. return new HttpComponentsClientHttpRequestFactory();
  51. }
  52. public static ClientHttpRequestFactory getRequestFactory() {
  53. Properties properties = System.getProperties();
  54. String proxyHost = properties.getProperty("http.proxyHost");
  55. int proxyPort = properties.containsKey("http.proxyPort") ? Integer.valueOf(properties.getProperty("http.proxyPort")) : 80;
  56. if (HTTP_COMPONENTS_AVAILABLE) {
  57. return HttpComponentsClientRequestFactoryCreator.createRequestFactory(proxyHost, proxyPort);
  58. } else {
  59. SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
  60. if (proxyHost != null) {
  61. requestFactory.setProxy(new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyHost, proxyPort)));
  62. }
  63. return requestFactory;
  64. }
  65. }
  66. /**
  67. * Decorates a request factory to buffer responses so that the responses may be repeatedly read.
  68. * @param requestFactory the request factory to be decorated for buffering
  69. * @return a buffering request factory
  70. */
  71. public static ClientHttpRequestFactory bufferRequests(ClientHttpRequestFactory requestFactory) {
  72. return new BufferingClientHttpRequestFactory(requestFactory);
  73. }
  74. private static final boolean HTTP_COMPONENTS_AVAILABLE = ClassUtils.isPresent("org.apache.http.client.HttpClient", ClientHttpRequestFactory.class.getClassLoader());
  75. public static class HttpComponentsClientRequestFactoryCreator {
  76. private static boolean isAllTrust = false;
  77. public static ClientHttpRequestFactory createRequestFactory(String proxyHost, int proxyPort) {
  78. HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory() {
  79. @Override
  80. protected HttpContext createHttpContext(HttpMethod httpMethod, URI uri) {
  81. HttpClientContext context = new HttpClientContext();
  82. context.setAttribute("http.protocol.expect-continue", false);
  83. return context;
  84. }
  85. };
  86. if (proxyHost != null) {
  87. HttpHost proxy = new HttpHost(proxyHost, proxyPort);
  88. CloseableHttpClient httpClient = isAllTrust ? getAllTrustClient(proxy) : getClient(proxy);
  89. requestFactory.setHttpClient(httpClient);
  90. }
  91. return requestFactory;
  92. }
  93. private static CloseableHttpClient getClient(HttpHost proxy) {
  94. return HttpClients.custom()
  95. .setProxy(proxy)
  96. .build();
  97. }
  98. private static CloseableHttpClient getAllTrustClient(HttpHost proxy) {
  99. return HttpClients.custom()
  100. .setProxy(proxy)
  101. .setSSLContext(getSSLContext())
  102. .setSSLHostnameVerifier(new NoopHostnameVerifier())
  103. .build();
  104. }
  105. private static SSLContext getSSLContext() {
  106. try {
  107. KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
  108. TrustStrategy allTrust = new TrustStrategy() {
  109. @Override
  110. public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException {
  111. return true;
  112. }
  113. };
  114. return SSLContexts.custom().useProtocol("SSL").loadTrustMaterial(trustStore, allTrust).build();
  115. } catch (KeyStoreException e) {
  116. e.printStackTrace();
  117. } catch (KeyManagementException e) {
  118. e.printStackTrace();
  119. } catch (NoSuchAlgorithmException e) {
  120. e.printStackTrace();
  121. }
  122. return null;
  123. }
  124. }
  125. /**
  126. * Trust all SSL certificates.
  127. * For use when using {@link HttpComponentsClientHttpRequestFactory} in a test environment. Not recommended for general use.
  128. * @param isAllTrust if true, all certificates will be trusted.
  129. */
  130. public static void setAllTrust(boolean isAllTrust) {
  131. HttpComponentsClientRequestFactoryCreator.isAllTrust = isAllTrust;
  132. }
  133. }