/maven-javadoc-plugin/src/test/java/org/apache/maven/plugin/javadoc/ProxyServer.java

https://github.com/tobrien/maven-plugins · Java · 242 lines · 145 code · 29 blank · 68 comment · 17 complexity · 6f88aef013d4ae7e4ddcdc3f4e4a1d40 MD5 · raw file

  1. package org.apache.maven.plugin.javadoc;
  2. /*
  3. * Licensed to the Apache Software Foundation (ASF) under one
  4. * or more contributor license agreements. See the NOTICE file
  5. * distributed with this work for additional information
  6. * regarding copyright ownership. The ASF licenses this file
  7. * to you under the Apache License, Version 2.0 (the
  8. * "License"); you may not use this file except in compliance
  9. * with the License. You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing,
  14. * software distributed under the License is distributed on an
  15. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  16. * KIND, either express or implied. See the License for the
  17. * specific language governing permissions and limitations
  18. * under the License.
  19. */
  20. import java.io.IOException;
  21. import java.net.InetAddress;
  22. import java.net.UnknownHostException;
  23. import java.util.Map;
  24. import javax.servlet.ServletException;
  25. import javax.servlet.ServletRequest;
  26. import javax.servlet.ServletResponse;
  27. import javax.servlet.http.HttpServletRequest;
  28. import javax.servlet.http.HttpServletResponse;
  29. import org.mortbay.jetty.Connector;
  30. import org.mortbay.jetty.Server;
  31. import org.mortbay.jetty.bio.SocketConnector;
  32. import org.mortbay.jetty.security.B64Code;
  33. import org.mortbay.jetty.servlet.Context;
  34. import org.mortbay.jetty.servlet.ServletHolder;
  35. import org.mortbay.proxy.AsyncProxyServlet;
  36. /**
  37. * A Proxy server.
  38. *
  39. * @author <a href="mailto:vincent.siveton@gmail.com">Vincent Siveton</a>
  40. * @version $Id$
  41. * @since 2.6
  42. */
  43. class ProxyServer
  44. {
  45. private Server proxyServer;
  46. /**
  47. * @param proxyServlet the wanted auth proxy servlet
  48. */
  49. public ProxyServer( AuthAsyncProxyServlet proxyServlet )
  50. {
  51. this( null, 0, proxyServlet );
  52. }
  53. /**
  54. * @param hostName the server name
  55. * @param port the server port
  56. * @param debug true to display System.err, false otherwise.
  57. * @param proxyServlet the wanted auth proxy servlet
  58. */
  59. public ProxyServer( String hostName, int port, AuthAsyncProxyServlet proxyServlet )
  60. {
  61. proxyServer = new Server();
  62. proxyServer.addConnector( getDefaultConnector( hostName, port ) );
  63. Context context = new Context( proxyServer, "/", 0 );
  64. context.addServlet( new ServletHolder( proxyServlet ), "/" );
  65. }
  66. /**
  67. * @return the host name
  68. */
  69. public String getHostName()
  70. {
  71. Connector connector = proxyServer.getConnectors()[0];
  72. return connector.getHost();
  73. }
  74. /**
  75. * @return the host port
  76. */
  77. public int getPort()
  78. {
  79. Connector connector = proxyServer.getConnectors()[0];
  80. return ( connector.getLocalPort() <= 0 ? connector.getPort() : connector.getLocalPort() );
  81. }
  82. /**
  83. * @throws Exception if any
  84. */
  85. public void start()
  86. throws Exception
  87. {
  88. if ( proxyServer != null )
  89. {
  90. proxyServer.start();
  91. }
  92. }
  93. /**
  94. * @throws Exception if any
  95. */
  96. public void stop()
  97. throws Exception
  98. {
  99. if ( proxyServer != null )
  100. {
  101. proxyServer.stop();
  102. }
  103. proxyServer = null;
  104. }
  105. private Connector getDefaultConnector( String hostName, int port )
  106. {
  107. Connector connector = new SocketConnector();
  108. if ( hostName != null )
  109. {
  110. connector.setHost( hostName );
  111. }
  112. else
  113. {
  114. try
  115. {
  116. connector.setHost( InetAddress.getLocalHost().getCanonicalHostName() );
  117. }
  118. catch ( UnknownHostException e )
  119. {
  120. // nop
  121. }
  122. }
  123. if ( port > 0 )
  124. {
  125. connector.setPort( port );
  126. }
  127. return connector;
  128. }
  129. /**
  130. * A proxy servlet with authentication support.
  131. */
  132. static class AuthAsyncProxyServlet
  133. extends AsyncProxyServlet
  134. {
  135. private Map<String, String> authentications;
  136. private long sleepTime = 0;
  137. /**
  138. * Constructor for non authentication servlet.
  139. */
  140. public AuthAsyncProxyServlet()
  141. {
  142. super();
  143. }
  144. /**
  145. * Constructor for authentication servlet.
  146. *
  147. * @param authentications a map of user/password
  148. */
  149. public AuthAsyncProxyServlet( Map<String, String> authentications )
  150. {
  151. this();
  152. this.authentications = authentications;
  153. }
  154. /**
  155. * Constructor for authentication servlet.
  156. *
  157. * @param authentications a map of user/password
  158. * @param sleepTime a positive time to sleep the service thread (for timeout)
  159. */
  160. public AuthAsyncProxyServlet( Map<String, String> authentications, long sleepTime )
  161. {
  162. this();
  163. this.authentications = authentications;
  164. this.sleepTime = sleepTime;
  165. }
  166. /** {@inheritDoc} */
  167. public void service( ServletRequest req, ServletResponse res )
  168. throws ServletException, IOException
  169. {
  170. final HttpServletRequest request = (HttpServletRequest) req;
  171. final HttpServletResponse response = (HttpServletResponse) res;
  172. if ( this.authentications != null && !this.authentications.isEmpty() )
  173. {
  174. String proxyAuthorization = request.getHeader( "Proxy-Authorization" );
  175. if ( proxyAuthorization != null && proxyAuthorization.startsWith( "Basic " ) )
  176. {
  177. String proxyAuth = proxyAuthorization.substring( 6 );
  178. String authorization = B64Code.decode( proxyAuth );
  179. String[] authTokens = authorization.split( ":" );
  180. String user = authTokens[0];
  181. String password = authTokens[1];
  182. if ( this.authentications.get( user ) == null )
  183. {
  184. throw new IllegalArgumentException( user + " not found in the map!" );
  185. }
  186. if ( sleepTime > 0 )
  187. {
  188. try
  189. {
  190. Thread.sleep( sleepTime );
  191. }
  192. catch ( InterruptedException e )
  193. {
  194. // nop
  195. }
  196. }
  197. String authPass = this.authentications.get( user ).toString();
  198. if ( password.equals( authPass ) )
  199. {
  200. // could throw exceptions...
  201. super.service( req, res );
  202. return;
  203. }
  204. }
  205. // Proxy-Authenticate Basic realm="CCProxy Authorization"
  206. response.addHeader( "Proxy-Authenticate", "Basic realm=\"Jetty Proxy Authorization\"" );
  207. response.setStatus( HttpServletResponse.SC_PROXY_AUTHENTICATION_REQUIRED );
  208. return;
  209. }
  210. super.service( req, res );
  211. }
  212. }
  213. }