Skip to content

Commit

Permalink
Added server for stand-alone image with client and mgmt rest service …
Browse files Browse the repository at this point in the history
…and empty web application
  • Loading branch information
kwakeroni committed Jun 3, 2018
1 parent c671240 commit 117f135
Show file tree
Hide file tree
Showing 13 changed files with 871 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,13 @@
import org.slf4j.LoggerFactory;
import org.slf4j.MDC;

import javax.ws.rs.*;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Response;
import java.util.Map;
import java.util.Objects;
Expand Down Expand Up @@ -37,7 +43,6 @@ public RestBackendAdapter(BusinessParametersBackend<?> backend, BackendWireForma
this.wireFormatterContext = Objects.requireNonNull(wireFormatterContext, "wireFormatterContext");
}

@Path("/")
@GET
@Produces({TEXT_PLAIN})
public String getInfo() {
Expand Down
169 changes: 169 additions & 0 deletions parameters-application/parameters-standalone-app/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>parameters-application</artifactId>
<groupId>be.kwakeroni.parameters.application</groupId>
<version>0.1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>parameters-standalone-app</artifactId>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>be.kwakeroni.parameters</groupId>
<artifactId>parameters-deps</artifactId>
<version>${project.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>be.kwakeroni.parameters.management</groupId>
<artifactId>parameters-management</artifactId>
<version>${project.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>be.kwakeroni.parameters.backend</groupId>
<artifactId>parameters-backend</artifactId>
<version>${project.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>be.kwakeroni.parameters.adapter</groupId>
<artifactId>parameters-adapter</artifactId>
<version>${project.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>be.kwakeroni.parameters.basic</groupId>
<artifactId>parameters-basic-backend</artifactId>
<version>${project.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>be.kwakeroni.parameters.client</groupId>
<artifactId>parameters-client</artifactId>
<version>${project.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>be.kwakeroni.parameters.core</groupId>
<artifactId>parameters-core</artifactId>
<version>${project.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

<dependencies>
<dependency>
<groupId>be.kwakeroni.parameters.management</groupId>
<artifactId>parameters-management-rest</artifactId>
</dependency>
<dependency>
<groupId>be.kwakeroni.parameters.adapter</groupId>
<artifactId>parameters-adapter-direct</artifactId>
</dependency>
<dependency>
<groupId>be.kwakeroni.parameters.adapter</groupId>
<artifactId>parameters-adapter-rest</artifactId>
</dependency>
<dependency>
<groupId>be.kwakeroni.parameters.backend</groupId>
<artifactId>parameters-backend-inmemory</artifactId>
</dependency>
<dependency>
<groupId>be.kwakeroni.parameters.basic</groupId>
<artifactId>parameters-basic-backend</artifactId>
</dependency>
<dependency>
<groupId>be.kwakeroni.parameters.basic</groupId>
<artifactId>parameters-basic-common</artifactId>
</dependency>
<dependency>
<groupId>be.kwakeroni.parameters.basic</groupId>
<artifactId>parameters-basic-inmemory</artifactId>
</dependency>
<dependency>
<groupId>be.kwakeroni.parameters.client</groupId>
<artifactId>parameters-client-api</artifactId>
</dependency>
<dependency>
<groupId>be.kwakeroni.parameters.basic</groupId>
<artifactId>parameters-basic-client</artifactId>
</dependency>
<dependency>
<groupId>be.kwakeroni.parameters.basic</groupId>
<artifactId>parameters-basic-wireformat-raw</artifactId>
</dependency>
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>jsr311-api</artifactId>
<version>1.1.1</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20160810</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-client</artifactId>
<version>1.19.1</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-json</artifactId>
<version>1.19.1</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-servlet</artifactId>
<version>1.19.1</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
</dependency>
<dependency>
<groupId>be.kwakeroni.parameters.core</groupId>
<artifactId>parameters-test-support</artifactId>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package be.kwakeroni.parameters.app;

interface Configuration {

public int getPort();

public String getContextPath();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package be.kwakeroni.parameters.app;

import be.kwakeroni.parameters.adapter.rest.RestBackendAdapter;
import be.kwakeroni.parameters.adapter.rest.factory.RestBackendAdapterFactory;
import be.kwakeroni.parameters.management.rest.RestParameterManagement;
import be.kwakeroni.parameters.management.rest.factory.RestParameterManagementFactory;
import com.sun.jersey.api.container.ContainerFactory;
import com.sun.jersey.api.container.httpserver.HttpServerFactory;
import com.sun.jersey.api.core.ApplicationAdapter;
import com.sun.jersey.api.core.ResourceConfig;
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpServer;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.core.Application;
import java.io.IOException;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

class Server implements AutoCloseable {

private final Configuration configuration;
private HttpServer httpServer;


Server(Configuration configuration) {
this.configuration = configuration;
}

synchronized void start() throws IOException {
if (this.httpServer != null) return;

String uri = String.format("http://127.0.0.1:%s/%s", this.configuration.getPort(), this.configuration.getContextPath());

this.httpServer = HttpServerFactory.create(uri,
ContainerFactory.createContainer(HttpHandler.class, getResourceConfig(), null));

this.httpServer.start();

}

synchronized void stop() {
if (this.httpServer != null) {
this.httpServer.stop(0);
this.httpServer = null;
}
}

@Override
public void close() {
this.stop();
}

private ResourceConfig getResourceConfig() {

Root root = new Root();
RestBackendAdapter restBackend = new RestBackendAdapterFactory().newInstance();
RestParameterManagement restManagement = new RestParameterManagementFactory().newInstance();

@Path("/web")
class WebManagement extends StaticContent {
private WebManagement() {
// TODO path
super(Paths.get("."), "index.html");
}
}
StaticContent webManagement = new WebManagement();

ResourceConfig config = new ApplicationAdapter(ofSingletons(root, restBackend, restManagement, webManagement));
config.setPropertiesAndFeatures(Collections.singletonMap("com.sun.jersey.api.json.POJOMappingFeature", true));
return config;
}

@Path("/")
public static final class Root {
@GET
public String hello() {
return "Business Parameters";
}
}

private static Application ofSingletons(Object... singletons) {
return new Application() {
@Override
public Set<Object> getSingletons() {
return new HashSet<>(Arrays.asList(singletons));
}
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package be.kwakeroni.parameters.app;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriInfo;
import java.net.URI;
import java.nio.file.Files;
import java.util.Optional;

public abstract class StaticContent {

private final java.nio.file.Path contentDirectory;
private final String indexPage;

StaticContent(java.nio.file.Path contentDirectory, String indexPage) {
this.contentDirectory = contentDirectory.toAbsolutePath().normalize();
this.indexPage = indexPage;
}


@GET
public Response getInfo(@Context UriInfo uriInfo) {
URI requestUri = uriInfo.getRequestUri();
URI contextUri = (requestUri.getPath().endsWith("/")) ? requestUri : requestUri.resolve(requestUri.getPath() + "/");

return Response.seeOther(contextUri.resolve(indexPage)).build();
}

@GET
@Path("{path:.*}")
public Response get(@PathParam("path") String path) {

java.nio.file.Path resourcePath = contentDirectory.resolve(path);

return ifExists(resourcePath)
.map(this::getContents)
.orElseGet(() -> Response.status(Response.Status.NOT_FOUND))
.build();
}

private Optional<java.nio.file.Path> ifExists(java.nio.file.Path resource) {
// Do not serve files outside of the content directory.
// For security reasons it is preferable to treat such requests like non-existing files (404)
// rather than returning a 'forbidden' response (403).

return Optional.of(resource)
.map(java.nio.file.Path::normalize)
.filter(path -> path.startsWith(contentDirectory))
.filter(Files::exists)
.filter(Files::isRegularFile)
.filter(Files::isReadable);
}

private Response.ResponseBuilder getContents(java.nio.file.Path resource) {
try {
return getContents0(resource);
} catch (Exception e) {
// TODO Log
e.printStackTrace();
return Response.serverError();
}
}

protected Response.ResponseBuilder getContents0(java.nio.file.Path resource) throws Exception {
return Response.ok()
.type(Files.probeContentType(resource))
.entity(Files.newInputStream(resource));
}
}
Loading

0 comments on commit 117f135

Please sign in to comment.