package jules;

import java.io.IOException;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpServer;
import org.apache.lucene.util.fst.PairOutputs;

public class WebService {
    public static void main(String[] args) throws Exception {
        HttpServer server = HttpServer.create(new InetSocketAddress(8000), 0);
        server.createContext("/", new MyHandler());
        server.setExecutor(null); // creates a default executor
        server.start();
    }

    static class MyHandler implements HttpHandler {
        public void handle(HttpExchange t) throws IOException {
            String response = "nothing done yet";
            Map<String, String> qMap = new HashMap<String, String>();
            qMap.put("q", "kung");

            for (Map.Entry<String, String> entry : qMap.entrySet())
                System.out.println(entry.getKey() + " : " + entry.getValue());

            System.out.println("checking for key");
            if (qMap.containsKey("q")) {
                System.out.println("key found");
                StringBuilder sb = new StringBuilder();
                String q = qMap.get("q");
                List<Map<String, String>> results =  jules.Indexer.query(q, true);
                for (Map<String, String> result : results) {
                    sb.append("===========================================\n");
                    for (Map.Entry<String, String> entry : result.entrySet()) {
                        // If it's text... we'll just take the context in this baseline
                        sb.append(entry.getKey() + ": \n");
                        if (entry.getKey().equals("text")) {
                            int indexOfHit = entry.getValue().toLowerCase().indexOf(q);
                            String context = entry.getValue().toLowerCase().subSequence(indexOfHit-20, indexOfHit+20).toString()+"\n";
                            sb.append(context);
                        } else {
                            sb.append(entry.getValue() + "\n");
                        }
                    }
                    sb.append("===========================================\n");
                }
                response = sb.toString();
            } else {
                System.out.println("!key found");
                response = "no query found! specify the q parameter!";
            }
            System.out.println("serving response");
            t.sendResponseHeaders(200, response.getBytes().length);
            OutputStream os = t.getResponseBody();
            os.write(response.getBytes());
            os.close();
        }
    }
    static private Map<String, String> queryToMap(String query){
        Map<String, String> result = new HashMap<String, String>();
        System.out.println("parsing: " + query);
        for (String param : query.split("&")) {
            String pair[] = param.split("=");
            if (pair.length>1) {
                result.put(pair[0], pair[1]);
            }else{
                result.put(pair[0], "");
            }
        }
        return result;
    }
}
@@ -173,16 +173,180 @@
 l.Map;%0A%0A
+%0Aimport com.carrotsearch.ant.tasks.junit4.dependencies.com.google.gson.JsonArray;%0Aimport com.carrotsearch.ant.tasks.junit4.dependencies.com.google.gson.JsonObject;%0A
 import c
@@ -469,55 +469,8 @@
 ver;
-%0Aimport org.apache.lucene.util.fst.PairOutputs;
 %0A%0Apu
@@ -625,17 +625,17 @@
 dress(80
-0
+8
 0), 0);%0A
@@ -932,24 +932,52 @@
  = %22
-nothing done yet
+No query found, specify the query parameter.
 %22;%0A 
@@ -986,32 +986,33 @@
          Map
+ 
 %3CString,
  String%3E qMa
@@ -991,33 +991,32 @@
     Map %3CString,
- 
 String%3E qMap = n
@@ -1018,223 +1018,94 @@
 p = 
-new HashMap%3CString, String%3E();%0A            qMap.put(%22q%22, %22kung%22);%0A%0A            for (Map.Entry%3CString, String%3E entry : qMap.entrySet())%0A                System.out.println(entry.getKey() + %22 : %22 + entry.getValue
+queryToMap(t.getRequestURI().getQuery() == null ? %22%22 : t.getRequestURI().getQuery
 ());%0A
-%0A
     
@@ -1423,16 +1423,73 @@
  true);%0A
+                JsonArray jsonResults = new JsonArray();%0A
         
@@ -1565,65 +1565,40 @@
     
-sb.append(%22===========================================%5Cn%22
+JsonObject res = new JsonObject(
 );%0A 
@@ -2156,16 +2156,95 @@
 ntext);%0A
+                            res.addProperty(entry.getKey(), entry.getValue());%0A
         
@@ -2360,312 +2360,220 @@
     
-%7D%0A                    %7D%0A                    sb.append(%22===========================================%5Cn%22);%0A                %7D%0A                response = sb.toString();%0A            %7D else %7B%0A                System.out.println(%22!key found%22);%0A                response = %22no query found! specify the q parameter!%22
+    res.addProperty(entry.getKey(), entry.getValue());%0A                        %7D%0A                    %7D%0A                    jsonResults.add(res);%0A                %7D%0A                response = jsonResults.toString()
 ;%0A  
@@ -2834,24 +2834,25 @@
     %7D%0A    %7D%0A
+%0A
     static p

add Json and handling of the q-parameter