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.openschedule.api.impl.AbstractOpenSchedulApiBinding.java

/**
 * Returns a {@link ByteArrayHttpMessageConverter} to be used by the internal {@link RestTemplate} when consuming image or other binary resources.
 * By default, the message converter supports "image/jpeg", "image/gif", and "image/png" media types.
 * Override to customize the message converter (for example, to set supported media types).
 * To remove/replace this or any of the other message converters that are registered by default, override the getMessageConverters() method instead.    
 *//*from w ww  .j a  v  a  2s.c om*/
protected ByteArrayHttpMessageConverter getByteArrayMessageConverter() {
    ByteArrayHttpMessageConverter converter = new ByteArrayHttpMessageConverter();
    converter.setSupportedMediaTypes(
            Arrays.asList(MediaType.IMAGE_JPEG, MediaType.IMAGE_GIF, MediaType.IMAGE_PNG));
    return converter;
}

From source file:com.abixen.platform.core.controller.common.AbstractUserController.java

@RequestMapping(value = "/{id}/avatar/{hash}", method = RequestMethod.GET)
public ResponseEntity<byte[]> getUserAvatar(@PathVariable Long id, @PathVariable String hash)
        throws IOException {
    InputStream in = null;/*from  www. j  av a  2  s  .c  o  m*/
    try {
        in = new FileInputStream(
                platformResourceConfigurationProperties.getImageLibraryDirectory() + "/user-avatar/" + hash);
    } catch (FileNotFoundException e) {
        in = new FileInputStream(
                platformResourceConfigurationProperties.getImageLibraryDirectory() + "/user-avatar/avatar.png");
    }
    byte[] b = IOUtils.toByteArray(in);

    in.close();

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

    return new ResponseEntity<byte[]>(b, headers, HttpStatus.CREATED);

}

From source file:org.focusns.web.modules.profile.ProjectUserWidget.java

@RequestMapping("/user-avatar/download")
public ResponseEntity<byte[]> doAvatar(@RequestParam Long userId, @RequestParam(required = false) Boolean temp,
        @RequestParam(required = false) Integer width, @RequestParam(required = false) Integer height,
        WebRequest webRequest) throws IOException {
    ///*from  w  w w .j  a  va  2s.c o  m*/
    boolean notModified = false;
    InputStream inputStream = null;
    //
    ProjectUser projectUser = projectUserService.getProjectUser(userId);
    Object[] avatarCoordinates = CoordinateHelper.getAvatarCoordinates(projectUser);
    if (temp != null && temp.booleanValue()) {
        long lastModified = storageService.checkTempResource(avatarCoordinates);
        if (lastModified > 0 && webRequest.checkNotModified(lastModified)) {
            notModified = true;
        } else {
            inputStream = storageService.loadTempResource(avatarCoordinates);
        }
    } else if (width == null || height == null) {
        long lastModified = storageService.checkResource(avatarCoordinates);
        if (lastModified > 0 && webRequest.checkNotModified(lastModified)) {
            notModified = true;
        } else {
            inputStream = storageService.loadResource(avatarCoordinates);
        }
    } else {
        Object size = width + "x" + height;
        long lastModified = storageService.checkSizedResource(size, avatarCoordinates);
        if (lastModified > 0 && webRequest.checkNotModified(lastModified)) {
            notModified = true;
        } else {
            inputStream = storageService.loadSizedResource(size, avatarCoordinates);
        }
    }
    //
    if (notModified) {
        return new ResponseEntity<byte[]>(HttpStatus.NOT_MODIFIED);
    }
    //
    return WebUtils.getResponseEntity(FileCopyUtils.copyToByteArray(inputStream), MediaType.IMAGE_PNG);
}

From source file:com.abixen.platform.core.application.service.UserManagementService.java

public ResponseEntity<byte[]> getUserAvatar(String hash) throws IOException {
    InputStream in;/*w w w  . j  a  v a2  s  . c o m*/
    try {
        in = new FileInputStream(
                platformResourceConfigurationProperties.getImageLibraryDirectory() + "/user-avatar/" + hash);
    } catch (FileNotFoundException e) {
        in = new FileInputStream(
                platformResourceConfigurationProperties.getImageLibraryDirectory() + "/user-avatar/avatar.png");
    }
    byte[] b = IOUtils.toByteArray(in);

    in.close();

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

    return new ResponseEntity(b, headers, HttpStatus.CREATED);
}

From source file:com.aspose.showcase.qrcodegen.web.api.controller.QRCodeManagementController.java

private ImageFormatDTO getRequestedImageFormat(HttpHeaders responseHeaders, String acceptHeaderValue,
        String format) {/* ww w.j av a2  s  . c o m*/

    ImageFormatDTO generatedImageFormat = new ImageFormatDTO(ImageFormat.getPng(), MediaType.IMAGE_PNG,
            com.aspose.barcode.BarCodeImageFormat.Png);
    responseHeaders.setContentType(MediaType.IMAGE_PNG);

    if (StringUtils.isBlank(acceptHeaderValue) && StringUtils.isBlank(format)) {
        return new ImageFormatDTO(ImageFormat.getPng(), MediaType.IMAGE_PNG,
                com.aspose.barcode.BarCodeImageFormat.Png);
    }

    String requestedFormat = "Png";

    if (StringUtils.isNotBlank(format)) {
        requestedFormat = format.trim();

    } else if (StringUtils.isNotBlank(acceptHeaderValue)) {
        requestedFormat = StringUtils.removeStartIgnoreCase(acceptHeaderValue.trim(), "image/");
    }

    if ("Jpeg".equalsIgnoreCase(requestedFormat)) {

        generatedImageFormat = new ImageFormatDTO(ImageFormat.getJpeg(), MediaType.IMAGE_JPEG,
                com.aspose.barcode.BarCodeImageFormat.Jpeg);
        responseHeaders.setContentType(MediaType.IMAGE_JPEG);

    } else if ("Png".equalsIgnoreCase(requestedFormat)) {

        generatedImageFormat = new ImageFormatDTO(ImageFormat.getPng(), MediaType.IMAGE_PNG,
                com.aspose.barcode.BarCodeImageFormat.Png);
        responseHeaders.setContentType(MediaType.IMAGE_PNG);

    } else if ("Gif".equalsIgnoreCase(requestedFormat)) {

        generatedImageFormat = new ImageFormatDTO(ImageFormat.getGif(), MediaType.IMAGE_GIF,
                com.aspose.barcode.BarCodeImageFormat.Gif);
        responseHeaders.setContentType(MediaType.IMAGE_GIF);

    } else if ("Tiff".equalsIgnoreCase(requestedFormat)) {

        generatedImageFormat = new ImageFormatDTO(ImageFormat.getTiff(),
                new MediaType(MEDIATYPE_IMAGE, MEDIATYPE_IMAGE_TIFF),
                com.aspose.barcode.BarCodeImageFormat.Tiff);
        responseHeaders.setContentType(new MediaType("image", MEDIATYPE_IMAGE_TIFF));

    } else if ("Bmp".equalsIgnoreCase(requestedFormat)) {

        generatedImageFormat = new ImageFormatDTO(ImageFormat.getBmp(),
                new MediaType(MEDIATYPE_IMAGE, MEDIATYPE_IMAGE_BMP), com.aspose.barcode.BarCodeImageFormat.Bmp);
        responseHeaders.setContentType(new MediaType("image", MEDIATYPE_IMAGE_BMP));

    } else {

        generatedImageFormat = new ImageFormatDTO(ImageFormat.getPng(), MediaType.IMAGE_PNG,
                com.aspose.barcode.BarCodeImageFormat.Png);
        responseHeaders.setContentType(MediaType.IMAGE_PNG);
    }

    return generatedImageFormat;
}

From source file:com.viewer.controller.ViewerController.java

@SuppressWarnings("unused")
private MediaType GetContentType(ConvertImageFileType convertImageFileType) {
    MediaType contentType;//  w  w  w.j  a  v a  2s  . c  om
    switch (convertImageFileType) {
    case JPG:
        contentType = MediaType.IMAGE_JPEG;
        break;
    case BMP:
        contentType = MediaType.ALL;
        break;
    case PNG:
        contentType = MediaType.IMAGE_PNG;
        break;
    default:
        throw new IllegalArgumentException();
    }

    return contentType;
}

From source file:com.sitewhere.web.rest.controllers.AssignmentsController.java

/**
 * Get the default symbol for a device assignment.
 * //w  ww.  j ava2  s .  c  om
 * @param token
 * @param servletRequest
 * @param response
 * @return
 * @throws SiteWhereException
 */
@RequestMapping(value = "/{token}/symbol", method = RequestMethod.GET)
@ResponseBody
@ApiOperation(value = "Get default symbol for assignment")
public ResponseEntity<byte[]> getDeviceAssignmentSymbol(
        @ApiParam(value = "Assignment token", required = true) @PathVariable String token,
        HttpServletRequest servletRequest, HttpServletResponse response) throws SiteWhereException {
    Tracer.start(TracerCategory.RestApiCall, "getDeviceAssignmentSymbol", LOGGER);
    try {
        IDeviceAssignment assignment = assureAssignmentWithoutUserValidation(token, servletRequest);
        IEntityUriProvider provider = DefaultEntityUriProvider.getInstance();
        ISymbolGeneratorManager symbols = SiteWhere.getServer()
                .getDeviceCommunication(getTenant(servletRequest, false)).getSymbolGeneratorManager();
        ISymbolGenerator generator = symbols.getDefaultSymbolGenerator();
        if (generator != null) {
            byte[] image = generator.getDeviceAssigmentSymbol(assignment, provider);

            final HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.IMAGE_PNG);
            return new ResponseEntity<byte[]>(image, headers, HttpStatus.CREATED);
        } else {
            return null;
        }
    } finally {
        Tracer.stop(LOGGER);
    }
}

From source file:de.appsolve.padelcampus.controller.ImagesController.java

@RequestMapping(value = "image/{sha256}", consumes = MediaType.ALL_VALUE, produces = {
        MediaType.IMAGE_PNG_VALUE, MediaType.IMAGE_JPEG_VALUE, MediaType.IMAGE_GIF_VALUE, "image/svg+xml" })
public ResponseEntity<byte[]> showImage(@PathVariable("sha256") String sha256) {
    Image image = imageBaseDAO.findBySha256(sha256);
    if (image != null && image.getContent() != null) {
        byte[] byteArray = image.getContent();
        ResponseEntity.BodyBuilder builder = ResponseEntity.ok()
                .header(HttpHeaders.CACHE_CONTROL, String.format("public,max-age=%s,immutable", ONE_YEAR))
                .contentLength(byteArray.length).contentType(MediaType.IMAGE_PNG);

        if (!StringUtils.isEmpty(image.getContentType())) {
            try {
                MediaType mediaType = MediaType.parseMediaType(image.getContentType());
                builder.contentType(mediaType);
            } catch (InvalidMediaTypeException e) {
                LOG.warn(e.getMessage(), e);
            }/*  w w  w.jav a  2  s .c  o m*/
        }
        return builder.body(byteArray);
    }
    LOG.warn(String.format("Unable to display image %s", sha256));
    return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}

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)) {/* ww  w. ja v  a2  s. c om*/
        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");
    }// w  ww . j ava 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);
}