public JacksonParser(final ObjectMapper mapper, final MediaType type) { this.mapper = mapper; this.matcher = MediaType.matcher(type); }
/** * Produces a matcher for the given media type. * * @param acceptable The acceptable/target media type. * @return A media type matcher. */ public static Matcher matcher(final MediaType acceptable) { return matcher(ImmutableList.of(acceptable)); }
/** * Test if the route definition can consume a media type. * * @param type A media type to test. * @return True, if the route can consume the given media type. */ public boolean canConsume(final MediaType type) { return MediaType.matcher(Arrays.asList(type)).matches(consumes); }
/** * Test if the route definition can consume a media type. * * @param types A media types to test. * @return True, if the route can produces the given media type. */ public boolean canProduce(final List<MediaType> types) { return MediaType.matcher(types).matches(produces); }
@Override public boolean accepts(final MediaType type) { if (matcher == null) { matcher = MediaType.matcher(produces); } return matcher.matches(type); }
@Override @SuppressWarnings("unchecked") public <T> T get(final List<MediaType> types) { Supplier<Object> provider = MediaType .matcher(types) .first(ImmutableList.copyOf(data.keySet())) .map(it -> data.remove(it)) .orElseThrow( () -> new Err(Status.NOT_ACCEPTABLE, Joiner.on(", ").join(types))); return (T) provider.get(); }
@Override public Optional<MediaType> accepts(final List<MediaType> types) { requireNonNull(types, "Media types are required."); return MediaType.matcher(accept()).first(types); }
/** * Test if the route definition can consume a media type. * * @param type A media type to test. * @return True, if the route can consume the given media type. */ public boolean canConsume(final String type) { return MediaType.matcher(MediaType.valueOf(type)).matches(consumes); }
/** * Test if the route matches the given verb, path, content type and accept header. * * @param method A HTTP verb. * @param path Current HTTP path. * @param contentType The <code>Content-Type</code> header. * @param accept The <code>Accept</code> header. * @return A route or an empty optional. */ @Nonnull public Optional<Route> matches(final String method, final String path, final MediaType contentType, final List<MediaType> accept) { String fpath = method + path; if (excludes.size() > 0 && excludes(fpath)) { return Optional.empty(); } RouteMatcher matcher = cpattern.matcher(fpath); if (matcher.matches()) { List<MediaType> result = MediaType.matcher(accept).filter(this.produces); if (result.size() > 0 && canConsume(contentType)) { // keep accept when */* List<MediaType> produces = result.size() == 1 && result.get(0).name().equals("*/*") ? accept : this.produces; return Optional .of(asRoute(method, matcher, produces, new RouteSourceImpl(declaringClass, line))); } } return Optional.empty(); }
public JacksonParser(final ObjectMapper mapper, final MediaType type) { this.mapper = mapper; this.matcher = MediaType.matcher(type); }
/** * Produces a matcher for the given media type. * * @param acceptable The acceptable/target media type. * @return A media type matcher. */ public static Matcher matcher(final MediaType acceptable) { return matcher(ImmutableList.of(acceptable)); }
/** * Test if the route definition can consume a media type. * * @param type A media type to test. * @return True, if the route can consume the given media type. */ public boolean canConsume(final MediaType type) { return MediaType.matcher(Arrays.asList(type)).matches(consumes); }
@Override public boolean accepts(final MediaType type) { if (matcher == null) { matcher = MediaType.matcher(produces); } return matcher.matches(type); }
/** * Test if the route definition can consume a media type. * * @param types A media types to test. * @return True, if the route can produces the given media type. */ public boolean canProduce(final List<MediaType> types) { return MediaType.matcher(types).matches(produces); }
@Override @SuppressWarnings("unchecked") public <T> T get(final List<MediaType> types) { Supplier<Object> provider = MediaType .matcher(types) .first(ImmutableList.copyOf(data.keySet())) .map(it -> data.remove(it)) .orElseThrow( () -> new Err(Status.NOT_ACCEPTABLE, Joiner.on(", ").join(types))); return (T) provider.get(); }
@Override public Optional<MediaType> accepts(final List<MediaType> types) { requireNonNull(types, "Media types are required."); return MediaType.matcher(accept()).first(types); }
/** * Test if the route definition can consume a media type. * * @param type A media type to test. * @return True, if the route can consume the given media type. */ public boolean canConsume(final String type) { return MediaType.matcher(MediaType.valueOf(type)).matches(consumes); }
/** * Test if the route matches the given verb, path, content type and accept header. * * @param method A HTTP verb. * @param path Current HTTP path. * @param contentType The <code>Content-Type</code> header. * @param accept The <code>Accept</code> header. * @return A route or an empty optional. */ @Nonnull public Optional<Route> matches(final String method, final String path, final MediaType contentType, final List<MediaType> accept) { String fpath = method + path; if (excludes.size() > 0 && excludes(fpath)) { return Optional.empty(); } RouteMatcher matcher = cpattern.matcher(fpath); if (matcher.matches()) { List<MediaType> result = MediaType.matcher(accept).filter(this.produces); if (result.size() > 0 && canConsume(contentType)) { // keep accept when */* List<MediaType> produces = result.size() == 1 && result.get(0).name().equals("*/*") ? accept : this.produces; return Optional .of(asRoute(method, matcher, produces, new RouteSourceImpl(declaringClass, line))); } } return Optional.empty(); }