use of com.google.common.net.MediaType in project dropwizard by dropwizard.
the class AssetServlet method doGet.
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
try {
final StringBuilder builder = new StringBuilder(req.getServletPath());
if (req.getPathInfo() != null) {
builder.append(req.getPathInfo());
}
final CachedAsset cachedAsset = loadAsset(builder.toString());
if (cachedAsset == null) {
resp.sendError(HttpServletResponse.SC_NOT_FOUND);
return;
}
if (isCachedClientSide(req, cachedAsset)) {
resp.sendError(HttpServletResponse.SC_NOT_MODIFIED);
return;
}
final String rangeHeader = req.getHeader(HttpHeaders.RANGE);
final int resourceLength = cachedAsset.getResource().length;
ImmutableList<ByteRange> ranges = ImmutableList.of();
boolean usingRanges = false;
// http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html
if (rangeHeader != null) {
final String ifRange = req.getHeader(HttpHeaders.IF_RANGE);
if (ifRange == null || cachedAsset.getETag().equals(ifRange)) {
try {
ranges = parseRangeHeader(rangeHeader, resourceLength);
} catch (NumberFormatException e) {
resp.sendError(HttpServletResponse.SC_REQUESTED_RANGE_NOT_SATISFIABLE);
return;
}
if (ranges.isEmpty()) {
resp.sendError(HttpServletResponse.SC_REQUESTED_RANGE_NOT_SATISFIABLE);
return;
}
resp.setStatus(HttpServletResponse.SC_PARTIAL_CONTENT);
usingRanges = true;
resp.addHeader(HttpHeaders.CONTENT_RANGE, "bytes " + Joiner.on(",").join(ranges) + "/" + resourceLength);
}
}
resp.setDateHeader(HttpHeaders.LAST_MODIFIED, cachedAsset.getLastModifiedTime());
resp.setHeader(HttpHeaders.ETAG, cachedAsset.getETag());
final String mimeTypeOfExtension = req.getServletContext().getMimeType(req.getRequestURI());
MediaType mediaType = DEFAULT_MEDIA_TYPE;
if (mimeTypeOfExtension != null) {
try {
mediaType = MediaType.parse(mimeTypeOfExtension);
if (defaultCharset != null && mediaType.is(MediaType.ANY_TEXT_TYPE)) {
mediaType = mediaType.withCharset(defaultCharset);
}
} catch (IllegalArgumentException ignore) {
// ignore
}
}
if (mediaType.is(MediaType.ANY_VIDEO_TYPE) || mediaType.is(MediaType.ANY_AUDIO_TYPE) || usingRanges) {
resp.addHeader(HttpHeaders.ACCEPT_RANGES, "bytes");
}
resp.setContentType(mediaType.type() + '/' + mediaType.subtype());
if (mediaType.charset().isPresent()) {
resp.setCharacterEncoding(mediaType.charset().get().toString());
}
try (ServletOutputStream output = resp.getOutputStream()) {
if (usingRanges) {
for (ByteRange range : ranges) {
output.write(cachedAsset.getResource(), range.getStart(), range.getEnd() - range.getStart() + 1);
}
} else {
output.write(cachedAsset.getResource());
}
}
} catch (RuntimeException | URISyntaxException ignored) {
resp.sendError(HttpServletResponse.SC_NOT_FOUND);
}
}
use of com.google.common.net.MediaType in project guava by hceylan.
the class MediaTypeTest method testCreateAudioType.
public void testCreateAudioType() {
MediaType newType = MediaType.createAudioType("yams");
assertEquals("audio", newType.type());
assertEquals("yams", newType.subtype());
}
use of com.google.common.net.MediaType in project guava by hceylan.
the class MediaTypeTest method testWithParameters_invalidAttribute.
public void testWithParameters_invalidAttribute() {
MediaType mediaType = MediaType.parse("text/plain");
ImmutableListMultimap<String, String> parameters = ImmutableListMultimap.of("a", "1", "@", "2", "b", "3");
try {
mediaType.withParameters(parameters);
fail();
} catch (IllegalArgumentException expected) {
}
}
use of com.google.common.net.MediaType in project guava by hceylan.
the class MediaTypeTest method testWithParameter_invalidAttribute.
public void testWithParameter_invalidAttribute() {
MediaType mediaType = MediaType.parse("text/plain");
try {
mediaType.withParameter("@", "2");
fail();
} catch (IllegalArgumentException expected) {
}
}
use of com.google.common.net.MediaType in project guava by hceylan.
the class MediaTypeTest method testGetCharset_unsupportedCharset.
public void testGetCharset_unsupportedCharset() {
MediaType mediaType = MediaType.parse("text/plain; charset=utf-wtf");
try {
mediaType.charset();
fail();
} catch (UnsupportedCharsetException expected) {
}
}
Aggregations