diff --git a/its/core-it-suite/src/test/java/org/apache/maven/it/HttpServer.java b/its/core-it-suite/src/test/java/org/apache/maven/it/HttpServer.java index e94780ac15..7d2bf2fad7 100644 --- a/its/core-it-suite/src/test/java/org/apache/maven/it/HttpServer.java +++ b/its/core-it-suite/src/test/java/org/apache/maven/it/HttpServer.java @@ -26,174 +26,204 @@ import org.eclipse.jetty.util.thread.QueuedThreadPool; import org.eclipse.jetty.util.thread.ScheduledExecutorScheduler; import com.google.common.io.ByteStreams; -import com.google.common.io.Closer; /** - * An HTTP server that handles all requests on a given port from a specified source, optionally secured using BASIC auth by providing a username and password. - * - * The source can either be a URL or a directory. When a request is made the request is satisfied from the provided source. + * An HTTP server that handles all requests on a given port from a specified source, optionally secured using BASIC auth + * by providing a username and password. The source can either be a URL or a directory. When a request is made the + * request is satisfied from the provided source. * * @author Jason van Zyl - * */ -public class HttpServer { +public class HttpServer +{ - private final Server server; - private final StreamSource source; - private final String username; - private final String password; - - public HttpServer(int port, String username, String password, StreamSource source) { - this.username = username; - this.password = password; - this.source = source; - this.server = server(port); - } - - public void start() throws Exception { - server.start(); - //server.join(); - } - - public void stop() throws Exception { - server.stop(); - } - - public int port() { - return ((ServerConnector) server.getConnectors()[0]).getLocalPort(); - } - - private Server server(int port) { - - QueuedThreadPool threadPool = new QueuedThreadPool(); - threadPool.setMaxThreads(500); - Server server = new Server(threadPool); - server.addBean(new ScheduledExecutorScheduler()); - - ServerConnector http = new ServerConnector(server); - http.setPort(port); - http.setIdleTimeout(30000); - server.addConnector(http); - - StreamSourceHandler handler = new StreamSourceHandler(source); - - if (username != null && password != null) { - HashLoginService loginService = new HashLoginService("Test Server"); - loginService.putUser(username, new Password(password), new String[] { - "user" - }); - server.addBean(loginService); - - ConstraintSecurityHandler security = new ConstraintSecurityHandler(); - server.setHandler(security); - - Constraint constraint = new Constraint(); - constraint.setName("auth"); - constraint.setAuthenticate(true); - constraint.setRoles(new String[] { - "user", "admin" - }); - - ConstraintMapping mapping = new ConstraintMapping(); - mapping.setPathSpec("/*"); - mapping.setConstraint(constraint); - - security.setConstraintMappings(Collections.singletonList(mapping)); - security.setAuthenticator(new BasicAuthenticator()); - security.setLoginService(loginService); - security.setHandler(handler); - } else { - server.setHandler(handler); - } - return server; - } - - public static HttpServerBuilder builder() { - return new HttpServerBuilder(); - } - - public static class HttpServerBuilder { - - private int port; - private String username; - private String password; - private StreamSource source; - - public HttpServerBuilder port(int port) { - this.port = port; - return this; - } - - public HttpServerBuilder username(String username) { - this.username = username; - return this; - } - - public HttpServerBuilder password(String password) { - this.password = password; - return this; - } - - public HttpServerBuilder source(final String source) { - this.source = new StreamSource() { - public InputStream stream(String path) throws IOException { - return new URL(String.format("%s/%s", source, path)).openStream(); - } - }; - return this; - } - - public HttpServerBuilder source(final File source) { - this.source = new StreamSource() { - public InputStream stream(String path) throws IOException { - return new FileInputStream(new File(source, path)); - } - }; - return this; - } - - public HttpServer build() { - return new HttpServer(port, username, password, source); - } - } - - public static interface StreamSource { - InputStream stream(String path) throws IOException; - } - - public static class StreamSourceHandler extends AbstractHandler { + private final Server server; private final StreamSource source; - public StreamSourceHandler(StreamSource source) { - this.source = source; + private final String username; + + private final String password; + + public HttpServer( int port, String username, String password, StreamSource source ) + { + this.username = username; + this.password = password; + this.source = source; + this.server = server( port ); } - public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { - response.setContentType("application/octet-stream"); - response.setStatus(HttpServletResponse.SC_OK); - Closer closer = Closer.create(); - try { - // /target --> target - InputStream in = closer.register(source.stream(target.substring(1))); - OutputStream out = closer.register(response.getOutputStream()); - ByteStreams.copy(in, out); - } catch (Throwable e) { - throw closer.rethrow(e); - } finally { - closer.close(); - } - baseRequest.setHandled(true); + public void start() + throws Exception + { + server.start(); + // server.join(); } - } - public static void main(String[] args) throws Exception { - HttpServer server = HttpServer.builder() // - .port(0) // - .username("maven") // - .password("secret") // - .source(new File("/tmp/repo")) // + public void stop() + throws Exception + { + server.stop(); + } + + public int port() + { + return ( (ServerConnector) server.getConnectors()[0] ).getLocalPort(); + } + + private Server server( int port ) + { + + QueuedThreadPool threadPool = new QueuedThreadPool(); + threadPool.setMaxThreads( 500 ); + Server server = new Server( threadPool ); + server.addBean( new ScheduledExecutorScheduler() ); + + ServerConnector http = new ServerConnector( server ); + http.setPort( port ); + http.setIdleTimeout( 30000 ); + server.addConnector( http ); + + StreamSourceHandler handler = new StreamSourceHandler( source ); + + if ( username != null && password != null ) + { + HashLoginService loginService = new HashLoginService( "Test Server" ); + loginService.putUser( username, new Password( password ), new String[] { "user" } ); + server.addBean( loginService ); + + ConstraintSecurityHandler security = new ConstraintSecurityHandler(); + server.setHandler( security ); + + Constraint constraint = new Constraint(); + constraint.setName( "auth" ); + constraint.setAuthenticate( true ); + constraint.setRoles( new String[] { "user", "admin" } ); + + ConstraintMapping mapping = new ConstraintMapping(); + mapping.setPathSpec( "/*" ); + mapping.setConstraint( constraint ); + + security.setConstraintMappings( Collections.singletonList( mapping ) ); + security.setAuthenticator( new BasicAuthenticator() ); + security.setLoginService( loginService ); + security.setHandler( handler ); + } + else + { + server.setHandler( handler ); + } + return server; + } + + public static HttpServerBuilder builder() + { + return new HttpServerBuilder(); + } + + public static class HttpServerBuilder + { + + private int port; + + private String username; + + private String password; + + private StreamSource source; + + public HttpServerBuilder port( int port ) + { + this.port = port; + return this; + } + + public HttpServerBuilder username( String username ) + { + this.username = username; + return this; + } + + public HttpServerBuilder password( String password ) + { + this.password = password; + return this; + } + + public HttpServerBuilder source( final String source ) + { + this.source = new StreamSource() + { + @Override + public InputStream stream( String path ) + throws IOException + { + return new URL( String.format( "%s/%s", source, path ) ).openStream(); + } + }; + return this; + } + + public HttpServerBuilder source( final File source ) + { + this.source = new StreamSource() + { + @Override + public InputStream stream( String path ) + throws IOException + { + return new FileInputStream( new File( source, path ) ); + } + }; + return this; + } + + public HttpServer build() + { + return new HttpServer( port, username, password, source ); + } + } + + public static interface StreamSource + { + InputStream stream( String path ) + throws IOException; + } + + public static class StreamSourceHandler + extends AbstractHandler + { + + private final StreamSource source; + + public StreamSourceHandler( StreamSource source ) + { + this.source = source; + } + + @Override + public void handle( String target, Request baseRequest, HttpServletRequest request, + HttpServletResponse response ) + throws IOException, ServletException + { + response.setContentType( "application/octet-stream" ); + response.setStatus( HttpServletResponse.SC_OK ); + try(InputStream in = source.stream( target.substring( 1 ) ); OutputStream out = response.getOutputStream() ) { + ByteStreams.copy( in, out ); + } + baseRequest.setHandled( true ); + } + } + + public static void main( String[] args ) + throws Exception + { + HttpServer server = HttpServer.builder() // + .port( 0 ) // + .username( "maven" ) // + .password( "secret" ) // + .source( new File( "/tmp/repo" ) ) // .build(); - server.start(); - } + server.start(); + } }