@Test public void testGetJsonPayload() { String payload = ""; String url = "https: String returnedPayload = getPayloadServiceImpl.getJsonPayload(url, payload); assertEquals(returnedPayload, ""); url = "http: returnedPayload = getPayloadServiceImpl.getJsonPayload(url, payload); assertNotEquals(returnedPayload, ""); url = "http: Map<String, Object> root = new HashMap<>(); List <Object>images = new ArrayList<>(); Map<String, Object> imageRoot = new HashMap<>(); images.add(imageRoot); root.put("images", images); imageRoot.put("imageURI", "https: List <Object>positions = new ArrayList<>(); positions.add(0); imageRoot.put("positions", positions); imageRoot.put("width", 1024); imageRoot.put("height", 768); payload = new Gson().toJson(root); returnedPayload = getPayloadServiceImpl.getJsonPayload(url, payload); LOG.info(returnedPayload); url = "http: imageRoot.put("imageURI", "https: positions.add(3); Map<String, Object> imageRoot2 = new HashMap<>(); images.add(imageRoot2); imageRoot2.put("imageURI", "https: List <Object> positions2 = new ArrayList<>(); positions2.add(278); imageRoot2.put("positions", positions2); imageRoot2.put("width", 1024); imageRoot2.put("height", 768); payload = new Gson().toJson(root); returnedPayload = getPayloadServiceImpl.getJsonPayload(url, payload); LOG.info(returnedPayload); }
@Override public String getJsonPayload(String url, String payload){ try { HttpPost request = new HttpPost(url); StringEntity params = new StringEntity(payload); LOG.info(payload); request.addHeader("content-type", "application/json; charset=utf-8"); request.setEntity(params); HttpResponse response = httpClient.execute(request); if (response.getStatusLine().getStatusCode() != 200) { throw new CoordinatePayloadException("Failed : HTTP error code : " + response.getStatusLine().getStatusCode()); } InputStreamReader isr = new InputStreamReader(response.getEntity().getContent()); BufferedReader br = new BufferedReader(isr); StringBuilder sb = new StringBuilder(); String line; while ((line = br.readLine()) != null) { sb.append(line); } isr.close(); LOG.info("JsonPayload from getJsonPayload is " + sb.toString()); return sb.toString(); }catch (Exception ex) { LOG.error("Exception getting post for " + url, ex); } return ""; }
GetPayloadServiceImpl implements GetPayloadService { @Override public String getJsonPayload(String url, String payload){ try { HttpPost request = new HttpPost(url); StringEntity params = new StringEntity(payload); LOG.info(payload); request.addHeader("content-type", "application/json; charset=utf-8"); request.setEntity(params); HttpResponse response = httpClient.execute(request); if (response.getStatusLine().getStatusCode() != 200) { throw new CoordinatePayloadException("Failed : HTTP error code : " + response.getStatusLine().getStatusCode()); } InputStreamReader isr = new InputStreamReader(response.getEntity().getContent()); BufferedReader br = new BufferedReader(isr); StringBuilder sb = new StringBuilder(); String line; while ((line = br.readLine()) != null) { sb.append(line); } isr.close(); LOG.info("JsonPayload from getJsonPayload is " + sb.toString()); return sb.toString(); }catch (Exception ex) { LOG.error("Exception getting post for " + url, ex); } return ""; } }
GetPayloadServiceImpl implements GetPayloadService { @Override public String getJsonPayload(String url, String payload){ try { HttpPost request = new HttpPost(url); StringEntity params = new StringEntity(payload); LOG.info(payload); request.addHeader("content-type", "application/json; charset=utf-8"); request.setEntity(params); HttpResponse response = httpClient.execute(request); if (response.getStatusLine().getStatusCode() != 200) { throw new CoordinatePayloadException("Failed : HTTP error code : " + response.getStatusLine().getStatusCode()); } InputStreamReader isr = new InputStreamReader(response.getEntity().getContent()); BufferedReader br = new BufferedReader(isr); StringBuilder sb = new StringBuilder(); String line; while ((line = br.readLine()) != null) { sb.append(line); } isr.close(); LOG.info("JsonPayload from getJsonPayload is " + sb.toString()); return sb.toString(); }catch (Exception ex) { LOG.error("Exception getting post for " + url, ex); } return ""; } GetPayloadServiceImpl(); }
GetPayloadServiceImpl implements GetPayloadService { @Override public String getJsonPayload(String url, String payload){ try { HttpPost request = new HttpPost(url); StringEntity params = new StringEntity(payload); LOG.info(payload); request.addHeader("content-type", "application/json; charset=utf-8"); request.setEntity(params); HttpResponse response = httpClient.execute(request); if (response.getStatusLine().getStatusCode() != 200) { throw new CoordinatePayloadException("Failed : HTTP error code : " + response.getStatusLine().getStatusCode()); } InputStreamReader isr = new InputStreamReader(response.getEntity().getContent()); BufferedReader br = new BufferedReader(isr); StringBuilder sb = new StringBuilder(); String line; while ((line = br.readLine()) != null) { sb.append(line); } isr.close(); LOG.info("JsonPayload from getJsonPayload is " + sb.toString()); return sb.toString(); }catch (Exception ex) { LOG.error("Exception getting post for " + url, ex); } return ""; } GetPayloadServiceImpl(); @Override String getJsonPayload(String url, String payload); }
GetPayloadServiceImpl implements GetPayloadService { @Override public String getJsonPayload(String url, String payload){ try { HttpPost request = new HttpPost(url); StringEntity params = new StringEntity(payload); LOG.info(payload); request.addHeader("content-type", "application/json; charset=utf-8"); request.setEntity(params); HttpResponse response = httpClient.execute(request); if (response.getStatusLine().getStatusCode() != 200) { throw new CoordinatePayloadException("Failed : HTTP error code : " + response.getStatusLine().getStatusCode()); } InputStreamReader isr = new InputStreamReader(response.getEntity().getContent()); BufferedReader br = new BufferedReader(isr); StringBuilder sb = new StringBuilder(); String line; while ((line = br.readLine()) != null) { sb.append(line); } isr.close(); LOG.info("JsonPayload from getJsonPayload is " + sb.toString()); return sb.toString(); }catch (Exception ex) { LOG.error("Exception getting post for " + url, ex); } return ""; } GetPayloadServiceImpl(); @Override String getJsonPayload(String url, String payload); static final String SERVICE_NAME; }