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