/**
 * Copyright (C) 2016 Joshua Auerbach 
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.qcert.sql;

import fi.iki.elonen.NanoHTTPD;

/**
 * A small nonohttpd-based server that does SQL parse-encode (to sexp) via http get requests.
 */
public class EncodingService extends NanoHTTPD {
	public EncodingService(int port) {
		super(port);
	}

	/* (non-Javadoc)
	 * @see fi.iki.elonen.NanoHTTPD#serve(fi.iki.elonen.NanoHTTPD.IHTTPSession)
	 */
	@Override
	public Response serve(IHTTPSession session) {
		// TODO
		return null;
	}

}

@@ -618,16 +618,93 @@
 t.sql;%0A%0A
+import java.io.IOException;%0Aimport java.util.HashMap;%0Aimport java.util.Map;%0A%0A
 import f
@@ -937,194 +937,1969 @@
 %0A%09%7D%0A
-%0A%09/* (non-Javadoc)%0A%09 * @see fi.iki.elonen.NanoHTTPD#serve(fi.iki.elonen.NanoHTTPD.IHTTPSession)%0A%09 */%0A%09@Override%0A%09public Response serve(IHTTPSession session) %7B%0A%09%09// TODO%0A%09%09return null;%0A%09%7D%0A
+%09%0A%09public static void main(String%5B%5D args) throws IOException %7B%0A%09%09int port = args.length == 0 ? 9879 : Integer.parseInt(args%5B0%5D);%0A%09%09EncodingService svc = new EncodingService(port);%0A%09%09svc.start(NanoHTTPD.SOCKET_READ_TIMEOUT, false);%0A%09%7D%0A%0A%09/* (non-Javadoc)%0A%09 * @see fi.iki.elonen.NanoHTTPD#serve(fi.iki.elonen.NanoHTTPD.IHTTPSession)%0A%09 */%0A%09@Override%0A%09public Response serve(IHTTPSession session) %7B%0A        Map%3CString, String%3E files = new HashMap%3CString, String%3E();%0A        Method method = session.getMethod();%0A        if (Method.POST.equals(method)) %7B%0A            try %7B%0A                session.parseBody(files);%0A            %7D catch (IOException ioe) %7B%0A            %09System.out.println(%22I/O Exception parsing body%22);%0A                return newFixedLengthResponse(Response.Status.INTERNAL_ERROR, NanoHTTPD.MIME_PLAINTEXT, %22SERVER INTERNAL ERROR: IOException: %22 + ioe.getMessage());%0A            %7D catch (ResponseException re) %7B%0A            %09System.out.println(%22Response Exception parsing body%22);%0A                return newFixedLengthResponse(re.getStatus(), NanoHTTPD.MIME_PLAINTEXT, re.getMessage());%0A            %7D%0A            String query = files.get(%22postData%22);%0A            if (query == null) %7B%0A            %09System.out.println(%22No postData in body%22);%0A                return newFixedLengthResponse(Response.Status.BAD_REQUEST, NanoHTTPD.MIME_PLAINTEXT, %22Invalid input%22);%0A            %7D%0A            try %7B %0A            %09return newFixedLengthResponse(Response.Status.OK, NanoHTTPD.MIME_PLAINTEXT, PrestoEncoder.parseAndEncode(query, false));%0A            %7D catch (Throwable t) %7B%0A            %09t.printStackTrace();%0A            %09return newFixedLengthResponse(Response.Status.BAD_REQUEST, NanoHTTPD.MIME_PLAINTEXT, %22*ERROR*: %22 + t.getMessage());%0A            %7D%0A        %7D else %7B%0A        %09System.out.println(%22Rejecting non-post request%22);%0A            return newFixedLengthResponse(Response.Status.BAD_REQUEST, NanoHTTPD.MIME_PLAINTEXT, %22Only POST requests accepted%22);%0A        %7D%0A%09%7D
 %0A%7D%0A

Complete the encoding (web) service.