package de.felixbruns.jotify.gateway;

import java.io.IOException;
import java.io.OutputStream;
import java.nio.charset.Charset;
import java.util.Map;

import org.json.JSONException;
import org.json.XML;

import com.sun.net.httpserver.Headers;
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;

import de.felixbruns.jotify.gateway.util.URIUtilities;

public abstract class GatewayHandler implements HttpHandler {
	public void handle(HttpExchange exchange) throws IOException {
		/* Get request method and query. */
		String requestMethod = exchange.getRequestMethod();
		String requestQuery  = exchange.getRequestURI().getQuery();
		
		/* Get request parameters. */
		Map<String, String> params = URIUtilities.parseQuery(requestQuery);
		
		/* Get response body and headers. */
		OutputStream responseBody    = exchange.getResponseBody();
		Headers      responseHeaders = exchange.getResponseHeaders();
		String       responseString;
		byte[]       responseBytes;
		
		/* Set Access-Control headers.*/
		responseHeaders.set("Access-Control-Allow-Origin", "*");
		responseHeaders.set("Access-Control-Allow-Methods", "GET, OPTIONS");
		responseHeaders.set("Access-Control-Allow-Headers", "X-Requested-With");
		responseHeaders.set("Access-Control-Max-Age", "1728000");
		
		/* Get response string depending on request method. */
		if(requestMethod.equalsIgnoreCase("OPTIONS")){
			responseString = "";
		}
		else if(requestMethod.equalsIgnoreCase("GET")){
			responseString = this.handle(params);
		}
		else{
			responseString = "<error>Method not supported</error>";
		}
		
		/* Set 'Content-Type' header and encode response depending on output format. */
		if(params.get("format") != null && params.get("format").equals("json")){
			responseHeaders.set("Content-Type", "application/json");
			
			/* Convert XML to JSON object or fail with JSON encoded error message. */
			try{
				responseString = XML.toJSONObject(responseString).toString(4);
			}
			catch(JSONException e){
				responseString = "{\"error\": \"" + e.getMessage().replace("\"", "\\\"") + "\"}";
			}
		}
		else{
			responseHeaders.set("Content-Type", "text/xml");
		}
		
		/* Get response bytes. */
		responseBytes = responseString.getBytes(Charset.forName("UTF-8"));
		
		/* Send response code, length and headers. */
		exchange.sendResponseHeaders(200, responseBytes.length);
		
		/* Write response string to output stream. */
		responseBody.write(responseBytes);
		responseBody.close();
	}
	
	public abstract String handle(Map<String, String> params);
}

@@ -47,27 +47,120 @@
 java.io.
-IOException
+BufferedReader;%0Aimport java.io.IOException;%0Aimport java.io.InputStream;%0Aimport java.io.InputStreamReader
 ;%0Aimport
@@ -215,16 +215,42 @@
 harset;%0A
+import java.util.HashMap;%0A
 import j
@@ -850,46 +850,658 @@
 s = 
-URIUtilities.parseQuery(requestQuery);
+new HashMap%3CString, String%3E();%0A%09%09%0A%09%09if(requestMethod.equalsIgnoreCase(%22GET%22))%7B%0A%09%09%09params = URIUtilities.parseQuery(requestQuery);%0A%09%09%7D%0A%09%09else if(requestMethod.equalsIgnoreCase(%22POST%22))%7B%0A%09%09%09InputStream    input   = exchange.getRequestBody();%0A%09%09%09BufferedReader reader  = new BufferedReader(new InputStreamReader(input));%0A%09%09%09StringBuilder  builder = new StringBuilder();%0A%09%09%09String         line;%0A%09%09%09%0A%09%09%09/* Convert input stream to string. */%0A%09%09%09while((line = reader.readLine()) != null)%7B%0A%09%09%09%09builder.append(line);%0A%09%09%09%7D%0A%09%09%09%0A%09%09%09/* Close input stream. */%0A%09%09%09input.close();%0A%09%09%09%0A%09%09%09/* Parse query. */%0A%09%09%09params = URIUtilities.parseQuery(builder.toString());%0A%09%09%7D
 %0A%09%09%0A
@@ -1881,20 +1881,26 @@
 %22, %22
-GET, 
 OPTIONS
+, GET, POST
 %22);%0A
@@ -2167,24 +2167,24 @@
 g = %22%22;%0A%09%09%7D%0A
-
 %09%09else if(re
@@ -2262,24 +2262,120 @@
 arams);%0A%09%09%7D%0A
+%09%09else if(requestMethod.equalsIgnoreCase(%22POST%22))%7B%0A%09%09%09responseString = this.handle(params);%0A%09%09%7D%0A
 %09%09else%7B%0A%09%09%09r

Allow POST in jotify gateway.