@Override public boolean matches(final MediaType type) { return MediaType.js.matches(type); }
private static boolean multipart(final HttpServletRequest req) { String contentType = req.getContentType(); return contentType != null && contentType.toLowerCase().startsWith(MediaType.multipart.name()); }
/** * 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; }
public RequestImpl(final Injector injector, final NativeRequest req, final String contextPath, final int port, final Route route, final Charset charset, final List<Locale> locales, final Map<Object, Object> scope, final Map<String, Object> locals, final long timestamp) { this.injector = injector; this.req = req; this.route = route; this.scope = scope; this.locals = locals; this.contextPath = contextPath; Optional<String> accept = req.header("Accept"); this.accept = accept.isPresent() ? MediaType.parse(accept.get()) : MediaType.ALL; this.lang = req.header("Accept-Language"); this.locales = locales; this.port = port; Optional<String> type = req.header("Content-Type"); this.type = type.isPresent() ? MediaType.valueOf(type.get()) : MediaType.all; String cs = this.type.params().get("charset"); this.charset = cs != null ? Charset.forName(cs) : charset; this.files = new ArrayList<>(); this.timestamp = timestamp; }
@Override public Object parse(final TypeLiteral<?> type, final Context ctx) throws Throwable { MediaType ctype = ctx.type(); if (ctype.isAny()) { // */* return ctx.next(); } if (ctype.matches(this.type)) { return ctx .ifbody(body -> gson.fromJson(body.text(), type.getType())) .ifparam(values -> gson.fromJson(values.first(), type.getType())); } return ctx.next(); }
private static Err handle406or415(final Set<Route.Definition> routeDefs, final String method, final String path, final MediaType contentType, final List<MediaType> accept) { for (Route.Definition routeDef : routeDefs) { Optional<Route> route = routeDef.matches(method, path, MediaType.all, MediaType.ALL); if (route.isPresent() && !route.get().pattern().contains("*")) { if (!routeDef.canProduce(accept)) { return new Err(Status.NOT_ACCEPTABLE, accept.stream() .map(MediaType::name) .collect(Collectors.joining(", "))); } if (!contentType.isAny()) { return new Err(Status.UNSUPPORTED_MEDIA_TYPE, contentType.name()); } } } return null; }
private FormData parseForm() { if (form == null) { form = NO_FORM; try { String tmpdir = conf.getString("application.tmpdir"); String charset = conf.getString("application.charset"); String value = exchange.getRequestHeaders().getFirst("Content-Type"); if (value != null) { MediaType type = MediaType.valueOf(value); if (MediaType.form.name().equals(type.name())) { blocking.get(); form = new FormEncodedDataDefinition() .setDefaultEncoding(charset) .create(exchange) .parseBlocking(); } else if (MediaType.multipart.name().equals(type.name())) { blocking.get(); form = new MultiPartParserDefinition() .setTempFileLocation(new File(tmpdir).toPath()) .setDefaultEncoding(charset) .create(exchange) .parseBlocking(); } } } catch (IOException x) { } } return form; }
/** * 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); }
@Override public Object parse(final TypeLiteral<?> type, final Context ctx) throws Throwable { MediaType ctype = ctx.type(); if (ctype.isAny()) { // */* return ctx.next(); } JavaType javaType = mapper.constructType(type.getType()); if (matcher.matches(ctype) && mapper.canDeserialize(javaType)) { return ctx .ifparam(values -> mapper.readValue(values.iterator().next(), javaType)) .ifbody(body -> mapper.readValue(body.bytes(), javaType)); } return ctx.next(); }
@Override public Object parse(final TypeLiteral<?> type, final Context ctx) throws Throwable { MediaType ctype = ctx.type(); if (ctype.isAny()) { // */* return ctx.next(); } if (ctype.matches(this.type)) { return ctx .ifbody(body -> jsonb.fromJson(body.text(), type.getType())) .ifparam(values -> jsonb.fromJson(values.first(), type.getType())); } return ctx.next(); }
private static Err handle406or415(final Set<Route.Definition> routeDefs, final String method, final String path, final MediaType contentType, final List<MediaType> accept) { for (Route.Definition routeDef : routeDefs) { Optional<Route> route = routeDef.matches(method, path, MediaType.all, MediaType.ALL); if (route.isPresent() && !route.get().pattern().contains("*")) { if (!routeDef.canProduce(accept)) { return new Err(Status.NOT_ACCEPTABLE, accept.stream() .map(MediaType::name) .collect(Collectors.joining(", "))); } if (!contentType.isAny()) { return new Err(Status.UNSUPPORTED_MEDIA_TYPE, contentType.name()); } } } return null; }
private FormData parseForm() { if (form == null) { form = NO_FORM; try { String tmpdir = conf.getString("application.tmpdir"); String charset = conf.getString("application.charset"); String value = exchange.getRequestHeaders().getFirst("Content-Type"); if (value != null) { MediaType type = MediaType.valueOf(value); if (MediaType.form.name().equals(type.name())) { blocking.get(); form = new FormEncodedDataDefinition() .setDefaultEncoding(charset) .create(exchange) .parseBlocking(); } else if (MediaType.multipart.name().equals(type.name())) { blocking.get(); form = new MultiPartParserDefinition() .setTempFileLocation(new File(tmpdir).toPath()) .setDefaultEncoding(charset) .create(exchange) .parseBlocking(); } } } catch (IOException x) { } } return form; }
/** * 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(); }
/** * 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)); }
@Override public Object parse(final TypeLiteral<?> type, final Context ctx) throws Throwable { MediaType ctype = ctx.type(); if (ctype.isAny()) { // */* return ctx.next(); } JavaType javaType = mapper.constructType(type.getType()); if (matcher.matches(ctype) && mapper.canDeserialize(javaType)) { return ctx .ifparam(values -> mapper.readValue(values.iterator().next(), javaType)) .ifbody(body -> mapper.readValue(body.bytes(), javaType)); } return ctx.next(); }