| <%# |
| * Licensed to the Apache Software Foundation (ASF) under one |
| * or more contributor license agreements. See the NOTICE file |
| * distributed with this work for additional information |
| * regarding copyright ownership. The ASF licenses this file |
| * to you under the Apache License, Version 2.0 (the |
| * "License"); you may not use this file except in compliance |
| * with the License. You may obtain a copy of the License at |
| * |
| * http://www.apache.org/licenses/LICENSE-2.0 |
| * |
| * Unless required by applicable law or agreed to in writing, software |
| * distributed under the License is distributed on an "AS IS" BASIS, |
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| * See the License for the specific language governing permissions and |
| * limitations under the License. |
| * |
| * |
| -%> |
| package <%= polygene.packageName %>.bootstrap.connectivity; |
| |
| import java.util.HashMap; |
| <% if( hasFeature('security') ) { %> |
| import java.io.File; |
| import org.apache.polygene.library.http.SecureJettyConfiguration; |
| import org.apache.polygene.library.http.SecureJettyServiceAssembler; |
| import org.apache.polygene.library.shiro.web.assembly.HttpShiroAssembler; |
| import org.apache.polygene.library.shiro.ini.ShiroIniConfiguration; |
| <% } else { %> |
| import org.apache.polygene.library.http.JettyConfiguration; |
| import org.apache.polygene.library.http.JettyServiceAssembler; |
| <% } %> |
| import org.apache.polygene.api.common.Visibility; |
| import org.apache.polygene.bootstrap.AssemblyException; |
| import org.apache.polygene.bootstrap.LayerAssembly; |
| import org.apache.polygene.bootstrap.ModuleAssembly; |
| import org.apache.polygene.bootstrap.layered.ModuleAssembler; |
| import org.apache.polygene.library.restlet.PolygeneServerServlet; |
| |
| import <%= polygene.packageName %>.rest.<%= polygene.name %>RestApplication; |
| |
| import static org.apache.polygene.library.http.Servlets.addServlets; |
| import static org.apache.polygene.library.http.Servlets.serve; |
| |
| public class HttpServerModule |
| implements ModuleAssembler |
| {<% if( hasFeature('security') ) { %> |
| private static final int DEFAULT_PORT = 8443; |
| public static final String KS_PASSWORD = "1234"; // TODO: Secure the key stores |
| public static final String SERVER_KEYSTORE_FILENAME = "security/<%= polygene.name.toLowerCase() %>-app.key"; |
| public static final String SERVER_KEYSTORE_TYPE = "JCEKS"; |
| public static final String TRUSTSTORE_FILENAME = "security/<%= polygene.name.toLowerCase() %>-app.key"; |
| public static final String TRUSTSTORE_TYPE = "JCEKS"; |
| <% } else { |
| %> protected static final int DEFAULT_PORT = 8080; |
| <% } %> |
| public static String NAME = "HTTP Server Module"; |
| |
| @Override |
| public ModuleAssembly assemble( LayerAssembly layer, ModuleAssembly module ) |
| throws AssemblyException |
| { |
| <% if( hasFeature('security') ) { |
| %> new HttpShiroAssembler() |
| .withConfig( module, Visibility.module ); |
| module.forMixin( ShiroIniConfiguration.class ).declareDefaults().iniResourcePath().set("web-shiro.ini"); |
| new SecureJettyServiceAssembler() |
| .withConfig( module, Visibility.layer ) |
| .visibleIn( Visibility.layer ) |
| .assemble( module ); |
| SecureJettyConfiguration defaults = module.forMixin( SecureJettyConfiguration.class ).declareDefaults(); |
| defaults.keystorePath().set( new File( SERVER_KEYSTORE_FILENAME ).getAbsolutePath() ); |
| defaults.keystoreType().set( "JCEKS" ); |
| defaults.keystorePassword().set( KS_PASSWORD ); |
| defaults.truststorePath().set( new File( TRUSTSTORE_FILENAME ).getAbsolutePath() ); |
| defaults.truststoreType().set( TRUSTSTORE_TYPE ); |
| defaults.truststorePassword().set( KS_PASSWORD ); |
| defaults.wantClientAuth().set( Boolean.FALSE ); |
| <% } else { |
| %> new JettyServiceAssembler() |
| .withConfig( module, Visibility.layer ) |
| .visibleIn( Visibility.layer ) |
| .assemble( module ); |
| JettyConfiguration defaults = module.forMixin( JettyConfiguration.class ).declareDefaults(); |
| <% } %> |
| defaults.hostName().set( System.getProperty( "<%= polygene.name.toLowerCase() %>.http.servername", "127.0.0.1" )); |
| defaults.port().set( DEFAULT_PORT ); |
| HashMap<String, String> initParams = new HashMap<>(); |
| initParams.put("org.restlet.application", <%= polygene.name %>RestApplication.class.getName() ); |
| addServlets( serve( "/*" ).with(PolygeneServerServlet.class ).withInitParams( initParams ) ).to( module ); |
| return module; |
| } |
| } |