Search in sources :

Example 16 with MediaType

use of com.google.common.net.MediaType in project guava by hceylan.

the class MediaTypeTest method testCreateVideoType.

public void testCreateVideoType() {
    MediaType newType = MediaType.createVideoType("yams");
    assertEquals("video", newType.type());
    assertEquals("yams", newType.subtype());
}
Also used : MediaType(com.google.common.net.MediaType)

Example 17 with MediaType

use of com.google.common.net.MediaType in project guava by hceylan.

the class MediaTypeTest method testGetCharset_tooMany.

public void testGetCharset_tooMany() {
    MediaType mediaType = MediaType.parse("text/plain; charset=utf-8; charset=utf-16");
    try {
        mediaType.charset();
        fail();
    } catch (IllegalStateException expected) {
    }
}
Also used : MediaType(com.google.common.net.MediaType)

Example 18 with MediaType

use of com.google.common.net.MediaType in project keywhiz by square.

the class FileAssetServlet method doGet.

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    try {
        ByteSource asset = loadAsset(req.getRequestURI());
        if (asset == null) {
            resp.sendError(HttpServletResponse.SC_NOT_FOUND);
            return;
        }
        final String mimeTypeOfExtension = req.getServletContext().getMimeType(req.getRequestURI());
        MediaType mediaType = DEFAULT_MEDIA_TYPE;
        if (mimeTypeOfExtension != null) {
            try {
                mediaType = MediaType.parse(mimeTypeOfExtension);
                if (mediaType.is(MediaType.ANY_TEXT_TYPE)) {
                    mediaType = mediaType.withCharset(DEFAULT_CHARSET);
                }
            } catch (IllegalArgumentException ignore) {
            }
        }
        resp.setContentType(mediaType.type() + "/" + mediaType.subtype());
        if (mediaType.charset().isPresent()) {
            resp.setCharacterEncoding(mediaType.charset().get().toString());
        }
        try (OutputStream output = resp.getOutputStream()) {
            asset.copyTo(output);
        }
    } catch (RuntimeException ignored) {
        resp.sendError(HttpServletResponse.SC_NOT_FOUND);
    }
}
Also used : OutputStream(java.io.OutputStream) ByteSource(com.google.common.io.ByteSource) MediaType(com.google.common.net.MediaType)

Aggregations

MediaType (com.google.common.net.MediaType)18 HttpEntity (org.apache.http.HttpEntity)3 ServletOutputStream (javax.servlet.ServletOutputStream)2 ByteSource (com.google.common.io.ByteSource)1 OutputStream (java.io.OutputStream)1 URISyntaxException (java.net.URISyntaxException)1 Charset (java.nio.charset.Charset)1 IllegalCharsetNameException (java.nio.charset.IllegalCharsetNameException)1 UnsupportedCharsetException (java.nio.charset.UnsupportedCharsetException)1 MimeType (javax.activation.MimeType)1 HttpResponse (org.apache.http.HttpResponse)1 Test (org.junit.Test)1