From a6f37f88a13681fad6107b9c0e98408439baa7ec Mon Sep 17 00:00:00 2001 From: Prasanna Kumar Jagannathan <37613906+jagnathan@users.noreply.github.com> Date: Tue, 19 Dec 2023 13:35:36 -0500 Subject: [PATCH] Added integration with Matchminer API Added integration with Matchminer API --- .../cbioportal/web/MatchMinerController.java | 36 +++++++++++++------ 1 file changed, 26 insertions(+), 10 deletions(-) diff --git a/web/src/main/java/org/cbioportal/web/MatchMinerController.java b/web/src/main/java/org/cbioportal/web/MatchMinerController.java index 0217b565e75..634ed951817 100644 --- a/web/src/main/java/org/cbioportal/web/MatchMinerController.java +++ b/web/src/main/java/org/cbioportal/web/MatchMinerController.java @@ -12,11 +12,14 @@ import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.client.RestClientException; import org.springframework.web.client.RestTemplate; import javax.servlet.http.HttpServletRequest; +import java.io.UnsupportedEncodingException; import java.net.URI; import java.net.URISyntaxException; +import java.net.URLEncoder; import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; @@ -32,13 +35,15 @@ public class MatchMinerController { @Value("${matchminer.token:}") private String token; - @RequestMapping(value = "/**", produces = "application/json") + @RequestMapping(value = "/api/trial_match", produces = "application/json") public ResponseEntity proxy(@RequestBody(required = false) JSONObject body, HttpMethod method, HttpServletRequest request) { try { String path = request.getRequestURI(); + String querystring = request.getQueryString(); int mmindex = path.indexOf("/matchminer"); String requestpath = path.substring(mmindex+11); - URI uri = new URI(this.url + requestpath); + URI uri = new URI(this.url + requestpath + "?" +URLEncoder.encode(querystring,"UTF-8")); + uri = new URI(this.url + requestpath + "?" +querystring); HttpHeaders httpHeaders = new HttpHeaders(); String contentType = request.getHeader("Content-Type"); @@ -51,19 +56,30 @@ public ResponseEntity proxy(@RequestBody(required = false) JSONObject bo String authHeader = "Basic " + new String(encodedAuth); httpHeaders.set("Authorization", authHeader); } - - RestTemplate restTemplate = new RestTemplate(); - restTemplate.getMessageConverters().add(0, new StringHttpMessageConverter(StandardCharsets.UTF_8)); - ResponseEntity responseEntity = restTemplate.exchange(uri, method, new HttpEntity<>(body, httpHeaders), String.class); - // The response might be a json object or a json array, so I return Object to cover both cases. - Object response = new JSONParser().parse(responseEntity.getBody()); - return new ResponseEntity<>(response, responseEntity.getStatusCode()); + if (this.url != "") { + RestTemplate restTemplate = new RestTemplate(); + restTemplate.getMessageConverters().add(0, new StringHttpMessageConverter(StandardCharsets.UTF_8)); + ResponseEntity responseEntity = restTemplate.exchange(uri, method, new HttpEntity<>(body, httpHeaders), String.class); + // The response might be a json object or a json array, so I return Object to cover both cases. + HttpStatus responseStatus = responseEntity.getStatusCode(); + if (responseStatus.is2xxSuccessful()) { + Object response = new JSONParser().parse(responseEntity.getBody()); + return new ResponseEntity<>(response, responseEntity.getStatusCode()); + } + } + return new ResponseEntity<>(HttpStatus.NO_CONTENT); + } catch(RestClientException e) { + LOG.error("Error occurred", e); + return new ResponseEntity<>(HttpStatus.BAD_REQUEST); + } catch (UnsupportedEncodingException e) { + LOG.error("Error occurred", e); + return new ResponseEntity<>(HttpStatus.BAD_REQUEST); } catch (URISyntaxException e) { LOG.error("Error occurred", e); return new ResponseEntity<>(HttpStatus.BAD_REQUEST); } catch (ParseException e) { LOG.error("Error occurred", e); return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); - } + } } }