Example usage for com.google.common.net MediaType PNG

List of usage examples for com.google.common.net MediaType PNG

Introduction

In this page you can find the example usage for com.google.common.net MediaType PNG.

Prototype

MediaType PNG

To view the source code for com.google.common.net MediaType PNG.

Click Source Link

Usage

From source file:helper.ThumbnailGenerator.java

/**
 * @param ts/*w w w.ja  v  a  2  s. com*/
 *            the actual content to create a thumbnail from
 * @param contentType
 *            the MediaType for the content
 * @param size
 *            acually the width
 * @param name 
 * @return a thumbnail file
 */
public static File createThumbnail(InputStream ts, MediaType contentType, int size, String name) {
    File result = null;
    try {

        if (contentType.is(MediaType.JPEG)) {
            result = generateThumbnailFromImage(ts, size, "jpeg", name);
        } else if (contentType.is(MediaType.PNG)) {
            result = generateThumbnailFromImage(ts, size, "png", name);
        } else if (contentType.is(MediaType.GIF)) {
            result = generateThumbnailFromImage(ts, size, "gif", name);
        } else if (contentType.is(MediaType.PDF)) {
            result = generateThumbnailFromPdf(ts, size, name);
        } else {
            result = generateMimeTypeImage(contentType, size, name);
        }
    } catch (Throwable e) {
        play.Logger.warn("", e);
        result = generateThumbnailFromImage(Play.application().resourceAsStream(THUMBNAIL_EXCEPTION_PIC), size,
                "png", name);
    }
    return result;
}

From source file:be.nbb.cli.util.Utils.java

private static MediaType getMediaTypeByExtension(File file) {
    String fileName = file.getName().toLowerCase(Locale.ROOT);
    if (fileName.endsWith(".json")) {
        return MediaType.JSON_UTF_8;
    }//from www  .  java  2s. co m
    if (fileName.endsWith(".xml")) {
        return MediaType.XML_UTF_8;
    }
    if (fileName.endsWith(".png")) {
        return MediaType.PNG;
    }
    if (fileName.endsWith(".svg")) {
        return MediaType.SVG_UTF_8;
    }
    if (fileName.endsWith(".yaml")) {
        return YAML;
    }
    return null;
}

From source file:com.twitter.common.webassets.bootstrap.BootstrapModule.java

@Override
protected void configure() {
    register("css/bootstrap-responsive.min.css", version + "/css/bootstrap-responsive.min.css",
            MediaType.CSS_UTF_8.toString());
    register("css/bootstrap.min.css", version + "/css/bootstrap.min.css", MediaType.CSS_UTF_8.toString());
    register("img/glyphicons-halflings-white.png", version + "/img/glyphicons-halflings-white.png",
            MediaType.PNG.toString());
    register("img/glyphicons-halflings.png", version + "/img/glyphicons-halflings.png",
            MediaType.PNG.toString());/*from w w  w  .  ja v a 2 s. co  m*/
    register("js/bootstrap.min.js", version + "/js/bootstrap.min.js", MediaType.JAVASCRIPT_UTF_8.toString());
}

From source file:org.haiku.haikudepotserver.pkg.controller.PkgScreenshotController.java

private void handleHeadOrGet(RequestMethod requestMethod, HttpServletResponse response, Integer targetWidth,
        Integer targetHeight, String format, String screenshotCode) throws IOException {

    if (targetWidth <= 0 || targetWidth > SCREENSHOT_SIDE_LIMIT) {
        throw new BadSize();
    }//w  ww  .ja  v  a2 s . co m

    if (targetHeight <= 0 || targetHeight > SCREENSHOT_SIDE_LIMIT) {
        throw new BadSize();
    }

    if (Strings.isNullOrEmpty(screenshotCode)) {
        throw new MissingScreenshotCode();
    }

    if (Strings.isNullOrEmpty(format) || !"png".equals(format)) {
        throw new MissingOrBadFormat();
    }

    ObjectContext context = serverRuntime.newContext();
    PkgScreenshot screenshot = PkgScreenshot.tryGetByCode(context, screenshotCode)
            .orElseThrow(ScreenshotNotFound::new);

    response.setContentType(MediaType.PNG.toString());
    response.setHeader(HttpHeaders.CACHE_CONTROL, "max-age=3600");

    response.setDateHeader(HttpHeaders.LAST_MODIFIED,
            screenshot.getPkg().getModifyTimestampSecondAccuracy().getTime());

    switch (requestMethod) {
    case HEAD:
        ByteCounterOutputStream byteCounter = new ByteCounterOutputStream(ByteStreams.nullOutputStream());
        pkgScreenshotService.writePkgScreenshotImage(byteCounter, context, screenshot, targetWidth,
                targetHeight);
        response.setHeader(HttpHeaders.CONTENT_LENGTH, Long.toString(byteCounter.getCounter()));

        break;

    case GET:
        pkgScreenshotService.writePkgScreenshotImage(response.getOutputStream(), context, screenshot,
                targetWidth, targetHeight);
        break;

    default:
        throw new IllegalStateException("unhandled request method; " + requestMethod);
    }

}

From source file:com.mastfrog.acteur.ClasspathResourcePage.java

protected MediaType getContentType(Path path) {
    MediaType type = responseHeaders.getContentType();
    if (type != null) {
        return type;
    }/*from   ww w  .  java 2 s  . c  o  m*/
    String pth = path.toString();
    if (pth.endsWith("svg")) {
        return MediaType.SVG_UTF_8;
    } else if (pth.endsWith("css")) {
        return MediaType.CSS_UTF_8;
    } else if (pth.endsWith("html")) {
        return MediaType.HTML_UTF_8;
    } else if (pth.endsWith("json")) {
        return MediaType.JSON_UTF_8;
    } else if (pth.endsWith("js")) {
        return MediaType.JAVASCRIPT_UTF_8;
    } else if (pth.endsWith("gif")) {
        return MediaType.GIF;
    } else if (pth.endsWith("jpg")) {
        return MediaType.JPEG;
    } else if (pth.endsWith("png")) {
        return MediaType.PNG;
    }
    return null;
}

From source file:com.twitter.aurora.scheduler.http.ServletModule.java

private void registerAsset(String resourceLocation, String registerLocation) {
    MediaType mediaType;//  ww  w. java  2 s .  c o m

    if (registerLocation.endsWith(".png")) {
        mediaType = MediaType.PNG;
    } else if (registerLocation.endsWith(".js")) {
        mediaType = MediaType.JAVASCRIPT_UTF_8;
    } else if (registerLocation.endsWith(".html")) {
        mediaType = MediaType.HTML_UTF_8;
    } else if (registerLocation.endsWith(".css")) {
        mediaType = MediaType.CSS_UTF_8;
    } else {
        throw new IllegalArgumentException("Could not determine media type for " + registerLocation);
    }

    Registration.registerHttpAsset(binder(), registerLocation, ServletModule.class, resourceLocation,
            mediaType.toString(), true);
}

From source file:org.haiku.haikudepotserver.pkg.controller.PkgScreenshotController.java

/**
 * <p>This one downloads the raw data that is stored for a screenshot.</p>
 *///  ww  w  .j  av  a2  s  . c o m

@RequestMapping(value = "/{" + KEY_SCREENSHOTCODE + "}/raw", method = RequestMethod.GET)
public void handleRawGet(HttpServletResponse response,
        @PathVariable(value = KEY_SCREENSHOTCODE) String screenshotCode) throws IOException {

    if (Strings.isNullOrEmpty(screenshotCode)) {
        throw new MissingScreenshotCode();
    }

    ObjectContext context = serverRuntime.newContext();
    PkgScreenshot screenshot = PkgScreenshot.tryGetByCode(context, screenshotCode)
            .orElseThrow(ScreenshotNotFound::new);
    byte[] data = screenshot.tryGetPkgScreenshotImage().get().getData();
    org.haiku.haikudepotserver.dataobjects.MediaType mediaType = screenshot.tryGetPkgScreenshotImage().get()
            .getMediaType();

    // TODO - find a better way to do this.
    String extension = null;

    if (mediaType.getCode().equals(MediaType.PNG.toString())) {
        extension = "png";
    }

    if (null == extension) {
        throw new IllegalStateException(
                "the media type for the screenshot is not able to be converted into a file extension");
    }

    response.setContentLength(data.length);
    response.setContentType(mediaType.getCode());
    response.setHeader(HttpHeaders.CONTENT_DISPOSITION, String.format("attachment; filename=\"%s__%s.%s\"",
            screenshot.getPkg().getName(), screenshot.getCode(), extension));

    response.getOutputStream().write(data);
}

From source file:info.magnolia.ui.imageprovider.DefaultImageProvider.java

private Object getThumbnailResource(Node node, String workspace, String generator) {
    Object resource = null;/*  ww w  .j av a2 s  .  co  m*/
    try {
        final Node imageNode = node.getNode(definition.getOriginalImageNodeName());
        String mimeType = imageNode.getProperty(FileProperties.PROPERTY_CONTENTTYPE).getString();

        if (isImage(mimeType)) {
            if (MediaType.SVG_UTF_8.is(MediaType.parse(mimeType))) {
                ImageStreamSource iss = new ImageStreamSource(imageNode.getIdentifier(), workspace);
                // By default a StreamResource is cached for one year - filename contains the last modified date so that image is cached by the browser until changed.
                String filename = imageNode.getIdentifier()
                        + LastModified.getLastModified(imageNode).getTimeInMillis();
                StreamResource streamResource = new StreamResource(iss, filename);
                streamResource.setMIMEType(mimeType);
                resource = streamResource;
            } else {
                String path = getGeneratorImagePath(workspace, node, generator);
                if (StringUtils.isNotBlank(path)) {
                    resource = new ExternalResource(path, MediaType.PNG.toString());
                }
            }
        } else {
            resource = createIconFontResource(mimeType);
        }
    } catch (RepositoryException e) {
        log.debug("Could not get name or identifier from imageNode: {}", e.getMessage());
    }
    return resource;
}

From source file:org.haiku.haikudepotserver.pkg.controller.PkgIconController.java

/**
 * @param isAsFallback is true if the request was originally for a package, but fell back to this generic.
 *///ww  w .  j a v a2  s . c  o  m

private void handleGenericHeadOrGet(RequestMethod requestMethod, HttpServletResponse response, Integer size,
        boolean isAsFallback) throws IOException {

    if (null == size) {
        size = 64; // largest natural size
    }

    size = normalizeSize(size);
    byte[] data = renderedPkgIconRepository.renderGeneric(size);
    response.setHeader(HttpHeaders.CONTENT_LENGTH, Integer.toString(data.length));
    response.setContentType(MediaType.PNG.toString());

    if (isAsFallback) {
        response.setHeader(HttpHeaders.CACHE_CONTROL, "no-cache, no-store, must-revalidate");
        response.setHeader(HttpHeaders.PRAGMA, "no-cache");
        response.setHeader(HttpHeaders.EXPIRES, "0");
    } else {
        response.setDateHeader(HttpHeaders.LAST_MODIFIED, startupMillis);
    }

    if (requestMethod == RequestMethod.GET) {
        response.getOutputStream().write(data);
    }
}

From source file:org.apache.aurora.scheduler.http.ServletModule.java

private MediaType getMediaType(String filePath) {
    if (filePath.endsWith(".png")) {
        return MediaType.PNG;
    } else if (filePath.endsWith(".js")) {
        return MediaType.JAVASCRIPT_UTF_8;
    } else if (filePath.endsWith(".html")) {
        return MediaType.HTML_UTF_8;
    } else if (filePath.endsWith(".css")) {
        return MediaType.CSS_UTF_8;
    } else if (filePath.endsWith(".svg")) {
        return MediaType.SVG_UTF_8;
    } else if (filePath.endsWith(".ttf") || filePath.endsWith(".eot") || filePath.endsWith(".woff")) {

        // MediaType doesn't have any mime types for fonts. Instead of magic strings, we let the
        // browser interpret the mime type and modern browsers can do this well.
        return MediaType.ANY_TYPE;
    } else {/*  www.  j  a v a2 s.c o  m*/
        throw new IllegalArgumentException("Could not determine media type for " + filePath);
    }
}