Skip to content

Commit

Permalink
Added integration with Matchminer API
Browse files Browse the repository at this point in the history
Added integration with Matchminer API
  • Loading branch information
jagnathan committed Dec 19, 2023
1 parent 429445f commit a6f37f8
Showing 1 changed file with 26 additions and 10 deletions.
36 changes: 26 additions & 10 deletions web/src/main/java/org/cbioportal/web/MatchMinerController.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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<Object> 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");
Expand All @@ -51,19 +56,30 @@ public ResponseEntity<Object> 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<String> 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<String> 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);
}
}
}
}

0 comments on commit a6f37f8

Please sign in to comment.