List of usage examples for com.google.common.net MediaType is
public boolean is(MediaType mediaTypeRange)
From source file:co.freeside.betamax.io.ContentTypes.java
public static boolean isTextContentType(String contentType) { if (contentType == null) { return false; }// w ww . j ava2s . c o m MediaType mediaType = MediaType.parse(contentType); if (mediaType.is(ANY_TEXT_TYPE) || mediaType.is(FORM_DATA)) { return true; } if (mediaType.is(ANY_APPLICATION_TYPE) && TEXT_CONTENT_TYPE_PATTERN.matcher(mediaType.subtype()).matches()) { return true; } return false; }
From source file:org.wisdom.api.http.Negotiation.java
/** * 'Accept' based negotiation.//from w w w . ja v a 2 s .c o m * This method determines the result to send to the client based on the 'Accept' header of the request. * The returned result is enhanced with the 'Vary' header set to {@literal Accept}. * <p/> * This methods retrieves the accepted media type in their preference order and check, * one-by-one if the given results match one of them. So, it ensures we get the most acceptable result. * * @param results the set of result structured as follows: mime-type -> result. The mime-type (keys) must be * valid mime type such as 'application/json' or 'text/html'. * @return the selected result, or a result with the status {@link org.wisdom.api.http.Status#NOT_ACCEPTABLE} if * none of the given results match the request. */ public static Result accept(Map<String, ? extends Result> results) { Context context = Context.CONTEXT.get(); if (context == null) { throw new IllegalStateException("Negotiation cannot be achieved outside of a request"); } Collection<MediaType> accepted = context.request().mediaTypes(); // accepted cannot be empty, if the header is missing text/* is added. for (MediaType media : accepted) { // Do we have a matching key. for (Map.Entry<String, ? extends Result> entry : results.entrySet()) { MediaType input = MediaType.parse(entry.getKey()); if (input.is(media)) { return entry.getValue().with(HeaderNames.VARY, HeaderNames.ACCEPT); } } } return Results.status(Status.NOT_ACCEPTABLE); }
From source file:helper.ThumbnailGenerator.java
/** * @param ts/*from www . ja v a2s. c o m*/ * 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:helper.ThumbnailGenerator.java
private static File generateMimeTypeImage(MediaType contentType, int size, String name) { File result = null;//from w w w. j av a 2s. c om try { if (contentType.is(MediaType.ANY_AUDIO_TYPE)) { result = generateThumbnailFromImage(Play.application().resourceAsStream(AUDIO_PIC), size, "png", name); } else if (contentType.is(MediaType.ANY_IMAGE_TYPE)) { result = generateThumbnailFromImage(Play.application().resourceAsStream(IMAGE_PIC), size, "png", name); } else if (contentType.is(MediaType.ANY_TEXT_TYPE) || contentType.is(MediaType.OOXML_DOCUMENT) || contentType.is(MediaType.MICROSOFT_WORD)) { result = generateThumbnailFromImage(Play.application().resourceAsStream(TEXT_PIC), size, "png", name); } else if (contentType.is(MediaType.ANY_VIDEO_TYPE)) { result = generateThumbnailFromImage(Play.application().resourceAsStream(VIDEO_PIC), size, "png", name); } else if (contentType.is(MediaType.ZIP)) { result = generateThumbnailFromImage(Play.application().resourceAsStream(ZIP_PIC), size, "png", name); } else if (contentType.is(MediaType.PDF)) { result = generateThumbnailFromImage(Play.application().resourceAsStream(PDF_PIC), size, "png", name); } else { result = generateThumbnailFromImage(Play.application().resourceAsStream(MIMETYPE_NOT_FOUND_PIC), size, "png", name); } } catch (Throwable e) { play.Logger.warn("", e); result = generateThumbnailFromImage(Play.application().resourceAsStream(EXCEPTION_ON_APPLY_MIMETYPE), size, "png", name); } return result; }
From source file:io.github.mike10004.vhs.testsupport.MakeFileUploadHar.java
private static NanoHTTPD.Response parseBody(MediaType contentType, byte[] data, Map<String, TypedContent> storage) { if (contentType.is(MediaType.parse("multipart/form-data"))) { @Nullable//from w w w .ja va2 s .c o m String boundary = contentType.parameters().get("boundary").stream().findFirst().orElse(null); if (boundary == null) { return NanoHTTPD.newFixedLengthResponse(NanoHTTPD.Response.Status.BAD_REQUEST, "text/plain", "Bad Request: Content type is multipart/form-data but boundary missing"); } List<FormDataPart> formDataParts; try { formDataParts = new NanohttpdFormDataParser().decodeMultipartFormData(contentType, data); } catch (MultipartFormDataParser.BadMultipartFormDataException e) { e.printStackTrace(System.err); return NanoHTTPD.newFixedLengthResponse( MakeTestHar.lookup(MultipartFormDataParser.BadMultipartFormDataException.STATUS_CODE), "text/plain", e.getMessage()); } System.out.format("%d parts parsed from request body%n", formDataParts.size()); formDataParts.forEach(part -> { System.out.format(" %s%n", part); part.headers.forEach((name, value) -> { System.out.format(" %s: %s%n", name, value); }); }); List<TypedContent> files = formDataParts.stream() .filter(p -> p.contentDisposition != null && p.contentDisposition.getFilename() != null) .map(p -> p.file).collect(Collectors.toList()); if (files.isEmpty()) { return NanoHTTPD.newFixedLengthResponse(NanoHTTPD.Response.Status.BAD_REQUEST, "text/plain", "Bad Request: no files"); } AtomicReference<UUID> lastId = new AtomicReference<>(null); files.forEach(fileData -> { UUID id = UUID.randomUUID(); lastId.set(id); storage.put(id.toString(), fileData); }); return redirect("/file?id=" + lastId.get()); } else { return NanoHTTPD.newFixedLengthResponse(NanoHTTPD.Response.Status.BAD_REQUEST, "text/plain", "Bad Request: expected content type = multipart/form-data"); } }
From source file:org.waveprotocol.box.server.rpc.AttachmentServlet.java
/** * Check if mime type is suitable to be deliver as an inline content * or as a file./*from ww w.j a v a 2 s . co m*/ * @param mimeType * @return */ private static boolean isWebContent(String mimeType) { boolean isWebContent = false; try { MediaType mt = MediaType.parse(mimeType); isWebContent = mt.is(MediaType.ANY_IMAGE_TYPE) || mt.is(MediaType.ANY_VIDEO_TYPE); } catch (IllegalArgumentException e) { LOG.warning("Unable to decode mime type " + mimeType != null ? mimeType : "null"); } return isWebContent; }
From source file:org.inakirj.imagerulette.utils.ImageUtils.java
/** * Validate HTTP URI.//from w w w . ja v a2s. c o m * * @param uri * the URI * @return true, if given URI is an static image */ public static boolean isValidImageURI(String uri) { HttpURLConnection con; try { URL obj = new URL(uri); con = (HttpURLConnection) obj.openConnection(); String contentType = con.getContentType(); MediaType mt = MediaType.parse(contentType); return mt.is(MediaType.ANY_IMAGE_TYPE); } catch (Exception e) { return false; } }
From source file:com.github.avarabyeu.restendpoint.serializer.ByteArraySerializer.java
@Override public final boolean canRead(MediaType mimeType, Class<?> resultType) { return mimeType.is(MediaType.ANY_TYPE) && byte[].class.equals(resultType); }
From source file:org.wisdom.content.engines.Engine.java
/** * Finds the 'best' content serializer for the given accept headers. * * @param mediaTypes the ordered set of {@link com.google.common.net.MediaType} from the {@code ACCEPT} header. * @return the best serializer from the list matching the {@code ACCEPT} header, {@code null} if none match *//*from w w w . jav a 2 s.co m*/ @Override public ContentSerializer getBestSerializer(Collection<MediaType> mediaTypes) { if (mediaTypes == null || mediaTypes.isEmpty()) { mediaTypes = ImmutableList.of(MediaType.HTML_UTF_8); } for (MediaType type : mediaTypes) { for (ContentSerializer ser : serializers) { MediaType mt = MediaType.parse(ser.getContentType()); if (mt.is(type.withoutParameters())) { return ser; } } } return null; }
From source file:com.github.avarabyeu.restendpoint.serializer.StringSerializer.java
/** * Checks whether mime types is supported by this serializer implementation *///from w w w .j a v a2s . c o m @Override public boolean canRead(@Nonnull MediaType mimeType, Class<?> resultType) { MediaType type = mimeType.withoutParameters(); return (type.is(MediaType.ANY_TEXT_TYPE) || MediaType.APPLICATION_XML_UTF_8.withoutParameters().is(type) || MediaType.JSON_UTF_8.withoutParameters().is(type)) && String.class.equals(resultType); }