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