Skip to content

Commit

Permalink
Extract http calls to the session service to the handler
Browse files Browse the repository at this point in the history
  • Loading branch information
forus committed Jun 27, 2024
1 parent a5e3c21 commit 542aab9
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,32 @@


import java.nio.charset.Charset;
import java.util.Collections;
import java.util.List;

import com.mongodb.BasicDBObject;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang3.StringUtils;
import org.cbioportal.web.PublicVirtualStudiesController;
import org.cbioportal.web.parameter.VirtualStudy;
import org.cbioportal.web.parameter.VirtualStudyData;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatusCode;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

@Component
public class SessionServiceRequestHandler {

private static final Logger LOG = LoggerFactory.getLogger(SessionServiceRequestHandler.class);

@Value("${session.service.url:}")
private String sessionServiceURL;

Expand Down Expand Up @@ -62,4 +74,68 @@ public String getSessionDataJson(SessionType type, String id) throws Exception {
return responseEntity.getBody();
}

/**
* Gets virtual study by id
* @param id - id of the virtual study to read
* @return virtual study
*/
public VirtualStudy getVirtualStudyById(String id) {
ResponseEntity<VirtualStudy> responseEntity = new RestTemplate()
.exchange(sessionServiceURL + "/virtual_study/" + id,
HttpMethod.GET,
new HttpEntity<>(getHttpHeaders()),
VirtualStudy.class);
HttpStatusCode statusCode = responseEntity.getStatusCode();
VirtualStudy virtualStudy = responseEntity.getBody();
if (!statusCode.is2xxSuccessful() || virtualStudy == null) {
LOG.error("The downstream server replied with statusCode={} and body={}." +
" Replying with the same status code to the client.",
statusCode, virtualStudy);
throw new IllegalStateException("The downstream server response is not successful");
}
return responseEntity.getBody();
}

/**
* Get list of virtual studies by username
* @param username - user assigned to the virtual study
* @return - list of virtual studies
*/
public List<VirtualStudy> getVirtualStudiesForUser(String username) {
BasicDBObject basicDBObject = new BasicDBObject();
basicDBObject.put("data.users", username);
ResponseEntity<List<VirtualStudy>> responseEntity = new RestTemplate().exchange(
sessionServiceURL + "/virtual_study/query/fetch",
HttpMethod.POST,
new HttpEntity<>(basicDBObject.toString(), getHttpHeaders()),
new ParameterizedTypeReference<>() {
});

return responseEntity.getBody();
}

/**
* Creates a virtual study out of virtual study definition (aka virtualStudyData)
* @param virtualStudyData - definition of virtual study
* @return virtual study object with id and the virtualStudyData
*/
public VirtualStudy createVirtualStudy(VirtualStudyData virtualStudyData) {
ResponseEntity<VirtualStudy> responseEntity = new RestTemplate().exchange(
sessionServiceURL + "/virtual_study",
HttpMethod.POST,
new HttpEntity<>(virtualStudyData, getHttpHeaders()),
new ParameterizedTypeReference<>() {
});

return responseEntity.getBody();
}

public void softRemoveVirtualStudy(String id) {
VirtualStudy virtualStudy = getVirtualStudyById(id);
VirtualStudyData data = virtualStudy.getData();
data.setUsers(Collections.emptySet());
new RestTemplate()
.put(sessionServiceURL + "/virtual_study/" + id,
new HttpEntity<>(data, getHttpHeaders()));
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.cbioportal.web;

import com.mongodb.BasicDBObject;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
Expand All @@ -13,11 +12,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.HttpStatusCode;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.DeleteMapping;
Expand All @@ -28,10 +23,8 @@
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.server.ResponseStatusException;

import java.util.Collections;
import java.util.List;
import java.util.Set;

Expand All @@ -47,36 +40,22 @@ public class PublicVirtualStudiesController {

private final SessionServiceRequestHandler sessionServiceRequestHandler;

private final String sessionServiceURL;

private final CancerTypeService cancerTypeService;

public PublicVirtualStudiesController(
@Value("${session.endpoint.publisher-api-key:}") String requiredPublisherApiKey,
SessionServiceRequestHandler sessionServiceRequestHandler,
@Value("${session.service.url:}") String sessionServiceURL,
CancerTypeService cancerTypeService
) {
this.requiredPublisherApiKey = requiredPublisherApiKey;
this.sessionServiceRequestHandler = sessionServiceRequestHandler;
this.sessionServiceURL = sessionServiceURL;
this.cancerTypeService = cancerTypeService;
}

@GetMapping
@ApiResponse(responseCode = "200", description = "OK", content = @Content(schema = @Schema(implementation = VirtualStudy.class)))
public ResponseEntity<List<VirtualStudy>> getPublicVirtualStudies() {
//TODO move this logic to sessionServiceRequestHandler?
BasicDBObject basicDBObject = new BasicDBObject();
basicDBObject.put("data.users", ALL_USERS);
ResponseEntity<List<VirtualStudy>> responseEntity = new RestTemplate().exchange(
sessionServiceURL + "/virtual_study/query/fetch",
HttpMethod.POST,
new HttpEntity<>(basicDBObject.toString(), sessionServiceRequestHandler.getHttpHeaders()),
new ParameterizedTypeReference<>() {
});

List<VirtualStudy> virtualStudies = responseEntity.getBody();
List<VirtualStudy> virtualStudies = sessionServiceRequestHandler.getVirtualStudiesForUser(ALL_USERS);
return new ResponseEntity<>(virtualStudies, HttpStatus.OK);
}

Expand Down Expand Up @@ -105,15 +84,9 @@ public ResponseEntity<VirtualStudy> publishVirtualStudyData(
if (pmid != null) {
virtualStudyDataToPublish.setPmid(pmid);
}
//TODO move this logic to sessionServiceRequestHandler?
ResponseEntity<VirtualStudy> responseEntity = new RestTemplate().exchange(
sessionServiceURL + "/virtual_study",
HttpMethod.POST,
new HttpEntity<>(virtualStudyDataToPublish, sessionServiceRequestHandler.getHttpHeaders()),
new ParameterizedTypeReference<>() {
});
VirtualStudy virtualStudy = sessionServiceRequestHandler.createVirtualStudy(virtualStudyDataToPublish);

return new ResponseEntity<>(responseEntity.getBody(), HttpStatus.OK);
return new ResponseEntity<>(virtualStudy, HttpStatus.OK);
}

@PostMapping("/{id}")
Expand All @@ -128,26 +101,10 @@ public ResponseEntity<VirtualStudy> publishVirtualStudy(
|| !requiredPublisherApiKey.equals(providedPublisherApiKey)) {
throw new AccessForbiddenException("The provided publisher API key is not correct.");
}
ResponseEntity<VirtualStudy> responseEntity = getVirtualStudyById(id);
HttpStatusCode statusCode = responseEntity.getStatusCode();
VirtualStudy virtualStudy = responseEntity.getBody();
if (!statusCode.is2xxSuccessful() || virtualStudy == null) {
LOG.error("The downstream server replied with statusCode={} and body={}." +
" Replying with the same status code to the client.",
statusCode, virtualStudy);
throw new IllegalStateException("The downstream server response is not successful");
}
VirtualStudy virtualStudy = sessionServiceRequestHandler.getVirtualStudyById(id);
return publishVirtualStudyData(virtualStudy.getData(), providedPublisherApiKey, typeOfCancerId, pmid);
}

private ResponseEntity<VirtualStudy> getVirtualStudyById(String id) {
return new RestTemplate()
.exchange(sessionServiceURL + "/virtual_study/" + id,
HttpMethod.GET,
new HttpEntity<>(sessionServiceRequestHandler.getHttpHeaders()),
VirtualStudy.class);
}

@DeleteMapping("/{id}")
@ApiResponse(responseCode = "200", description = "OK")
public ResponseEntity retractVirtualStudy(
Expand All @@ -158,20 +115,7 @@ public ResponseEntity retractVirtualStudy(
|| !requiredPublisherApiKey.equals(providedPublisherApiKey)) {
throw new AccessForbiddenException("The provided publisher API key is not correct.");
}
ResponseEntity<VirtualStudy> responseEntity = getVirtualStudyById(id);
HttpStatusCode statusCode = responseEntity.getStatusCode();
VirtualStudy virtualStudy = responseEntity.getBody();
if (!statusCode.is2xxSuccessful() || virtualStudy == null) {
LOG.error("The downstream server replied with statusCode={} and body={}." +
" Replying with the same status code to the client.",
statusCode, virtualStudy);
throw new IllegalStateException("The downstream server response is not successful");
}
VirtualStudyData data = virtualStudy.getData();
data.setUsers(Collections.emptySet());
new RestTemplate()
.put(sessionServiceURL + "/virtual_study/" + id,
new HttpEntity<>(data, sessionServiceRequestHandler.getHttpHeaders()));
sessionServiceRequestHandler.softRemoveVirtualStudy(id);
return ResponseEntity.ok().build();
}

Expand Down

0 comments on commit 542aab9

Please sign in to comment.