package testWebService;

import java.io.BufferedReader;
import java.io.File;
import java.io.InputStream;
import java.io.InputStreamReader;

import javax.servlet.ServletContext;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import com.conordevilly.ocr.neuralnetwork.*;
 
@Path("/")
public class RestService {
	@javax.ws.rs.core.Context 
	ServletContext context;
	NeuralNetwork nn;
	
	public RestService(){
		/*
		NeuralNetwork nn = PersistanceManager.readNN(new File("/nn.data"));
		if(nn == null){
			nn = new NeuralNetwork(10, 26);
		}
		*/
	}

	@POST
	@Path("/ocr")
	@Consumes(MediaType.APPLICATION_JSON)
	public Response processData(InputStream incomingData) {
		StringBuilder strBuilder = new StringBuilder();

		if(nn == null){
			//NeuralNetwork nn = PersistanceManager.readNN(new File(context.getContextPath()));
			System.out.println(context.toString());
		}

		try {
			BufferedReader in = new BufferedReader(new InputStreamReader(incomingData));
			String line = null;

			while ((line = in.readLine()) != null) {
				strBuilder.append(line);
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
 
		return Response.status(200).entity(strBuilder.toString()).build();
	}
 
	@GET
	@Path("/test")
	@Produces(MediaType.TEXT_PLAIN)
	public Response testREST(InputStream incomingData) {
		String result = "Hello World!";
		return Response.status(200).entity(result).build();
	}
}
@@ -14,24 +14,61 @@
 ebService;%0A%0A
+import java.awt.image.BufferedImage;%0A
 import java.
@@ -107,16 +107,77 @@
 o.File;%0A
+import java.io.FileOutputStream;%0Aimport java.io.IOException;%0A
 import j
@@ -230,16 +230,74 @@
 Reader;%0A
+import java.nio.file.Files;%0A%0Aimport javax.imageio.ImageIO;
 %0Aimport 
@@ -557,24 +557,84 @@
 .Response;%0A%0A
+import org.json.JSONException;%0Aimport org.json.JSONObject;%0A%0A
 import com.c
@@ -666,16 +666,56 @@
 work.*;%0A
+import com.sun.jersey.core.util.Base64;%0A
  %0A@Path(
@@ -983,16 +983,17 @@
 %09%09*/%0A%09%7D%0A
+%09
 %0A%09@POST%0A
@@ -1074,565 +1074,636 @@
 cess
-Data(InputStream incomingData) %7B%0A%09%09StringBuilder strBuilder = new StringBuilder(
+(JSONObject inputJSON) throws JSONException, IOException%7B%0A%09%09String fileData = (String) inputJSON.get(%22fileData%22
 );%0A
-%0A
 %09%09
-if(nn == null)%7B%0A%09%09%09//NeuralNetwork nn = PersistanceManager
+File fileIn = convStringToFile(fileData);%0A%09%09BufferedImage imgIn = ImageIO
 .read
-NN(new File(context.getContextPath()));%0A%09%09%09System.out.println(context.toString()
+(fileIn);%0A%09%09String guess = nn.processMultiImage(imgIn);%0A%09%09%0A%09%09return Response.status(200).entity(guess).build(
 );%0A
-%09
 %09%7D%0A
-%0A%09%09try %7B%0A%09%09%09BufferedReader in = new BufferedReader(new InputStreamReader(incomingData));%0A%09%09%09String line = null;%0A%0A%09%09%09while ((line = in.readLine()) != null) %7B%0A%09%09%09%09strBuilder.append(line);%0A%09%09%09%7D%0A%0A%09%09%7D catch (Exception e) %7B%0A%09%09%09e.printStackTrace();%0A%09%09%7D%0A %0A%09%09return Response.status(200).entity(strBuilder.toString()).build()
+%09%0A%09//Converts a string of encoded data to a file%0A%09private File convStringToFile(String encodedData) throws IOException%7B%0A%09%09File ret = new File(%22/tmp.jpg%22);%0A%09%09byte%5B%5D rawData = Base64.decode(encodedData);%0A%0A%09%09FileOutputStream writer = new FileOutputStream(ret);%0A%09%09writer.write(rawData);%0A%09%09writer.close();%0A%0A%09%09return ret
 ;%0A%09%7D%0A
- 
 %0A%09@G
@@ -1784,32 +1784,8 @@
 EST(
-InputStream incomingData
 ) %7B%0A

Convert response from Java Net -> Jersey Response