Repository: maven-integration-testing
Updated Branches:
  refs/heads/master bae4c23a4 -> b3a9ea084


- Use Maven formatting
- Use Java7 try-with-resources


Project: http://git-wip-us.apache.org/repos/asf/maven-integration-testing/repo
Commit: 
http://git-wip-us.apache.org/repos/asf/maven-integration-testing/commit/b3a9ea08
Tree: 
http://git-wip-us.apache.org/repos/asf/maven-integration-testing/tree/b3a9ea08
Diff: 
http://git-wip-us.apache.org/repos/asf/maven-integration-testing/diff/b3a9ea08

Branch: refs/heads/master
Commit: b3a9ea084262f765216706468924256338b658b4
Parents: bae4c23
Author: Jason van Zyl <ja...@tesla.io>
Authored: Thu Apr 2 08:48:53 2015 -0400
Committer: Jason van Zyl <ja...@tesla.io>
Committed: Thu Apr 2 08:48:53 2015 -0400

----------------------------------------------------------------------
 .../java/org/apache/maven/it/HttpServer.java    | 312 ++++++++++---------
 1 file changed, 171 insertions(+), 141 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/maven-integration-testing/blob/b3a9ea08/core-it-suite/src/test/java/org/apache/maven/it/HttpServer.java
----------------------------------------------------------------------
diff --git a/core-it-suite/src/test/java/org/apache/maven/it/HttpServer.java 
b/core-it-suite/src/test/java/org/apache/maven/it/HttpServer.java
index e94780a..7d2bf2f 100644
--- a/core-it-suite/src/test/java/org/apache/maven/it/HttpServer.java
+++ b/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 {
-
-  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 class HttpServer
+{
 
-  public static HttpServerBuilder builder() {
-    return new HttpServerBuilder();
-  }
+    private final Server server;
 
-  public static class HttpServerBuilder {
+    private final StreamSource source;
 
-    private int port;
-    private String username;
-    private String password;
-    private StreamSource source;
+    private final String username;
 
-    public HttpServerBuilder port(int port) {
-      this.port = port;
-      return this;
+    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 HttpServerBuilder username(String username) {
-      this.username = username;
-      return this;
+    public void start()
+        throws Exception
+    {
+        server.start();
+        // server.join();
     }
 
-    public HttpServerBuilder password(String password) {
-      this.password = password;
-      return this;
+    public void stop()
+        throws Exception
+    {
+        server.stop();
     }
 
-    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 int port()
+    {
+        return ( (ServerConnector) server.getConnectors()[0] ).getLocalPort();
     }
 
-    public HttpServerBuilder source(final File source) {
-      this.source = new StreamSource() {
-        public InputStream stream(String path) throws IOException {
-          return new FileInputStream(new File(source, path));
+    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 );
         }
-      };
-      return this;
+        else
+        {
+            server.setHandler( handler );
+        }
+        return server;
     }
 
-    public HttpServer build() {
-      return new HttpServer(port, username, password, source);
+    public static HttpServerBuilder builder()
+    {
+        return new HttpServerBuilder();
     }
-  }
 
-  public static interface StreamSource {
-    InputStream stream(String path) throws IOException;
-  }
+    public static class HttpServerBuilder
+    {
 
-  public static class StreamSourceHandler extends AbstractHandler {
+        private int port;
 
-    private final StreamSource source;
+        private String username;
+
+        private String password;
+
+        private StreamSource source;
+
+        public HttpServerBuilder port( int port )
+        {
+            this.port = port;
+            return this;
+        }
 
-    public StreamSourceHandler(StreamSource source) {
-      this.source = source;
+        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 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 static interface StreamSource
+    {
+        InputStream stream( String path )
+            throws IOException;
     }
-  }
-
-  public static void main(String[] args) throws Exception {
-    HttpServer server = HttpServer.builder() //
-        .port(0) //
-        .username("maven") //
-        .password("secret") //
-        .source(new File("/tmp/repo")) //
+
+    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();
+    }
 }

Reply via email to