Advertisement
Guest User

Untitled

a guest
Sep 28th, 2016
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.40 KB | None | 0 0
  1. package com.mistrix.REST.server;
  2.  
  3. import com.sun.net.httpserver.HttpExchange;
  4. import com.sun.net.httpserver.HttpHandler;
  5. import com.sun.net.httpserver.HttpServer;
  6. import com.mistrix.REST.configuration.Configuration;
  7.  
  8. import java.io.IOException;
  9. import java.io.OutputStream;
  10. import java.lang.reflect.InvocationTargetException;
  11. import java.lang.reflect.Method;
  12. import java.net.InetSocketAddress;
  13. import java.util.HashMap;
  14. import java.util.Map;
  15.  
  16. public class Server {
  17.  
  18.     public static Map<String, Method> serviceRead = new HashMap<>();
  19.     public static Map<String, Method> serviceMethod = new HashMap<>();
  20.  
  21.     public static void main(String[] args) throws Exception {
  22.         start();
  23.     }
  24.  
  25.     public static void start() throws Exception {
  26.         Configuration.addRoutes();
  27.         System.out.println("Adding services");
  28.         HttpServer server = HttpServer.create(new InetSocketAddress(8000), 0);
  29.         for(Map.Entry<String, Method> entry : serviceRead.entrySet()) {
  30.             Method method = entry.getValue();
  31.             server.createContext("/" + entry.getKey(), new ReadHandler(method, ""));
  32.         }
  33.         for(Map.Entry<String, Method> entry : serviceMethod.entrySet()) {
  34.             Method method = entry.getValue();
  35.             server.createContext("/" + entry.getKey(), new MethodHandler(method, ""));
  36.         }
  37.         server.setExecutor(null);
  38.         System.out.println("Starting web server");
  39.         server.start();
  40.         System.out.println("Web server started");
  41.     }
  42.  
  43.  
  44.     static class ReadHandler implements HttpHandler {
  45.  
  46.         private final Method method;
  47.         private String response;
  48.  
  49.         public ReadHandler(Method method, String response) {
  50.             this.method = method;
  51.             this.response = response;
  52.         }
  53.  
  54.         @Override
  55.         public void handle(HttpExchange t) throws IOException {
  56.             if(t.getRemoteAddress().getAddress().getHostAddress().toString().equals(Configuration.ipv6_adress)) {
  57.                 try {
  58.                     Object value = method.invoke(null, t.getRequestURI().getQuery());
  59.                     this.response = (String) value;
  60.                 } catch (IllegalAccessException e) {
  61.                     e.printStackTrace();
  62.                 } catch (InvocationTargetException e) {
  63.                     e.printStackTrace();
  64.                 }
  65.                 t.sendResponseHeaders(200, response.length());
  66.                 try (OutputStream os = t.getResponseBody()) {
  67.                     os.write(response.getBytes());
  68.                 }
  69.             }
  70.         }
  71.     }
  72.  
  73.     static class MethodHandler implements HttpHandler {
  74.  
  75.         private final Method method;
  76.         private String response;
  77.  
  78.         public MethodHandler(Method method, String response) {
  79.             this.method = method;
  80.             this.response = response;
  81.         }
  82.  
  83.         @Override
  84.         public void handle(HttpExchange t) throws IOException {
  85.             if(t.getRemoteAddress().getAddress().getHostAddress().toString().equals(Configuration.ipv6_adress)) {
  86.                 try {
  87.                     method.invoke(null, t.getRequestURI().getQuery());
  88.                 } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
  89.                     e.printStackTrace();
  90.                 }
  91.                 t.sendResponseHeaders(200, response.length());
  92.                 try (OutputStream os = t.getResponseBody()) {
  93.                     os.write(response.getBytes());
  94.                 }
  95.             }
  96.         }
  97.     }
  98.  
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement