Skip to content

Commit

Permalink
Add application stack filter to PicsureInfoService
Browse files Browse the repository at this point in the history
Updated PicsureInfoService to restrict resources based on application stack. If the application stack configuration value exists and is not blank, resources will be filtered for that stack only. JAXRSConfiguration updated to initialize application stack. This helps when there is more than one application stack.
  • Loading branch information
Gcolon021 committed Mar 7, 2024
1 parent 68f7ac7 commit f85636a
Show file tree
Hide file tree
Showing 2 changed files with 118 additions and 89 deletions.
Original file line number Diff line number Diff line change
@@ -1,45 +1,60 @@
package edu.harvard.dbmi.avillach;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.annotation.PostConstruct;
import javax.ejb.Startup;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;

@Startup
@ApplicationPath("PICSURE")
public class JAXRSConfiguration extends Application {

private Logger logger = LoggerFactory.getLogger(JAXRSConfiguration.class);

public static String rolesClaim;

@PostConstruct
public void init() {
logger.info("Starting pic-sure core app.");

logger.info("Initializing roles claim.");
initializeRolesClaim();
logger.info("Finished initializing roles claim.");

}

private void initializeRolesClaim(){
try{
Context ctx = new InitialContext();
rolesClaim = (String) ctx.lookup("global/roles_claim");
ctx.close();
} catch (NamingException e) {
rolesClaim = "privileges";
}
}

public JAXRSConfiguration(){
}

}
package edu.harvard.dbmi.avillach;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.annotation.PostConstruct;
import javax.ejb.Startup;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;

@Startup
@ApplicationPath("PICSURE")
public class JAXRSConfiguration extends Application {

private Logger logger = LoggerFactory.getLogger(JAXRSConfiguration.class);

public static String rolesClaim;

public static String application_stack;

@PostConstruct
public void init() {
logger.info("Starting pic-sure core app.");

logger.info("Initializing roles claim.");
initializeRolesClaim();
logger.info("Finished initializing roles claim.");

logger.info("Load optional properties.");
initializeApplicationStack();
logger.info("Finished loading optional properties.");
}

private void initializeApplicationStack() {
try {
Context ctx = new InitialContext();
application_stack = (String) ctx.lookup("global/application_stack");
ctx.close();
} catch (NamingException e) {
// Currently, this parameter is optional and only used if there is more than one application stack.
application_stack = null;
}
}

private void initializeRolesClaim() {
try {
Context ctx = new InitialContext();
rolesClaim = (String) ctx.lookup("global/roles_claim");
ctx.close();
} catch (NamingException e) {
rolesClaim = "privileges";
}
}

public JAXRSConfiguration() {}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package edu.harvard.dbmi.avillach.service;

import com.fasterxml.jackson.databind.ObjectMapper;
import edu.harvard.dbmi.avillach.JAXRSConfiguration;
import edu.harvard.dbmi.avillach.data.entity.Resource;
import edu.harvard.dbmi.avillach.data.repository.ResourceRepository;
import edu.harvard.dbmi.avillach.domain.GeneralQueryRequest;
Expand All @@ -9,6 +10,7 @@
import edu.harvard.dbmi.avillach.util.Utilities;
import edu.harvard.dbmi.avillach.util.exception.ApplicationException;
import edu.harvard.dbmi.avillach.util.exception.ProtocolException;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -21,55 +23,67 @@

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));

// We need a way to only return resources for this stack.
// Going to create an optional property in the standalone.xml
if (StringUtils.isNotBlank(JAXRSConfiguration.application_stack)) {
// The resource resourceRSPath will contain the stack value
// http://dictionary.b.${env_private_dns_name}:8080/dictionary/pic-sure
// The application stack is a, b, c, etc.
// I will look for %.${application_stack}.% in the url
return resourceRepo.list().stream()
.filter(resource -> resource.getResourceRSPath().contains("." + JAXRSConfiguration.application_stack + "."))
.collect(Collectors.toMap(Resource::getUuid, Resource::getName));
}

return resourceRepo.list().stream().collect(Collectors.toMap(Resource::getUuid, Resource::getName));
}
}

0 comments on commit f85636a

Please sign in to comment.