List of usage examples for org.springframework.http MediaType TEXT_PLAIN
MediaType TEXT_PLAIN
To view the source code for org.springframework.http MediaType TEXT_PLAIN.
Click Source Link
From source file:it.geosolutions.opensdi2.mvc.BaseFileManager.java
/** * Download a file with a stream/*from w ww . j a v a 2 s .c o m*/ * * @param contentType * @param contentDisposition * @param resp * @param fileName * @param filePath * @return */ protected ResponseEntity<byte[]> serveImageThumb(HttpServletResponse resp, String fileName, String filePath) { String contentType = "image/jpg"; final HttpHeaders headers = new HttpHeaders(); File toServeUp = new File(filePath); InputStream inputStream = null; String thumbPath = filePath + "_thumb"; File fileThumb = new File(thumbPath); if (fileThumb.exists()) { try { inputStream = new FileInputStream(fileThumb); } catch (FileNotFoundException e) { // Also useful, this is a good was to serve down an error // message String msg = "ERROR: Could not find the file specified."; headers.setContentType(MediaType.TEXT_PLAIN); return new ResponseEntity<byte[]>(msg.getBytes(), headers, HttpStatus.NOT_FOUND); } } else { try { inputStream = getImageThumb(toServeUp, thumbPath); fileThumb = new File(thumbPath); } catch (Exception e) { // Also useful, this is a good was to serve down an error // message String msg = "ERROR: Could not find the file specified."; headers.setContentType(MediaType.TEXT_PLAIN); return new ResponseEntity<byte[]>(msg.getBytes(), headers, HttpStatus.NOT_FOUND); } } // content type resp.setContentType(contentType); Long fileSize = fileThumb.length(); resp.setContentLength(fileSize.intValue()); OutputStream outputStream = null; try { outputStream = resp.getOutputStream(); } catch (IOException e) { String msg = "ERROR: Could not generate output stream."; headers.setContentType(MediaType.TEXT_PLAIN); return new ResponseEntity<byte[]>(msg.getBytes(), headers, HttpStatus.NOT_FOUND); } byte[] buffer = new byte[1024]; int read = 0; try { while ((read = inputStream.read(buffer)) != -1) { outputStream.write(buffer, 0, read); } // close the streams to prevent memory leaks outputStream.flush(); outputStream.close(); inputStream.close(); } catch (Exception e) { String msg = "ERROR: Could not read file."; headers.setContentType(MediaType.TEXT_PLAIN); return new ResponseEntity<byte[]>(msg.getBytes(), headers, HttpStatus.NOT_FOUND); } return null; }
From source file:it.geosolutions.opensdi2.mvc.BaseFileManager.java
/** * Download a file with a stream/* ww w . j a v a 2s .c o m*/ * * @param contentType * @param contentDisposition * @param resp * @param fileName * @param filePath * @return */ protected ResponseEntity<byte[]> download(String contentType, String contentDisposition, HttpServletResponse resp, String fileName, String filePath) { final HttpHeaders headers = new HttpHeaders(); File toServeUp = new File(filePath); InputStream inputStream = null; try { inputStream = new FileInputStream(toServeUp); } catch (FileNotFoundException e) { // Also useful, this is a good was to serve down an error message String msg = "ERROR: Could not find the file specified."; headers.setContentType(MediaType.TEXT_PLAIN); return new ResponseEntity<byte[]>(msg.getBytes(), headers, HttpStatus.NOT_FOUND); } // content type if (contentType != null) { resp.setContentType(contentType); } // content disposition if (contentDisposition != null) { resp.setHeader("Content-Disposition", contentDisposition); } Long fileSize = toServeUp.length(); resp.setContentLength(fileSize.intValue()); OutputStream outputStream = null; try { outputStream = resp.getOutputStream(); } catch (IOException e) { String msg = "ERROR: Could not generate output stream."; headers.setContentType(MediaType.TEXT_PLAIN); if (inputStream != null) { try { inputStream.close(); } catch (IOException e1) { // nothing } } return new ResponseEntity<byte[]>(msg.getBytes(), headers, HttpStatus.NOT_FOUND); } byte[] buffer = new byte[1024]; int read = 0; try { while ((read = inputStream.read(buffer)) != -1) { outputStream.write(buffer, 0, read); } // close the streams to prevent memory leaks outputStream.flush(); outputStream.close(); inputStream.close(); } catch (Exception e) { String msg = "ERROR: Could not read file."; headers.setContentType(MediaType.TEXT_PLAIN); return new ResponseEntity<byte[]>(msg.getBytes(), headers, HttpStatus.NOT_FOUND); } return null; }
From source file:org.bonitasoft.web.designer.controller.PageResourceTest.java
@Test public void should_load_page_asset_on_disk_with_content_type_text() throws Exception { Path expectedFile = widgetRepositoryPath.resolve("pbLabel/pbLabel.js"); when(pageAssetService.findAssetPath("page-id", "asset.js", AssetType.JAVASCRIPT.getPrefix())) .thenReturn(expectedFile);/*w w w . ja v a 2 s .c om*/ mockMvc.perform(get("/rest/pages/page-id/assets/js/asset.js?format=text")).andExpect(status().isOk()) .andExpect(content().bytes(readAllBytes(expectedFile))) .andExpect(content().contentType(MediaType.TEXT_PLAIN)) .andExpect(header().string("Content-Length", String.valueOf(expectedFile.toFile().length()))) .andExpect(header().string("Content-Disposition", "inline; filename=\"pbLabel.js\"")) .andExpect(content().encoding("UTF-8")); }
From source file:org.bonitasoft.web.designer.controller.WidgetResourceTest.java
@Test public void should_load_widget_asset_on_disk_with_content_type_text() throws Exception { Path expectedFile = widgetRepositoryPath.resolve("pbLabel/pbLabel.js"); when(widgetAssetService.findAssetPath("widget-id", "asset.js", AssetType.JAVASCRIPT.getPrefix())) .thenReturn(expectedFile);// w w w . ja va 2 s. c o m mockMvc.perform(get("/rest/widgets/widget-id/assets/js/asset.js?format=text")).andExpect(status().isOk()) .andExpect(content().bytes(readAllBytes(expectedFile))) .andExpect(content().contentType(MediaType.TEXT_PLAIN)) .andExpect(header().string("Content-Length", String.valueOf(expectedFile.toFile().length()))) .andExpect(header().string("Content-Disposition", "inline; filename=\"pbLabel.js\"")) .andExpect(content().encoding("UTF-8")); }
From source file:com.orange.ngsi2.server.Ngsi2BaseControllerTest.java
@Test public void checkRetrieveTextPlainAttributeValueNotImplemented() throws Exception { mockMvc.perform(get("/v2/ni/entities/Bcn-Welt/attrs/temperature/value").contentType(MediaType.TEXT_PLAIN) .header("Host", "localhost").accept(MediaType.TEXT_PLAIN)) .andExpect(content().string( "error: 501 | description: this operation 'Retrieve Attribute Value' is not implemented | affectedItems: []")) .andExpect(status().isNotImplemented()); }
From source file:com.orange.ngsi2.server.Ngsi2BaseControllerTest.java
@Test public void checkRetrieveTextPlainAttributeValueInvalidSyntax() throws Exception { mockMvc.perform(get("/v2/ni/entities/Bcn&Welt/attrs/temperature/value").contentType(MediaType.TEXT_PLAIN) .header("Host", "localhost").accept(MediaType.TEXT_PLAIN)) .andExpect(content().string( "error: 400 | description: The incoming request is invalid in this context. Bcn&Welt has a bad syntax. | affectedItems: []")) .andExpect(status().isBadRequest()); }
From source file:com.orange.ngsi2.server.Ngsi2BaseControllerTest.java
@Test public void checkTextPlainRetrieveAttributeValueOK() throws Exception { mockMvc.perform(get("/v2/i/entities/Bcn-Welt/attrs/temperature/value").header("Host", "localhost") .accept(MediaType.TEXT_PLAIN)).andExpect(content().string("25.0")).andExpect(status().isOk()); }
From source file:com.orange.ngsi2.server.Ngsi2BaseControllerTest.java
@Test public void checkTextPlainRetrieveAttributeValueStringOK() throws Exception { mockMvc.perform(get("/v2/i/entities/Bcn-Welt/attrs/hello/value").header("Host", "localhost") .accept(MediaType.TEXT_PLAIN)).andExpect(content().string("\"hello, world\"")) .andExpect(status().isOk()); }
From source file:com.orange.ngsi2.server.Ngsi2BaseControllerTest.java
@Test public void checkTextPlainRetrieveAttributeValueJsonObjectOK() throws Exception { mockMvc.perform(get("/v2/i/entities/Bcn-Welt/attrs/pressure/value").header("Host", "localhost") .accept(MediaType.TEXT_PLAIN)) .andExpect(content().string( "{\"address\":\"Ronda de la Comunicacions\",\"zipCode\":28050,\"city\":\"Madrid\",\"country\":\"Spain\"}")) .andExpect(status().isOk()); }
From source file:com.orange.ngsi2.server.Ngsi2BaseControllerTest.java
@Test public void checkTextPlainRetrieveAttributeValueBooleanOK() throws Exception { mockMvc.perform(get("/v2/i/entities/Bcn-Welt/attrs/on/value").header("Host", "localhost") .accept(MediaType.TEXT_PLAIN)).andExpect(content().string("true")).andExpect(status().isOk()); }