View Javadoc
1   /**
2    * Copyright 2005-2014 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.ksb.server;
17  
18  import org.apache.log4j.Logger;
19  import org.eclipse.jetty.server.Connector;
20  import org.eclipse.jetty.server.handler.DefaultHandler;
21  import org.eclipse.jetty.server.handler.HandlerCollection;
22  import org.eclipse.jetty.server.handler.HandlerList;
23  import org.eclipse.jetty.server.nio.SelectChannelConnector;
24  import org.eclipse.jetty.server.ssl.SslSelectChannelConnector;
25  import org.eclipse.jetty.util.ssl.SslContextFactory;
26  import org.eclipse.jetty.server.Server;
27  import org.eclipse.jetty.webapp.WebAppContext;
28  import org.kuali.rice.core.api.config.property.Config;
29  import org.kuali.rice.core.api.config.property.ConfigContext;
30  import org.kuali.rice.core.api.exception.RiceRuntimeException;
31  import org.kuali.rice.core.api.security.credentials.CredentialsSource;
32  import org.kuali.rice.core.api.security.credentials.CredentialsSourceFactory;
33  import org.kuali.rice.ksb.BaseTestServer;
34  import org.kuali.rice.ksb.security.credentials.UsernamePasswordCredentialsSource;
35  
36  import java.net.URL;
37  import java.util.Collections;
38  import java.util.List;
39  
40  public class TestClient1 extends BaseTestServer {
41  
42      private static final Logger LOG = Logger.getLogger(TestClient1.class);
43  
44      /**
45       * Class that does late binding of config params used to configure our server
46       */
47      public static final class ConfigConstants {
48          public final String WEB_ROOT = "org/kuali/rice/ksb/testclient1";
49          public final String CONTEXT = "/TestClient1";
50          public final Integer SERVER_HTTP_PORT = Integer.valueOf(ConfigContext.getCurrentContextConfig().getProperty("ksb.client1.port"));
51          public final Integer SERVER_HTTPS_PORT = Integer.valueOf(ConfigContext.getCurrentContextConfig().getProperty("ksb.client1.ssl.port"));
52          public final String KEYSTORE_PASS = ConfigContext.getCurrentContextConfig().getKeystorePassword();
53          public final String KEYSTORE_PATH; // assigned in constructor
54  
55          public ConfigConstants() {
56              String keystoreFileTmp = null;
57  
58              try {
59                  keystoreFileTmp = ConfigContext.getCurrentContextConfig().getKeystoreFile();
60              } catch (Exception e) {
61                  throw new RiceRuntimeException("Couldn't get keystore file location", e);
62              }
63  
64              KEYSTORE_PATH = keystoreFileTmp;
65          }
66      }
67  
68      /**
69       * Creates a Server that exposes the TestClient1 services via http and https
70       *
71       * @return the Server instance
72       */
73      @Override
74      protected Server createServer() {
75  
76          // Need this CredentialsSourceFactory in our config to enable our test of basic auth
77          // with our httpInvoker-echoServiceSecure
78  
79          registerTestCredentialsSourceFactory();
80  
81          ConfigConstants configConstants = new ConfigConstants();
82  
83          Server server = new Server();
84  
85          SelectChannelConnector connector0 = new SelectChannelConnector();
86          connector0.setPort(configConstants.SERVER_HTTP_PORT);
87          connector0.setMaxIdleTime(30000);
88          connector0.setRequestHeaderSize(8192);
89  
90          SslSelectChannelConnector ssl_connector = new SslSelectChannelConnector();
91  
92          ssl_connector.setPort(configConstants.SERVER_HTTPS_PORT);
93          SslContextFactory cf = ssl_connector.getSslContextFactory();
94          cf.setKeyStore(configConstants.KEYSTORE_PATH);
95          cf.setKeyStorePassword(configConstants.KEYSTORE_PASS);
96          cf.setKeyManagerPassword(configConstants.KEYSTORE_PASS);
97  
98          server.setConnectors(new Connector[]{connector0, ssl_connector});
99  
100         URL webRoot = getClass().getClassLoader().getResource(configConstants.WEB_ROOT);
101         String location = webRoot.getPath();
102 
103         LOG.debug("#####################################");
104 		LOG.debug("#");
105 		LOG.debug("#  Starting Client1 using following web root " + location);
106 		LOG.debug("#");
107 		LOG.debug("#####################################");
108 
109         WebAppContext context = new WebAppContext();
110         context.setResourceBase(location);
111         context.setContextPath(configConstants.CONTEXT);
112 
113         HandlerCollection handlers = new HandlerCollection();
114         handlers.addHandler(context);
115         server.setHandler(handlers);
116 
117         server.setDumpAfterStart(true);
118         //server.setDumpBeforeStop(true);
119 
120         return server;
121     }
122 
123     /**
124      * Registers a CredentialsSourceFactory with our ConfigContext.  Needed to verify that services requiring basic auth
125      * work.
126      */
127     private void registerTestCredentialsSourceFactory() {
128         List<CredentialsSource> credentialsSources =
129                 Collections.<CredentialsSource>singletonList(
130                         new UsernamePasswordCredentialsSource("gilesp", "thuperthecret"));
131 
132         CredentialsSourceFactory credentialsSourceFactory = new CredentialsSourceFactory();
133         credentialsSourceFactory.setCredentialsSources(credentialsSources);
134 
135         try {
136             credentialsSourceFactory.afterPropertiesSet();
137         } catch (Exception e) {
138             throw new RiceRuntimeException(e);
139         }
140 
141         ConfigContext.getCurrentContextConfig().putObject(Config.CREDENTIALS_SOURCE_FACTORY, credentialsSourceFactory);
142     }
143 }