@Test public void testIOException() throws Exception { Annotator annotator = mock(Annotator.class); RouterNanoHTTPD.UriResource uriResource = mock(RouterNanoHTTPD.UriResource.class); when(uriResource.initParameter(Annotator.class)).thenReturn(annotator); NanoHTTPD.IHTTPSession session = mock(NanoHTTPD.IHTTPSession.class); doThrow(new IOException()).when(session).parseBody(anyMap()); InstanceController controller = new InstanceController(); NanoHTTPD.Response response = controller.post(uriResource,null,session); assertEquals(NanoHTTPD.Response.Status.BAD_REQUEST, response.getStatus()); String body = IOUtils.toString(response.getData()); assertTrue(body.startsWith("Error reading request")); }
@Override public Response post(RouterNanoHTTPD.UriResource uriResource, Map<String, String> map, NanoHTTPD.IHTTPSession session) { Annotator annotator = uriResource.initParameter(Annotator.class); JsonArray jInstances; JsonParser parser = new JsonParser(); try{ String body = readBody(session); jInstances = parser.parse(body).getAsJsonArray(); } catch (Exception ex){ return ResponseGenerator.generateResponse(String.format("Error reading request: %s", ex.toString()), Response.Status.BAD_REQUEST); } TextAnnotation[] textAnnotations = new TextAnnotation[jInstances.size()]; String[] errors = new String[textAnnotations.length]; parseTextAnnotations(jInstances, textAnnotations, errors); annotateInstances(annotator, textAnnotations, errors); JsonArray newJInstances = serializeInstances(parser, textAnnotations, errors); return NanoHTTPD.newFixedLengthResponse(newJInstances.toString()); }
InstanceController implements RouterNanoHTTPD.UriResponder { @Override public Response post(RouterNanoHTTPD.UriResource uriResource, Map<String, String> map, NanoHTTPD.IHTTPSession session) { Annotator annotator = uriResource.initParameter(Annotator.class); JsonArray jInstances; JsonParser parser = new JsonParser(); try{ String body = readBody(session); jInstances = parser.parse(body).getAsJsonArray(); } catch (Exception ex){ return ResponseGenerator.generateResponse(String.format("Error reading request: %s", ex.toString()), Response.Status.BAD_REQUEST); } TextAnnotation[] textAnnotations = new TextAnnotation[jInstances.size()]; String[] errors = new String[textAnnotations.length]; parseTextAnnotations(jInstances, textAnnotations, errors); annotateInstances(annotator, textAnnotations, errors); JsonArray newJInstances = serializeInstances(parser, textAnnotations, errors); return NanoHTTPD.newFixedLengthResponse(newJInstances.toString()); } }
InstanceController implements RouterNanoHTTPD.UriResponder { @Override public Response post(RouterNanoHTTPD.UriResource uriResource, Map<String, String> map, NanoHTTPD.IHTTPSession session) { Annotator annotator = uriResource.initParameter(Annotator.class); JsonArray jInstances; JsonParser parser = new JsonParser(); try{ String body = readBody(session); jInstances = parser.parse(body).getAsJsonArray(); } catch (Exception ex){ return ResponseGenerator.generateResponse(String.format("Error reading request: %s", ex.toString()), Response.Status.BAD_REQUEST); } TextAnnotation[] textAnnotations = new TextAnnotation[jInstances.size()]; String[] errors = new String[textAnnotations.length]; parseTextAnnotations(jInstances, textAnnotations, errors); annotateInstances(annotator, textAnnotations, errors); JsonArray newJInstances = serializeInstances(parser, textAnnotations, errors); return NanoHTTPD.newFixedLengthResponse(newJInstances.toString()); }  }
InstanceController implements RouterNanoHTTPD.UriResponder { @Override public Response post(RouterNanoHTTPD.UriResource uriResource, Map<String, String> map, NanoHTTPD.IHTTPSession session) { Annotator annotator = uriResource.initParameter(Annotator.class); JsonArray jInstances; JsonParser parser = new JsonParser(); try{ String body = readBody(session); jInstances = parser.parse(body).getAsJsonArray(); } catch (Exception ex){ return ResponseGenerator.generateResponse(String.format("Error reading request: %s", ex.toString()), Response.Status.BAD_REQUEST); } TextAnnotation[] textAnnotations = new TextAnnotation[jInstances.size()]; String[] errors = new String[textAnnotations.length]; parseTextAnnotations(jInstances, textAnnotations, errors); annotateInstances(annotator, textAnnotations, errors); JsonArray newJInstances = serializeInstances(parser, textAnnotations, errors); return NanoHTTPD.newFixedLengthResponse(newJInstances.toString()); }  @Override Response post(RouterNanoHTTPD.UriResource uriResource, Map<String, String> map, NanoHTTPD.IHTTPSession session); @Override Response get(RouterNanoHTTPD.UriResource uriResource, Map<String, String> map, NanoHTTPD.IHTTPSession ihttpSession); @Override Response put(RouterNanoHTTPD.UriResource uriResource, Map<String, String> map, NanoHTTPD.IHTTPSession ihttpSession); @Override Response delete(RouterNanoHTTPD.UriResource uriResource, Map<String, String> map, NanoHTTPD.IHTTPSession ihttpSession); @Override Response other(String s, RouterNanoHTTPD.UriResource uriResource, Map<String, String> map, NanoHTTPD.IHTTPSession ihttpSession); }
InstanceController implements RouterNanoHTTPD.UriResponder { @Override public Response post(RouterNanoHTTPD.UriResource uriResource, Map<String, String> map, NanoHTTPD.IHTTPSession session) { Annotator annotator = uriResource.initParameter(Annotator.class); JsonArray jInstances; JsonParser parser = new JsonParser(); try{ String body = readBody(session); jInstances = parser.parse(body).getAsJsonArray(); } catch (Exception ex){ return ResponseGenerator.generateResponse(String.format("Error reading request: %s", ex.toString()), Response.Status.BAD_REQUEST); } TextAnnotation[] textAnnotations = new TextAnnotation[jInstances.size()]; String[] errors = new String[textAnnotations.length]; parseTextAnnotations(jInstances, textAnnotations, errors); annotateInstances(annotator, textAnnotations, errors); JsonArray newJInstances = serializeInstances(parser, textAnnotations, errors); return NanoHTTPD.newFixedLengthResponse(newJInstances.toString()); }  @Override Response post(RouterNanoHTTPD.UriResource uriResource, Map<String, String> map, NanoHTTPD.IHTTPSession session); @Override Response get(RouterNanoHTTPD.UriResource uriResource, Map<String, String> map, NanoHTTPD.IHTTPSession ihttpSession); @Override Response put(RouterNanoHTTPD.UriResource uriResource, Map<String, String> map, NanoHTTPD.IHTTPSession ihttpSession); @Override Response delete(RouterNanoHTTPD.UriResource uriResource, Map<String, String> map, NanoHTTPD.IHTTPSession ihttpSession); @Override Response other(String s, RouterNanoHTTPD.UriResource uriResource, Map<String, String> map, NanoHTTPD.IHTTPSession ihttpSession);  }