View Javadoc

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      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      private int filterCount = 0;
72  
73      private int httpPort;
74  
75      public ServerFixture( final boolean ssl )
76          throws URISyntaxException, IOException
77      {
78          server = new Server();
79          if ( ssl )
80          {
81              SslSocketConnector connector = new SslSocketConnector();
82              String keystore = getResource( SERVER_SSL_KEYSTORE_RESOURCE_PATH ).getAbsolutePath();
83  
84              Logger.getLogger( ServerFixture.class ).info( "TCK Keystore path: " + keystore );
85              System.setProperty( "javax.net.ssl.keyStore", keystore );
86              System.setProperty( "javax.net.ssl.trustStore", keystore );
87  
88              // connector.setHost( SERVER_HOST );
89              //connector.setPort( port );
90              connector.setKeystore( keystore );
91              connector.setPassword( SERVER_SSL_KEYSTORE_PASSWORD );
92              connector.setKeyPassword( SERVER_SSL_KEYSTORE_PASSWORD );
93  
94              server.addConnector( connector );
95          }
96          else
97          {
98              Connector connector = new SelectChannelConnector();
99              connector.setHost( "localhost" );
100             //connector.setPort( port );
101             server.addConnector( connector );
102         }
103 
104         Constraint constraint = new Constraint();
105         constraint.setName( Constraint.__BASIC_AUTH );
106 
107         constraint.setRoles( new String[]{ "allowed" } );
108         constraint.setAuthenticate( true );
109 
110         ConstraintMapping cm = new ConstraintMapping();
111         cm.setConstraint( constraint );
112         cm.setPathSpec( "/protected/*" );
113 
114         securityHandler = new SecurityHandler();
115 
116         securityRealm = new HashUserRealm( "Test Server" );
117 
118         securityHandler.setUserRealm( securityRealm );
119         securityHandler.setConstraintMappings( new ConstraintMapping[]{ cm } );
120 
121         webappContext = new WebAppContext();
122         webappContext.setContextPath( "/" );
123 
124         File base = getResource( SERVER_ROOT_RESOURCE_PATH );
125         logger.info( "docroot: " + base );
126         webappContext.setWar( base.getAbsolutePath() );
127         webappContext.addHandler( securityHandler );
128 
129         SessionHandler sessionHandler = webappContext.getSessionHandler();
130         ( (AbstractSessionManager) sessionHandler.getSessionManager() ).setUsingCookies( false );
131 
132         HandlerCollection handlers = new HandlerCollection();
133         handlers.setHandlers( new Handler[]{ webappContext, new DefaultHandler() } );
134 
135         server.setHandler( handlers );
136     }
137 
138     public void addFilter( final String pathSpec, final Filter filter )
139     {
140         String name = "filter" + filterCount++;
141 
142         FilterMapping fm = new FilterMapping();
143         fm.setPathSpec( pathSpec );
144         fm.setFilterName( name );
145 
146         FilterHolder fh = new FilterHolder( filter );
147         fh.setName( name );
148 
149         webappContext.getServletHandler().addFilter( fh, fm );
150     }
151 
152     public void addServlet( final String pathSpec, final Servlet servlet )
153     {
154         webappContext.getServletHandler().addServletWithMapping( new ServletHolder( servlet ), pathSpec );
155     }
156 
157     public void addUser( final String user, final String password )
158     {
159         securityRealm.put( user, password );
160         securityRealm.addUserToRole( user, "allowed" );
161     }
162 
163     public Server getServer()
164     {
165         return server;
166     }
167 
168     public WebAppContext getWebappContext()
169     {
170         return webappContext;
171     }
172 
173     public void stop()
174         throws Exception
175     {
176         if ( server != null )
177         {
178             server.stop();
179         }
180     }
181 
182     public void start()
183         throws Exception
184     {
185         if ( server.isStarted() || server.isRunning() )
186         {
187             return;
188         }
189         server.start();
190 
191         int total = 0;
192         while ( total < 3000 && !server.isStarted() )
193         {
194             server.wait( 10 );
195             total += 10;
196         }
197 
198         if ( !server.isStarted() )
199         {
200             throw new IllegalStateException( "Server didn't start in: " + total + "ms." );
201         }
202         this.httpPort = server.getConnectors()[0].getLocalPort();
203     }
204 
205     public int getHttpPort()
206     {
207         return httpPort;
208     }
209 }