| package pta.de.core.controller; |
| |
| import org.apache.log4j.Logger; |
| import org.eclipse.jetty.http.HttpStatus; |
| import pta.de.core.exceptions.HttpStatusException; |
| |
| import javax.ws.rs.core.Response; |
| import java.io.IOException; |
| import java.util.HashMap; |
| import java.util.Map; |
| import java.util.Properties; |
| |
| public abstract class BaseWebService { |
| public interface Invokable <T> { |
| public T invoke() throws Exception; |
| } |
| private final Map<String, Long> currentTimeMeasures = new HashMap<>(); |
| private final Logger logger; |
| |
| public BaseWebService(Logger logger) { |
| this.logger = logger; |
| } |
| |
| protected Response invokeRunnable(Invokable runnable) |
| { |
| try { // NOSONAR |
| Object o = runnable.invoke(); |
| return Response.ok(o).build(); |
| } catch (HttpStatusException hse) { |
| logger.error(hse); |
| return Response.status(hse.getHttpStatus()).build(); |
| } |
| catch (Exception e) { |
| logger.error("Caught unexpected Exception:", e); |
| return Response.status(HttpStatus.INTERNAL_SERVER_ERROR_500).build(); |
| } |
| } |
| |
| |
| |
| protected static String getVersionString() { |
| try { |
| // determine static VersionInfo |
| final Properties properties = new Properties(); |
| |
| properties.load(BaseWebService.class.getClassLoader().getResourceAsStream("project.properties")); |
| |
| String beversion = properties.getProperty("backend.version"); |
| if( beversion.contains("$")) { |
| beversion = "LOCAL-DEV"; |
| } |
| return beversion; |
| } catch (IOException e) { |
| throw new RuntimeException("Exception during start up"); |
| } |
| } |
| |
| |
| |
| } |