Coverage Report - org.apache.maven.wagon.tck.http.fixture.ServerFixture
 
Classes in this File Line Coverage Branch Coverage Complexity
ServerFixture
0%
0/65
0%
0/10
0
 
 1  
 package org.apache.maven.wagon.tck.http.fixture;
 2  
 
 3  
 /*
 4  
  * Licensed to the Apache Software Foundation (ASF) under one
 5  
  * or more contributor license agreements.  See the NOTICE file
 6  
  * distributed with this work for additional information
 7  
  * regarding copyright ownership.  The ASF licenses this file
 8  
  * to you under the Apache License, Version 2.0 (the
 9  
  * "License"); you may not use this file except in compliance
 10  
  * with the License.  You may obtain a copy of the License at
 11  
  *
 12  
  *   http://www.apache.org/licenses/LICENSE-2.0
 13  
  *
 14  
  * Unless required by applicable law or agreed to in writing,
 15  
  * software distributed under the License is distributed on an
 16  
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 17  
  * KIND, either express or implied.  See the License for the
 18  
  * specific language governing permissions and limitations
 19  
  * under the License.
 20  
  */
 21  
 
 22  
 import static org.apache.maven.wagon.tck.http.util.TestUtil.getResource;
 23  
 
 24  
 import org.mortbay.jetty.Connector;
 25  
 import org.mortbay.jetty.Handler;
 26  
 import org.mortbay.jetty.Server;
 27  
 import org.mortbay.jetty.handler.DefaultHandler;
 28  
 import org.mortbay.jetty.handler.HandlerCollection;
 29  
 import org.mortbay.jetty.nio.SelectChannelConnector;
 30  
 import org.mortbay.jetty.security.Constraint;
 31  
 import org.mortbay.jetty.security.ConstraintMapping;
 32  
 import org.mortbay.jetty.security.HashUserRealm;
 33  
 import org.mortbay.jetty.security.SecurityHandler;
 34  
 import org.mortbay.jetty.security.SslSocketConnector;
 35  
 import org.mortbay.jetty.servlet.AbstractSessionManager;
 36  
 import org.mortbay.jetty.servlet.FilterHolder;
 37  
 import org.mortbay.jetty.servlet.FilterMapping;
 38  
 import org.mortbay.jetty.servlet.ServletHolder;
 39  
 import org.mortbay.jetty.servlet.SessionHandler;
 40  
 import org.mortbay.jetty.webapp.WebAppContext;
 41  
 
 42  
 import java.io.File;
 43  
 import java.io.IOException;
 44  
 import java.net.URISyntaxException;
 45  
 
 46  
 import javax.servlet.Filter;
 47  
 import javax.servlet.Servlet;
 48  
 
 49  
 public class ServerFixture
 50  
 {
 51  
     public static final String SERVER_ROOT_RESOURCE_PATH = "default-server-root";
 52  
 
 53  
     public static final String SERVER_SSL_KEYSTORE_RESOURCE_PATH = "ssl/keystore";
 54  
 
 55  
     public static final String SERVER_SSL_KEYSTORE_PASSWORD = "password";
 56  
 
 57  
     public static final String SERVER_HOST = "localhost";
 58  
 
 59  
     private final Server server;
 60  
 
 61  
     private final WebAppContext webappContext;
 62  
 
 63  
     private final HashUserRealm securityRealm;
 64  
 
 65  
     private final SecurityHandler securityHandler;
 66  
 
 67  0
     private int filterCount = 0;;
 68  
 
 69  
     public ServerFixture( final int port, final boolean ssl )
 70  
         throws URISyntaxException, IOException
 71  0
     {
 72  0
         server = new Server();
 73  0
         if ( ssl )
 74  
         {
 75  0
             SslSocketConnector connector = new SslSocketConnector();
 76  0
             String keystore = getResource( SERVER_SSL_KEYSTORE_RESOURCE_PATH ).getAbsolutePath();
 77  
 
 78  
             // connector.setHost( SERVER_HOST );
 79  0
             connector.setPort( port );
 80  0
             connector.setKeystore( keystore );
 81  0
             connector.setPassword( SERVER_SSL_KEYSTORE_PASSWORD );
 82  0
             connector.setKeyPassword( SERVER_SSL_KEYSTORE_PASSWORD );
 83  
 
 84  0
             server.addConnector( connector );
 85  0
         }
 86  
         else
 87  
         {
 88  0
             Connector connector = new SelectChannelConnector();
 89  0
             connector.setHost( "localhost" );
 90  0
             connector.setPort( port );
 91  0
             server.addConnector( connector );
 92  
         }
 93  
 
 94  0
         Constraint constraint = new Constraint();
 95  0
         constraint.setName( Constraint.__BASIC_AUTH );
 96  
 
 97  0
         constraint.setRoles( new String[] { "allowed" } );
 98  0
         constraint.setAuthenticate( true );
 99  
 
 100  0
         ConstraintMapping cm = new ConstraintMapping();
 101  0
         cm.setConstraint( constraint );
 102  0
         cm.setPathSpec( "/protected/*" );
 103  
 
 104  0
         securityHandler = new SecurityHandler();
 105  
 
 106  0
         securityRealm = new HashUserRealm( "Test Server" );
 107  
 
 108  0
         securityHandler.setUserRealm( securityRealm );
 109  0
         securityHandler.setConstraintMappings( new ConstraintMapping[] { cm } );
 110  
 
 111  0
         webappContext = new WebAppContext();
 112  0
         webappContext.setContextPath( "/" );
 113  
 
 114  0
         File base = getResource( SERVER_ROOT_RESOURCE_PATH );
 115  0
         System.out.println( "docroot: " + base );
 116  0
         webappContext.setWar( base.getAbsolutePath() );
 117  0
         webappContext.addHandler( securityHandler );
 118  
 
 119  0
         SessionHandler sessionHandler = webappContext.getSessionHandler();
 120  0
         ( (AbstractSessionManager) sessionHandler.getSessionManager() ).setUsingCookies( false );
 121  
 
 122  0
         HandlerCollection handlers = new HandlerCollection();
 123  0
         handlers.setHandlers( new Handler[] { webappContext, new DefaultHandler() } );
 124  
 
 125  0
         server.setHandler( handlers );
 126  0
     }
 127  
 
 128  
     public void addFilter( final String pathSpec, final Filter filter )
 129  
     {
 130  0
         String name = "filter" + filterCount++;
 131  
 
 132  0
         FilterMapping fm = new FilterMapping();
 133  0
         fm.setPathSpec( pathSpec );
 134  0
         fm.setFilterName( name );
 135  
 
 136  0
         FilterHolder fh = new FilterHolder( filter );
 137  0
         fh.setName( name );
 138  
 
 139  0
         webappContext.getServletHandler().addFilter( fh, fm );
 140  0
     }
 141  
 
 142  
     public void addServlet( final String pathSpec, final Servlet servlet )
 143  
     {
 144  0
         webappContext.getServletHandler().addServletWithMapping( new ServletHolder( servlet ), pathSpec );
 145  0
     }
 146  
 
 147  
     public void addUser( final String user, final String password )
 148  
     {
 149  0
         securityRealm.put( user, password );
 150  0
         securityRealm.addUserToRole( user, "allowed" );
 151  0
     }
 152  
 
 153  
     public Server getServer()
 154  
     {
 155  0
         return server;
 156  
     }
 157  
 
 158  
     public WebAppContext getWebappContext()
 159  
     {
 160  0
         return webappContext;
 161  
     }
 162  
 
 163  
     public void stop()
 164  
         throws Exception
 165  
     {
 166  0
         if ( server != null )
 167  
         {
 168  0
             server.stop();
 169  
         }
 170  0
     }
 171  
 
 172  
     public void start()
 173  
         throws Exception
 174  
     {
 175  0
         server.start();
 176  
 
 177  0
         int total = 0;
 178  0
         while ( total < 3000 && !server.isStarted() )
 179  
         {
 180  0
             server.wait( 10 );
 181  0
             total += 10;
 182  
         }
 183  
 
 184  0
         if ( !server.isStarted() )
 185  
         {
 186  0
             throw new IllegalStateException( "Server didn't start in: " + total + "ms." );
 187  
         }
 188  0
     }
 189  
 
 190  
 }