Coverage Report - org.apache.maven.wagon.tck.http.fixture.ServerFixture
 
Classes in this File Line Coverage Branch Coverage Complexity
ServerFixture
0 %
0/71
0 %
0/14
2
 
 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 org.apache.log4j.Logger;
 23  
 import org.mortbay.jetty.Connector;
 24  
 import org.mortbay.jetty.Handler;
 25  
 import org.mortbay.jetty.Server;
 26  
 import org.mortbay.jetty.handler.DefaultHandler;
 27  
 import org.mortbay.jetty.handler.HandlerCollection;
 28  
 import org.mortbay.jetty.nio.SelectChannelConnector;
 29  
 import org.mortbay.jetty.security.Constraint;
 30  
 import org.mortbay.jetty.security.ConstraintMapping;
 31  
 import org.mortbay.jetty.security.HashUserRealm;
 32  
 import org.mortbay.jetty.security.SecurityHandler;
 33  
 import org.mortbay.jetty.security.SslSocketConnector;
 34  
 import org.mortbay.jetty.servlet.AbstractSessionManager;
 35  
 import org.mortbay.jetty.servlet.FilterHolder;
 36  
 import org.mortbay.jetty.servlet.FilterMapping;
 37  
 import org.mortbay.jetty.servlet.ServletHolder;
 38  
 import org.mortbay.jetty.servlet.SessionHandler;
 39  
 import org.mortbay.jetty.webapp.WebAppContext;
 40  
 
 41  
 import javax.servlet.Filter;
 42  
 import javax.servlet.Servlet;
 43  
 import java.io.File;
 44  
 import java.io.IOException;
 45  
 import java.net.URISyntaxException;
 46  
 
 47  
 import static org.apache.maven.wagon.tck.http.util.TestUtil.getResource;
 48  
 
 49  
 public class ServerFixture
 50  
 {
 51  0
     private static Logger logger = Logger.getLogger( ServerFixture.class );
 52  
 
 53  
     public static final String SERVER_ROOT_RESOURCE_PATH = "default-server-root";
 54  
 
 55  
     // it seems that some JDKs have a problem if you use different key stores
 56  
     // so we gonna reuse the keystore which is is used in the wagon implementations already
 57  
     public static final String SERVER_SSL_KEYSTORE_RESOURCE_PATH = "ssl/keystore";
 58  
 
 59  
     public static final String SERVER_SSL_KEYSTORE_PASSWORD = "wagonhttp";
 60  
 
 61  
     public static final String SERVER_HOST = "localhost";
 62  
 
 63  
     private final Server server;
 64  
 
 65  
     private final WebAppContext webappContext;
 66  
 
 67  
     private final HashUserRealm securityRealm;
 68  
 
 69  
     private final SecurityHandler securityHandler;
 70  
 
 71  0
     private int filterCount = 0;
 72  
 
 73  
     private int httpPort;
 74  
 
 75  
     public ServerFixture( final boolean ssl )
 76  
         throws URISyntaxException, IOException
 77  0
     {
 78  0
         server = new Server();
 79  0
         if ( ssl )
 80  
         {
 81  0
             SslSocketConnector connector = new SslSocketConnector();
 82  0
             String keystore = getResource( SERVER_SSL_KEYSTORE_RESOURCE_PATH ).getAbsolutePath();
 83  
 
 84  0
             Logger.getLogger( ServerFixture.class ).info( "TCK Keystore path: " + keystore );
 85  0
             System.setProperty( "javax.net.ssl.keyStore", keystore );
 86  0
             System.setProperty( "javax.net.ssl.trustStore", keystore );
 87  
 
 88  
             // connector.setHost( SERVER_HOST );
 89  
             //connector.setPort( port );
 90  0
             connector.setKeystore( keystore );
 91  0
             connector.setPassword( SERVER_SSL_KEYSTORE_PASSWORD );
 92  0
             connector.setKeyPassword( SERVER_SSL_KEYSTORE_PASSWORD );
 93  
 
 94  0
             server.addConnector( connector );
 95  0
         }
 96  
         else
 97  
         {
 98  0
             Connector connector = new SelectChannelConnector();
 99  0
             connector.setHost( "localhost" );
 100  
             //connector.setPort( port );
 101  0
             server.addConnector( connector );
 102  
         }
 103  
 
 104  0
         Constraint constraint = new Constraint();
 105  0
         constraint.setName( Constraint.__BASIC_AUTH );
 106  
 
 107  0
         constraint.setRoles( new String[]{ "allowed" } );
 108  0
         constraint.setAuthenticate( true );
 109  
 
 110  0
         ConstraintMapping cm = new ConstraintMapping();
 111  0
         cm.setConstraint( constraint );
 112  0
         cm.setPathSpec( "/protected/*" );
 113  
 
 114  0
         securityHandler = new SecurityHandler();
 115  
 
 116  0
         securityRealm = new HashUserRealm( "Test Server" );
 117  
 
 118  0
         securityHandler.setUserRealm( securityRealm );
 119  0
         securityHandler.setConstraintMappings( new ConstraintMapping[]{ cm } );
 120  
 
 121  0
         webappContext = new WebAppContext();
 122  0
         webappContext.setContextPath( "/" );
 123  
 
 124  0
         File base = getResource( SERVER_ROOT_RESOURCE_PATH );
 125  0
         logger.info( "docroot: " + base );
 126  0
         webappContext.setWar( base.getAbsolutePath() );
 127  0
         webappContext.addHandler( securityHandler );
 128  
 
 129  0
         SessionHandler sessionHandler = webappContext.getSessionHandler();
 130  0
         ( (AbstractSessionManager) sessionHandler.getSessionManager() ).setUsingCookies( false );
 131  
 
 132  0
         HandlerCollection handlers = new HandlerCollection();
 133  0
         handlers.setHandlers( new Handler[]{ webappContext, new DefaultHandler() } );
 134  
 
 135  0
         server.setHandler( handlers );
 136  0
     }
 137  
 
 138  
     public void addFilter( final String pathSpec, final Filter filter )
 139  
     {
 140  0
         String name = "filter" + filterCount++;
 141  
 
 142  0
         FilterMapping fm = new FilterMapping();
 143  0
         fm.setPathSpec( pathSpec );
 144  0
         fm.setFilterName( name );
 145  
 
 146  0
         FilterHolder fh = new FilterHolder( filter );
 147  0
         fh.setName( name );
 148  
 
 149  0
         webappContext.getServletHandler().addFilter( fh, fm );
 150  0
     }
 151  
 
 152  
     public void addServlet( final String pathSpec, final Servlet servlet )
 153  
     {
 154  0
         webappContext.getServletHandler().addServletWithMapping( new ServletHolder( servlet ), pathSpec );
 155  0
     }
 156  
 
 157  
     public void addUser( final String user, final String password )
 158  
     {
 159  0
         securityRealm.put( user, password );
 160  0
         securityRealm.addUserToRole( user, "allowed" );
 161  0
     }
 162  
 
 163  
     public Server getServer()
 164  
     {
 165  0
         return server;
 166  
     }
 167  
 
 168  
     public WebAppContext getWebappContext()
 169  
     {
 170  0
         return webappContext;
 171  
     }
 172  
 
 173  
     public void stop()
 174  
         throws Exception
 175  
     {
 176  0
         if ( server != null )
 177  
         {
 178  0
             server.stop();
 179  
         }
 180  0
     }
 181  
 
 182  
     public void start()
 183  
         throws Exception
 184  
     {
 185  0
         if ( server.isStarted() || server.isRunning() )
 186  
         {
 187  0
             return;
 188  
         }
 189  0
         server.start();
 190  
 
 191  0
         int total = 0;
 192  0
         while ( total < 3000 && !server.isStarted() )
 193  
         {
 194  0
             server.wait( 10 );
 195  0
             total += 10;
 196  
         }
 197  
 
 198  0
         if ( !server.isStarted() )
 199  
         {
 200  0
             throw new IllegalStateException( "Server didn't start in: " + total + "ms." );
 201  
         }
 202  0
         this.httpPort = server.getConnectors()[0].getLocalPort();
 203  0
     }
 204  
 
 205  
     public int getHttpPort()
 206  
     {
 207  0
         return httpPort;
 208  
     }
 209  
 }