Skip to content

Instantly share code, notes, and snippets.

@hassanrehman
Created July 16, 2019 08:24
Show Gist options
  • Save hassanrehman/63c8eb34092341beba1f344fdac3f6c4 to your computer and use it in GitHub Desktop.
Save hassanrehman/63c8eb34092341beba1f344fdac3f6c4 to your computer and use it in GitHub Desktop.
To create a local endpoint which receives parameters like that of command line, but runs the JVM only once
package com.company;
import technology.tabula.*;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.DefaultParser;
import org.apache.commons.cli.ParseException;
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpServer;
import java.io.*;
import java.net.InetSocketAddress;
import java.net.URLDecoder;
import java.util.*;
import java.net.URI;
public class Main {
//TODO: make this via cmd line args
public static String IDENTITY = "fJxjt3ceM6LUhVInkTww8mmq6uOLgIse";
// https://github.com/tabulapdf/tabula-java/blob/master/src/test/java/technology/tabula/TestCommandLineApp.java
private static String csvFromCommandLineArgs(String[] args) throws ParseException {
System.out.println("params: " + Arrays.toString(args));
CommandLineParser parser = new DefaultParser();
CommandLine cmd = parser.parse(CommandLineApp.buildOptions(), args);
StringBuilder stringBuilder = new StringBuilder();
new CommandLineApp(stringBuilder, cmd).extractTables(cmd);
return stringBuilder.toString();
}
public static Map<String, LinkedList<String>> splitQuery(URI url) throws UnsupportedEncodingException {
Map<String, LinkedList<String>> queryPairs = new LinkedHashMap<String, LinkedList<String>>();
String query = url.getQuery();
String[] pairs = query.split("&");
for (String pair : pairs) {
int idx = pair.indexOf("=");
String key = URLDecoder.decode(pair.substring(0, idx), "UTF-8");
String value = URLDecoder.decode(pair.substring(idx + 1), "UTF-8");
if( !queryPairs.containsKey(key) ) {
queryPairs.put(key, new LinkedList<String>());
}
queryPairs.get(key).add( value );
}
return queryPairs;
}
static class CommandHandler implements HttpHandler {
@Override
public void handle(HttpExchange t) throws IOException {
Map<String, LinkedList<String>> queryParts = splitQuery(t.getRequestURI());
LinkedList<String> cmdParts = new LinkedList<String>();
for (String key : queryParts.keySet()) {
for( String value: queryParts.get(key) ) {
if( !key.equals("blank") ) cmdParts.add(key);
cmdParts.add(value);
}
}
String[] cmd = new String[cmdParts.size()];
cmdParts.toArray(cmd);
String response = null;
int responseCode = 200;
try{
response = csvFromCommandLineArgs( cmd );
}catch(ParseException pe){
response = "ParseError: " + pe.getMessage();
responseCode = 400;
}
System.out.println("response: " + response);
t.sendResponseHeaders(responseCode, response.length());
OutputStream os = t.getResponseBody();
os.write(response.getBytes());
os.close();
}
}
static class IdentityHandler implements HttpHandler {
@Override
public void handle(HttpExchange t) throws IOException {
String response = IDENTITY;
t.sendResponseHeaders(200, response.length());
OutputStream os = t.getResponseBody();
os.write(response.getBytes());
os.close();
}
}
public static void main(String[] args) {
try{
int port = 8000;
HttpServer server = HttpServer.create(new InetSocketAddress(port), 0);
server.createContext("/execute", new CommandHandler());
server.createContext("/identity", new IdentityHandler());
server.setExecutor(null); // creates a default executor
System.out.println("Listening on http://127.0.0.1:"+port);
server.start();
}catch(IOException pe){
System.out.println(pe);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment