List of usage examples for org.springframework.http HttpHeaders setContentLength
public void setContentLength(long contentLength)
From source file:org.shaf.server.controller.CmdActionController.java
/** * Gets data from the server.//from w w w . ja v a2s . c o m * * @param storage * the resource type. * @param alias * the data alias. * @return the entity for downloading data. * @throws Exception * if an error occurs. */ @RequestMapping(value = "/download/{storage}/{alias}", method = RequestMethod.GET) public @ResponseBody ResponseEntity<InputStreamResource> onDownload(@PathVariable String storage, @PathVariable String alias) throws Exception { LOG.debug("CALL service: /cmd/download/{" + storage + "}/{" + alias + "}"); Firewall firewall = super.getFirewall(); String username = super.getUserName(); StorageDriver driver = OPER.getStorageDriver(firewall, username, StorageType.CONSUMER, storage); HttpHeaders header = new HttpHeaders(); header.setContentType(MediaType.APPLICATION_OCTET_STREAM); header.setContentLength(driver.getLength(alias)); header.setContentDispositionFormData("attachment", alias); return new ResponseEntity<InputStreamResource>(new InputStreamResource(driver.getInputStream(alias)), header, HttpStatus.OK); }
From source file:com.highcharts.export.controller.ExportController.java
@RequestMapping(value = "/files/{name}.{ext}", method = RequestMethod.GET) public HttpEntity<byte[]> getFile(@PathVariable("name") String name, @PathVariable("ext") String extension) throws SVGConverterException, IOException { Path path = Paths.get(TempDir.getOutputDir().toString(), name + "." + extension); String filename = path.toString(); MimeType mime = getMime(extension); ByteArrayOutputStream stream = writeFileToStream(filename); HttpHeaders headers = new HttpHeaders(); headers.add("Content-Type", mime.getType() + "; charset=utf-8"); headers.setContentLength(stream.size()); return new HttpEntity<byte[]>(stream.toByteArray(), headers); }
From source file:org.mitre.openid.connect.web.ClientAPI.java
/** * Get the logo image for a client//from www .j a v a 2 s .co m * @param id */ @RequestMapping(value = "/{id}/logo", method = RequestMethod.GET, produces = { MediaType.IMAGE_GIF_VALUE, MediaType.IMAGE_JPEG_VALUE, MediaType.IMAGE_PNG_VALUE }) public ResponseEntity<byte[]> getClientLogo(@PathVariable("id") Long id, Model model) { ClientDetailsEntity client = clientService.getClientById(id); if (client == null) { return new ResponseEntity<>(HttpStatus.NOT_FOUND); } else if (Strings.isNullOrEmpty(client.getLogoUri())) { return new ResponseEntity<>(HttpStatus.NOT_FOUND); } else { // get the image from cache CachedImage image = clientLogoLoadingService.getLogo(client); HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.parseMediaType(image.getContentType())); headers.setContentLength(image.getLength()); return new ResponseEntity<>(image.getData(), headers, HttpStatus.OK); } }
From source file:bjerne.gallery.controller.GalleryController.java
/** * Method used to return the binary of a gallery file ( * {@link GalleryFile#getActualFile()} ). This method handles 304 redirects * (if file has not changed) and range headers if requested by browser. The * range parts is particularly important for videos. The correct response * status is set depending on the circumstances. * <p>//from ww w. j a va2 s.c o m * NOTE: the range logic should NOT be considered a complete implementation * - it's a bare minimum for making requests for byte ranges work. * * @param request * Request * @param galleryFile * Gallery file * @return The binary of the gallery file, or a 304 redirect, or a part of * the file. * @throws IOException * If there is an issue accessing the binary file. */ private ResponseEntity<InputStreamResource> returnResource(WebRequest request, GalleryFile galleryFile) throws IOException { LOG.debug("Entering returnResource()"); if (request.checkNotModified(galleryFile.getActualFile().lastModified())) { return null; } File file = galleryFile.getActualFile(); String contentType = galleryFile.getContentType(); String rangeHeader = request.getHeader(HttpHeaders.RANGE); long[] ranges = getRangesFromHeader(rangeHeader); long startPosition = ranges[0]; long fileTotalSize = file.length(); long endPosition = ranges[1] != 0 ? ranges[1] : fileTotalSize - 1; long contentLength = endPosition - startPosition + 1; LOG.debug("contentLength: {}, file length: {}", contentLength, fileTotalSize); LOG.debug("Returning resource {} as inputstream. Start position: {}", file.getCanonicalPath(), startPosition); InputStream boundedInputStream = new BoundedInputStream(new FileInputStream(file), endPosition + 1); InputStream is = new BufferedInputStream(boundedInputStream, 65536); InputStreamResource inputStreamResource = new InputStreamResource(is); HttpHeaders responseHeaders = new HttpHeaders(); responseHeaders.setContentLength(contentLength); responseHeaders.setContentType(MediaType.valueOf(contentType)); responseHeaders.add(HttpHeaders.ACCEPT_RANGES, "bytes"); if (StringUtils.isNotBlank(rangeHeader)) { is.skip(startPosition); String contentRangeResponseHeader = "bytes " + startPosition + "-" + endPosition + "/" + fileTotalSize; responseHeaders.add(HttpHeaders.CONTENT_RANGE, contentRangeResponseHeader); LOG.debug("{} was not null but {}. Adding header {} to response: {}", HttpHeaders.RANGE, rangeHeader, HttpHeaders.CONTENT_RANGE, contentRangeResponseHeader); } HttpStatus status = (startPosition == 0 && contentLength == fileTotalSize) ? HttpStatus.OK : HttpStatus.PARTIAL_CONTENT; LOG.debug("Returning {}. Status: {}, content-type: {}, {}: {}, contentLength: {}", file, status, contentType, HttpHeaders.CONTENT_RANGE, responseHeaders.get(HttpHeaders.CONTENT_RANGE), contentLength); return new ResponseEntity<InputStreamResource>(inputStreamResource, responseHeaders, status); }
From source file:net.sf.jsog.spring.StringJsogHttpMessageConverterTest.java
@Test public void testWrite() throws Exception { // TODO: Make this test more robust // Setup//from ww w. j a v a 2s . c o m String encoding = "ISO-8859-1"; String expected = "\"foobar\""; MediaType contentType = JSON_CONTENT_TYPE; HttpOutputMessage output = createMock(HttpOutputMessage.class); ByteArrayOutputStream baos = new ByteArrayOutputStream(); expect(output.getBody()).andReturn(baos); HttpHeaders headers = createMock(HttpHeaders.class); expect(output.getHeaders()).andReturn(headers).anyTimes(); headers.setContentType(contentType); expectLastCall(); headers.setContentLength(expected.getBytes(encoding).length); expectLastCall(); // Execution replay(headers, output); instance.write(JSOG.parse(expected), contentType, output); // Verification verify(headers, output); assertEquals(expected, baos.toString(encoding)); }
From source file:net.sf.jsog.spring.StringJsogHttpMessageConverterTest.java
@Test public void testWriteObject() throws Exception { // TODO: Make this test more robust // Setup//from ww w . j ava 2 s . c om String encoding = "ISO-8859-1"; String expected = "{\"foo\":\"bar\"}"; MediaType contentType = JSON_CONTENT_TYPE; HttpOutputMessage output = createMock(HttpOutputMessage.class); ByteArrayOutputStream baos = new ByteArrayOutputStream(); expect(output.getBody()).andReturn(baos); HttpHeaders headers = createMock(HttpHeaders.class); expect(output.getHeaders()).andReturn(headers).anyTimes(); headers.setContentType(contentType); expectLastCall(); headers.setContentLength(expected.getBytes(encoding).length); expectLastCall(); // Execution replay(headers, output); instance.write(JSOG.parse(expected), contentType, output); // Verification verify(headers, output); assertEquals(expected, baos.toString(encoding)); }
From source file:net.sf.jsog.spring.StringJsogHttpMessageConverterTest.java
@Test public void testWriteArray() throws Exception { // TODO: Make this test more robust // Setup/*w w w. j a va2 s . co m*/ String encoding = "ISO-8859-1"; String expected = "[\"foo\",\"bar\"]"; MediaType contentType = JSON_CONTENT_TYPE; HttpOutputMessage output = createMock(HttpOutputMessage.class); ByteArrayOutputStream baos = new ByteArrayOutputStream(); expect(output.getBody()).andReturn(baos); HttpHeaders headers = createMock(HttpHeaders.class); expect(output.getHeaders()).andReturn(headers).anyTimes(); headers.setContentType(contentType); expectLastCall(); headers.setContentLength(expected.getBytes(encoding).length); expectLastCall(); // Execution replay(headers, output); instance.write(JSOG.parse(expected), contentType, output); // Verification verify(headers, output); assertEquals(expected, baos.toString(encoding)); }
From source file:net.sf.jsog.spring.StringJsogHttpMessageConverterTest.java
@Test public void testWriteCustomEncoding() throws Exception { // TODO: Make this test more robust // Setup//from w w w. ja v a 2s. c om String encoding = "UTF-8"; instance.setEncoding(encoding); String expected = "\"foobar\""; MediaType contentType = JSON_CONTENT_TYPE; HttpOutputMessage output = createMock(HttpOutputMessage.class); ByteArrayOutputStream baos = new ByteArrayOutputStream(); expect(output.getBody()).andReturn(baos); HttpHeaders headers = createMock(HttpHeaders.class); expect(output.getHeaders()).andReturn(headers).anyTimes(); headers.setContentType(contentType); expectLastCall(); headers.setContentLength(expected.getBytes(encoding).length); expectLastCall(); // Execution replay(headers, output); instance.write(JSOG.parse(expected), contentType, output); // Verification verify(headers, output); assertEquals(expected, baos.toString(encoding)); }
From source file:net.sf.jsog.spring.StringJsogHttpMessageConverterTest.java
@Test public void testWriteCustomEncodingSetByCharset() throws Exception { // TODO: Make this test more robust // Setup/*from w w w . j a v a 2 s.com*/ String encoding = "UTF-8"; instance.setEncoding(Charset.forName(encoding)); String expected = "\"foobar\""; MediaType contentType = JSON_CONTENT_TYPE; HttpOutputMessage output = createMock(HttpOutputMessage.class); ByteArrayOutputStream baos = new ByteArrayOutputStream(); expect(output.getBody()).andReturn(baos); HttpHeaders headers = createMock(HttpHeaders.class); expect(output.getHeaders()).andReturn(headers).anyTimes(); headers.setContentType(contentType); expectLastCall(); headers.setContentLength(expected.getBytes(encoding).length); expectLastCall(); // Execution replay(headers, output); instance.write(JSOG.parse(expected), contentType, output); // Verification verify(headers, output); assertEquals(expected, baos.toString(encoding)); }
From source file:net.sf.jsog.spring.StringJsogHttpMessageConverterTest.java
@Test public void testWriteCustomOutputContentType() throws Exception { // TODO: Make this test more robust // Setup//from w w w. java2 s . c o m String encoding = "US-ASCII"; instance.setEncoding(encoding); String expected = "\"foobar\""; MediaType contentType = new MediaType("text", "plain", Charset.forName(encoding)); instance.setOutputContentType(contentType); HttpOutputMessage output = createMock(HttpOutputMessage.class); ByteArrayOutputStream baos = new ByteArrayOutputStream(); expect(output.getBody()).andReturn(baos); HttpHeaders headers = createMock(HttpHeaders.class); expect(output.getHeaders()).andReturn(headers).anyTimes(); headers.setContentType(contentType); expectLastCall(); headers.setContentLength(expected.getBytes(encoding).length); expectLastCall(); // Execution replay(headers, output); instance.write(JSOG.parse(expected), contentType, output); // Verification verify(headers, output); assertEquals(expected, baos.toString(encoding)); }