diff --git a/config/disabledAlgorithms.json b/config/disabledAlgorithms.json new file mode 100644 index 0000000000000000000000000000000000000000..d92a3b2f1421d2dad3a140503d1c0effa964bdfb --- /dev/null +++ b/config/disabledAlgorithms.json @@ -0,0 +1,3 @@ +[ + "3C" +] \ No newline at end of file diff --git a/src/main/java/eu/hbp/mip/controllers/AlgorithmsApi.java b/src/main/java/eu/hbp/mip/controllers/AlgorithmsApi.java index 6b92c691a633ecbd4c16ccea813fc8b3c3533742..27e9b8e5db7cf2339bfea34fee0d17893ee97336 100644 --- a/src/main/java/eu/hbp/mip/controllers/AlgorithmsApi.java +++ b/src/main/java/eu/hbp/mip/controllers/AlgorithmsApi.java @@ -5,17 +5,20 @@ import com.github.jmchilton.blend4j.galaxy.GalaxyInstanceFactory; import com.github.jmchilton.blend4j.galaxy.WorkflowsClient; import com.github.jmchilton.blend4j.galaxy.beans.Workflow; import com.google.gson.Gson; +import com.google.gson.reflect.TypeToken; import eu.hbp.mip.controllers.galaxy.retrofit.RetroFitGalaxyClients; import eu.hbp.mip.controllers.galaxy.retrofit.RetrofitClientInstance; import eu.hbp.mip.model.AlgorithmDTO; import eu.hbp.mip.model.UserInfo; import eu.hbp.mip.model.galaxy.WorkflowDTO; +import eu.hbp.mip.utils.CustomResourceLoader; import eu.hbp.mip.utils.HTTPUtil; import eu.hbp.mip.utils.UserActionLogging; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; +import org.springframework.core.io.Resource; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; @@ -28,6 +31,8 @@ import java.util.ArrayList; import java.util.LinkedList; import java.util.List; +import static eu.hbp.mip.utils.ErrorMessages.disabledAlgorithmsCouldNotBeLoaded; +import static eu.hbp.mip.utils.InputStreamConverter.convertInputStreamToString; import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE; @RestController @@ -54,10 +59,10 @@ public class AlgorithmsApi { public ResponseEntity<List<AlgorithmDTO>> getAlgorithms() { UserActionLogging.LogUserAction(userInfo.getUser().getUsername(), "List all algorithms", ""); - List<AlgorithmDTO> exaremeAlgorithms = getExaremeAlgorithms(); - List<AlgorithmDTO> galaxyAlgorithms = getGalaxyWorkflows(); + LinkedList<AlgorithmDTO> exaremeAlgorithms = getExaremeAlgorithms(); + LinkedList<AlgorithmDTO> galaxyAlgorithms = getGalaxyWorkflows(); - List<AlgorithmDTO> algorithms = new LinkedList<>(); + LinkedList<AlgorithmDTO> algorithms = new LinkedList<>(); if (exaremeAlgorithms != null) { algorithms.addAll(exaremeAlgorithms); } else { @@ -71,7 +76,23 @@ public class AlgorithmsApi { "Getting galaxy workflows failed and returned null"); } - return ResponseEntity.ok(algorithms); + List<String> disabledAlgorithms = new ArrayList<>(); + try { + disabledAlgorithms = getDisabledAlgorithms(); + } catch (IOException e) { + UserActionLogging.LogUserAction(userInfo.getUser().getUsername(), "List all algorithms", + disabledAlgorithmsCouldNotBeLoaded); + } + + // Remove any disabled algorithm + LinkedList<AlgorithmDTO> allowedAlgorithms = new LinkedList<>(); + for (AlgorithmDTO algorithm : algorithms) { + if (!disabledAlgorithms.contains(algorithm.getName())) { + allowedAlgorithms.add(algorithm); + } + } + + return ResponseEntity.ok(allowedAlgorithms); } /** @@ -79,16 +100,20 @@ public class AlgorithmsApi { * * @return a list of AlgorithmDTOs or null if something fails */ - public List<AlgorithmDTO> getExaremeAlgorithms() { + public LinkedList<AlgorithmDTO> getExaremeAlgorithms() { UserActionLogging.LogUserAction(userInfo.getUser().getUsername(), "List exareme algorithms", ""); - List<AlgorithmDTO> algorithms = new LinkedList<>(); + LinkedList<AlgorithmDTO> algorithms = new LinkedList<>(); // Get exareme algorithms try { StringBuilder response = new StringBuilder(); HTTPUtil.sendGet(exaremeAlgorithmsUrl, response); - algorithms = gson.fromJson(response.toString(), algorithms.getClass()); + algorithms = gson.fromJson( + response.toString(), + new TypeToken<LinkedList<AlgorithmDTO>>() { + }.getType() + ); } catch (IOException e) { UserActionLogging.LogUserAction(userInfo.getUser().getUsername(), "List exareme algorithms", "An exception occurred: " + e.getMessage()); return null; @@ -104,10 +129,10 @@ public class AlgorithmsApi { * * @return a list of AlgorithmDTOs or null if something fails */ - public List<AlgorithmDTO> getGalaxyWorkflows() { + public LinkedList<AlgorithmDTO> getGalaxyWorkflows() { UserActionLogging.LogUserAction(userInfo.getUser().getUsername(), "List Galaxy workflows", ""); - List<Workflow> workflowList = null; + List<Workflow> workflowList; try { // Get all the workflows with the galaxy client final GalaxyInstance instance = GalaxyInstanceFactory.get(galaxyUrl, galaxyApiKey); @@ -146,7 +171,7 @@ public class AlgorithmsApi { UserActionLogging.LogUserAction(userInfo.getUser().getUsername(), "List Galaxy workflows", "Workflows fetched: " + workflows.size()); // Convert the workflows to algorithms - List<AlgorithmDTO> algorithms = new LinkedList<>(); + LinkedList<AlgorithmDTO> algorithms = new LinkedList<>(); for (WorkflowDTO workflow : workflows) { UserActionLogging.LogUserAction(userInfo.getUser().getUsername(), "List Galaxy workflows", "Converting workflow: " + workflow); @@ -159,4 +184,25 @@ public class AlgorithmsApi { UserActionLogging.LogUserAction(userInfo.getUser().getUsername(), "List Galaxy workflows", "Completed!"); return algorithms; } + + @Autowired + private CustomResourceLoader resourceLoader; + + /** + * Fetches the disabled algorithms from a .json file + * + * @return a list with their names + * @throws IOException when the file could not be loaded + */ + List<String> getDisabledAlgorithms() throws IOException { + + Resource resource = resourceLoader.getResource("file:/opt/portal/api/disabledAlgorithms.json"); + + List<String> response = gson.fromJson(convertInputStreamToString( + resource.getInputStream()), + new TypeToken<List<String>>() { + }.getType() + ); + return response; + } } diff --git a/src/main/java/eu/hbp/mip/controllers/PathologiesApi.java b/src/main/java/eu/hbp/mip/controllers/PathologiesApi.java index f4eb2a7d623583cbdf3a00753a26dc499ffc1f39..7b6b3e65017204ea3e52897e15a2345a82deeda4 100644 --- a/src/main/java/eu/hbp/mip/controllers/PathologiesApi.java +++ b/src/main/java/eu/hbp/mip/controllers/PathologiesApi.java @@ -10,9 +10,11 @@ import eu.hbp.mip.model.PathologyDTO; import eu.hbp.mip.model.UserInfo; import eu.hbp.mip.utils.ClaimUtils; import eu.hbp.mip.utils.CustomResourceLoader; +import eu.hbp.mip.utils.InputStreamConverter; import eu.hbp.mip.utils.UserActionLogging; import io.swagger.annotations.Api; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.beans.factory.annotation.Value; import org.springframework.core.io.Resource; import org.springframework.http.ResponseEntity; @@ -21,12 +23,10 @@ import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; -import java.io.ByteArrayOutputStream; import java.io.IOException; -import java.io.InputStream; -import java.nio.charset.StandardCharsets; import java.util.List; +import static eu.hbp.mip.utils.ErrorMessages.pathologiesCouldNotBeLoaded; import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE; @RestController @@ -54,10 +54,10 @@ public class PathologiesApi { Resource resource = resourceLoader.getResource("file:/opt/portal/api/pathologies.json"); List<PathologyDTO> allPathologies; try { - allPathologies = gson.fromJson(convertInputStreamToString(resource.getInputStream()), new TypeToken<List<PathologyDTO>>() { + allPathologies = gson.fromJson(InputStreamConverter.convertInputStreamToString(resource.getInputStream()), new TypeToken<List<PathologyDTO>>() { }.getType()); } catch (IOException e) { - return ResponseEntity.badRequest().body("The pathologies.json file could not be read."); + return ResponseEntity.badRequest().body(pathologiesCouldNotBeLoaded); } // If authentication is disabled return everything @@ -68,18 +68,4 @@ public class PathologiesApi { return ResponseEntity.ok().body(ClaimUtils.getAuthorizedPathologies( userInfo.getUser().getUsername(), authentication.getAuthorities(), allPathologies)); } - - // Pure Java - private static String convertInputStreamToString(InputStream inputStream) throws IOException { - - ByteArrayOutputStream result = new ByteArrayOutputStream(); - byte[] buffer = new byte[1024]; - int length; - while ((length = inputStream.read(buffer)) != -1) { - result.write(buffer, 0, length); - } - - return result.toString(StandardCharsets.UTF_8.name()); - - } } diff --git a/src/main/java/eu/hbp/mip/model/UserInfo.java b/src/main/java/eu/hbp/mip/model/UserInfo.java index 293ce9534cf78c0f27b5f742ca924540072e2fea..71eee3573b485d690c810b42c1344cc2170fb9f5 100644 --- a/src/main/java/eu/hbp/mip/model/UserInfo.java +++ b/src/main/java/eu/hbp/mip/model/UserInfo.java @@ -13,7 +13,7 @@ import org.springframework.stereotype.Component; import javax.inject.Named; @Component -@Scope(value = "session", proxyMode=ScopedProxyMode.TARGET_CLASS) +@Scope(value = "session", proxyMode = ScopedProxyMode.TARGET_CLASS) @Named("userInfo") public class UserInfo { diff --git a/src/main/java/eu/hbp/mip/utils/ErrorMessages.java b/src/main/java/eu/hbp/mip/utils/ErrorMessages.java new file mode 100644 index 0000000000000000000000000000000000000000..9f672f81b8bb57c559015a52bc65e44a0da7bd72 --- /dev/null +++ b/src/main/java/eu/hbp/mip/utils/ErrorMessages.java @@ -0,0 +1,7 @@ +package eu.hbp.mip.utils; + +public class ErrorMessages { + + public static String pathologiesCouldNotBeLoaded = "The pathologies could not be loaded."; + public static String disabledAlgorithmsCouldNotBeLoaded = "The disabled algorithms could not be loaded."; +} diff --git a/src/main/java/eu/hbp/mip/utils/InputStreamConverter.java b/src/main/java/eu/hbp/mip/utils/InputStreamConverter.java new file mode 100644 index 0000000000000000000000000000000000000000..ebb8f9d999dd73381ae9f203c12361fb381cba9b --- /dev/null +++ b/src/main/java/eu/hbp/mip/utils/InputStreamConverter.java @@ -0,0 +1,21 @@ +package eu.hbp.mip.utils; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.nio.charset.StandardCharsets; + +public class InputStreamConverter { + + // Pure Java + public static String convertInputStreamToString(InputStream inputStream) throws IOException { + ByteArrayOutputStream result = new ByteArrayOutputStream(); + byte[] buffer = new byte[1024]; + int length; + while ((length = inputStream.read(buffer)) != -1) { + result.write(buffer, 0, length); + } + + return result.toString(StandardCharsets.UTF_8.name()); + } +}