List of usage examples for com.google.common.net MediaType parse
public static MediaType parse(String input)
From source file:co.freeside.betamax.io.ContentTypes.java
public static boolean isTextContentType(String contentType) { if (contentType == null) { return false; }//w ww . j a v a2 s . co 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:michal.szymanski.util.URLs.java
public static MediaType getMimeTypeOf(URL url) { HttpURLConnection connection; String result = null;/* w ww . j a v a 2s . c o m*/ try { connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod(HttpMethod.HEAD); connection.connect(); result = connection.getContentType(); } catch (Exception e) { result = MediaType.ANY_TYPE.toString(); } return MediaType.parse(result); }
From source file:be.nbb.cli.util.Utils.java
@Nonnull public static Optional<MediaType> getMediaType(@Nonnull Optional<String> mediaType, @Nonnull Optional<File> file) { if (mediaType.isPresent()) { return Optional.of(MediaType.parse(mediaType.get())); }/*w w w .ja va 2s .co m*/ if (file.isPresent()) { return getMediaType(file.get()); } return Optional.empty(); }
From source file:google.registry.security.JsonHttp.java
/** * Extracts a JSON object from a servlet request. * * @return JSON object or {@code null} on error, in which case servlet should return. * @throws IOException if we failed to read from {@code req}. *///w ww . j a v a 2 s .c o m @Nullable @SuppressWarnings("unchecked") public static Map<String, ?> read(HttpServletRequest req) throws IOException { if (!"POST".equals(req.getMethod()) && !"PUT".equals(req.getMethod())) { logger.warning("JSON request payload only allowed for POST/PUT"); return null; } if (!JSON_UTF_8.is(MediaType.parse(req.getContentType()))) { logger.warningfmt("Invalid JSON Content-Type: %s", req.getContentType()); return null; } try (Reader jsonReader = req.getReader()) { try { return checkNotNull((Map<String, ?>) JSONValue.parseWithException(jsonReader)); } catch (ParseException | NullPointerException | ClassCastException e) { logger.warning(e, "Malformed JSON"); return null; } } }
From source file:at.ac.univie.isc.asio.matcher.MimeMatcher.java
@Override protected boolean matchesSafely(final String item) { final MediaType actual = MediaType.parse(item.toLowerCase(Locale.ENGLISH)); return mainTypeCompatible(actual) && subTypeCompatible(actual) && parametersCompatible(actual); }
From source file:io.rebolt.http.utils.HeaderParser.java
public HeaderParser(final String headerString) { requireNonNull(headerString);/* w ww. j ava2 s. c om*/ this.origin = headerString; this.mediaTypes = Lists.newLinkedList(); StringUtil.split(',', headerString).forEach(entry -> { try { mediaTypes.add(MediaType.parse(entry)); } catch (Exception ignored) { } }); }
From source file:org.wisdom.api.http.Negotiation.java
/** * 'Accept' based negotiation.// w w w .j a va2 s .com * 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:at.ac.univie.isc.asio.matcher.RestAssuredMatchers.java
/** * handles 'type/subtype+extension' format *//* ww w . j av a 2s . c o m*/ public static Matcher<String> compatibleTo(final String rawExpectedType) { return new MimeMatcher(MediaType.parse(rawExpectedType.toLowerCase(Locale.ENGLISH))); }
From source file:software.betamax.message.AbstractMessage.java
@Override public String getContentType() { String header = getHeader(CONTENT_TYPE); if (Strings.isNullOrEmpty(header)) { return DEFAULT_CONTENT_TYPE; } else {// w w w. j ava2s.co m return MediaType.parse(header).withoutParameters().toString(); } }
From source file:google.registry.request.RequestModule.java
@Provides @Header("Content-Type") static MediaType provideContentType(HttpServletRequest req) { try {//from w ww . jav a 2 s . c om return MediaType.parse(req.getContentType()); } catch (IllegalArgumentException | NullPointerException e) { throw new UnsupportedMediaTypeException("Bad Content-Type header", e); } }