]> matita.cs.unibo.it Git - logicplayer.git/blob - server/tesi/src/com/company/httpget.java
079cc019d6e9d3ddca729145608eb30a91e137ce
[logicplayer.git] / server / tesi / src / com / company / httpget.java
1 package com.company;
2
3 import java.io.*;
4 import java.net.*;
5 import java.security.NoSuchAlgorithmException;
6 import java.util.*;
7
8 import com.sun.net.httpserver.HttpExchange;
9 import com.sun.net.httpserver.HttpHandler;
10 import com.sun.net.httpserver.HttpServer;
11 import java.sql.*;
12
13 public class httpget implements Runnable {
14
15         httpget(){}
16
17         public static Map<String, String> splitQuery(String query) throws UnsupportedEncodingException 
18         {
19                 Map<String, String> query_pairs = new LinkedHashMap<String, String>();
20                 String[] pairs = query.split("&");
21                 for (String pair : pairs) 
22                 {
23                         int idx = pair.indexOf("=");
24                         query_pairs.put(URLDecoder.decode(pair.substring(0, idx), "UTF-8"), URLDecoder.decode(pair.substring(idx + 1), "UTF-8"));
25                 }
26                 return query_pairs;
27         }
28
29         @Override
30         public void run() {
31                 try {
32                         HttpServer server = HttpServer.create(new InetSocketAddress(global.portaHttpServer), 0);
33                         server.createContext("/test", new MyHandler());
34                         server.setExecutor(null); // creates a default executor
35                         server.start();
36                 }catch (Exception e)
37                 {
38                         e.printStackTrace();
39                 }
40     }
41
42     static class MyHandler implements HttpHandler 
43     {
44         public void handle(HttpExchange t) throws IOException
45         {
46                         
47             /*String response = "This is the response";
48             System.out.println("mannaggia santa");
49             t.sendResponseHeaders(200, response.length());
50             OutputStream os = t.getResponseBody();
51             os.write(response.getBytes());*/
52                         URI req=t.getRequestURI();
53             String query=req.getQuery();
54             Map<String, String> result=splitQuery(query);
55             String id=result.get("id");
56             //System.out.println(id);
57             try
58             {
59                                 dbConnect request=new dbConnect();
60                                 String returnQuery=request.dbQuery("3"+id);
61                                 t.sendResponseHeaders(200, returnQuery.length());
62                                 OutputStream os = t.getResponseBody();
63                                 os.write(returnQuery.getBytes());
64                                 os.close();
65                         }
66                         catch(SQLException e)
67                         {}
68                         catch(ClassNotFoundException c)
69                         {}
70                         catch(IllegalAccessException a)
71                         {}
72                         catch(InstantiationException i)
73                         {} catch (NoSuchAlgorithmException e) {
74                                 e.printStackTrace();
75                         }
76                 }
77     }
78
79 }