I have some code but I need it to be as a RESTful service wh
I have some code but I need it to be as a RESTful service, which I can post to Amazon web services
import java.util.*;
public class JSON_Test{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
System.out.println(\"** Enter input in Single line **\");
String input = sc.nextLine();
//System.out.println(input);
StringBuffer name = new StringBuffer();
StringBuffer list_str = new StringBuffer();
int[] original;
int name_in=0;
boolean name_flag = false;
int i = 0;
int len = input.length();
while(i<len && input.charAt(i) == \' \'){
i++;
}
if(i==len){
System.out.println(\"Incorrect input !!\");
System.exit(0);
}
if(input.charAt(i) == \'{\'){
i++;
while( i<len && input.charAt(i) == \' \'){
i++;
}
if(i==len){
System.out.println(\"Incorrect input !!\");
System.exit(0);
}
if(input.charAt(i) == \'\\\"\'){
i++;
while(i<len && input.charAt(i) != \'\\\"\'){
name.append(input.charAt(i));
i++;
}
if(i==len){
System.out.println(\"Incorrect input !!\");
System.exit(0);
}
i++;
while( i<len && input.charAt(i) == \' \'){
i++;
}
if(i==len){
System.out.println(\"Incorrect input !!\");
System.exit(0);
}
if(input.charAt(i) ==\':\'){
i++;
while( i<len && input.charAt(i) == \' \'){
i++;
}
if(i==len){
System.out.println(\"Incorrect input !!\");
System.exit(0);
}
if(input.charAt(i)==\'[\'){
i++;
if(i==len){
System.out.println(\"Incorrect input !!\");
System.exit(0);
}
while(i<len && input.charAt(i) != \']\'){
list_str.append(input.charAt(i));
i++;
}
StringTokenizer st = new StringTokenizer(list_str.toString(),\",\");
original = new int[st.countTokens()];
while (st.hasMoreTokens()) {
try{
original[name_in] = Integer.parseInt(st.nextToken());
name_in++;
}
catch(Exception e){
System.out.println(\"Incorrect Input. \ \\t Hint : Can\'t find \\\"numbers\\\" in the input\");
System.exit(0);
}
}
i++;
while( i<len && input.charAt(i) == \' \'){
i++;
}
if( i >= len || input.charAt(i) != \'}\' ){
System.out.println(\"Incorrect Input. \ \\t Hint : Closing Tag\");
System.exit(0);
}
Merge mms = new Merge();
long startTime = System.currentTimeMillis();
mms.sort(original);
long stopTime = System.currentTimeMillis();
long elapsedTime = stopTime - startTime;
System.out.print(\"{ \\\"outList\\\"\\t:[\");
for(int j=0; j<original.length;j++){
System.out.print(original[j]);
if(j != original.length-1){
System.out.print(\",\");
}
}
System.out.print(\"],\ \\\"algorithm\\\" : \\\"Mergesort\\\",\ \");
System.out.print(\" \\\"timeMs\\\" : \"+ elapsedTime +\" } \");
}
}
}
else{
System.out.println(\"Incorrect Input. \ \\t Hint : Can\'t find \\\"inlist\\\" Starting quote \' \\\" \' \");
System.exit(0);
}
}
else{
System.out.println(\"Incorrect Input. \ \\t Hint : Can\'t find \\\"JSON list\\\" Starting brace - \\\"{\\\" \");
System.exit(0);
}
}
}
class Merge{
private int[] array;
private int[] tempArr;
private int length;
Merge(){
}
public void sort(int inputArr[]) {
this.array = inputArr;
this.length = inputArr.length;
this.tempArr = new int[length];
MergeSort_Partition(0, length - 1);
}
private void MergeSort_Partition(int low, int high) {
if (low < high) {
int middle = low + (high - low) / 2;
MergeSort_Partition(low, middle);
MergeSort_Partition(middle + 1, high);
sortwith_Parts(low, middle, high);
}
}
private void sortwith_Parts(int low, int middle, int high) {
for (int i = low; i <= high; i++) {
tempArr[i] = array[i];
}
int i = low;
int j = middle + 1;
int k = low;
while (i <= middle && j <= high) {
if (tempArr[i] <= tempArr[j]) {
array[k] = tempArr[i];
i++;
} else {
array[k] = tempArr[j];
j++;
}
k++;
}
while (i <= middle) {
array[k] = tempArr[i];
k++;
i++;
}
}
}
Solution
package InsertionSort; // Import required java libraries import java.io.*; import javax.servlet.*; import javax.servlet.http.*; import javax.json.*; import java.util.List; import java.util.Arrays; import java.util.*; // Extend HttpServlet class public class ReverseList extends HttpServlet { // Standard servlet method public void init() throws ServletException { // Do any required initialization here - likely none } // Standard servlet method - we will handle a POST operation public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doService(request, response); } // Standard servlet method - we will not respond to GET public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // Set response content type and return an error message response.setContentType(\"application/json\"); PrintWriter out = response.getWriter(); out.println(\"{ \'message\' : \'Use POST!\'}\"); } // Our main worker method // Parses messages e.g. {\"inList\" : [5, 32, 3, 12]} // Returns the list reversed. private void doService(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // Get received JSON data from HTTP request BufferedReader br = new BufferedReader(new InputStreamReader(request.getInputStream())); String jsonStr = \"\"; if(br != null){ jsonStr = br.readLine(); } // Create JsonReader object StringReader strReader = new StringReader(jsonStr); JsonReader reader = Json.createReader(strReader); // Get the singular JSON object (name:value pair) in this message. JsonObject obj = reader.readObject(); //Json object that holds the output of the sort JsonArrayBuilder outArrayBuilder = Json.createArrayBuilder(); //From the object get the array named \"inList\" JsonArray inArray = obj.getJsonArray(\"inList\"); int arrLen = inArray.size(); int[] arr = new int[arrLen]; int temp; long sortBegin = 0; long sortEnd = 0; long timeSorted = 0; String errorCheckString = inArray.toString(); char c; boolean validJson = false; int strLen = errorCheckString.length(); for (int t = 0; t < errorCheckString.length(); t++){ c = errorCheckString.charAt(t); if (c >= \'0\' && c <=9 ) { validJson = true; } if (c == \'[\' || c == \',\' || c == \']\' || c == \'-\') { validJson = true; } } /* if (errorCheckString.endsWith(\",\")) { validJson = false; }*/ if (validJson == true) { //Begin time on insertion sort sortBegin = System.currentTimeMillis(); //Take json arry and change to java array for (int j = 0; j < inArray.size(); j++) { arr[j] = inArray.getInt(j); } //Insertion sort alogorithm for (int k = 0; k < arr.length; k++) { for (int l = 0; l < arr.length; l++) { if (arr[k] < arr[l]) { temp = arr[l]; arr[l] = arr[k]; arr[k] = temp; } } } // Java array back to JSON array for (int i = 0; i < arr.length; i++) { outArrayBuilder.add(arr[i]); } //End time on sort sortEnd = System.currentTimeMillis(); // Total time that it took to sort the JSON timeSorted = sortEnd - sortBegin; //Set response content type to be JSON response.setContentType(\"application/json\"); //Send back the response JSON message PrintWriter out = response.getWriter(); out.print(\"{ \" + \"\\\"outList\\\" : \" + outArrayBuilder.build().toString() + \",\"); out.print(\"\\\"Algorithm\\\" : \" + \"\\\"Insertion Sort\\\"\" + \",\"); out.print(\"\\\"timeMS\\\" : \" + \"\\\"\"+ timeSorted + \"\\\"\" + \"}\"); } if(validJson == false){ int testTime = 4; response.setContentType(\"application/json\"); //Send back the response JSON message PrintWriter out = response.getWriter(); out.print(\"{ \" + \"\\\"outList\\\" : \" + outArrayBuilder.build().toString() + \",\"); out.print(\"\\\"Algorithm\\\" : \" + \"\\\"Insertion Sort\\\"\" + \",\"); out.print(\"\\\"timeMS\\\" : \" + \"\\\"\"+ testTime + \"\\\"\" + \"}\"); } } // Standard Servlet method public void destroy() { // Do any required tear-down here, likely nothing. } }


