Skip to content

Commit

Permalink
#10: Update getPalette to load palette file from kbInstallFolder rath…
Browse files Browse the repository at this point in the history
…er than /static/data. Create new getImage REST method to get an image stored in kbInstallFolder (icons folder).
  • Loading branch information
kenmeacham committed May 3, 2023
1 parent 2ead2ee commit 003e822
Showing 1 changed file with 22 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.rmi.UnexpectedException;
import java.text.SimpleDateFormat;
import java.util.Date;
Expand All @@ -53,13 +54,13 @@

import com.fasterxml.jackson.databind.ObjectMapper;

import org.apache.jena.query.Dataset;
import org.keycloak.representations.idm.UserRepresentation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.core.io.FileSystemResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
Expand All @@ -80,7 +81,6 @@
import uk.ac.soton.itinnovation.security.modelquerier.dto.RiskCalcResultsDB;
import uk.ac.soton.itinnovation.security.modelvalidator.ModelValidator;
import uk.ac.soton.itinnovation.security.modelvalidator.Progress;
import uk.ac.soton.itinnovation.security.semanticstore.AStoreWrapper;
import uk.ac.soton.itinnovation.security.semanticstore.IStoreWrapper;
import uk.ac.soton.itinnovation.security.semanticstore.util.SparqlHelper;
import uk.ac.soton.itinnovation.security.systemmodeller.auth.KeycloakAdminClient;
Expand All @@ -101,15 +101,9 @@
import uk.ac.soton.itinnovation.security.systemmodeller.rest.exceptions.UserForbiddenFromDomainException;
import uk.ac.soton.itinnovation.security.systemmodeller.semantics.ModelObjectsHelper;
import uk.ac.soton.itinnovation.security.systemmodeller.semantics.StoreModelManager;
import uk.ac.soton.itinnovation.security.systemmodeller.util.PaletteGenerator;
import uk.ac.soton.itinnovation.security.systemmodeller.util.ReportGenerator;
import uk.ac.soton.itinnovation.security.systemmodeller.util.SecureUrlHelper;

import uk.ac.soton.itinnovation.security.modelquerier.util.ModelStack;
import uk.ac.soton.itinnovation.security.modelquerier.JenaQuerierDB;
import uk.ac.soton.itinnovation.security.modelquerier.dto.ModelExportDB;
import uk.ac.soton.itinnovation.security.semanticstore.JenaTDBStoreWrapper;

/**
* Includes all operations of the Model Controller Service.
*/
Expand Down Expand Up @@ -139,6 +133,9 @@ public class ModelController {
@Value("${admin-role}")
public String adminRole;

@Value("${knowledgebases.install.folder}")
private String kbInstallFolder;

/**
* Take the user IDs of the model owner, editor and modifier and look up the current username for them
*/
Expand Down Expand Up @@ -511,30 +508,29 @@ public ResponseEntity<Map<String, RiskLevelCount>> getModelRiskVector(@PathVaria
String ontology = model.getDomainGraph().substring(model.getDomainGraph().lastIndexOf("/")+1);

try {
String paletteFile = "/static/data/palette-" + ontology + ".json";
URL paletteResource = this.getClass().getResource(paletteFile);
if (paletteResource == null) {
logger.warn("Palette missing: {}", paletteFile);
logger.warn("Creating now..");
boolean paletteCreated = PaletteGenerator.createPalette(model.getDomainGraph(), modelObjectsHelper);

if (paletteCreated) {
//try to load resource again
paletteResource = this.getClass().getResource(paletteFile);
}

if (!paletteCreated || paletteResource == null) {
throw new NotFoundErrorException("Could not create new palette");
}
}
map = objectMapper.readValue(new File(paletteResource.getPath()), Map.class);
String domainModelFolderPath = kbInstallFolder + File.separator + ontology;
String paletteFile = "palette.json";
File palette = new File(domainModelFolderPath, paletteFile);
logger.info("Loading palette file: {}", palette.getAbsolutePath());
map = objectMapper.readValue(palette, Map.class);
} catch (IOException e) {
logger.error("Could not read palette", e);
throw new NotFoundErrorException("Could not load palette");
}
return ResponseEntity.ok().body(map);
}

@RequestMapping(value = "/images/{domainModel}/{image}" , method = RequestMethod.GET)
public ResponseEntity<FileSystemResource> getImage(@PathVariable String domainModel, @PathVariable String image) throws IOException {
String imageFile = kbInstallFolder + File.separator + domainModel + File.separator + "icons" + File.separator + image;
Path path = new File(imageFile).toPath();
FileSystemResource resource = new FileSystemResource(path);

return ResponseEntity.ok()
.contentType(MediaType.parseMediaType(Files.probeContentType(path)))
.body(resource);
}

/**
* This method forces a checkout even if another user is currently editing a model, as for example
* when a user chooses the option to take over editing of a model.
Expand Down

0 comments on commit 003e822

Please sign in to comment.