]> matita.cs.unibo.it Git - logicplayer.git/blob - server/com/company/httpget.java
New version
[logicplayer.git] / server / 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 (IOException e) {
37                         e.printStackTrace();
38                 }
39         }
40
41     static class MyHandler implements HttpHandler 
42     {
43         public void handle(HttpExchange t) throws IOException
44         {
45                         URI req=t.getRequestURI();
46             String query=req.getQuery();
47             Map<String, String> result=splitQuery(query);
48             String id=result.get("id");
49             try
50             {
51
52                                 String returnQuery=global.request.dbQuery("3"+id);
53                                 t.sendResponseHeaders(200, returnQuery.length());
54                                 OutputStream os = t.getResponseBody();
55                                 os.write(returnQuery.getBytes());
56                                 os.close();
57                         } catch (ClassNotFoundException e) {
58                                 e.printStackTrace();
59                         } catch (SQLException e) {
60                                 e.printStackTrace();
61                         } catch (InstantiationException e) {
62                                 e.printStackTrace();
63                         } catch (NoSuchAlgorithmException e) {
64                                 e.printStackTrace();
65                         } catch (IllegalAccessException e) {
66                                 e.printStackTrace();
67                         }
68                 }
69     }
70
71 }