Initial groundwork for WDIWTLT servlet.

This commit is contained in:
Jonathan Bernard 2016-03-16 02:54:43 -05:00
parent 392c94f39c
commit fb098c6603
7 changed files with 130 additions and 1 deletions

24
rest-server/build.gradle Normal file
View File

@ -0,0 +1,24 @@
apply plugin: 'groovy'
apply plugin: 'war'
apply plugin: "jetty"
dependencies {
compile localGroovy()
compile 'ch.qos.logback:logback-classic:1.1.3'
compile 'ch.qos.logback:logback-core:1.1.3'
compile 'org.slf4j:slf4j-api:1.7.14'
compile 'com.impossibl.pgjdbc-ng:pgjdbc-ng:0.3'
compile 'com.lambdaworks:scrypt:1.4.0'
compile 'com.zaxxer:HikariCP-java6:2.3.2'
compile 'javax:javaee-api:7.0'
compile 'javax.ws.rs:javax.ws.rs-api:2.0.1'
compile project(":wdiwtlt-core")
runtime 'com.fasterxml.jackson.jaxrs:jackson-jaxrs-json-provider:2.3.2'
runtime 'org.glassfish.jersey.containers:jersey-container-servlet:2.16'
runtime 'org.glassfish.jersey.media:jersey-media-json-jackson:2.16'
providedCompile 'javax.servlet:javax.servlet-api:3.1.0'
testCompile 'junit:junit:4.12'
testRuntime 'com.h2database:h2:1.4.186' }

View File

@ -0,0 +1,12 @@
package com.jdbernard.nlsongs.rest;
import javax.ws.rs.NameBinding;
import java.lang.annotation.ElementType;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
@NameBinding
@Target({ ElementType.TYPE, ElementType.METHOD })
@Retention(value = RetentionPolicy.RUNTIME)
public @interface AllowCors {}

View File

@ -0,0 +1,30 @@
package com.jdbernard.nlsongs.rest;
import javax.annotation.Priority;
import javax.ws.rs.Priorities;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.container.ContainerResponseContext;
import javax.ws.rs.container.ContainerResponseFilter;
import javax.ws.rs.ext.Provider;
@Provider @AllowCors @Priority(Priorities.HEADER_DECORATOR)
public class CorsResponseFilter implements ContainerResponseFilter {
@Override
public void filter(ContainerRequestContext reqCtx,
ContainerResponseContext respCtx) {
MultivaluedMap<String, Object> headers = respCtx.getHeaders();
headers.add("Access-Control-Allow-Origin",
reqCtx.getHeaderString("Origin"));
headers.add("Access-Control-Allow-Methods",
"GET, POST, PUT, DELETE, OPTIONS");
headers.add("Access-Control-Allow-Headers",
reqCtx.getHeaderString("Access-Control-Request-Headers"));
}
}

View File

@ -0,0 +1,12 @@
package com.jdbernard.nlsongs.rest;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
@Path("v1/ping") @AllowCors
public class PingResource {
@GET
@Produces("text/plain")
public String ping() { return "pong"; } }

View File

@ -0,0 +1,8 @@
package com.jdbernard.nlsongs.servlet
import com.jdbernard.wdiwtlt.db.DbApi
public class WdiwtltContext {
public static DbApi dbapi
}

View File

@ -0,0 +1,43 @@
package com.jdbernard.nlsongs.servlet
import javax.servlet.ServletContext
import javax.servlet.ServletContextEvent
import javax.servlet.ServletContextListener
import com.jdbernard.wdiwtlt.db.DbApi
import com.zaxxer.hikari.HikariConfig
import com.zaxxer.hikari.HikariDataSource
public final class WdiwtltContextListener implements ServletContextListener {
public void contextInitialized(ServletContextEvent event) {
def context = event.servletContext
// Load the context configuration.
Properties props = new Properties()
WdiwtltContextListener.getResourceAsStream(
context.getInitParameter('context.config.file')).withStream { is ->
props.load(is) }
// Create the pooled data source
HikariConfig hcfg = new HikariConfig(
context.getInitParameter('datasource.config.file'))
HikariDataSource hds = new HikariDataSource(hcfg)
// Create the NLSonsDB instance.
DbApi dbapi = new DbApi(hds)
context.setAttribute('dbapi', dbapi)
WdiwtltContext.dbapi = dbapi }
public void contextDestroyed(ServletContextEvent event) {
def context = event.servletContext
// Shutdown the DB API instance (it will shut down the data source).
DbApi dbapi = context.getAttribute('dbapi')
if (dbapi) dbapi.shutdown()
context.removeAttribute('dbapi') }
}

View File

@ -1,4 +1,4 @@
include 'core', 'cli' include 'core', 'cli', 'rest-server'
project(":core").name ="wdiwtlt-core" project(":core").name ="wdiwtlt-core"
project(":cli").name = "wdiwtlt-cli" project(":cli").name = "wdiwtlt-cli"