Skip to content

Commit 03903b5

Browse files
committed
Keep HTTPServletResponse object within controller
1 parent 7d27475 commit 03903b5

File tree

5 files changed

+35
-46
lines changed

5 files changed

+35
-46
lines changed

src/main/java/org/commonwl/view/workflow/WorkflowController.java

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,9 @@ public FileSystemResource downloadGraphSvg(@PathVariable("domain") String domain
263263
String path = (String) request.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE);
264264
path = extractPath(path, 8);
265265
GitDetails gitDetails = getGitDetails(domain, owner, repoName, branch, path);
266-
return workflowService.getWorkflowGraph("svg", gitDetails, response);
266+
response.setHeader("Content-Disposition", "inline; filename=\"graph.svg\"");
267+
FileSystemResource test = workflowService.getWorkflowGraph("svg", gitDetails);
268+
return test;
267269
}
268270

269271
/**
@@ -278,7 +280,8 @@ public FileSystemResource downloadGraphSvgGeneric(@PathVariable("branch") String
278280
HttpServletResponse response) throws IOException {
279281
String path = (String) request.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE);
280282
GitDetails gitDetails = getGitDetails(11, path, branch);
281-
return workflowService.getWorkflowGraph("svg", gitDetails, response);
283+
response.setHeader("Content-Disposition", "inline; filename=\"graph.svg\"");
284+
return workflowService.getWorkflowGraph("svg", gitDetails);
282285
}
283286

284287
/**
@@ -301,7 +304,8 @@ public FileSystemResource downloadGraphPng(@PathVariable("domain") String domain
301304
String path = (String) request.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE);
302305
path = extractPath(path, 8);
303306
GitDetails gitDetails = getGitDetails(domain, owner, repoName, branch, path);
304-
return workflowService.getWorkflowGraph("png", gitDetails, response);
307+
response.setHeader("Content-Disposition", "inline; filename=\"graph.png\"");
308+
return workflowService.getWorkflowGraph("png", gitDetails);
305309
}
306310

307311
/**
@@ -316,7 +320,8 @@ public FileSystemResource downloadGraphPngGeneric(@PathVariable("branch") String
316320
HttpServletResponse response) throws IOException {
317321
String path = (String) request.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE);
318322
GitDetails gitDetails = getGitDetails(11, path, branch);
319-
return workflowService.getWorkflowGraph("png", gitDetails, response);
323+
response.setHeader("Content-Disposition", "inline; filename=\"graph.png\"");
324+
return workflowService.getWorkflowGraph("png", gitDetails);
320325
}
321326

322327
/**
@@ -339,7 +344,8 @@ public FileSystemResource downloadGraphDot(@PathVariable("domain") String domain
339344
String path = (String) request.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE);
340345
path = extractPath(path, 8);
341346
GitDetails gitDetails = getGitDetails(domain, owner, repoName, branch, path);
342-
return workflowService.getWorkflowGraph("xdot", gitDetails, response);
347+
response.setHeader("Content-Disposition", "inline; filename=\"graph.dot\"");
348+
return workflowService.getWorkflowGraph("xdot", gitDetails);
343349
}
344350

345351
/**
@@ -354,7 +360,8 @@ public FileSystemResource downloadGraphDotGeneric(@PathVariable("branch") String
354360
HttpServletResponse response) throws IOException {
355361
String path = (String) request.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE);
356362
GitDetails gitDetails = getGitDetails(12, path, branch);
357-
return workflowService.getWorkflowGraph("xdot", gitDetails, response);
363+
response.setHeader("Content-Disposition", "inline; filename=\"graph.dot\"");
364+
return workflowService.getWorkflowGraph("xdot", gitDetails);
358365
}
359366

360367
/**

src/main/java/org/commonwl/view/workflow/WorkflowPermalinkController.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,8 @@ public FileSystemResource getGraphAsSvg(@PathVariable("commitid") String commitI
155155
HttpServletRequest request,
156156
HttpServletResponse response) {
157157
Workflow workflow = getWorkflow(commitId, request);
158-
return workflowService.getWorkflowGraph("svg", workflow.getRetrievedFrom(), response);
158+
response.setHeader("Content-Disposition", "inline; filename=\"graph.svg\"");
159+
return workflowService.getWorkflowGraph("svg", workflow.getRetrievedFrom());
159160
}
160161

161162
/**
@@ -169,7 +170,8 @@ public FileSystemResource getGraphAsPng(@PathVariable("commitid") String commitI
169170
HttpServletRequest request,
170171
HttpServletResponse response) {
171172
Workflow workflow = getWorkflow(commitId, request);
172-
return workflowService.getWorkflowGraph("png", workflow.getRetrievedFrom(), response);
173+
response.setHeader("Content-Disposition", "inline; filename=\"graph.png\"");
174+
return workflowService.getWorkflowGraph("png", workflow.getRetrievedFrom());
173175
}
174176

175177
/**
@@ -183,7 +185,8 @@ public FileSystemResource getGraphAsXDot(@PathVariable("commitid") String commit
183185
HttpServletRequest request,
184186
HttpServletResponse response) {
185187
Workflow workflow = getWorkflow(commitId, request);
186-
return workflowService.getWorkflowGraph("xdot", workflow.getRetrievedFrom(), response);
188+
response.setHeader("Content-Disposition", "inline; filename=\"graph.dot\"");
189+
return workflowService.getWorkflowGraph("xdot", workflow.getRetrievedFrom());
187190
}
188191

189192

src/main/java/org/commonwl/view/workflow/WorkflowService.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@
3939
import org.springframework.data.domain.Pageable;
4040
import org.springframework.stereotype.Service;
4141

42-
import javax.servlet.http.HttpServletResponse;
4342
import java.io.File;
4443
import java.io.IOException;
4544
import java.nio.file.Files;
@@ -381,12 +380,10 @@ public Workflow findByCommitAndPath(String commitID, String path) throws Workflo
381380
* Get a graph in a particular format and return it
382381
* @param format The format for the graph file
383382
* @param gitDetails The Git details of the workflow
384-
* @param response The response object for setting content-disposition header
385383
* @return A FileSystemResource representing the graph
386384
* @throws WorkflowNotFoundException Error getting the workflow or format
387385
*/
388-
public FileSystemResource getWorkflowGraph(String format, GitDetails gitDetails,
389-
HttpServletResponse response)
386+
public FileSystemResource getWorkflowGraph(String format, GitDetails gitDetails)
390387
throws WorkflowNotFoundException {
391388
// Determine file extension from format
392389
String extension;
@@ -410,7 +407,6 @@ public FileSystemResource getWorkflowGraph(String format, GitDetails gitDetails,
410407

411408
// Generate graph and serve the file
412409
File out = graphVizService.getGraph(workflow.getID() + "." + extension, workflow.getVisualisationDot(), format);
413-
response.setHeader("Content-Disposition", "inline; filename=\"graph." + extension + "\"");
414410
return new FileSystemResource(out);
415411
}
416412

src/test/java/org/commonwl/view/researchobject/ROBundleServiceTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ public void generateAndSaveROBundle() throws Exception {
138138
// Check cwl aggregation information
139139
PathMetadata cwlAggregate = manifest.getAggregation(
140140
bundleRoot.resolve("lobSTR-workflow.cwl"));
141-
assertEquals("https://raw.githubusercontent.com/common-workflow-language/workflows/933bf2a1a1cce32d88f88f136275535da9df0954/lobstr-draft3/lobSTR-workflow.cwl",
141+
assertEquals("https://w3id.org/cwl/v/git/null/workflows/lobSTR/lobSTR-workflow.cwl?format=raw",
142142
cwlAggregate.getRetrievedFrom().toString());
143143
assertEquals("Mark Robinson", cwlAggregate.getAuthoredBy().get(0).getName());
144144
assertEquals("mailto:[email protected]", cwlAggregate.getAuthoredBy().get(0).getUri().toString());

src/test/java/org/commonwl/view/workflow/WorkflowControllerTest.java

Lines changed: 14 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import org.mockito.Matchers;
3232
import org.mockito.Mockito;
3333
import org.springframework.boot.test.context.SpringBootTest;
34+
import org.springframework.core.io.FileSystemResource;
3435
import org.springframework.data.web.PageableHandlerMethodArgumentResolver;
3536
import org.springframework.test.context.junit4.SpringRunner;
3637
import org.springframework.test.web.servlet.MockMvc;
@@ -289,7 +290,7 @@ public void downloadROBundle() throws Exception {
289290
// Bundle exists and can be downloaded
290291
mockMvc.perform(get("/robundle/github.com/owner/repo/blob/branch/path/to/workflow.cwl"))
291292
.andExpect(status().isOk())
292-
.andExpect(content().contentType("application/ro+zip"));
293+
.andExpect(content().contentType("application/vnd.wf4ever.robundle+zip"));
293294

294295
// Bundle does not exist, 404 error
295296
mockMvc.perform(get("/robundle/github.com/owner/repo/blob/branch/path/to/workflow.cwl"))
@@ -304,56 +305,38 @@ public void downloadROBundle() throws Exception {
304305
public void downloadGraphVizFiles() throws Exception {
305306

306307
// Mock service to return mock workflow
307-
Workflow mockWorkflow = Mockito.mock(Workflow.class);
308308
WorkflowService mockWorkflowService = Mockito.mock(WorkflowService.class);
309-
when(mockWorkflowService.getWorkflow(Mockito.any(GitDetails.class)))
310-
.thenReturn(mockWorkflow)
311-
.thenReturn(null)
312-
.thenReturn(mockWorkflow)
313-
.thenReturn(null)
314-
.thenReturn(mockWorkflow)
315-
.thenReturn(null);
316-
317-
// Mock service to return files
318-
GraphVizService mockGraphVizService = Mockito.mock(GraphVizService.class);
319-
when(mockGraphVizService.getGraph(anyString(), anyString(), anyString()))
320-
.thenReturn(roBundleFolder.newFile("graph.svg").getAbsoluteFile())
321-
.thenReturn(roBundleFolder.newFile("graph.png").getAbsoluteFile())
322-
.thenReturn(roBundleFolder.newFile("graph.dot").getAbsoluteFile());
309+
when(mockWorkflowService.getWorkflowGraph(anyString(), anyObject()))
310+
.thenReturn(new FileSystemResource("src/test/resources/graphviz/testVis.svg"))
311+
.thenReturn(new FileSystemResource("src/test/resources/graphviz/testVis.png"))
312+
.thenReturn(new FileSystemResource("src/test/resources/graphviz/testWorkflow.dot"))
313+
.thenThrow(new WorkflowNotFoundException());
323314

324315
// Mock controller/MVC
325316
WorkflowController workflowController = new WorkflowController(
326317
Mockito.mock(WorkflowFormValidator.class),
327318
mockWorkflowService,
328-
mockGraphVizService);
319+
Mockito.mock(GraphVizService.class));
329320
MockMvc mockMvc = MockMvcBuilders
330321
.standaloneSetup(workflowController)
331322
.build();
332323

333-
// Image exists and can be downloaded
324+
// Images exist and can be downloaded
334325
mockMvc.perform(get("/graph/svg/github.com/owner/repo/blob/branch/path/to/workflow.cwl"))
335326
.andExpect(status().isOk())
336327
.andExpect(content().contentType("image/svg+xml"));
337-
338-
// Image does not exist, 404 error
339-
mockMvc.perform(get("/graph/svg/github.com/owner/repo/blob/branch/path/to/workflow.cwl"))
340-
.andExpect(status().isNotFound());
341-
342-
// Image exists and can be downloaded
343328
mockMvc.perform(get("/graph/png/github.com/owner/repo/blob/branch/path/to/workflow.cwl"))
344329
.andExpect(status().isOk())
345330
.andExpect(content().contentType("image/png"));
346-
347-
// Image does not exist, 404 error
348-
mockMvc.perform(get("/graph/png/github.com/owner/repo/blob/branch/path/to/workflow.cwl"))
349-
.andExpect(status().isNotFound());
350-
351-
// Image exists and can be downloaded
352331
mockMvc.perform(get("/graph/xdot/github.com/owner/repo/blob/branch/path/to/workflow.cwl"))
353332
.andExpect(status().isOk())
354333
.andExpect(content().contentType("text/vnd.graphviz"));
355334

356-
// Image does not exist, 404 error
335+
// Images do not exist, 404 error
336+
mockMvc.perform(get("/graph/png/github.com/owner/repo/blob/branch/path/to/workflow.cwl"))
337+
.andExpect(status().isNotFound());
338+
mockMvc.perform(get("/graph/svg/github.com/owner/repo/blob/branch/path/to/workflow.cwl"))
339+
.andExpect(status().isNotFound());
357340
mockMvc.perform(get("/graph/xdot/github.com/owner/repo/blob/branch/path/to/workflow.cwl"))
358341
.andExpect(status().isNotFound());
359342

0 commit comments

Comments
 (0)