Skip to content

Instantly share code, notes, and snippets.

@lizettepreiss
Created October 30, 2018 14:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lizettepreiss/d00bb91ac31a0f4272dfd8d819c2bc8c to your computer and use it in GitHub Desktop.
Save lizettepreiss/d00bb91ac31a0f4272dfd8d819c2bc8c to your computer and use it in GitHub Desktop.
HTTP Server
package preiss;
import com.sun.net.httpserver.Headers;
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpServer;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.util.concurrent.Executors;
/**
* @author preissl
*/
public class HTTPServer implements HttpHandler {
private String context = "/fbmsg";
int port = 4245;
String strResponse = "";
boolean testOK = true; // Set to true if want to test positive response.
public static void main(String[] args) {
new HTTPServer().run();
}
public void run() {
HttpServer server = null;
try {
server = HttpServer.create(new InetSocketAddress(port), 10);
server.setExecutor(Executors.newFixedThreadPool(10));
server.start();
server.createContext(context, this);
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
public void handle(HttpExchange httpExch) throws IOException {
StringBuilder sb = new StringBuilder();
try (InputStream inputStream = httpExch.getRequestBody()) {
sb = new StringBuilder();
int i;
while ((i = inputStream.read()) != -1) {
sb.append((char) i);
}
}
if (testOK) {
replyOK(httpExch);
} else {
replyError(httpExch);
}
}
/**
*
* @param httpExch
*
* Sends a 200 OK back
*/
private void replyOK(HttpExchange httpExch) {
try {
Headers responseHeaders = httpExch.getResponseHeaders();
responseHeaders.add("Content-Type", ("application/json"));
strResponse = getResponse();
if (strResponse != null) {
httpExch.sendResponseHeaders(200, strResponse.length());
try (OutputStream os = httpExch.getResponseBody()) {
os.write(strResponse.getBytes());
}
} else {
httpExch.sendResponseHeaders(200, -1);
}
} catch (IOException e1) {
System.out.println(e1.getMessage());
}
}
private String getResponse() {
return "Any String You Want To";
}
/**
*
* @param httpExch
* There are any number of ways to create an error in the response. This method sends back a 404 and also mismatched the content length
* and the content, to create an unexpected EOF error.
*
*/
private void replyError(HttpExchange httpExch) {
String replyString = "Error";
try {
Headers responseHeaders = httpExch.getResponseHeaders();
responseHeaders.add("Content-Type", ("application/json"));
httpExch.sendResponseHeaders(404, 0);
try (OutputStream os = httpExch.getResponseBody()) {
os.write(replyString.getBytes());
}
} catch (IOException e1) {
System.out.println(e1.getMessage());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment