|
19 | 19 |
|
20 | 20 | package org.commonwl.viewer.web;
|
21 | 21 |
|
| 22 | +import com.github.jabbalaci.graphviz.GraphViz; |
22 | 23 | import org.apache.commons.lang.StringUtils;
|
| 24 | +import org.apache.tomcat.util.http.fileupload.IOUtils; |
| 25 | +import org.apache.tomcat.util.http.parser.MediaType; |
23 | 26 | import org.commonwl.viewer.domain.GithubDetails;
|
24 | 27 | import org.commonwl.viewer.domain.Workflow;
|
25 | 28 | import org.commonwl.viewer.domain.WorkflowForm;
|
|
29 | 32 | import org.slf4j.Logger;
|
30 | 33 | import org.slf4j.LoggerFactory;
|
31 | 34 | import org.springframework.beans.factory.annotation.Autowired;
|
| 35 | +import org.springframework.beans.factory.annotation.Value; |
32 | 36 | import org.springframework.core.io.FileSystemResource;
|
33 | 37 | import org.springframework.stereotype.Controller;
|
34 | 38 | import org.springframework.validation.BindingResult;
|
35 | 39 | import org.springframework.web.bind.annotation.*;
|
| 40 | +import org.springframework.web.context.ServletContextAware; |
36 | 41 | import org.springframework.web.servlet.HandlerMapping;
|
37 | 42 | import org.springframework.web.servlet.ModelAndView;
|
38 | 43 |
|
| 44 | +import javax.servlet.ServletContext; |
39 | 45 | import javax.servlet.http.HttpServletRequest;
|
40 | 46 | import javax.servlet.http.HttpServletResponse;
|
41 | 47 | import javax.validation.Valid;
|
42 | 48 | import java.io.File;
|
| 49 | +import java.io.IOException; |
| 50 | +import java.io.InputStream; |
43 | 51 |
|
44 | 52 | @Controller
|
45 | 53 | public class WorkflowController {
|
@@ -191,4 +199,66 @@ public FileSystemResource downloadROBundle(@PathVariable("workflowID") String wo
|
191 | 199 | logger.info("Serving download for workflow " + workflowID + " [" + bundleDownload.toString() + "]");
|
192 | 200 | return new FileSystemResource(bundleDownload);
|
193 | 201 | }
|
| 202 | + |
| 203 | + /** |
| 204 | + * Download a generated DOT graph for a workflow as an svg |
| 205 | + * @param workflowID The ID of the workflow to download the graph for |
| 206 | + */ |
| 207 | + @RequestMapping(value = "/workflows/{workflowID}/graph/svg", |
| 208 | + method = RequestMethod.GET, |
| 209 | + produces = "image/svg+xml") |
| 210 | + @ResponseBody |
| 211 | + public FileSystemResource getGraphAsSvg(@Value("${graphvizStorage}") String graphvizStorage, |
| 212 | + @PathVariable("workflowID") String workflowID, |
| 213 | + HttpServletResponse response) throws IOException { |
| 214 | + |
| 215 | + // Get workflow from database |
| 216 | + Workflow workflowModel = workflowRepository.findOne(workflowID); |
| 217 | + |
| 218 | + // 404 error if workflow does not exist |
| 219 | + if (workflowModel == null) { |
| 220 | + throw new WorkflowNotFoundException(); |
| 221 | + } |
| 222 | + |
| 223 | + // Generate graphviz image if it does not already exist |
| 224 | + File out = new File(graphvizStorage + "/" + workflowID + ".svg"); |
| 225 | + if (!out.exists()) { |
| 226 | + GraphViz gv = new GraphViz(); |
| 227 | + gv.writeGraphToFile(gv.getGraph(workflowModel.getDotGraph(), "svg", "dot"), out.getAbsolutePath()); |
| 228 | + } |
| 229 | + |
| 230 | + // Output the graph image |
| 231 | + return new FileSystemResource(out); |
| 232 | + } |
| 233 | + |
| 234 | + /** |
| 235 | + * Download a generated DOT graph for a workflow as a png |
| 236 | + * @param workflowID The ID of the workflow to download the graph for |
| 237 | + */ |
| 238 | + @RequestMapping(value = "/workflows/{workflowID}/graph/png", |
| 239 | + method = RequestMethod.GET, |
| 240 | + produces = "image/png") |
| 241 | + @ResponseBody |
| 242 | + public FileSystemResource getGraphAsPng(@Value("${graphvizStorage}") String graphvizStorage, |
| 243 | + @PathVariable("workflowID") String workflowID, |
| 244 | + HttpServletResponse response) throws IOException { |
| 245 | + |
| 246 | + // Get workflow from database |
| 247 | + Workflow workflowModel = workflowRepository.findOne(workflowID); |
| 248 | + |
| 249 | + // 404 error if workflow does not exist |
| 250 | + if (workflowModel == null) { |
| 251 | + throw new WorkflowNotFoundException(); |
| 252 | + } |
| 253 | + |
| 254 | + // Generate graphviz image if it does not already exist |
| 255 | + File out = new File(graphvizStorage + "/" + workflowID + ".png"); |
| 256 | + if (!out.exists()) { |
| 257 | + GraphViz gv = new GraphViz(); |
| 258 | + gv.writeGraphToFile(gv.getGraph(workflowModel.getDotGraph(), "png", "dot"), out.getAbsolutePath()); |
| 259 | + } |
| 260 | + |
| 261 | + // Output the graph image |
| 262 | + return new FileSystemResource(out); |
| 263 | + } |
194 | 264 | }
|
0 commit comments