Java Code Examples for com.google.common.net.MediaType#is()
The following examples show how to use
com.google.common.net.MediaType#is() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DownloadTraceHttpHelper.java From tracecompass with Eclipse Public License 2.0 | 6 votes |
/** * Try to find the name of the file by using connection information. * * @param connection * HTTP connection * @return File name */ private static String getFileName(HttpURLConnection connection) { String fileName = getLastSegmentUrl(connection.getURL().toString()); String contentType = connection.getContentType(); if (contentType != null) { MediaType type = MediaType.parse(contentType); if (type.is(MediaType.ANY_APPLICATION_TYPE)) { String contentDisposition = connection.getHeaderField(CONTENT_DISPOSITION); if (contentDisposition != null) { String[] content = contentDisposition.split(";"); //$NON-NLS-1$ for (String string : content) { if (string.contains("filename=")) { //$NON-NLS-1$ int index = string.indexOf('"'); fileName = string.substring(index + 1, string.length() - 1); } } } } } return fileName; }
Example 2
Source File: Html5AttachmentGenerator.java From JGiven with Apache License 2.0 | 6 votes |
private File writeFile( AttachmentModel attachment, MediaType mediaType ) { String extension = getExtension( mediaType ); File targetFile = getTargetFile( attachment.getFileName(), extension ); try { if( attachment.isBinary() ) { if( mediaType.is( MediaType.ANY_IMAGE_TYPE ) ) { File thumbFile = getThumbnailFileFor( targetFile ); byte[] thumbnail = compressToThumbnail( attachment.getValue(), extension ); Files.write( thumbnail, thumbFile ); } Files.write( BaseEncoding.base64().decode( attachment.getValue() ), targetFile ); } else { Files.write( attachment.getValue(), targetFile, Charsets.UTF_8 ); } } catch( IOException e ) { log.error( "Error while trying to write attachment to file " + targetFile, e ); } return targetFile; }
Example 3
Source File: Negotiation.java From wisdom with Apache License 2.0 | 6 votes |
/** * 'Accept' based negotiation. * 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); }
Example 4
Source File: RequestFromVertx.java From wisdom with Apache License 2.0 | 6 votes |
/** * Check if this request accepts a given media type. * * @return true if <code>mimeType</code> is in the Accept header, otherwise false */ @Override public boolean accepts(String mimeType) { String contentType = request.headers().get(HeaderNames.ACCEPT); if (contentType == null) { contentType = MimeTypes.HTML; } // For performance reason, we first try a full match: if (contentType.contains(mimeType)) { return true; } // Else check the media types: MediaType input = MediaType.parse(mimeType); for (MediaType type : mediaTypes()) { if (input.is(type)) { return true; } } return false; }
Example 5
Source File: FakeRequest.java From wisdom with Apache License 2.0 | 6 votes |
/** * Checks if this request accepts a given media type. * * @param mimeType the mime type to check. * @return {@literal null} as this is a fake request. */ @Override public boolean accepts(String mimeType) { String contentType = getHeader(HeaderNames.ACCEPT); if (contentType == null) { contentType = MimeTypes.HTML; } // For performance reason, we first try a full match: if (contentType.contains(mimeType)) { return true; } // Else check the media types: MediaType input = MediaType.parse(mimeType); for (MediaType type : mediaTypes()) { if (input.is(type)) { return true; } } return false; }
Example 6
Source File: Engine.java From wisdom with Apache License 2.0 | 6 votes |
/** * 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 */ @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; }
Example 7
Source File: HttpHandler.java From cassandra-exporter with Apache License 2.0 | 5 votes |
/*** * @return map of matched SupportedMediaTypes -> AcceptedMediaTypes */ private Multimap<MediaType, MediaType> checkAndGetPreferredMediaTypes(final List<MediaType> acceptedMediaTypes, final MediaType... supportedMediaTypes) { if (acceptedMediaTypes.isEmpty()) { // client didn't state that what they want, so give them the first preference final MediaType firstSupportedType = supportedMediaTypes[0]; return ImmutableMultimap.of(firstSupportedType, firstSupportedType); } final ImmutableMultimap.Builder<MediaType, MediaType> preferredMediaTypes = ImmutableMultimap.builder(); outer: for (final MediaType acceptedMediaType : acceptedMediaTypes) { for (final MediaType supportedMediaType : supportedMediaTypes) { if (supportedMediaType.is(acceptedMediaType.withoutParameters())) { preferredMediaTypes.put(supportedMediaType, acceptedMediaType); continue outer; } } } final ImmutableMultimap<MediaType, MediaType> map = preferredMediaTypes.build(); if (map.isEmpty()) { throw new HttpException(HttpResponseStatus.UNSUPPORTED_MEDIA_TYPE, "None of the specified acceptable media types are supported for this URI."); } return map; }
Example 8
Source File: HttpAssetServlet.java From robe with GNU Lesser General Public License v3.0 | 5 votes |
private void decideMimeAndEncoding(HttpServletRequest req, HttpServletResponse resp) { //Decide mimetype String mime = req.getServletContext().getMimeType(req.getRequestURI()); MediaType mediaType = (mime == null) ? DEFAULT_MEDIA_TYPE : MediaType.parse(mime); if (defaultCharset != null && mediaType.is(MediaType.ANY_TEXT_TYPE)) { mediaType = mediaType.withCharset(defaultCharset); } resp.setContentType(mediaType.type() + '/' + mediaType.subtype()); if (mediaType.charset().isPresent()) { resp.setCharacterEncoding(mediaType.charset().get().toString()); } }
Example 9
Source File: FileAssetServlet.java From robe with GNU Lesser General Public License v3.0 | 5 votes |
private void decideMimeAndEncoding(HttpServletRequest req, HttpServletResponse resp) { //Decide mimetype String mime = req.getServletContext().getMimeType(req.getRequestURI()); MediaType mediaType = (mime == null) ? DEFAULT_MEDIA_TYPE : MediaType.parse(mime); if (defaultCharset != null && mediaType.is(MediaType.ANY_TEXT_TYPE)) { mediaType = mediaType.withCharset(defaultCharset); } resp.setContentType(mediaType.type() + '/' + mediaType.subtype()); if (mediaType.charset().isPresent()) { resp.setCharacterEncoding(mediaType.charset().get().toString()); } }
Example 10
Source File: Html5AttachmentGenerator.java From JGiven with Apache License 2.0 | 5 votes |
private String getExtension( MediaType mediaType ) { if( mediaType.is( MediaType.SVG_UTF_8 ) ) { return "svg"; } if( mediaType.is( MediaType.ICO ) ) { return "ico"; } if( mediaType.is( MediaType.BMP ) ) { return "bmp"; } return mediaType.subtype(); }