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());
}
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) {
}
}
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);
}
}
Aggregations