Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 27 additions & 5 deletions src/main/java/org/commonwl/view/GlobalControllerErrorHandling.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@

package org.commonwl.view;

import java.util.Collections;

import org.commonwl.view.workflow.MultipleWorkflowsException;
import org.commonwl.view.workflow.RepresentationNotFoundException;
import org.commonwl.view.workflow.WorkflowNotFoundException;
import org.springframework.http.HttpHeaders;
Expand All @@ -29,7 +32,12 @@
import org.springframework.web.bind.annotation.ExceptionHandler;

/**
* Handles exception handling across the application
* Handles exception handling across the application.
* <p>
* Because of Spring Boot's content negotiation these handlers are needed when
* the error is returned with an otherwise "non acceptable" content type (e.g.
* Accept: image/svg+xml but we have to say 404 Not Found)
*
*/
@ControllerAdvice
public class GlobalControllerErrorHandling {
Expand All @@ -42,20 +50,34 @@ public class GlobalControllerErrorHandling {
public ResponseEntity<?> handleNotFound() {
final HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.TEXT_PLAIN);
return new ResponseEntity<>("Workflow could not be found", headers, HttpStatus.NOT_FOUND);
return new ResponseEntity<>("Workflow or git commit could not be found", headers, HttpStatus.NOT_FOUND);
}

/**
* More than one workflow (or workflow parts) found
*
* @return A text/uri-list of potential representations
*/
@ExceptionHandler(MultipleWorkflowsException.class)
public ResponseEntity<?> handleMultipleWorkflows(MultipleWorkflowsException ex) {
final HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.parseMediaType("text/uri-list"));
return new ResponseEntity<>(ex.toString(), headers, HttpStatus.MULTIPLE_CHOICES);
}

/**
* Workflow exists but representation is not found
* eg Generic git workflow asking for raw workflow URL
* Workflow exists but representation is not found eg Generic git workflow
* asking for raw workflow URL
*
* @return A plain text error message
*/
@ExceptionHandler(RepresentationNotFoundException.class)
public ResponseEntity<?> handleNoRepresentation() {
final HttpHeaders headers = new HttpHeaders();
headers.setVary(Collections.singletonList("Accept"));
headers.setContentType(MediaType.TEXT_PLAIN);
return new ResponseEntity<>("The workflow exists but the requested representation could not be found",
headers, HttpStatus.NOT_FOUND);
headers, HttpStatus.NOT_ACCEPTABLE);
}

}
65 changes: 40 additions & 25 deletions src/main/java/org/commonwl/view/WebConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@

import static org.springframework.http.MediaType.parseMediaType;

import org.commonwl.view.workflow.Workflow;
import org.commonwl.view.workflow.WorkflowPermalinkController;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.web.servlet.config.annotation.ContentNegotiationConfigurer;
Expand All @@ -31,39 +33,52 @@
public class WebConfig extends WebMvcConfigurerAdapter {

/**
* Ordered list of formats as presented on Workflow page - must match the
* .mediaType() strings below.
* Ordered list of formats as presented on Workflow page and supported for
* content negotiation.
*
* @see Workflow#getPermalink(Format)
* @see WorkflowPermalinkController
*
*/
public static enum formats {
html, json, turtle, jsonld, rdfxml, svg, png, dot, zip, ro, yaml, raw
public static enum Format {
// Browser
html(MediaType.TEXT_HTML),
// API
json(MediaType.APPLICATION_JSON),
// RDF
turtle("text/turtle"), jsonld("application/ld+json"), rdfxml("application/rdf+xml"),
// Images
svg("image/svg+xml"), png(MediaType.IMAGE_PNG), dot("text/vnd+graphviz"),
// Archives
zip("application/zip"), ro("application/vnd.wf4ever.robundle+zip"),
// raw redirects
yaml("text/x-yaml"), raw(MediaType.APPLICATION_OCTET_STREAM);

private final MediaType mediaType;

Format(MediaType mediaType) {
this.mediaType = mediaType;
}

Format(String mediaType) {
this.mediaType = parseMediaType(mediaType);
}

public MediaType mediaType() {
return mediaType;
}
}

/**
* Allows the use of the format query parameter to be used
* instead of the Accept HTTP header
* Allows the use of the format query parameter to be used instead of the Accept
* HTTP header
*/
@Override
public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
configurer.favorParameter(true)
// Browser
.mediaType("html", MediaType.TEXT_HTML)
// API
.mediaType("json", MediaType.APPLICATION_JSON)
// RDF
.mediaType("turtle", parseMediaType("text/turtle"))
.mediaType("jsonld", parseMediaType("application/ld+json"))
.mediaType("rdfxml", parseMediaType("application/rdf+xml"))
// Images
.mediaType("svg", parseMediaType("image/svg+xml"))
.mediaType("png", MediaType.IMAGE_PNG)
.mediaType("dot", parseMediaType("text/vnd+graphviz"))
// Archives
.mediaType("zip", parseMediaType("application/zip"))
.mediaType("ro", parseMediaType("application/vnd.wf4ever.robundle+zip"))
// raw redirects
.mediaType("yaml", parseMediaType("text/x-yaml"))
.mediaType("raw", MediaType.APPLICATION_OCTET_STREAM);
ContentNegotiationConfigurer c = configurer.favorParameter(true);
for (Format f : Format.values()) {
c = c.mediaType(f.name(), f.mediaType());
}
}

@Override
Expand Down
45 changes: 27 additions & 18 deletions src/main/java/org/commonwl/view/cwl/CWLService.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,20 @@

package org.commonwl.view.cwl;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.fasterxml.jackson.databind.node.TextNode;
import static org.apache.commons.io.FileUtils.readFileToString;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.StringWriter;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;
import org.apache.jena.iri.IRI;
Expand All @@ -48,15 +57,11 @@
import org.springframework.stereotype.Service;
import org.yaml.snakeyaml.Yaml;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.StringWriter;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.*;

import static org.apache.commons.io.FileUtils.readFileToString;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.fasterxml.jackson.databind.node.TextNode;

/**
* Provides CWL parsing for workflows to gather an overview
Expand Down Expand Up @@ -244,8 +249,10 @@ public Workflow parseWorkflowWithCwltool(Workflow basicModel,
String packedWorkflowID = gitDetails.getPackedId();

// Get paths to workflow
String url = basicModel.getPermalink();
String localPath = workflowFile.toAbsolutePath().toString();
String url = basicModel.getIdentifier();
String workflowFileURI = workflowFile.toAbsolutePath().toUri().toString();
String workTreeUri = workTree.toAbsolutePath().toUri().toString();
String localPath = workflowFileURI;
String gitPath = gitDetails.getPath();
if (packedWorkflowID != null) {
if (packedWorkflowID.charAt(0) != '#') {
Expand All @@ -259,8 +266,10 @@ public Workflow parseWorkflowWithCwltool(Workflow basicModel,
// Get RDF representation from cwltool
if (!rdfService.graphExists(url)) {
String rdf = cwlTool.getRDF(localPath);
rdf = rdf.replace("file://" + workTree.toAbsolutePath().toString(),
"https://w3id.org/cwl/view/git/" + latestCommit);
// Replace /tmp/123123 with permalink base
// NOTE: We do not just replace workflowFileURI, all referenced files will also get rewritten
rdf = rdf.replace(workTreeUri,
"https://w3id.org/cwl/view/git/" + latestCommit + "/");
// Workaround for common-workflow-language/cwltool#427
rdf = rdf.replace("<rdfs:>", "<http://www.w3.org/2000/01/rdf-schema#>");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
import org.apache.taverna.robundle.manifest.PathAnnotation;
import org.apache.taverna.robundle.manifest.PathMetadata;
import org.apache.taverna.robundle.manifest.Proxy;
import org.commonwl.view.WebConfig.Format;
import org.commonwl.view.cwl.CWLTool;
import org.commonwl.view.cwl.CWLValidationException;
import org.commonwl.view.cwl.RDFService;
Expand Down Expand Up @@ -137,7 +138,7 @@ public Bundle createBundle(Workflow workflow, GitDetails gitInfo) throws IOExcep
// TODO: Make this importedBy/On/From
manifest.setRetrievedBy(appAgent);
manifest.setRetrievedOn(manifest.getCreatedOn());
manifest.setRetrievedFrom(new URI(workflow.getPermalink("ro")));
manifest.setRetrievedFrom(new URI(workflow.getPermalink(Format.ro)));

// Make a directory in the RO bundle to store the files
Path bundleRoot = bundle.getRoot();
Expand All @@ -164,12 +165,12 @@ public Bundle createBundle(Workflow workflow, GitDetails gitInfo) throws IOExcep
File png = graphVizService.getGraph(workflow.getID() + ".png", workflow.getVisualisationDot(), "png");
Files.copy(png.toPath(), bundleRoot.resolve("visualisation.png"));
PathMetadata pngAggr = bundle.getManifest().getAggregation(bundleRoot.resolve("visualisation.png"));
pngAggr.setRetrievedFrom(new URI(workflow.getPermalink("png")));
pngAggr.setRetrievedFrom(new URI(workflow.getPermalink(Format.png)));

File svg = graphVizService.getGraph(workflow.getID() + ".svg", workflow.getVisualisationDot(), "svg");
Files.copy(svg.toPath(), bundleRoot.resolve("visualisation.svg"));
PathMetadata svgAggr = bundle.getManifest().getAggregation(bundleRoot.resolve("visualisation.svg"));
svgAggr.setRetrievedFrom(new URI(workflow.getPermalink("svg")));
svgAggr.setRetrievedFrom(new URI(workflow.getPermalink(Format.svg)));

// Add annotation files
GitDetails wfDetails = workflow.getRetrievedFrom();
Expand All @@ -192,7 +193,7 @@ public Bundle createBundle(Workflow workflow, GitDetails gitInfo) throws IOExcep
} catch (CWLValidationException ex) {
logger.error("Could not pack workflow when creating Research Object", ex.getMessage());
}
String rdfUrl = workflow.getPermalink();
String rdfUrl = workflow.getIdentifier();
if (rdfService.graphExists(rdfUrl)) {
addAggregation(bundle, manifestAnnotations, "workflow.ttl",
new String(rdfService.getModel(rdfUrl, "TURTLE")));
Expand Down Expand Up @@ -314,7 +315,7 @@ private void addFilesToBundle(GitDetails gitDetails, Bundle bundle, Path bundleP
// Attempt to get authors from cwl description - takes priority
ResultSet descAuthors = rdfService.getAuthors(bundlePath
.resolve(file.getName()).toString().substring(10),
workflow.getPermalink());
workflow.getIdentifier());
if (descAuthors.hasNext()) {
QuerySolution authorSoln = descAuthors.nextSolution();
HashableAgent newAuthor = new HashableAgent();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package org.commonwl.view.workflow;

import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

import org.commonwl.view.WebConfig.Format;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus;

/**
* Exception thrown when multiple workflows exist for the request
*/
@ResponseStatus(value = HttpStatus.MULTIPLE_CHOICES)
public class MultipleWorkflowsException extends RuntimeException {

private final Collection<Workflow> matches;

public MultipleWorkflowsException(Workflow match) {
this(Collections.singleton(match));
}

public MultipleWorkflowsException(Collection<Workflow> matches) {
if (matches.isEmpty()) {
throw new IllegalArgumentException("MultipleWorkflowsException, but empty list of workflows");
}
this.matches = matches;
}

// Always CRLF in text/uri-list
private final String CRLF = "\r\n";

public String getRawPermalink() {
// all raw URIs should be the same without ?part=
return matches.stream().findAny().get().getPermalink(Format.raw);
}

/**
* Generate a text/uri-list of potential representations/redirects
*
* @see https://www.iana.org/assignments/media-types/text/uri-list
*/
@Override
public String toString() {
StringBuffer sb = new StringBuffer();
sb.append("## Multiple workflow representations found");
sb.append(CRLF);
sb.append("# ");
sb.append(CRLF);

sb.append("# ");
sb.append(Format.raw.mediaType());
sb.append(CRLF);
sb.append(getRawPermalink());
sb.append(CRLF);

Set<String> seen = new HashSet<>();
// For each workflow, link to each remaining format
for (Workflow w : matches) {
if (!seen.add(w.getIdentifier())) {
// Skip permalink duplicates
continue;
}
sb.append("#");
sb.append(CRLF);
sb.append("# ");
sb.append(w.getIdentifier());
sb.append(CRLF);
for (Format f : Format.values()) {
if (f == Format.raw) {
// Already did that one above
continue;
}
sb.append("# ");
sb.append(f.mediaType());
sb.append(CRLF);
sb.append(w.getPermalink(f));
sb.append(CRLF);
}
}
return sb.toString();
}

}
Loading