Example usage for org.springframework.http HttpHeaders CONTENT_ENCODING

List of usage examples for org.springframework.http HttpHeaders CONTENT_ENCODING

Introduction

In this page you can find the example usage for org.springframework.http HttpHeaders CONTENT_ENCODING.

Prototype

String CONTENT_ENCODING

To view the source code for org.springframework.http HttpHeaders CONTENT_ENCODING.

Click Source Link

Document

The HTTP Content-Encoding header field name.

Usage

From source file:org.ow2.proactive.workflow_catalog.rest.controller.WorkflowControllerTest.java

@Test
public void testGetWorkflowsAsArchive() throws IOException {
    HttpServletResponse response = mock(HttpServletResponse.class);
    ServletOutputStream sos = mock(ServletOutputStream.class);
    when(response.getOutputStream()).thenReturn(sos);
    List<Long> idList = new ArrayList<>();
    idList.add(0L);/*from  w  w w .ja  va  2 s .co  m*/
    workflowController.get(1L, idList, Optional.of("zip"), response);
    verify(workflowService, times(1)).getWorkflowsAsArchive(1L, idList);
    verify(response, times(1)).setStatus(HttpServletResponse.SC_OK);
    verify(response, times(1)).setContentType("application/zip");
    verify(response, times(1)).addHeader(HttpHeaders.CONTENT_ENCODING, "binary");
    verify(response, times(1)).addHeader(HttpHeaders.CONTENT_DISPOSITION,
            "attachment; filename=\"archive.zip\"");
    verify(sos, times(1)).write(Mockito.any());
    verify(sos, times(1)).flush();
}

From source file:org.ow2.proactive.workflow_catalog.rest.controller.WorkflowController.java

@ApiOperation(value = "Gets a workflow's metadata by IDs", notes = "Returns metadata associated to the latest revision of the workflow.")
@ApiResponses(value = @ApiResponse(code = 404, message = "Bucket or workflow not found"))
@RequestMapping(value = "/buckets/{bucketId}/workflows/{idList}", method = GET)
public ResponseEntity<?> get(@PathVariable Long bucketId, @PathVariable List<Long> idList,
        @ApiParam(value = "Force response to return workflow XML content when set to 'xml'. Or extract workflows as ZIP when set to 'zip'.") @RequestParam(required = false) Optional<String> alt,
        HttpServletResponse response) throws MalformedURLException {
    if (alt.isPresent() && ZIP_EXTENSION.equals(alt.get())) {
        byte[] zip = workflowService.getWorkflowsAsArchive(bucketId, idList);

        response.setStatus(HttpServletResponse.SC_OK);
        response.setContentType("application/zip");
        response.addHeader(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"archive.zip\"");
        response.addHeader(HttpHeaders.CONTENT_ENCODING, "binary");
        try {//from  www .  ja v a2  s  .  co  m
            response.getOutputStream().write(zip);
            response.getOutputStream().flush();
        } catch (IOException ioe) {
            throw new RuntimeException(ioe);
        }
        return ResponseEntity.ok().build();
    } else {
        if (idList.size() == 1) {
            return workflowService.getWorkflowMetadata(bucketId, idList.get(0), alt);
        } else {
            return ResponseEntity.badRequest().build();
        }
    }
}