Refine search
/** * Builds a new Http.Context from a new request * @return a new Http.Context using the default request * * @deprecated Deprecated as of 2.7.0. See <a href="https://www.playframework.com/documentation/latest/JavaHttpContextMigration27">migration guide</a>. */ @Deprecated public static Http.Context httpContext() { return httpContext(new Http.RequestBuilder().build()); }
/** * Sets the uri. * @param str the uri * @return the builder instance */ public RequestBuilder uri(String str) { try { uri(new URI(str)); } catch (URISyntaxException e) { throw new IllegalArgumentException("Exception parsing URI", e); } return this; }
/** * Set a Form url encoded body to this request. */ public RequestBuilder bodyForm(Map<String,String> data) { Map<String,Seq<String>> seqs = new HashMap<>(); for (Entry<String,String> entry: data.entrySet()) { seqs.put(entry.getKey(), JavaConversions.asScalaBuffer(Arrays.asList(entry.getValue()))); } scala.collection.immutable.Map<String,Seq<String>> map = asScala(seqs); return body(new AnyContentAsFormUrlEncoded(map), "application/x-www-form-urlencoded"); }
/** * Set a Multipart Form url encoded body to this request saving it as a raw body. * * @param data the multipart-form parameters * @param temporaryFileCreator the temporary file creator. * @param mat a Akka Streams Materializer * @return the modified builder */ public RequestBuilder bodyRaw(List<MultipartFormData.Part<Source<ByteString, ?>>> data, Files.TemporaryFileCreator temporaryFileCreator, Materializer mat) { String boundary = MultipartFormatter.randomBoundary(); try { ByteString materializedData = MultipartFormatter .transform(Source.from(data), boundary) .runWith(Sink.reduce(ByteString::concat), mat) .toCompletableFuture() .get(); play.api.mvc.RawBuffer buffer = new play.api.mvc.RawBuffer(materializedData.size(), temporaryFileCreator.asScala(), materializedData); return body(new RequestBody(JavaParsers.toJavaRaw(buffer)), MultipartFormatter.boundaryToContentType(boundary)); } catch (InterruptedException | ExecutionException e) { throw new RuntimeException("Failure while materializing Multipart/Form Data", e); } }
/** * Set the body of the request. * * @param body The body. * @return the modified builder */ protected RequestBuilder body(RequestBody body) { if (body == null || body.as(Object.class) == null) { // assume null signifies no body; RequestBody is a wrapper for the actual body content headers(getHeaders().remove(HeaderNames.CONTENT_LENGTH).remove(HeaderNames.TRANSFER_ENCODING)); } else { if (!getHeaders().get(HeaderNames.TRANSFER_ENCODING).isPresent()) { final MultipartFormData<?> multipartFormData = body.asMultipartFormData(); if (multipartFormData != null) { header(HeaderNames.CONTENT_LENGTH, Long.toString(calcMultipartFormDataBodyLength(multipartFormData))); } else { int length = body.asBytes().length(); header(HeaderNames.CONTENT_LENGTH, Integer.toString(length)); } } } req = req.withBody(body); return this; }
/** * Build a new fake request. */ public static RequestBuilder fakeRequest(String method, String uri) { return new RequestBuilder().method(method).uri(uri); }
/** * Builds a new fake request. * @param method the request method. * @param uri the relative URL. * @return the request builder. */ public static RequestBuilder fakeRequest(String method, String uri) { return new RequestBuilder().method(method).uri(uri); }
/** * Builds a new fake request. * @param method the request method. * @param uri the relative URL. * @return the request builder. */ public static RequestBuilder fakeRequest(String method, String uri) { return new RequestBuilder().method(method).uri(uri); }
/** * Builds a new Http.Context from a new request * @return a new Http.Context using the default request * * @deprecated Deprecated as of 2.7.0. See <a href="https://www.playframework.com/documentation/latest/JavaHttpContextMigration27">migration guide</a>. */ @Deprecated public static Http.Context httpContext() { return httpContext(new Http.RequestBuilder().build()); }
@SuppressWarnings("unchecked") public static Result route(Application app, RequestBuilder requestBuilder, long timeout) { final scala.Option<scala.concurrent.Future<play.api.mvc.Result>> opt = play.api.test.Helpers.jRoute( app.getWrappedApplication(), requestBuilder.build()._underlyingRequest(), requestBuilder.bodyAsAnyContent()); return wrapScalaResult(Scala.orNull(opt), timeout); }
public static Result routeAndCall(Router router, RequestBuilder requestBuilder, long timeout) { try { Request request = requestBuilder.build(); if(router.routes().isDefinedAt(request._underlyingRequest())) { return invokeHandler(router.routes().apply(request._underlyingRequest()), request, timeout); } else { return null; } } catch(RuntimeException e) { throw e; } catch(Throwable t) { throw new RuntimeException(t); } }
@Deprecated public static Result route(Application app, RequestBuilder requestBuilder, byte[] body, long timeout) { return wrapScalaResult(Scala.orNull(play.api.test.Helpers.jRoute(app.getWrappedApplication(), requestBuilder.build()._underlyingRequest(), body)), timeout); }
public static Result routeAndCall(Class<? extends Router> router, RequestBuilder requestBuilder, long timeout) { try { Request request = requestBuilder.build(); Router routes = (Router) router.getClassLoader().loadClass(router.getName() + "$").getDeclaredField("MODULE$").get(null); if(routes.routes().isDefinedAt(request._underlyingRequest())) { return invokeHandler(routes.routes().apply(request._underlyingRequest()), request, timeout); } else { return null; } } catch(RuntimeException e) { throw e; } catch(Throwable t) { throw new RuntimeException(t); } }
/** * Route the request considering the given timeout. * * @param app The application used while routing and executing the request * @param requestBuilder the request builder * @param timeout the amount of time, in milliseconds, to wait for the body to be produced. * @return the result */ @SuppressWarnings("unchecked") public static Result route(Application app, RequestBuilder requestBuilder, long timeout) { final scala.Option<scala.concurrent.Future<play.api.mvc.Result>> opt = play.api.test.Helpers.jRoute( app.asScala(), requestBuilder.build().asScala(), requestBuilder.body() ); return wrapScalaResult(Scala.orNull(opt), timeout); }
/** * Route the request considering the given timeout. * * @param app The application used while routing and executing the request * @param requestBuilder the request builder * @param timeout the amount of time, in milliseconds, to wait for the body to be produced. * @return the result */ @SuppressWarnings("unchecked") public static Result route(Application app, RequestBuilder requestBuilder, long timeout) { final scala.Option<scala.concurrent.Future<play.api.mvc.Result>> opt = play.api.test.Helpers.jRoute( app.asScala(), requestBuilder.build().asScala(), requestBuilder.body() ); return wrapScalaResult(Scala.orNull(opt), timeout); }
/** * Route and call the request, respecting the given timeout. * * @param app The application used while routing and executing the request * @param router The router * @param requestBuilder The request builder * @param timeout The amount of time, in milliseconds, to wait for the body to be produced. * @return the result */ public static Result routeAndCall(Application app, Router router, RequestBuilder requestBuilder, long timeout) { try { Request request = requestBuilder.build(); return router.route(request).map(handler -> invokeHandler(app.asScala(), handler, request, timeout) ).orElse(null); } catch (RuntimeException e) { throw e; } catch (Throwable t) { throw new RuntimeException(t); } }
/** * Route and call the request, respecting the given timeout. * * @param app The application used while routing and executing the request * @param router The router type * @param requestBuilder The request builder * @param timeout The amount of time, in milliseconds, to wait for the body to be produced. * @return the result */ public static Result routeAndCall(Application app, Class<? extends Router> router, RequestBuilder requestBuilder, long timeout) { try { Request request = requestBuilder.build(); Router routes = (Router) router.getClassLoader().loadClass(router.getName() + "$").getDeclaredField("MODULE$").get(null); return routes.route(request).map(handler -> invokeHandler(app.asScala(), handler, request, timeout) ).orElse(null); } catch (RuntimeException e) { throw e; } catch (Throwable t) { throw new RuntimeException(t); } }
/** * Route and call the request, respecting the given timeout. * * @param app The application used while routing and executing the request * @param router The router * @param requestBuilder The request builder * @param timeout The amount of time, in milliseconds, to wait for the body to be produced. * @return the result */ public static Result routeAndCall(Application app, Router router, RequestBuilder requestBuilder, long timeout) { try { Request request = requestBuilder.build(); return router.route(request).map(handler -> invokeHandler(app.asScala(), handler, request, timeout) ).orElse(null); } catch (RuntimeException e) { throw e; } catch (Throwable t) { throw new RuntimeException(t); } }
/** * Route and call the request, respecting the given timeout. * * @param app The application used while routing and executing the request * @param router The router type * @param requestBuilder The request builder * @param timeout The amount of time, in milliseconds, to wait for the body to be produced. * @return the result */ public static Result routeAndCall(Application app, Class<? extends Router> router, RequestBuilder requestBuilder, long timeout) { try { Request request = requestBuilder.build(); Router routes = (Router) router.getClassLoader().loadClass(router.getName() + "$").getDeclaredField("MODULE$").get(null); return routes.route(request).map(handler -> invokeHandler(app.asScala(), handler, request, timeout) ).orElse(null); } catch (RuntimeException e) { throw e; } catch (Throwable t) { throw new RuntimeException(t); } }
/** * Set a Json Body to this request. * The <tt>Content-Type</tt> header of the request is set to <tt>application/json</tt>. * * @param node the Json Node * @return this builder, updated */ public RequestBuilder bodyJson(JsonNode node) { return body(new RequestBody(node), "application/json"); }