/** * Convert one or more media types expressed as String into a {@link MediaType}. * * @param types Media types to parse. * @return An list of immutable {@link MediaType}. * @throws Err.BadMediaType For bad media types. */ public static List<MediaType> valueOf(final String... types) throws Err.BadMediaType { requireNonNull(types, "Types are required."); List<MediaType> result = new ArrayList<>(); for (String type : types) { result.add(valueOf(type)); } return result; }
/** * Get a {@link MediaType} for a file extension. * * @param ext A file extension, like <code>js</code> or <code>css</code>. * @return A {@link MediaType} or empty optional for unknown file extensions. */ public static Optional<MediaType> byExtension(final String ext) { requireNonNull(ext, "An ext is required."); String key = "mime." + ext; if (types.hasPath(key)) { return Optional.of(MediaType.valueOf(types.getString("mime." + ext))); } return Optional.empty(); }
/** * True if the given type matches the <code>Accept</code> header. * * @param type The type to check for. * @return True if the given type matches the <code>Accept</code> header. */ default boolean accepts(final String type) { return accepts(MediaType.valueOf(type)); }
/** * Set the content type of this content. * * @param type A content type. * @return This content. */ @Nonnull public Result type(final String type) { return type(MediaType.valueOf(type)); }
/** * Set event media type. Useful for sengin json, xml, etc.. * * @param type Media Type. * @return This event. */ public Event type(final String type) { return type(MediaType.valueOf(type)); }
/** * Set the media types the route can produces. * * @param produces The media types to test for. * @return This instance. */ @Nonnull default T produces(final String... produces) { return produces(MediaType.valueOf(produces)); }
/** * 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 String... types) { return canProduce(MediaType.valueOf(types)); }
/** * Set the <code>Content-Type</code> header IF and ONLY IF, no <code>Content-Type</code> was set * yet. * * @param type A suggested type to use if one is missing. * @return This context. */ default Context type(final String type) { return type(MediaType.valueOf(type)); }
/** * Set the response media type and set the <code>Content-Type</code> header. * * @param type A media type. * @return This response. */ @Nonnull default Response type(final String type) { return type(MediaType.valueOf(type)); }
/** * Set the json type supported by this module, default is: <code>application/json</code>. * * @param type Media type. * @return This module. */ public Jackson type(final String type) { return type(MediaType.valueOf(type)); }
/** * Set the media types the route can produces. * * @param type The media types to test for. * @return This route definition. */ public Definition produces(final String type) { return produces(MediaType.valueOf(type)); }
/** * Set the media types the route can consume. * * @param consumes The media types to test for. * @return This instance. */ @Nonnull default T consumes(final String... consumes) { return consumes(MediaType.valueOf(consumes)); }
/** * Convert a raw value to the given type. This method will temporary set {@link MediaType} before * parsing a value, useful if a form field from a HTTP POST was send as json (or any other data). * * @param type The type to convert to. * @param mtype A media type to hint a parser. * @param <T> Target type. * @return Get a value when possible. */ @Nonnull default <T> T to(final Class<T> type, final String mtype) { return to(type, MediaType.valueOf(mtype)); }
/** * Add a when clause for a custom result for the given media-type. * * @param type A media type to test for. * @param supplier An object supplier. * @return This result. */ @Nonnull public Result when(final String type, final Supplier<Object> supplier) { return when(MediaType.valueOf(type), supplier); }
/** * Set the media types the route can consume. * * @param type The media types to test for. * @return This route definition. */ public Definition consumes(final String type) { return consumes(MediaType.valueOf(type)); }
/** * Convert a raw value to the given type. This method will temporary set {@link MediaType} before * parsing a value, useful if a form field from a HTTP POST was send as json (or any other data). * * @param type The type to convert to. * @param mtype A media type to hint a parser. * @param <T> Target type. * @return Get a value when possible. */ @Nonnull default <T> T to(final TypeLiteral<T> type, final String mtype) { return to(type, MediaType.valueOf(mtype)); }
private static List<MediaType> produces(final Method method) { Function<AnnotatedElement, Optional<List<MediaType>>> fn = (element) -> { Produces produces = element.getAnnotation(Produces.class); if (produces != null) { return Optional.of(MediaType.valueOf(produces.value())); } return Optional.empty(); }; // method level return fn.apply(method) // class level .orElseGet(() -> fn.apply(method.getDeclaringClass()) // none .orElse(MediaType.ALL)); }
private static List<MediaType> consumes(final Method method) { Function<AnnotatedElement, Optional<List<MediaType>>> fn = (element) -> { Consumes consumes = element.getAnnotation(Consumes.class); if (consumes != null) { return Optional.of(MediaType.valueOf(consumes.value())); } return Optional.empty(); }; // method level return fn.apply(method) // class level .orElseGet(() -> fn.apply(method.getDeclaringClass()) // none .orElse(MediaType.ALL)); }
@Override public void handle(final Request req, final Response rsp) throws Throwable { String CRLF = "\r\n"; StringBuilder buffer = new StringBuilder("TRACE ").append(req.path()) .append(" ").append(req.protocol()); for (Entry<String, Mutant> entry : req.headers().entrySet()) { buffer.append(CRLF).append(entry.getKey()).append(": ") .append(entry.getValue().toList(String.class).stream().collect(Collectors.joining(", "))); } buffer.append(CRLF); rsp.type(MediaType.valueOf("message/http")); rsp.length(buffer.length()); rsp.send(buffer.toString()); }
/** * 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); }