Last active
October 17, 2018 17:08
-
-
Save erikkaplun/90f348bb06ec156b2553fac7d0cac978 to your computer and use it in GitHub Desktop.
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
package httpserverhelloworld; | |
import java.io.*; | |
import java.net.InetSocketAddress; | |
import com.google.gson.Gson; | |
import com.sun.net.httpserver.HttpExchange; | |
import com.sun.net.httpserver.HttpHandler; | |
import com.sun.net.httpserver.HttpServer; | |
public class HttpServerHelloWorld { | |
public static void main(final String[] args) throws Exception { | |
final HttpServer server = HttpServer.create(new InetSocketAddress(8000), 0); | |
server.createContext("/test", new MyHandler().setSomeValue(123)); | |
server.createContext("/hello", new MyHandler().setSomeValue(321)); | |
server.createContext("/add", new AddHandler()); | |
CountHandler countHandler = new CountHandler(); | |
server.createContext("/count", countHandler); | |
server.createContext("/count-copy", countHandler); | |
server.createContext("/count-by-2", new CountHandler().setStepSize(2)); | |
server.setExecutor(null); // creates a default executor | |
server.start(); | |
} | |
private static class CountHandler implements HttpHandler { | |
private int stepSize = 1; | |
public CountHandler setStepSize(int x) { stepSize = x; return this; } | |
private int count = -1; | |
public void handle(HttpExchange t) throws IOException { | |
count += stepSize; | |
String response = "" + count + "\n"; | |
t.sendResponseHeaders(200, response.length()); | |
final OutputStream os = t.getResponseBody(); | |
os.write(response.getBytes()); | |
os.close(); | |
} | |
} | |
static class MyHandler implements HttpHandler { | |
private int someValue = 1; | |
public MyHandler setSomeValue(int x) { this.someValue = x; return this; } | |
public int getSomeValue() { return someValue; } | |
@Override | |
public void handle(final HttpExchange t) throws IOException { | |
String response; | |
if (t.getRequestMethod().equals("DELETE")) { | |
if (this.someValue < 200) { | |
response = "it's gone now..." + this.someValue; | |
} else { | |
response = "too valuable: " + this.someValue + "\n"; | |
} | |
} else { | |
response = "this is the response: " + t.getRequestMethod() + "\n"; | |
} | |
// 200 == HTTP OK | |
t.sendResponseHeaders(200, response.length()); | |
final OutputStream os = t.getResponseBody(); | |
os.write(response.getBytes()); | |
os.close(); | |
} | |
} | |
static class AddHandler implements HttpHandler { | |
@Override | |
public void handle(HttpExchange t) throws IOException { | |
System.out.println("handling"); | |
InputStream requestBody = t.getRequestBody(); | |
BufferedReader br = new BufferedReader(new InputStreamReader(requestBody)); | |
String data = "{\"a\": 5, \"b\": 7}"; | |
Gson gson = new Gson(); | |
AddingRoot adding = gson.fromJson(data, AddingRoot.class); | |
ResponseRoot responseRoot = new ResponseRoot(); | |
responseRoot.setC(adding.getA() + adding.getB()); | |
String responseJson = gson.toJson(responseRoot); | |
System.out.println(responseJson); | |
t.sendResponseHeaders(200, responseJson.length()); | |
final OutputStream os = t.getResponseBody(); | |
os.write(responseJson.getBytes()); | |
os.close(); | |
} | |
} | |
static class AddingRoot { | |
private int a, b; | |
public int getA() { return a; } | |
public int getB() { return b; } | |
} | |
static class ResponseRoot { | |
private int c; | |
public int getC() { return c; } | |
public void setC(int c) { this.c = c; } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment