Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

79
LINES

< > BotCompany Repo | #611 // HTTP server serving a snippet as a page (half-shortened JavaX example)

JavaX source code - run with: x30.jar

1  
!592 1000265 // auto import, standard functions
2  
3  
import com.sun.net.httpserver.HttpExchange;
4  
import com.sun.net.httpserver.HttpHandler;
5  
import com.sun.net.httpserver.HttpServer;
6  
7  
public class main {
8  
  private static HttpServer server;
9  
  private static int port = 8080;
10  
  private static String snippetID = "#2000335";
11  
12  
  public static void main(String[] args) throws Exception {
13  
    server = HttpServer.create(new InetSocketAddress(port), 0);
14  
    createHttpContext("/", new MainContext());
15  
    server.setExecutor(null); // creates a default executor
16  
    server.start();
17  
    System.out.println("HTTP server started (listening on port " + port + "!)");
18  
  }
19  
20  
  private static void createHttpContext(String path, HttpHandler handler) {
21  
    server.createContext(path, new SafeHttpHandler(handler));
22  
  }
23  
24  
  public static class SafeHttpHandler implements HttpHandler {
25  
    private HttpHandler handler;
26  
27  
    public SafeHttpHandler(HttpHandler handler) {
28  
      this.handler = handler;
29  
    }
30  
31  
    public void handle(HttpExchange exchange) throws IOException {
32  
      System.out.println("[web server] Handling request: " + exchange.getRequestURI());
33  
      try {
34  
        handler.handle(exchange);
35  
      } catch (Throwable e) {
36  
        String stackTrace = getStackTrace(e);
37  
        sendServerError(exchange, stackTrace);
38  
        System.out.println(stackTrace);
39  
      } finally {
40  
        exchange.close();
41  
        System.out.println("[web server] Done handling request: " + exchange.getRequestURI());
42  
      }
43  
    }
44  
  }
45  
46  
  public static String getStackTrace(Throwable e) {
47  
    StringWriter stringWriter = new StringWriter();
48  
    e.printStackTrace(new PrintWriter(stringWriter));
49  
    return stringWriter.toString();
50  
  }
51  
52  
  public static void sendHtml(HttpExchange exchange, String content) throws IOException {
53  
    int responseCode = 200;
54  
    String contentType = "text/html";
55  
    sendHttpTextResponse(exchange, responseCode, content, contentType);
56  
  }
57  
58  
  public static void sendHttpTextResponse(HttpExchange exchange, int responseCode, String content, String contentType) throws IOException {
59  
    byte[] bytes = content.getBytes("UTF-8");
60  
    String header = contentType + "; charset=utf-8";
61  
    System.out.println("Sending content type: " + header);
62  
    exchange.getResponseHeaders().set("Content-Type", header);
63  
    exchange.sendResponseHeaders(responseCode, bytes.length);
64  
    OutputStream os = exchange.getResponseBody();
65  
    os.write(bytes);
66  
    os.close();
67  
  }
68  
69  
  public static void sendServerError(HttpExchange exchange, String response) throws IOException {
70  
    sendHttpTextResponse(exchange, 500, response, "text/plain");
71  
  }
72  
73  
  public static class MainContext implements HttpHandler {
74  
    public void handle(HttpExchange httpExchange) throws IOException {
75  
      String html = loadSnippet(snippetID);
76  
      sendHtml(httpExchange, html);
77  
    }
78  
  }
79  
}

Author comment

Began life as a copy of #608

Snippet is loaded live at each request.

download  show line numbers  debug dex  old transpilations   

Travelled to 13 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #611
Snippet name: HTTP server serving a snippet as a page (half-shortened JavaX example)
Eternal ID of this version: #611/1
Text MD5: ce900b3a8f5df79e21fd334e633e9ab5
Author: stefan
Category:
Type: JavaX source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2015-05-06 19:41:28
Source code size: 2920 bytes / 79 lines
Pitched / IR pitched: No / Yes
Views / Downloads: 634 / 545
Referenced in: [show references]