]> matita.cs.unibo.it Git - logicplayer.git/blobdiff - server/com/company/httpget.java
shuffling + libs imported
[logicplayer.git] / server / com / company / httpget.java
diff --git a/server/com/company/httpget.java b/server/com/company/httpget.java
new file mode 100644 (file)
index 0000000..079cc01
--- /dev/null
@@ -0,0 +1,79 @@
+package com.company;
+
+import java.io.*;
+import java.net.*;
+import java.security.NoSuchAlgorithmException;
+import java.util.*;
+
+import com.sun.net.httpserver.HttpExchange;
+import com.sun.net.httpserver.HttpHandler;
+import com.sun.net.httpserver.HttpServer;
+import java.sql.*;
+
+public class httpget implements Runnable {
+
+       httpget(){}
+
+       public static Map<String, String> splitQuery(String query) throws UnsupportedEncodingException 
+       {
+               Map<String, String> query_pairs = new LinkedHashMap<String, String>();
+               String[] pairs = query.split("&");
+               for (String pair : pairs) 
+               {
+                       int idx = pair.indexOf("=");
+                       query_pairs.put(URLDecoder.decode(pair.substring(0, idx), "UTF-8"), URLDecoder.decode(pair.substring(idx + 1), "UTF-8"));
+               }
+               return query_pairs;
+       }
+
+       @Override
+       public void run() {
+               try {
+                       HttpServer server = HttpServer.create(new InetSocketAddress(global.portaHttpServer), 0);
+                       server.createContext("/test", new MyHandler());
+                       server.setExecutor(null); // creates a default executor
+                       server.start();
+               }catch (Exception e)
+               {
+                       e.printStackTrace();
+               }
+    }
+
+    static class MyHandler implements HttpHandler 
+    {
+        public void handle(HttpExchange t) throws IOException
+        {
+                       
+            /*String response = "This is the response";
+            System.out.println("mannaggia santa");
+            t.sendResponseHeaders(200, response.length());
+            OutputStream os = t.getResponseBody();
+            os.write(response.getBytes());*/
+                       URI req=t.getRequestURI();
+            String query=req.getQuery();
+            Map<String, String> result=splitQuery(query);
+            String id=result.get("id");
+            //System.out.println(id);
+            try
+            {
+                               dbConnect request=new dbConnect();
+                               String returnQuery=request.dbQuery("3"+id);
+                               t.sendResponseHeaders(200, returnQuery.length());
+                               OutputStream os = t.getResponseBody();
+                               os.write(returnQuery.getBytes());
+                               os.close();
+                       }
+                       catch(SQLException e)
+                       {}
+                       catch(ClassNotFoundException c)
+                       {}
+                       catch(IllegalAccessException a)
+                       {}
+                       catch(InstantiationException i)
+                       {} catch (NoSuchAlgorithmException e) {
+                               e.printStackTrace();
+                       }
+               }
+    }
+
+}