PageRenderTime 51ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/projects/ApacheHttpClient/src/test/org/apache/commons/httpclient/server/ProxyRequestHandler.java

http://google-enterprise-connector-sharepoint.googlecode.com/
Java | 166 lines | 111 code | 14 blank | 41 comment | 15 complexity | 715f6403f17c877d00666dc7c45e5365 MD5 | raw file
Possible License(s): LGPL-2.1, GPL-3.0, CPL-1.0, Apache-2.0, MPL-2.0-no-copyleft-exception, GPL-2.0
  1. /*
  2. * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//httpclient/src/test/org/apache/commons/httpclient/server/ProxyRequestHandler.java,v 1.11 2004/12/11 22:35:26 olegk Exp $
  3. * $Revision: 480424 $
  4. * $Date: 2006-11-29 11:26:49 +0530 (Wed, 29 Nov 2006) $
  5. *
  6. * ====================================================================
  7. *
  8. * Licensed to the Apache Software Foundation (ASF) under one or more
  9. * contributor license agreements. See the NOTICE file distributed with
  10. * this work for additional information regarding copyright ownership.
  11. * The ASF licenses this file to You under the Apache License, Version 2.0
  12. * (the "License"); you may not use this file except in compliance with
  13. * the License. You may obtain a copy of the License at
  14. *
  15. * http://www.apache.org/licenses/LICENSE-2.0
  16. *
  17. * Unless required by applicable law or agreed to in writing, software
  18. * distributed under the License is distributed on an "AS IS" BASIS,
  19. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  20. * See the License for the specific language governing permissions and
  21. * limitations under the License.
  22. * ====================================================================
  23. *
  24. * This software consists of voluntary contributions made by many
  25. * individuals on behalf of the Apache Software Foundation. For more
  26. * information on the Apache Software Foundation, please see
  27. * <http://www.apache.org/>.
  28. *
  29. */
  30. package org.apache.commons.httpclient.server;
  31. import java.io.IOException;
  32. import java.net.UnknownHostException;
  33. import org.apache.commons.httpclient.Header;
  34. import org.apache.commons.httpclient.HttpException;
  35. import org.apache.commons.httpclient.HttpStatus;
  36. import org.apache.commons.httpclient.HttpVersion;
  37. import org.apache.commons.httpclient.URI;
  38. import org.apache.commons.httpclient.URIException;
  39. import org.apache.commons.logging.Log;
  40. import org.apache.commons.logging.LogFactory;
  41. /**
  42. * @author Ortwin Glueck
  43. * @author Oleg Kalnichevski
  44. */
  45. public class ProxyRequestHandler implements HttpRequestHandler {
  46. private static final Log LOG = LogFactory.getLog(ProxyRequestHandler.class);
  47. private SimpleConnManager connmanager = null;
  48. public ProxyRequestHandler(final SimpleConnManager connmanager) {
  49. super();
  50. if (connmanager == null) {
  51. throw new IllegalArgumentException("Connection manager may not be null");
  52. }
  53. this.connmanager = connmanager;
  54. }
  55. /**
  56. * @see org.apache.commons.httpclient.server.HttpRequestHandler#processRequest(org.apache.commons.httpclient.server.SimpleHttpServerConnection)
  57. */
  58. public boolean processRequest(
  59. final SimpleHttpServerConnection conn,
  60. final SimpleRequest request) throws IOException
  61. {
  62. httpProxy(conn, request);
  63. return true;
  64. }
  65. private void httpProxy(
  66. final SimpleHttpServerConnection conn,
  67. final SimpleRequest request) throws IOException {
  68. RequestLine oldreqline = request.getRequestLine();
  69. URI uri = null;
  70. SimpleHost host = null;
  71. try {
  72. uri = new URI(oldreqline.getUri(), true);
  73. host = new SimpleHost(uri.getHost(), uri.getPort());
  74. } catch (URIException ex) {
  75. SimpleResponse response = ErrorResponse.getResponse(HttpStatus.SC_BAD_REQUEST);
  76. conn.writeResponse(response);
  77. return;
  78. }
  79. SimpleHttpServerConnection proxyconn = null;
  80. try {
  81. proxyconn = this.connmanager.openConnection(host);
  82. } catch (UnknownHostException e) {
  83. SimpleResponse response = ErrorResponse.getResponse(HttpStatus.SC_NOT_FOUND);
  84. conn.writeResponse(response);
  85. return;
  86. }
  87. try {
  88. proxyconn.setSocketTimeout(0);
  89. // Rewrite target url
  90. RequestLine newreqline = new RequestLine(
  91. oldreqline.getMethod(),
  92. uri.getEscapedPath(),
  93. oldreqline.getHttpVersion());
  94. request.setRequestLine(newreqline);
  95. // Remove proxy-auth headers if present
  96. request.removeHeaders("Proxy-Authorization");
  97. // Manage connection persistence
  98. Header connheader = request.getFirstHeader("Proxy-Connection");
  99. if (connheader != null) {
  100. if (connheader.getValue().equalsIgnoreCase("close")) {
  101. request.setHeader(new Header("Connection", "close"));
  102. }
  103. }
  104. request.removeHeaders("Proxy-Connection");
  105. proxyconn.writeRequest(request);
  106. SimpleResponse response = proxyconn.readResponse();
  107. if (response == null) {
  108. return;
  109. }
  110. response.setHeader(new Header("Via", "1.1 test (Test-Proxy)"));
  111. connheader = response.getFirstHeader("Connection");
  112. if (connheader != null) {
  113. String s = connheader.getValue();
  114. if (s.equalsIgnoreCase("close")) {
  115. response.setHeader(new Header("Proxy-Connection", "close"));
  116. conn.setKeepAlive(false);
  117. proxyconn.setKeepAlive(false);
  118. response.removeHeaders("Connection");
  119. }
  120. if (s.equalsIgnoreCase("keep-alive")) {
  121. response.setHeader(new Header("Proxy-Connection", "keep-alive"));
  122. conn.setKeepAlive(true);
  123. proxyconn.setKeepAlive(true);
  124. response.removeHeaders("Connection");
  125. }
  126. } else {
  127. // Use protocol default connection policy
  128. if (response.getHttpVersion().greaterEquals(HttpVersion.HTTP_1_1)) {
  129. conn.setKeepAlive(true);
  130. proxyconn.setKeepAlive(true);
  131. } else {
  132. conn.setKeepAlive(false);
  133. proxyconn.setKeepAlive(false);
  134. }
  135. }
  136. if ("HEAD".equalsIgnoreCase(request.getRequestLine().getMethod())) {
  137. // this is a head request, we don't want to send the actualy content
  138. response.setBody(null);
  139. }
  140. conn.writeResponse(response);
  141. } catch (HttpException e) {
  142. SimpleResponse response = ErrorResponse.getResponse(HttpStatus.SC_BAD_REQUEST);
  143. conn.writeResponse(response);
  144. proxyconn.setKeepAlive(false);
  145. } catch (IOException e) {
  146. LOG.warn(e.getMessage());
  147. proxyconn.setKeepAlive(false);
  148. } finally {
  149. this.connmanager.releaseConnection(host, proxyconn);
  150. }
  151. }
  152. }