Skip to content

Commit

Permalink
ALS-5387: Remove stack from resources table (#186)
Browse files Browse the repository at this point in the history
* [ALS-5422] Add persistence.xml to visualization resource (#161) (#162)
The visualization resource is failing to start due to an error injecting persistence unit into CDI managed bean. It is unable to find a persistence unit named ''.

* ALS-5387: Remove stack from resources table
* ALS-5387: More robust implementation of feature

---------

Co-authored-by: Gcolon021 <[email protected]>
Co-authored-by: GeorgeC <[email protected]>
  • Loading branch information
3 people authored Mar 12, 2024
1 parent 68f7ac7 commit 6c716b3
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 47 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package edu.harvard.dbmi.avillach.data.entity;

import java.io.StringReader;
import java.util.Optional;

import javax.json.Json;
import javax.json.JsonObject;
import javax.json.JsonReader;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.*;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
Expand All @@ -18,6 +18,9 @@ public class Resource extends BaseEntity{
@Column(length = 8192)
private String description;
private String targetURL;


@Convert(converter = ResourcePathConverter.class)
private String resourceRSPath;

@Column(length = 8192)
Expand Down Expand Up @@ -102,4 +105,31 @@ public String toString() {
.add("metadata", metadataObj)
.build().toString();
}

/**
* This resource path converter allows resource paths to contain a reference to a specific stack that is
* imputed at runtime based an an environment parameter. This allows multiple stacks to share the same database.
*
* The ___target_stack___ token in any resource path value will be replaced with the TARGET_STACK environment variable
*/
@Converter
public static class ResourcePathConverter implements AttributeConverter<String, String> {

public ResourcePathConverter() {
}

private static final Optional<String> targetStack = Optional.ofNullable(System.getProperty("TARGET_STACK", null));

@Override
public String convertToDatabaseColumn(String attribute) {
return attribute;
}

@Override
public String convertToEntityAttribute(String dbData) {
return targetStack
.map(stack -> dbData.replace("___target_stack___", stack))
.orElse(dbData);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ protected ResourceRepository() {
super(Resource.class);
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -21,55 +21,55 @@

public class PicsureInfoService {

private final Logger logger = LoggerFactory.getLogger(PicsureQueryService.class);
private final Logger logger = LoggerFactory.getLogger(PicsureQueryService.class);

private final static ObjectMapper mapper = new ObjectMapper();
private final static ObjectMapper mapper = new ObjectMapper();

@Inject
ResourceRepository resourceRepo;
@Inject
ResourceRepository resourceRepo;

@Inject
ResourceWebClient resourceWebClient;
@Inject
ResourceWebClient resourceWebClient;

/**
* Retrieve resource info for a specific resource.
*
* @param resourceId - Resource UUID
* @param credentialsQueryRequest - Contains resource specific credentials map
* @return a {@link edu.harvard.dbmi.avillach.domain.ResourceInfo ResourceInfo}
*/
public ResourceInfo info(UUID resourceId, QueryRequest credentialsQueryRequest, HttpHeaders headers) {
Resource resource = resourceRepo.getById(resourceId);
if (resource == null){
throw new ProtocolException(ProtocolException.RESOURCE_NOT_FOUND + resourceId.toString());
}
if (resource.getResourceRSPath() == null){
throw new ApplicationException(ApplicationException.MISSING_RESOURCE_PATH);
}
if (credentialsQueryRequest == null){
credentialsQueryRequest = new GeneralQueryRequest();
}
if (credentialsQueryRequest.getResourceCredentials() == null){
credentialsQueryRequest.setResourceCredentials(new HashMap<String, String>());
}
/**
* Retrieve resource info for a specific resource.
*
* @param resourceId - Resource UUID
* @param credentialsQueryRequest - Contains resource specific credentials map
* @return a {@link edu.harvard.dbmi.avillach.domain.ResourceInfo ResourceInfo}
*/
public ResourceInfo info(UUID resourceId, QueryRequest credentialsQueryRequest, HttpHeaders headers) {
Resource resource = resourceRepo.getById(resourceId);
if (resource == null) {
throw new ProtocolException(ProtocolException.RESOURCE_NOT_FOUND + resourceId.toString());
}
if (resource.getResourceRSPath() == null) {
throw new ApplicationException(ApplicationException.MISSING_RESOURCE_PATH);
}
if (credentialsQueryRequest == null) {
credentialsQueryRequest = new GeneralQueryRequest();
}
if (credentialsQueryRequest.getResourceCredentials() == null) {
credentialsQueryRequest.setResourceCredentials(new HashMap<String, String>());
}

logger.info("path=/info/{resourceId}, resourceId={}, requestSource={}, credentialsQueryRequest={}",
resourceId,
Utilities.getRequestSourceFromHeader(headers),
Utilities.convertQueryRequestToString(mapper, credentialsQueryRequest)
);
logger.info(
"path=/info/{resourceId}, resourceId={}, requestSource={}, credentialsQueryRequest={}", resourceId,
Utilities.getRequestSourceFromHeader(headers), Utilities.convertQueryRequestToString(mapper, credentialsQueryRequest)
);

credentialsQueryRequest.getResourceCredentials().put(ResourceWebClient.BEARER_TOKEN_KEY, resource.getToken());
return resourceWebClient.info(resource.getResourceRSPath(), credentialsQueryRequest);
}
credentialsQueryRequest.getResourceCredentials().put(ResourceWebClient.BEARER_TOKEN_KEY, resource.getToken());
return resourceWebClient.info(resource.getResourceRSPath(), credentialsQueryRequest);
}

/**
* Retrieve a list of all available resources.
*
* @return List containing limited metadata about all available resources and ids.
*/
public Map<UUID, String> resources(HttpHeaders headers) {
logger.info("path=/info/resources, requestSource={}", Utilities.getRequestSourceFromHeader(headers));
return resourceRepo.list().stream().collect(Collectors.toMap(Resource::getUuid, Resource::getName));
}
/**
* Retrieve a list of all available resources.
*
* @return List containing limited metadata about all available resources and ids.
*/
public Map<UUID, String> resources(HttpHeaders headers) {
logger.info("path=/info/resources, requestSource={}", Utilities.getRequestSourceFromHeader(headers));
return resourceRepo.list().stream().filter(resource -> !resource.getHidden())
.collect(Collectors.toMap(Resource::getUuid, Resource::getName));
}
}

0 comments on commit 6c716b3

Please sign in to comment.