Skip to content

Commit

Permalink
- add mini server
Browse files Browse the repository at this point in the history
- move everything around
  • Loading branch information
IO42630 committed Nov 12, 2021
1 parent 837d25c commit 52f5841
Show file tree
Hide file tree
Showing 120 changed files with 382 additions and 8 deletions.
1 change: 1 addition & 0 deletions GR_meta/README.md
@@ -0,0 +1 @@
If having trouble running, open/import by root pom.
5 changes: 5 additions & 0 deletions GR_meta/meta-http-server/LICENSE.md
@@ -0,0 +1,5 @@
Copyright (C) 2020 by IO42630 <ivan@olexyn.com>

Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
2 changes: 2 additions & 0 deletions GR_meta/meta-http-server/README.md
@@ -0,0 +1,2 @@
### Minimal HTTP Server
Embed by copying code in `MainExample`.
10 changes: 10 additions & 0 deletions GR_meta/meta-http-server/install-locally.sh
@@ -0,0 +1,10 @@
#!/bin/bash
version="0.1"
file="target/min-http-server-${version}.jar"
groupId="com.olexyn.min.http.server"
artifactId="min-http-server"



mvn package
mvn install:install-file -Dfile=${file} -DgroupId=${groupId} -DartifactId=${artifactId} -Dversion=${version} -Dpackaging=jar -DgeneratePom=true
129 changes: 129 additions & 0 deletions GR_meta/meta-http-server/pom.xml
@@ -0,0 +1,129 @@
<?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">
<modelVersion>4.0.0</modelVersion>
<groupId>ch.opendata.hackdaysbe</groupId>
<artifactId>meta-http-server</artifactId>
<version>0.1</version>
<name>min-http-server</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-server</artifactId>
<version>9.4.28.v20200408</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-webapp</artifactId>
<version>9.4.28.v20200408</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-servlets</artifactId>
<version>9.4.28.v20200408</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-servlet</artifactId>
<version>9.4.28.v20200408</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-util</artifactId>
<version>9.4.28.v20200408</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-http</artifactId>
<version>9.4.28.v20200408</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-io</artifactId>
<version>9.4.28.v20200408</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-client</artifactId>
<version>9.4.28.v20200408</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty.websocket</groupId>
<artifactId>websocket-server</artifactId>
<version>9.4.28.v20200408</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty.websocket</groupId>
<artifactId>websocket-client</artifactId>
<version>9.4.28.v20200408</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty.websocket</groupId>
<artifactId>websocket-servlet</artifactId>
<version>9.4.28.v20200408</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty.websocket</groupId>
<artifactId>websocket-api</artifactId>
<version>9.4.28.v20200408</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty.aggregate</groupId>
<artifactId>jetty-all</artifactId>
<version>9.4.28.v20200408</version>
<type>pom</type>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
<!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
<plugin>
<artifactId>maven-site-plugin</artifactId>
<version>3.7.1</version>
</plugin>
<plugin>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>3.0.0</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
@@ -0,0 +1,22 @@
package com.olexyn.min.http.server;

import com.olexyn.min.http.server.servlets.AsyncServlet;
import com.olexyn.min.http.server.servlets.BlockingServlet;


public class MainExample {
public static void main(String... args) {

MinJettyServer server = new MinJettyServer();

server.PORT = 8090;
server.MAX_THREADS = 100;
server.MIN_THREADS = 10;
server.IDLE_TIMEOUT = 120;

server.addServletWithMapping("/status", BlockingServlet.class);
server.addServletWithMapping("/heavy/async", AsyncServlet.class);

server.start();
}
}
@@ -0,0 +1,59 @@
package com.olexyn.min.http.server;

import org.eclipse.jetty.security.SecurityHandler;
import org.eclipse.jetty.server.Connector;
import org.eclipse.jetty.server.HandlerContainer;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.ServerConnector;
import org.eclipse.jetty.server.handler.HandlerWrapper;
import org.eclipse.jetty.server.session.SessionHandler;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.util.thread.QueuedThreadPool;

import javax.servlet.Servlet;
import java.util.HashMap;
import java.util.Map;

public class MinJettyServer {

public int PORT = 8080;
public int MAX_THREADS = 128;
public int MIN_THREADS = 16;
public int IDLE_TIMEOUT = 120;

final private Map<String, Class<? extends Servlet>> servletURIMapping = new HashMap<>();


public void start() {

QueuedThreadPool threadPool = new QueuedThreadPool(MAX_THREADS, MIN_THREADS, IDLE_TIMEOUT);
Server server = new Server(threadPool);
ServerConnector connector = new ServerConnector(server);


connector.setPort(PORT);
server.setConnectors(new Connector[]{connector});




ServletContextHandler servletHandler = new ServletContextHandler();

for (Map.Entry<String, Class<? extends Servlet>> entry : servletURIMapping.entrySet()) {
servletHandler.getServletHandler().addServletWithMapping(entry.getValue(), entry.getKey());
}

server.setHandler(servletHandler);
try {
server.start();
} catch (Exception e) {
e.printStackTrace();
}

}


public void addServletWithMapping(String path, Class<? extends Servlet> servlet) {
servletURIMapping.put(path, servlet);
}
}
@@ -0,0 +1,42 @@
package com.olexyn.min.http.server.servlets;

import javax.servlet.AsyncContext;
import javax.servlet.ServletOutputStream;
import javax.servlet.WriteListener;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;

public class AsyncServlet extends HttpServlet {
private final static String HEAVY_RESOURCE = "This is some heavy resource that will be served in an async way";

protected void doGet(HttpServletRequest request, final HttpServletResponse response) throws IOException {

final ByteBuffer content = ByteBuffer.wrap(HEAVY_RESOURCE.getBytes(StandardCharsets.UTF_8));

final AsyncContext async = request.startAsync();
final ServletOutputStream out = response.getOutputStream();
out.setWriteListener(new WriteListener() {
@Override
public void onWritePossible() throws IOException {
while (out.isReady()) {
if (!content.hasRemaining()) {
response.setStatus(200);
async.complete();
return;
}
out.write(content.get());
}
}

@Override
public void onError(Throwable t) {
getServletContext().log("Async Error", t);
async.complete();
}
});
}
}
@@ -0,0 +1,16 @@
package com.olexyn.min.http.server.servlets;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class BlockingServlet extends HttpServlet {

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {

response.setContentType("application/json");
response.setStatus(HttpServletResponse.SC_OK);
response.getWriter().println("{ \"status\": \"ok\"}");
}
}
File renamed without changes.
4 changes: 2 additions & 2 deletions GR_meta_xml/pom.xml → GR_meta/meta-xml/pom.xml
Expand Up @@ -4,8 +4,8 @@
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>hack</groupId>
<artifactId>data</artifactId>
<groupId>ch.opendata.hackdaysbe</groupId>
<artifactId>meta-xml</artifactId>
<version>1.0-SNAPSHOT</version>

<name>data</name>
Expand Down
@@ -1,4 +1,4 @@
package hack.data;
package ch.opendata.hackdaysbe;

import ch.cmiag.cdws.dokumente.Dokument;
import ch.cmiag.cdws.geschaefte.Geschaeft;
Expand Down
@@ -1,4 +1,4 @@
package hack.data;
package ch.opendata.hackdaysbe;

import java.io.File;
import java.net.MalformedURLException;
Expand All @@ -7,7 +7,7 @@
public class Utils {

public static URL getUrl(String type, String suffix) throws MalformedURLException {
String path = System.getProperty("user.dir") + "\\src\\main\\resources\\GR\\";
String path = System.getProperty("user.dir") + "\\meta-xml\\src\\main\\resources\\GR\\";
File file = new File(path+ type + suffix);
return file.toURI().toURL();
}
Expand Down
@@ -1,4 +1,4 @@
package hack.data.pdf.read.stub;
package ch.opendata.hackdaysbe.pdf.read.stub;

import org.apache.tika.metadata.Metadata;
import org.apache.tika.parser.ParseContext;
Expand Down
@@ -1,4 +1,4 @@
package hack.data.pdf.read.stub;
package ch.opendata.hackdaysbe.pdf.read.stub;

import java.io.ByteArrayOutputStream;
import java.io.File;
Expand Down
File renamed without changes.
@@ -1,4 +1,4 @@
package hack.data;
package ch.opendata.hackdaysbe;

import static org.junit.Assert.assertTrue;

Expand Down

0 comments on commit 52f5841

Please sign in to comment.