Loading Java_MarkLogic_Template/MarkLogic_Template/src/main/java/edu/psu/MarkLogic/demo/controller/MLRestController.java +52 −1 Original line number Diff line number Diff line Loading @@ -162,10 +162,61 @@ public class MLRestController { @GetMapping(value = "/restQueryDocShow") public String restQueryDocShow() { return "restQueryDoc"; } @PostMapping(value = "/restCTSQueryDocShow") @GetMapping(value = "/restCTSQueryDocShow") public String restCTSQueryDocShow() { return "restCTSQueryDoc"; } @PostMapping(value = "/restCTSQueryDoc") public ResponseEntity<ArrayList<ConfidenceFitness>> restQueryCTSDocShow(@RequestParam("cts_query_string") String cts_query_string) { byte[] ba = null; ArrayList<ConfidenceFitness> alcf = new ArrayList<>(); cts_query_string = cts_query_string.replace(" ", "%20"); // a space is changed to the HTML code %20 to prevent error JsonNode actualObj = null; try { CredentialsProvider provider = new BasicCredentialsProvider(); Credentials credentials = new UsernamePasswordCredentials("admin", "admin"); // provide username & password provider.setCredentials(AuthScope.ANY, credentials); HttpClient httpClient = HttpClientBuilder.create().setDefaultCredentialsProvider(provider).build(); String uri = "http://192.168.56.200:8000/LATEST/search?q=" + cts_query_string; // provide the REST API URI URI my_uri = new URI(uri); HttpUriRequest request = RequestBuilder.get() .setUri(my_uri) .setHeader(HttpHeaders.ACCEPT, "application/json") .build(); HttpResponse response = httpClient.execute(request); System.out.println(response.toString()); ByteArrayOutputStream baos = new ByteArrayOutputStream(); response.getEntity().writeTo(baos); ba = baos.toByteArray(); // provide the code to get byte[] String content = IOUtils.toString(ba, "UTF-8"); // please provide code using IOUtils.toString method to get content (how?) System.out.println("content: " + content); ObjectMapper mapper = new ObjectMapper(); actualObj = mapper.readTree(content); if (actualObj.get("results") != null) { JsonNode doc_array = actualObj.get("results"); for (JsonNode jn : doc_array) { ConfidenceFitness cf = new ConfidenceFitness(); cf.setUri(jn.get("uri").asText()); // provide code to complete the content for the cf object cf.setConfidence(jn.get("confidence").asDouble()); cf.setFitness(jn.get("fitness").asDouble()); //System.out.println(cf.getUri() + "\n" + cf.getConfidence() + "\n" + cf.getFitness()); alcf.add(cf); } } } catch (IOException | URISyntaxException ex) { Logger.getLogger(MLRestController.class.getName()).log(Level.SEVERE, null, ex); } return ResponseEntity.accepted().body(alcf); } @GetMapping(value = "/restQueryDoc", produces = MediaType.APPLICATION_JSON_VALUE) Loading Loading
Java_MarkLogic_Template/MarkLogic_Template/src/main/java/edu/psu/MarkLogic/demo/controller/MLRestController.java +52 −1 Original line number Diff line number Diff line Loading @@ -162,10 +162,61 @@ public class MLRestController { @GetMapping(value = "/restQueryDocShow") public String restQueryDocShow() { return "restQueryDoc"; } @PostMapping(value = "/restCTSQueryDocShow") @GetMapping(value = "/restCTSQueryDocShow") public String restCTSQueryDocShow() { return "restCTSQueryDoc"; } @PostMapping(value = "/restCTSQueryDoc") public ResponseEntity<ArrayList<ConfidenceFitness>> restQueryCTSDocShow(@RequestParam("cts_query_string") String cts_query_string) { byte[] ba = null; ArrayList<ConfidenceFitness> alcf = new ArrayList<>(); cts_query_string = cts_query_string.replace(" ", "%20"); // a space is changed to the HTML code %20 to prevent error JsonNode actualObj = null; try { CredentialsProvider provider = new BasicCredentialsProvider(); Credentials credentials = new UsernamePasswordCredentials("admin", "admin"); // provide username & password provider.setCredentials(AuthScope.ANY, credentials); HttpClient httpClient = HttpClientBuilder.create().setDefaultCredentialsProvider(provider).build(); String uri = "http://192.168.56.200:8000/LATEST/search?q=" + cts_query_string; // provide the REST API URI URI my_uri = new URI(uri); HttpUriRequest request = RequestBuilder.get() .setUri(my_uri) .setHeader(HttpHeaders.ACCEPT, "application/json") .build(); HttpResponse response = httpClient.execute(request); System.out.println(response.toString()); ByteArrayOutputStream baos = new ByteArrayOutputStream(); response.getEntity().writeTo(baos); ba = baos.toByteArray(); // provide the code to get byte[] String content = IOUtils.toString(ba, "UTF-8"); // please provide code using IOUtils.toString method to get content (how?) System.out.println("content: " + content); ObjectMapper mapper = new ObjectMapper(); actualObj = mapper.readTree(content); if (actualObj.get("results") != null) { JsonNode doc_array = actualObj.get("results"); for (JsonNode jn : doc_array) { ConfidenceFitness cf = new ConfidenceFitness(); cf.setUri(jn.get("uri").asText()); // provide code to complete the content for the cf object cf.setConfidence(jn.get("confidence").asDouble()); cf.setFitness(jn.get("fitness").asDouble()); //System.out.println(cf.getUri() + "\n" + cf.getConfidence() + "\n" + cf.getFitness()); alcf.add(cf); } } } catch (IOException | URISyntaxException ex) { Logger.getLogger(MLRestController.class.getName()).log(Level.SEVERE, null, ex); } return ResponseEntity.accepted().body(alcf); } @GetMapping(value = "/restQueryDoc", produces = MediaType.APPLICATION_JSON_VALUE) Loading