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.eclipse.jetty.security.ConstraintMapping;
23  import org.eclipse.jetty.security.ConstraintSecurityHandler;
24  import org.eclipse.jetty.security.HashLoginService;
25  import org.eclipse.jetty.server.Handler;
26  import org.eclipse.jetty.server.Server;
27  import org.eclipse.jetty.server.ServerConnector;
28  import org.eclipse.jetty.server.handler.DefaultHandler;
29  import org.eclipse.jetty.server.handler.HandlerCollection;
30  import org.eclipse.jetty.server.session.AbstractSessionManager;
31  import org.eclipse.jetty.server.session.SessionHandler;
32  import org.eclipse.jetty.servlet.FilterHolder;
33  import org.eclipse.jetty.servlet.FilterMapping;
34  import org.eclipse.jetty.servlet.ServletHolder;
35  import org.eclipse.jetty.util.security.Constraint;
36  import org.eclipse.jetty.util.security.Password;
37  import org.eclipse.jetty.util.ssl.SslContextFactory;
38  import org.eclipse.jetty.webapp.WebAppContext;
39  import org.slf4j.Logger;
40  import org.slf4j.LoggerFactory;
41  
42  import javax.servlet.Filter;
43  import javax.servlet.Servlet;
44  import java.io.File;
45  import java.io.IOException;
46  import java.net.URISyntaxException;
47  
48  import static org.apache.maven.wagon.tck.http.util.TestUtil.getResource;
49  
50  /**
51   *
52   */
53  public class ServerFixture
54  {
55      private static Logger logger = LoggerFactory.getLogger( ServerFixture.class );
56  
57      public static final String SERVER_ROOT_RESOURCE_PATH = "default-server-root";
58  
59      // it seems that some JDKs have a problem if you use different key stores
60      // so we gonna reuse the keystore which is is used in the wagon implementations already
61      public static final String SERVER_SSL_KEYSTORE_RESOURCE_PATH = "ssl/keystore";
62  
63      public static final String SERVER_SSL_KEYSTORE_PASSWORD = "wagonhttp";
64  
65      public static final String SERVER_HOST = "localhost";
66  
67      private final Server server;
68      private ServerConnector serverConnector;
69  
70      private final WebAppContext webappContext;
71  
72      private final HashLoginService loginService;
73  
74      private final ConstraintSecurityHandler securityHandler;
75  
76      private int filterCount = 0;
77  
78      private int httpPort;
79  
80      public ServerFixture( final boolean ssl )
81          throws URISyntaxException, IOException
82      {
83          server = new Server();
84          if ( ssl )
85          {
86              String keystore = getResource( SERVER_SSL_KEYSTORE_RESOURCE_PATH ).getAbsolutePath();
87  
88              LoggerFactory.getLogger( ServerFixture.class ).info( "TCK Keystore path: " + keystore );
89              System.setProperty( "javax.net.ssl.keyStore", keystore );
90              System.setProperty( "javax.net.ssl.trustStore", keystore );
91  
92              SslContextFactory sslContextFactory = new SslContextFactory();
93              sslContextFactory.setKeyStorePath( keystore );
94              sslContextFactory.setKeyStorePassword( SERVER_SSL_KEYSTORE_PASSWORD );
95              sslContextFactory.setKeyManagerPassword( SERVER_SSL_KEYSTORE_PASSWORD );
96              serverConnector = new ServerConnector( server, sslContextFactory );
97              server.addConnector( serverConnector );
98          }
99          else
100         {
101             serverConnector = new ServerConnector( server );
102             serverConnector.setHost( "localhost" );
103             //connector.setPort( port );
104             server.addConnector( serverConnector );
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 ConstraintSecurityHandler();
118 
119         loginService = new HashLoginService( "Test Server" );
120 
121         securityHandler.setLoginService( loginService );
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.setHandler( 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         loginService.putUser( user, new Password( password ), new String[] { "allowed" } );
163     }
164 
165     public Server getServer()
166     {
167         return server;
168     }
169 
170     public WebAppContext getWebappContext()
171     {
172         return webappContext;
173     }
174 
175     public void stop()
176         throws Exception
177     {
178         if ( server != null )
179         {
180             server.stop();
181         }
182     }
183 
184     public void start()
185         throws Exception
186     {
187         if ( server.isStarted() || server.isRunning() )
188         {
189             return;
190         }
191         server.start();
192 
193         int total = 0;
194         while ( total < 3 * 1000 && !server.isStarted() )
195         {
196             server.wait( 10 );
197             total += 10;
198         }
199 
200         if ( !server.isStarted() )
201         {
202             throw new IllegalStateException( "Server didn't start in: " + total + "ms." );
203         }
204         this.httpPort = serverConnector.getLocalPort();
205     }
206 
207     public int getHttpPort()
208     {
209         return httpPort;
210     }
211 }