}
@Override
public void initialize(final StructrConf additionalConfig) {
final StructrConf finalConfig = new StructrConf();
// Default configuration
finalConfig.setProperty(APPLICATION_TITLE, "structr server");
finalConfig.setProperty(APPLICATION_HOST, "0.0.0.0");
finalConfig.setProperty(APPLICATION_HTTP_PORT, "8082");
finalConfig.setProperty(APPLICATION_HTTPS_ENABLED, "false");
finalConfig.setProperty(APPLICATION_HTTPS_PORT, "8083");
finalConfig.setProperty(ASYNC, "false");
finalConfig.setProperty(SERVLETS, "JsonRestServlet");
finalConfig.setProperty("JsonRestServlet.class", JsonRestServlet.class.getName());
finalConfig.setProperty("JsonRestServlet.path", "/structr/rest/*");
finalConfig.setProperty("JsonRestServlet.resourceprovider", DefaultResourceProvider.class.getName());
finalConfig.setProperty("JsonRestServlet.authenticator", SuperUserAuthenticator.class.getName());
finalConfig.setProperty("JsonRestServlet.user.class", "org.structr.dynamic.User");
finalConfig.setProperty("JsonRestServlet.user.autocreate", "false");
finalConfig.setProperty("JsonRestServlet.defaultview", PropertyView.Public);
finalConfig.setProperty("JsonRestServlet.outputdepth", "3");
Services.mergeConfiguration(finalConfig, additionalConfig);
String mainClassName = (String) finalConfig.get(MAIN_CLASS);
Class mainClass = null;
if (mainClassName != null) {
logger.log(Level.INFO, "Running main class {0}", new Object[]{mainClassName});
try {
mainClass = Class.forName(mainClassName);
} catch (ClassNotFoundException ex) {
logger.log(Level.WARNING, "Did not find class for main class from config " + mainClassName, ex);
}
}
String sourceJarName = (mainClass != null ? mainClass : getClass()).getProtectionDomain().getCodeSource().getLocation().toString();
final boolean isTest = Boolean.parseBoolean(finalConfig.getProperty(Services.TESTING, "false"));
if (!isTest && StringUtils.stripEnd(sourceJarName, System.getProperty("file.separator")).endsWith("classes")) {
String jarFile = System.getProperty("jarFile");
if (StringUtils.isEmpty(jarFile)) {
throw new IllegalArgumentException(getClass().getName() + " was started in an environment where the classloader cannot determine the JAR file containing the main class.\n"
+ "Please specify the path to the JAR file in the parameter -DjarFile.\n"
+ "Example: -DjarFile=${project.build.directory}/${project.artifactId}-${project.version}.jar");
}
sourceJarName = jarFile;
}
// load configuration from properties file
applicationName = finalConfig.getProperty(APPLICATION_TITLE);
host = finalConfig.getProperty(APPLICATION_HOST);
basePath = finalConfig.getProperty(Services.BASE_PATH);
httpPort = parseInt(finalConfig.getProperty(APPLICATION_HTTP_PORT), 8082);
maxIdleTime = parseInt(System.getProperty("maxIdleTime"), 30000);
requestHeaderSize = parseInt(System.getProperty("requestHeaderSize"), 8192);
async = parseBoolean(finalConfig.getProperty(ASYNC), false);
if (async) {
logger.log(Level.INFO, "Running in asynchronous mode");
}
// other properties
String keyStorePath = finalConfig.getProperty(APPLICATION_KEYSTORE_PATH);
String keyStorePassword = finalConfig.getProperty(APPLICATION_KEYSTORE_PASSWORD);
String contextPath = System.getProperty("contextPath", "/");
String logPrefix = "structr";
boolean enableRewriteFilter = true; // configurationFile.getProperty(Services.
boolean enableHttps = parseBoolean(finalConfig.getProperty(APPLICATION_HTTPS_ENABLED), false);
boolean enableGzipCompression = true; //
boolean logRequests = false; //
int httpsPort = parseInt(finalConfig.getProperty(APPLICATION_HTTPS_PORT), 8083);
// get current base path
basePath = System.getProperty("home", basePath);
if (basePath.isEmpty()) {
// use cwd and, if that fails, /tmp as a fallback
basePath = System.getProperty("user.dir", "/tmp");
}
// create base directory if it does not exist
File baseDir = new File(basePath);
if (!baseDir.exists()) {
baseDir.mkdirs();
}
server = new Server(httpPort);
ContextHandlerCollection contexts = new ContextHandlerCollection();
contexts.addHandler(new DefaultHandler());
final ServletContextHandler servletContext = new ServletContextHandler(server, contextPath, true, true);
final List<Connector> connectors = new LinkedList<>();
// create resource collection from base path & source JAR
try {
servletContext.setBaseResource(new ResourceCollection(Resource.newResource(basePath), JarResource.newJarResource(Resource.newResource(sourceJarName))));
} catch (Throwable t) {
logger.log(Level.WARNING, "Base resource {0} not usable: {1}", new Object[]{basePath, t.getMessage()});
}
// this is needed for the filters to work on the root context "/"
servletContext.addServlet("org.eclipse.jetty.servlet.DefaultServlet", "/");
servletContext.setInitParameter("org.eclipse.jetty.servlet.Default.dirAllowed", "false");
if (enableRewriteFilter) {
FilterHolder rewriteFilter = new FilterHolder(UrlRewriteFilter.class);
rewriteFilter.setInitParameter("confPath", "urlrewrite.xml");
servletContext.addFilter(rewriteFilter, "/*", EnumSet.of(DispatcherType.REQUEST, async ? DispatcherType.ASYNC : DispatcherType.FORWARD));
}
if (enableGzipCompression) {
FilterHolder gzipFilter = async ? new FilterHolder(AsyncGzipFilter.class) : new FilterHolder(GzipFilter.class);
gzipFilter.setInitParameter("mimeTypes", "text/html,text/plain,text/css,text/javascript,application/json");
gzipFilter.setInitParameter("bufferSize", "32768");
gzipFilter.setInitParameter("minGzipSize", "256");
gzipFilter.setInitParameter("deflateCompressionLevel", "9");
gzipFilter.setInitParameter("methods", "GET,POST");
servletContext.addFilter(gzipFilter, "/*", EnumSet.of(DispatcherType.REQUEST, async ? DispatcherType.ASYNC : DispatcherType.FORWARD));
}
contexts.addHandler(servletContext);
// enable request logging
if (logRequests || "true".equals(finalConfig.getProperty("log.requests", "false"))) {
String etcPath = basePath + "/etc";
File etcDir = new File(etcPath);
if (!etcDir.exists()) {