Example usage for org.springframework.http MediaType IMAGE_JPEG

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

Introduction

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

Prototype

MediaType IMAGE_JPEG

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

Click Source Link

Document

Public constant media type for image/jpeg .

Usage

From source file:com.teasoft.teavote.controller.ElectionInfoController.java

@RequestMapping(value = "resources/election-info/logo-img", method = RequestMethod.GET)
@ResponseBody/*from   w  w  w.j ava2  s .  c o  m*/
public HttpEntity<byte[]> getElectionLogo(HttpServletRequest request) throws Exception {

    JSONResponse jSONResponse = new JSONResponse();
    String home = System.getProperty("user.home");
    String rootLocation = home + File.separator + ".tvconfig";
    //Get external properties and get values for comminisioner and orgName
    //Load properties
    Properties prop = appProp.getProperties();
    ConfigLocation configLocation = new ConfigLocation();
    prop.load(configLocation.getExternalProperties());

    byte[] logo = null;
    HttpHeaders httpHeaders = new HttpHeaders();
    //Get teavote-logo.jpg as byte[]
    if (Files.exists(Paths.get(configLocation.getConfigPath() + File.separator + "teavote-logo.jpg"))) {
        InputStream is = new FileInputStream(
                configLocation.getConfigPath() + File.separator + "teavote-logo.jpg");
        logo = IOUtils.toByteArray(is);
        httpHeaders.setContentType(MediaType.IMAGE_JPEG);
        is.close();
    }

    return new ResponseEntity<>(logo, httpHeaders, HttpStatus.OK);

}

From source file:de.tobiasbruns.content.storage.ContentControllerITCase.java

@Test
public void test12_loadBinaryFile() throws Exception {
    mockMvc.perform(get(BASE + "/folder/testimage.jpeg")).andExpect(status().isOk())
            .andExpect(content().contentType(MediaType.IMAGE_JPEG))//
            .andDo(TestUtils.writeDoc("readBinaryContent"));
}

From source file:it.smartcommunitylab.climb.contextstore.controller.ChildController.java

@RequestMapping(value = "/api/image/download/{imageType}/{ownerId}/{objectId}", method = RequestMethod.GET)
public @ResponseBody HttpEntity<byte[]> downloadImage(@PathVariable String imageType,
        @PathVariable String ownerId, @PathVariable String objectId, HttpServletRequest request,
        HttpServletResponse response) throws Exception {
    Criteria criteria = Criteria.where("objectId").is(objectId);
    Child child = storage.findOneData(Child.class, criteria, ownerId);
    if (!validateAuthorizationByExp(ownerId, child.getInstituteId(), child.getSchoolId(), null, "ALL",
            request)) {//w ww  .j a  va 2 s  .  c  o  m
        throw new UnauthorizedException("Unauthorized Exception: token not valid");
    }
    String name = objectId + "." + imageType;
    String path = imageUploadDir + "/" + name;
    if (logger.isInfoEnabled()) {
        logger.info("downloadImage:" + name);
    }
    FileInputStream in = new FileInputStream(new File(path));
    byte[] image = IOUtils.toByteArray(in);
    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.IMAGE_PNG);
    if (imageType.toLowerCase().equals("png")) {
        headers.setContentType(MediaType.IMAGE_PNG);
    } else if (imageType.toLowerCase().equals("gif")) {
        headers.setContentType(MediaType.IMAGE_GIF);
    } else if (imageType.toLowerCase().equals("jpg")) {
        headers.setContentType(MediaType.IMAGE_JPEG);
    } else if (imageType.toLowerCase().equals("jpeg")) {
        headers.setContentType(MediaType.IMAGE_JPEG);
    }
    headers.setContentLength(image.length);
    return new HttpEntity<byte[]>(image, headers);
}

From source file:it.smartcommunitylab.climb.domain.controller.ChildController.java

@RequestMapping(value = "/api/child/image/download/{imageType}/{ownerId}/{objectId}", method = RequestMethod.GET)
public @ResponseBody HttpEntity<byte[]> downloadImage(@PathVariable String imageType,
        @PathVariable String ownerId, @PathVariable String objectId, HttpServletRequest request,
        HttpServletResponse response) throws Exception {
    Criteria criteria = Criteria.where("objectId").is(objectId);
    Child child = storage.findOneData(Child.class, criteria, ownerId);
    if (child == null) {
        throw new EntityNotFoundException("child not found");
    }//from  w w w. j  a v a  2 s .  c o m
    if (!validateAuthorization(ownerId, child.getInstituteId(), child.getSchoolId(), null, null,
            Const.AUTH_RES_Image, Const.AUTH_ACTION_READ, request)) {
        throw new UnauthorizedException("Unauthorized Exception: token not valid");
    }
    String name = objectId + "." + imageType;
    String path = imageUploadDir + "/" + name;
    if (logger.isInfoEnabled()) {
        logger.info("downloadImage:" + name);
    }
    FileInputStream in = new FileInputStream(new File(path));
    byte[] image = IOUtils.toByteArray(in);
    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.IMAGE_PNG);
    if (imageType.toLowerCase().equals("png")) {
        headers.setContentType(MediaType.IMAGE_PNG);
    } else if (imageType.toLowerCase().equals("gif")) {
        headers.setContentType(MediaType.IMAGE_GIF);
    } else if (imageType.toLowerCase().equals("jpg")) {
        headers.setContentType(MediaType.IMAGE_JPEG);
    } else if (imageType.toLowerCase().equals("jpeg")) {
        headers.setContentType(MediaType.IMAGE_JPEG);
    }
    headers.setContentLength(image.length);
    return new HttpEntity<byte[]>(image, headers);
}

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);
    }/*from w  ww. j av a 2s  . c om*/

    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  ww w. j a va2s  . co  m*/
    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);
}