/** * Given: * * <pre> * text/html, application/xhtml; {@literal *}/{@literal *} * </pre> * * <pre> * first(text/html) -> returns text/html * first(application/json) -> returns application/json * </pre> * * @param candidates One ore more candidates media type. Required. * @return A first most relevant media type or an empty optional. */ private Optional<MediaType> doFirst(final List<MediaType> candidates) { List<MediaType> result = filter(candidates); return result.size() == 0 ? Optional.empty() : Optional.of(result.get(0)); } }
/** * Given: * * <pre> * text/html, application/xhtml; {@literal *}/{@literal *} * </pre> * * <pre> * matches(text/html) // true through text/html * matches(application/json) // true through {@literal *}/{@literal *} * </pre> * * @param candidates One ore more candidates media type. Required. * @return True if the matcher matches the given media type. */ public boolean matches(final List<MediaType> candidates) { return filter(candidates).size() > 0; }
/** * 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(); }
/** * 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(); }
/** * Given: * * <pre> * text/html, application/xhtml; {@literal *}/{@literal *} * </pre> * * <pre> * first(text/html) -> returns text/html * first(application/json) -> returns application/json * </pre> * * @param candidates One ore more candidates media type. Required. * @return A first most relevant media type or an empty optional. */ private Optional<MediaType> doFirst(final List<MediaType> candidates) { List<MediaType> result = filter(candidates); return result.size() == 0 ? Optional.empty() : Optional.of(result.get(0)); } }
/** * Given: * * <pre> * text/html, application/xhtml; {@literal *}/{@literal *} * </pre> * * <pre> * matches(text/html) // true through text/html * matches(application/json) // true through {@literal *}/{@literal *} * </pre> * * @param candidates One ore more candidates media type. Required. * @return True if the matcher matches the given media type. */ public boolean matches(final List<MediaType> candidates) { return filter(candidates).size() > 0; }