
de.citec.scie.web.ServerConfig Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of webservice Show documentation
Show all versions of webservice Show documentation
Module providing the webservice interface based on the Jetty
embedded webserver and the FreeMarker template engine. Defines a simple
format for providing textual annotations and produced output in HTML or
JSON. This module has no dependencies to the other SCIE modules (except
for the PDF text extractor) or the UIMA framework and thus can be used
in any context, where text is annotated by an algorithm and should be
presented to an end user.
The newest version!
/*
* SCIE -- Spinal Cord Injury Information Extraction
* Copyright (C) 2013, 2014
* Raphael Dickfelder, Jan Göpfert, Benjamin Paaßen, Andreas Stöckel
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see .
*/
package de.citec.scie.web;
import freemarker.template.Configuration;
import freemarker.template.DefaultObjectWrapper;
import freemarker.template.Template;
import freemarker.template.TemplateExceptionHandler;
import freemarker.template.Version;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;
import javax.servlet.MultipartConfigElement;
/**
* Contains the server configuration variables such as error messages and the
* location of the templates etc.
*
* @author Andreas Stöckel -- [email protected]
*/
class ServerConfig {
/**
* Confirmation preselection for development.
*/
public static final boolean CONFIRMATION_PRESELECTION = false;
/*
* Error messages and strings
*/
public static final String CHARSET = "UTF-8";
public static final String ERR_CONFIRM = "You must confirm this field";
public static final String ERR_NO_DOCUMENT = "You must select a document";
public static final String ERR_FORMAT = "You must select a valid format (html_web, html, json)";
public static final String ERR_UNSUPPORTED = "Unsupported file type/incorrect UTF-8 encoding";
/**
* Documentation file
*/
public static final Map DOCU_FILES = new HashMap<>();
static {
DOCU_FILES.put("/docu", "./README.html");
DOCU_FILES.put("/docu/", "./README.html");
DOCU_FILES.put("/docu/pdf_extractor.html", "./pdf_extractor.html");
DOCU_FILES.put("/docu/classifiers.html", "./classifiers.html");
DOCU_FILES.put("/docu/scie_1_0.html", "./scie_1_0.html");
}
/*
* Template generation
*/
private static final Configuration templateConfig = new Configuration();
static {
templateConfig.setClassForTemplateLoading(ServerHandler.class, "");
templateConfig.setObjectWrapper(new DefaultObjectWrapper());
templateConfig.setDefaultEncoding(CHARSET);
templateConfig.setTemplateExceptionHandler(TemplateExceptionHandler.HTML_DEBUG_HANDLER);
templateConfig.setIncompatibleImprovements(new Version(2, 3, 20));
}
public static final Template getTemplate(String path) throws IOException {
return templateConfig.getTemplate(path);
}
/*
* Mime type map
*/
private static final Map MIME_MAP = new HashMap<>();
static {
MIME_MAP.put("htm", "text/html");
MIME_MAP.put("html", "text/html");
MIME_MAP.put("css", "text/css");
MIME_MAP.put("js", "text/javascript");
MIME_MAP.put("json", "text/json");
MIME_MAP.put("txt", "text/plain");
MIME_MAP.put("png", "image/png");
MIME_MAP.put("jpg", "image/jpeg");
MIME_MAP.put("jpeg", "image/jpeg");
MIME_MAP.put("woff", "application/font-woff");
}
public static String getContentType(String filename) {
final String ext = filename.substring(filename.lastIndexOf('.') + 1);
String mime = MIME_MAP.getOrDefault(ext, "text/html");
if (mime.startsWith("text/")) {
return mime + ";charset=" + CHARSET;
}
return mime;
}
/*
* Output formats
*/
private static final Set VALID_FORMATS = new HashSet<>(
Arrays.asList(new String[]{"html_web", "html", "html_plain", "json"}));
public static boolean isValidFormat(String format) {
return VALID_FORMATS.contains(format.toLowerCase());
}
/*
* Multipart handler
*/
public static final long MAX_FILE_SIZE = 1024 * 1024 * 16; // 16 MB
public static final long MAX_REQUEST_SIZE = 1024 * 1024 * 16; // 16 MB
public static final int FILE_SIZE_THRESHOLD = 1024 * 512; // 512 K
public static final MultipartConfigElement MULTI_PART_CONFIG
= new MultipartConfigElement(
System.getProperty("java.io.tmpdir"),
MAX_FILE_SIZE,
MAX_REQUEST_SIZE,
FILE_SIZE_THRESHOLD);
/*
* Static directories
*/
private static final List STATIC_DIRS = Arrays.asList(
new String[]{"style", "script", "images", "assets"});
public static boolean isStatic(String target) {
return STATIC_DIRS.stream().anyMatch((dir) -> (target.startsWith("/" + dir + "/")));
}
public static boolean isDocu(String target) {
return getDocuFile(target) != null;
}
public static File getDocuFile(String target) {
if (DOCU_FILES.containsKey(target)) {
File f = new File(DOCU_FILES.get(target));
return f.exists() ? f : null;
}
return null;
}
/*
* API requests
*/
public static boolean isAPI(String target, String method) {
return target.equals("/api") || target.equals("/api/")
|| ((target.equals("/upload.html") || target.equals("/upload.htm"))
&& method.toUpperCase().equals("POST"));
}
/*
* Template requests
*/
public static boolean isTemplate(String target) {
return target.equals("/") || target.endsWith(".htm")
|| target.endsWith(".html");
}
/*
* Load management
*/
private static final int MAX_REQUESTS = 3;
private static final int WAIT_TIMEOUT = 5 * 1000;
private static final Map REQUESTS = new HashMap<>();
public static boolean registerRequestAndWait(String ip) {
// Fetch the semaphore from the REQUESTS list
Semaphore sem;
synchronized (REQUESTS) {
sem = REQUESTS.get(ip);
if (sem == null) {
sem = new Semaphore(MAX_REQUESTS);
REQUESTS.put(ip, sem);
}
}
// Acquire the semaphore, wait at least WAIT_TIMEOUT milliseconds
try {
return sem.tryAcquire(WAIT_TIMEOUT, TimeUnit.MILLISECONDS);
} catch (InterruptedException ex) {
return false;
}
}
public static void unregisterRequest(String ip) {
synchronized (REQUESTS) {
Semaphore sem = REQUESTS.get(ip);
if (sem != null) {
sem.release(); // Release the request
// Remove the IP from the request list if all requests have been
// worked on
if (sem.availablePermits() == MAX_REQUESTS) {
REQUESTS.remove(ip);
}
}
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy