-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
kwakeroni
committed
Nov 16, 2018
1 parent
60e02ac
commit aef2b08
Showing
28 changed files
with
976 additions
and
307 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
<?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-app-support</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>org.slf4j</groupId> | ||
<artifactId>slf4j-api</artifactId> | ||
</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>com.sun.jersey</groupId> | ||
<artifactId>jersey-server</artifactId> | ||
<version>1.19.1</version> | ||
<scope>compile</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>be.kwakeroni.parameters.core</groupId> | ||
<artifactId>parameters-test-support</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.junit.jupiter</groupId> | ||
<artifactId>junit-jupiter-api</artifactId> | ||
</dependency> | ||
</dependencies> | ||
</project> |
105 changes: 105 additions & 0 deletions
105
...eters-app-support/src/main/java/be/kwakeroni/parameters/app/support/SimpleRestServer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
package be.kwakeroni.parameters.app.support; | ||
|
||
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 org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import javax.ws.rs.GET; | ||
import javax.ws.rs.Path; | ||
import javax.ws.rs.core.Application; | ||
import java.io.IOException; | ||
import java.util.Collection; | ||
import java.util.Collections; | ||
import java.util.HashSet; | ||
import java.util.Set; | ||
|
||
|
||
public abstract class SimpleRestServer implements AutoCloseable { | ||
|
||
private static final Logger LOG = LoggerFactory.getLogger(SimpleRestServer.class); | ||
|
||
private HttpServer httpServer; | ||
|
||
protected abstract int getPort(); | ||
|
||
protected abstract String getContextPath(); | ||
|
||
protected abstract Collection<Object> getResources(); | ||
|
||
protected abstract String getHelloMessage(); | ||
|
||
protected synchronized void start() throws IOException { | ||
if (this.httpServer != null) return; | ||
|
||
|
||
String uri = String.format("http://127.0.0.1:%s/%s", | ||
getPort(), | ||
getContextPath()); | ||
|
||
LOG.info("Starting server at {}", uri); | ||
|
||
this.httpServer = HttpServerFactory.create(uri, | ||
ContainerFactory.createContainer(HttpHandler.class, getResourceConfig(), null)); | ||
|
||
|
||
this.httpServer.start(); | ||
|
||
LOG.info("Server ready at " + uri); | ||
} | ||
|
||
protected synchronized void stop() { | ||
if (this.httpServer != null) { | ||
this.httpServer.stop(0); | ||
this.httpServer = null; | ||
} | ||
} | ||
|
||
|
||
@Override | ||
public void close() { | ||
this.stop(); | ||
} | ||
|
||
|
||
protected ResourceConfig getResourceConfig() { | ||
Set<Object> resources = new HashSet<>(); | ||
resources.add(getRootResource()); | ||
resources.addAll(getResources()); | ||
|
||
Application application = new Application() { | ||
@Override | ||
public Set<Object> getSingletons() { | ||
return resources; | ||
} | ||
}; | ||
|
||
ResourceConfig config = new ApplicationAdapter(application); | ||
config.setPropertiesAndFeatures(Collections.singletonMap("com.sun.jersey.api.json.POJOMappingFeature", true)); | ||
return config; | ||
|
||
} | ||
|
||
protected Object getRootResource() { | ||
|
||
|
||
return new Root(); | ||
} | ||
|
||
@Path("/") | ||
public final class Root { | ||
private Root() { | ||
|
||
} | ||
|
||
@GET | ||
public String hello() { | ||
return getHelloMessage(); | ||
} | ||
} | ||
|
||
} |
11 changes: 11 additions & 0 deletions
11
...rameters-app-support/src/main/java/be/kwakeroni/parameters/app/support/StaticContent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package be.kwakeroni.parameters.app.support; | ||
|
||
import java.nio.file.Path; | ||
|
||
public interface StaticContent { | ||
|
||
public Path getContentDirectory(); | ||
|
||
public String getIndexPage(); | ||
|
||
} |
66 changes: 66 additions & 0 deletions
66
...s-app-support/src/main/java/be/kwakeroni/parameters/app/support/StaticContentFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package be.kwakeroni.parameters.app.support; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.UncheckedIOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.nio.file.StandardCopyOption; | ||
import java.util.Objects; | ||
import java.util.function.Supplier; | ||
import java.util.zip.ZipEntry; | ||
import java.util.zip.ZipInputStream; | ||
|
||
public class StaticContentFactory { | ||
|
||
private static final Logger LOG = LoggerFactory.getLogger(StaticContentFactory.class); | ||
|
||
private StaticContentFactory() { | ||
|
||
} | ||
|
||
public static StaticContent fromZip(Supplier<InputStream> zipFile, String indexFile, Path workDir) { | ||
LOG.info("Buffering web application into {}", workDir); | ||
|
||
try { | ||
Files.createDirectories(workDir); | ||
unzip(zipFile, workDir); | ||
} catch (IOException exc) { | ||
throw new UncheckedIOException(exc); | ||
} | ||
|
||
return new StaticContent() { | ||
@Override | ||
public Path getContentDirectory() { | ||
return workDir; | ||
} | ||
|
||
@Override | ||
public String getIndexPage() { | ||
return indexFile; | ||
} | ||
}; | ||
} | ||
|
||
|
||
private static void unzip(Supplier<InputStream> stream, java.nio.file.Path location) throws IOException { | ||
|
||
InputStream input = Objects.requireNonNull(stream.get(), "Could not obtain zip file"); | ||
|
||
try (ZipInputStream zipStream = new ZipInputStream(input)) { | ||
for (ZipEntry entry = zipStream.getNextEntry(); entry != null; entry = zipStream.getNextEntry()) { | ||
java.nio.file.Path dest = location.resolve(entry.getName()); | ||
if (entry.isDirectory()) { | ||
Files.createDirectories(dest); | ||
} else { | ||
Files.copy(zipStream, dest, StandardCopyOption.REPLACE_EXISTING); | ||
} | ||
} | ||
zipStream.closeEntry(); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.