Example usage for org.springframework.http MediaType IMAGE_PNG

List of usage examples for org.springframework.http MediaType IMAGE_PNG

Introduction

In this page you can find the example usage for org.springframework.http MediaType IMAGE_PNG.

Prototype

MediaType IMAGE_PNG

To view the source code for org.springframework.http MediaType IMAGE_PNG.

Click Source Link

Document

Public constant media type for image/png .

Usage

From source file:org.activiti.rest.service.api.identity.UserPictureResourceTest.java

public void testUpdatePictureWithCustomMimeType() throws Exception {
    User savedUser = null;//  www.j a v  a2s .c om
    try {
        User newUser = identityService.newUser("testuser");
        newUser.setFirstName("Fred");
        newUser.setLastName("McDonald");
        newUser.setEmail("no-reply@activiti.org");
        identityService.saveUser(newUser);
        savedUser = newUser;

        Map<String, String> additionalFields = new HashMap<String, String>();
        additionalFields.put("mimeType", MediaType.IMAGE_PNG.toString());

        HttpPut httpPut = new HttpPut(SERVER_URL_PREFIX
                + RestUrls.createRelativeResourceUrl(RestUrls.URL_USER_PICTURE, newUser.getId()));
        httpPut.setEntity(HttpMultipartHelper.getMultiPartEntity("myPicture.png", "image/png",
                new ByteArrayInputStream("this is the picture raw byte stream".getBytes()), additionalFields));
        closeResponse(executeBinaryRequest(httpPut, HttpStatus.SC_NO_CONTENT));

        Picture picture = identityService.getUserPicture(newUser.getId());
        assertNotNull(picture);
        assertEquals("image/png", picture.getMimeType());
        assertEquals("this is the picture raw byte stream", new String(picture.getBytes()));

    } finally {

        // Delete user after test passes or fails
        if (savedUser != null) {
            identityService.deleteUser(savedUser.getId());
        }
    }
}

From source file:org.glytoucan.web.controller.GlycanController.java

@RequestMapping(value = "/{accessionNumber}/image", method = RequestMethod.GET, produces = {
        MediaType.IMAGE_PNG_VALUE, MediaType.APPLICATION_XML_VALUE, MediaType.IMAGE_JPEG_VALUE })
@ApiOperation(value = "Retrieves glycan image by accession number", response = Byte[].class)
@ApiResponses(value = { @ApiResponse(code = 200, message = "Success"),
        @ApiResponse(code = 400, message = "Illegal argument"),
        @ApiResponse(code = 404, message = "Glycan does not exist"),
        @ApiResponse(code = 500, message = "Internal Server Error") })
public @ResponseBody ResponseEntity<byte[]> getGlycanImage(
        @ApiParam(required = true, value = "id of the glycan") @PathVariable("accessionNumber") String accessionNumber,
        @ApiParam(required = false, value = "format of the the glycan image", defaultValue = "png") @RequestParam("format") String format,
        @ApiParam(required = false, value = "notation to use to generate the image", defaultValue = "cfg") @RequestParam("notation") String notation,
        @ApiParam(required = false, value = "style of the image", defaultValue = "compact") @RequestParam("style") String style)
        throws Exception {

    HashMap<String, Object> data = new HashMap<String, Object>();
    data.put(GlycanClientQuerySpec.IMAGE_FORMAT, format);
    data.put(GlycanClientQuerySpec.IMAGE_NOTATION, notation);
    data.put(GlycanClientQuerySpec.IMAGE_STYLE, style);
    data.put(GlycanClientQuerySpec.ID, accessionNumber);
    byte[] bytes = gtcClient.getImage(data);

    HttpHeaders headers = new HttpHeaders();
    if (format == null || format.equalsIgnoreCase("png")) {
        headers.setContentType(MediaType.IMAGE_PNG);
    } else if (format.equalsIgnoreCase("svg")) {
        headers.setContentType(MediaType.APPLICATION_XML);
    } else if (format.equalsIgnoreCase("jpg") || format.equalsIgnoreCase("jpeg")) {
        headers.setContentType(MediaType.IMAGE_JPEG);
    }//w  ww.ja v  a  2s  . c  o m

    int noOfDays = 3650; // 10 years
    Calendar calendar = Calendar.getInstance();
    calendar.add(Calendar.DAY_OF_YEAR, noOfDays);
    Date date = calendar.getTime();
    headers.setExpires(date.getTime());
    logger.debug("expires on :>" + date.getTime() + "<");

    return new ResponseEntity<byte[]>(bytes, headers, HttpStatus.OK);
}

From source file:org.glytoucan.web.controller.GlycanController.java

@RequestMapping(value = "/image/glycan", method = RequestMethod.POST, consumes = { "application/xml",
        "application/json" }, produces = { MediaType.IMAGE_PNG_VALUE, MediaType.APPLICATION_XML_VALUE,
                MediaType.IMAGE_JPEG_VALUE })
@ApiOperation(value = "Retrieves glycan image by accession number", response = Byte[].class)
@ApiResponses(value = { @ApiResponse(code = 200, message = "Success"),
        @ApiResponse(code = 400, message = "Illegal argument"),
        @ApiResponse(code = 404, message = "Glycan does not exist"),
        @ApiResponse(code = 415, message = "Media type is not supported"),
        @ApiResponse(code = 500, message = "Internal Server Error") })
public @ResponseBody ResponseEntity<byte[]> getGlycanImageByStructure(
        @RequestBody(required = true) @ApiParam(required = true, value = "Glycan") @Valid GlycanInput glycan,
        @ApiParam(required = false, value = "format of the the glycan image", defaultValue = "png") @RequestParam("format") String format,
        @ApiParam(required = false, value = "notation to use to generate the image", defaultValue = "cfg") @RequestParam("notation") String notation,
        @ApiParam(required = false, value = "style of the image", defaultValue = "compact") @RequestParam("style") String style)
        throws Exception {

    Sugar sugarStructure = importParseValidate(glycan);
    if (sugarStructure == null) {
        throw new IllegalArgumentException("Structure cannot be imported");
    }//from   w w  w .  j a va2s  . com
    String exportedStructure;

    // export into GlycoCT
    try {
        exportedStructure = StructureParserValidator.exportStructure(sugarStructure);
    } catch (Exception e) {
        throw new IllegalArgumentException("Cannot export into common encoding: " + e.getMessage());
    }

    logger.debug(exportedStructure);
    byte[] bytes = imageGenerator.getImage(exportedStructure, format, notation, style);

    HttpHeaders headers = new HttpHeaders();
    if (format == null || format.equalsIgnoreCase("png")) {
        headers.setContentType(MediaType.IMAGE_PNG);
    } else if (format.equalsIgnoreCase("svg")) {
        headers.setContentType(MediaType.APPLICATION_XML);
    } else if (format.equalsIgnoreCase("jpg") || format.equalsIgnoreCase("jpeg")) {
        headers.setContentType(MediaType.IMAGE_JPEG);
    }
    return new ResponseEntity<byte[]>(bytes, headers, HttpStatus.OK);
}

From source file:org.mifos.ui.core.controller.UploadLogoController.java

@RequestMapping("/getMifosLogo")
public ResponseEntity<byte[]> getMifosLogo() throws IOException {
    ConfigurationLocator configurationLocator = new ConfigurationLocator();
    Resource logo = configurationLocator.getUploadedMifosLogo();
    InputStream in = logo.getInputStream();
    byte[] logoContent = IOUtils.toByteArray(in);
    in.close();/*w w w  .  j a  va  2  s  .c o m*/

    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.IMAGE_PNG);

    return new ResponseEntity<byte[]>(logoContent, headers, HttpStatus.OK);
}

From source file:us.fatehi.schemacrawler.webapp.SchemaCrawlerControllerHappyPathTest.java

@Test
public void happyPath() throws Exception {
    final InputStreamSource testDbStreamSource = new ClassPathResource("/test.db");
    final MockMultipartFile multipartFile = new MockMultipartFile("file", "test.db", "application/octet-stream",
            testDbStreamSource.getInputStream());

    final MvcResult result1 = mvc
            .perform(fileUpload("/schemacrawler").file(multipartFile).param("name", "Sualeh").param("email",
                    "sualeh@hotmail.com"))
            .andExpect(view().name("SchemaCrawlerDiagramResult")).andExpect(status().is2xxSuccessful())
            .andReturn();/*from w ww.  j a v  a  2 s. com*/

    final SchemaCrawlerDiagramRequest diagramRequest = (SchemaCrawlerDiagramRequest) result1.getModelAndView()
            .getModel().get("diagramRequest");
    final String key = diagramRequest.getKey();

    final Optional<Path> sqlitePathOptional = storageService.resolve(key, FileExtensionType.SQLITE_DB);
    assertThat(sqlitePathOptional.isPresent(), is(equalTo(true)));
    final Optional<Path> pngPathOptional = storageService.resolve(key, FileExtensionType.PNG);
    assertThat(pngPathOptional.isPresent(), is(equalTo(true)));
    final Optional<Path> jsonPathOptional = storageService.resolve(key, FileExtensionType.JSON);
    assertThat(jsonPathOptional.isPresent(), is(equalTo(true)));

    final SchemaCrawlerDiagramRequest schemaCrawlerDiagramRequestFromJson = SchemaCrawlerDiagramRequest
            .fromJson(IOUtils.toString(new FileReader(jsonPathOptional.get().toFile())));
    assertThat(diagramRequest, is(equalTo(schemaCrawlerDiagramRequestFromJson)));

    final MvcResult result2 = mvc.perform(get("/schemacrawler/" + key))
            .andExpect(view().name("SchemaCrawlerDiagram")).andExpect(status().is2xxSuccessful()).andReturn();
    assertThat(result2.getResponse().getContentAsString(), containsString("/schemacrawler/images/" + key));

    final MvcResult result3 = mvc.perform(get("/schemacrawler/images/" + key).accept(MediaType.IMAGE_PNG))
            .andExpect(status().isOk()).andReturn();
    final int contentLength = result3.getResponse().getContentLength();
    assertThat(contentLength, is(greaterThan(0)));
    // assertThat(contentLength, is(greaterThan(14_500)));
    // assertThat(contentLength, is(lessThan(15_500)));

}