private static boolean multipart(final HttpServletRequest req) { String contentType = req.getContentType(); return contentType != null && contentType.toLowerCase().startsWith(MediaType.multipart.name()); }
public RouteKey(final String method, final String path, final MediaType consumes, final List<MediaType> produces) { String c = consumes.name(); String p = produces.toString(); key = new StringBuilder(method.length() + path.length() + c.length() + p.length()) .append(method) .append(path) .append(c) .append(p) .toString(); this.method = method; this.path = path; this.consumes = consumes; this.produces = produces; }
@JsonAnyGetter Map<String, Object> attributes() { Map<String, Object> attributes = new LinkedHashMap<>(); if (formParameters != null) { attributes.put("body", ImmutableMap .of(MediaType.multipart.name(), ImmutableMap.of("properties", formParameters))); } else if (body != null) { if (mediaType != null) { attributes.put("body", mediaType.stream() .collect(Collectors.toMap(Function.identity(), it -> body.getRef()))); } else { attributes.put("body", body.getRef()); } } else if (mediaType != null) { attributes.put("body", mediaType.stream() .collect(Collectors.toMap(Function.identity(), it -> ImmutableMap.of()))); } return attributes; } }
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(); }
@Override public void handle(final String target, final Request baseRequest, final HttpServletRequest request, final HttpServletResponse response) throws IOException, ServletException { try { baseRequest.setHandled(true); String type = baseRequest.getContentType(); boolean multipart = false; if (type != null && type.toLowerCase().startsWith(MediaType.multipart.name())) { baseRequest.setAttribute(Request.__MULTIPART_CONFIG_ELEMENT, multiPartConfig); multipart = true; } ServletServletRequest nreq = new ServletServletRequest(request, tmpdir, multipart) .with(upgrade(baseRequest, request, response, webSocketServerFactory)); dispatcher.handle(nreq, new JettyResponse(nreq, response)); } catch (IOException | ServletException | RuntimeException ex) { baseRequest.setHandled(false); log.error("execution of: " + target + " resulted in error", ex); throw ex; } catch (Throwable ex) { baseRequest.setHandled(false); log.error("execution of: " + target + " resulted in error", ex); throw new IllegalStateException(ex); } }
boolean defaultMediaType = false; if (alltypes.size() == 0) { raml.setMediaType(ImmutableList.of(MediaType.json.name())); defaultMediaType = true; } else if (alltypes.size() == 1) { files.forEach(it -> { parameterFactory.accept(method::formParameter, it); method.setMediaType(ImmutableList.of(MediaType.multipart.name())); }); } else { consumes = ImmutableList.of(MediaType.json.name()); produces = ImmutableList.of(MediaType.json.name());
op.setConsumes(ImmutableList.of(MediaType.multipart.name())); } else if (it.kind() == RouteParameter.Kind.FORM) { op.setConsumes(ImmutableList.of(MediaType.form.name(), MediaType.multipart.name())); swagger.consumes(MediaType.json.name()); } else if (consumes.size() == 1) { swagger.consumes(consumes); swagger.produces(MediaType.json.name()); } else if (produces.size() == 1) { swagger.produces(produces);
public String sse(final String path, final int count) throws Exception { CountDownLatch latch = new CountDownLatch(count); String result = client.prepareGet("http://localhost:" + port + path) .addHeader("Content-Type", MediaType.sse.name()) .addHeader("last-event-id", count + "") .execute(new AsyncHandler<String>() {
private static boolean multipart(final HttpServletRequest req) { String contentType = req.getContentType(); return contentType != null && contentType.toLowerCase().startsWith(MediaType.multipart.name()); }
public RouteKey(final String method, final String path, final MediaType consumes, final List<MediaType> produces) { String c = consumes.name(); String p = produces.toString(); key = new StringBuilder(method.length() + path.length() + c.length() + p.length()) .append(method) .append(path) .append(c) .append(p) .toString(); this.method = method; this.path = path; this.consumes = consumes; this.produces = produces; }
@JsonAnyGetter Map<String, Object> attributes() { Map<String, Object> attributes = new LinkedHashMap<>(); if (formParameters != null) { attributes.put("body", ImmutableMap .of(MediaType.multipart.name(), ImmutableMap.of("properties", formParameters))); } else if (body != null) { if (mediaType != null) { attributes.put("body", mediaType.stream() .collect(Collectors.toMap(Function.identity(), it -> body.getRef()))); } else { attributes.put("body", body.getRef()); } } else if (mediaType != null) { attributes.put("body", mediaType.stream() .collect(Collectors.toMap(Function.identity(), it -> ImmutableMap.of()))); } return attributes; } }
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(); }
@Override public void handle(final String target, final Request baseRequest, final HttpServletRequest request, final HttpServletResponse response) throws IOException, ServletException { try { baseRequest.setHandled(true); String type = baseRequest.getContentType(); boolean multipart = false; if (type != null && type.toLowerCase().startsWith(MediaType.multipart.name())) { baseRequest.setAttribute(Request.__MULTIPART_CONFIG_ELEMENT, multiPartConfig); multipart = true; } ServletServletRequest nreq = new ServletServletRequest(request, tmpdir, multipart) .with(upgrade(baseRequest, request, response, webSocketServerFactory)); dispatcher.handle(nreq, new JettyResponse(nreq, response)); } catch (IOException | ServletException | RuntimeException ex) { baseRequest.setHandled(false); log.error("execution of: " + target + " resulted in error", ex); throw ex; } catch (Throwable ex) { baseRequest.setHandled(false); log.error("execution of: " + target + " resulted in error", ex); throw new IllegalStateException(ex); } }