Refine search
/** * Executes this action with the given HTTP request and returns the result. * * @param req the http request with which to execute this action * @return a promise to the action's result */ public CompletionStage<Result> call(Request req) { // TODO: Make this method abstract after removing call(Context) return Context.safeCurrent().map(threadLocalCtx -> { // A previous action did explicitly set a context onto the thread local (via Http.Context.current.set(...)) // Let's use that context so the user doesn't loose data he/she set onto that ctx (args,...) Context newCtx = threadLocalCtx.withRequest(req.removeAttr(CTX_ARGS)); Context.setCurrent(newCtx); return call(newCtx); }).orElseGet(() -> { // A previous action did not set a context explicitly, we simply create a new one to pass on the request Context ctx = new Context(req.removeAttr(CTX_ARGS), contextComponents); ctx.args = req.attrs().getOptional(CTX_ARGS).orElse(new HashMap<>()); return call(ctx); }); }
/** * Generates a JavaScript reverse router. * * @param name the router's name * @param ajaxMethod which asynchronous call method the user's browser will use (e.g. "jQuery.ajax") * @param routes the reverse routes for this router * @return the router * * @deprecated Deprecated as of 2.7.0. Use {@link #create(String, String, String, JavaScriptReverseRoute...)} instead. */ @Deprecated public static JavaScript create(String name, String ajaxMethod, JavaScriptReverseRoute... routes) { return create(name, ajaxMethod, play.mvc.Http.Context.current().request().host(), routes); } // TODO:
/** * Create a new context with the given request. * * The id, session, flash and args remain unchanged. * * This method is intended for use within a Java action, to create a new Context to pass to a delegate action. * * @param request The request to create the new header from. * @return The new context. * * @deprecated Deprecated as of 2.7.0. <a href="https://www.playframework.com/documentation/latest/JavaHttpContextMigration27">See migration guide.</a>. */ @Deprecated public Context withRequest(Request request) { return new Context(id, request.asScala(), request, response, session, flash, args, lang, components); } }
public final static AtmosphereRequest request(final Http.Request request, final Map<String, Object> additionalAttributes) throws Throwable { final String base = getBaseUri(request); final URI requestUri = new URI(base.substring(0, base.length() - 1) + request.uri()); String ct = "text/plain"; if (request.getHeader("Content-Type") != null) { ct = request.getHeader("Content-Type"); String method = request.method(); parseQueryString(qs, request.body().asText()); boolean hasBody = request.body() != null; byte[] body = {}; if (request.body().asText() != null) { body = request.body().asText().getBytes("utf-8"); } else if (request.body().asRaw() != null) { body = request.body().asBytes().toArray(); } else if (request.body().asJson() != null) { body = request.body().asJson().asText().getBytes("utf-8"); if (body.length == 0) { body = request.body().asJson().toString().getBytes("utf-8"); } else if (request.body().asXml() != null) { body = request.body().asXml().getTextContent().getBytes("utf-8"); URI.create(request.remoteAddress()); } catch (IllegalArgumentException e) { logger.trace("", e);
public PlayAsyncIOWriter(final Http.Request request, final Map<String, Object> additionalAttributes, final Http.Response response) { final String[] transport = request.queryString() != null ? request.queryString().get(HeaderConfig.X_ATMOSPHERE_TRANSPORT) : null; this.source = Source.<ByteString>actorRef(BUFFER_SIZE, OverflowStrategy.dropNew()).mapMaterializedValue(actorRef -> { out = new OutStream() { response.setContentType("text/event-stream");
protected Map<String,String> requestData(Http.Request request) { Map<String,String[]> urlFormEncoded = new HashMap<>(); if (request.body().asFormUrlEncoded() != null) { urlFormEncoded = request.body().asFormUrlEncoded(); } Map<String,String[]> multipartFormData = new HashMap<>(); if (request.body().asMultipartFormData() != null) { multipartFormData = request.body().asMultipartFormData().asFormUrlEncoded(); } Map<String,String> jsonData = new HashMap<>(); if (request.body().asJson() != null) { jsonData = play.libs.Scala.asJava( play.api.data.FormUtils.fromJson("", play.api.libs.json.Json.parse( play.libs.Json.stringify(request.body().asJson()) ) ) ); } Map<String,String> data = new HashMap<>(); fillDataWith(data, urlFormEncoded); fillDataWith(data, multipartFormData); jsonData.forEach(data::put); if(!request.method().equalsIgnoreCase(HttpVerbs.POST) && !request.method().equalsIgnoreCase(HttpVerbs.PUT) && !request.method().equalsIgnoreCase(HttpVerbs.PATCH)) { fillDataWith(data, request.queryString()); } return data; }
protected Map<String,String> requestData(Http.Request request) { Map<String,String[]> urlFormEncoded = new HashMap<>(); if (request.body().asFormUrlEncoded() != null) { urlFormEncoded = request.body().asFormUrlEncoded(); } Map<String,String[]> multipartFormData = new HashMap<>(); if (request.body().asMultipartFormData() != null) { multipartFormData = request.body().asMultipartFormData().asFormUrlEncoded(); } Map<String,String> jsonData = new HashMap<>(); if (request.body().asJson() != null) { jsonData = play.libs.Scala.asJava( play.api.data.FormUtils.fromJson("", play.api.libs.json.Json.parse( play.libs.Json.stringify(request.body().asJson()) ) ) ); } Map<String,String> data = new HashMap<>(); fillDataWith(data, urlFormEncoded); fillDataWith(data, multipartFormData); jsonData.forEach(data::put); if(!request.method().equalsIgnoreCase(HttpVerbs.POST) && !request.method().equalsIgnoreCase(HttpVerbs.PUT) && !request.method().equalsIgnoreCase(HttpVerbs.PATCH)) { fillDataWith(data, request.queryString()); } return data; }
protected Map<String,String> requestData(Http.Request request) { Map<String,String[]> urlFormEncoded = new HashMap<>(); if (request.body().asFormUrlEncoded() != null) { urlFormEncoded = request.body().asFormUrlEncoded(); } Map<String,String[]> multipartFormData = new HashMap<>(); if (request.body().asMultipartFormData() != null) { multipartFormData = request.body().asMultipartFormData().asFormUrlEncoded(); } Map<String,String> jsonData = new HashMap<>(); if (request.body().asJson() != null) { jsonData = play.libs.Scala.asJava( play.api.data.FormUtils.fromJson("", play.api.libs.json.Json.parse( play.libs.Json.stringify(request.body().asJson()) ) ) ); } Map<String,String> data = new HashMap<>(); fillDataWith(data, urlFormEncoded); fillDataWith(data, multipartFormData); jsonData.forEach(data::put); if(!request.method().equalsIgnoreCase(HttpVerbs.POST) && !request.method().equalsIgnoreCase(HttpVerbs.PUT) && !request.method().equalsIgnoreCase(HttpVerbs.PATCH)) { fillDataWith(data, request.queryString()); } return data; }
public StreamSocket(Request req, Response res) { this.params = params(req.queryString()); this.isAndroid = isAndroid(req.getHeader("user-agent")); this.chunks = new Chunks<String>(JavaResults.writeString(Codec.utf_8())) { @Override public void onReady(Chunks.Out<String> oout) { out = oout; out.onDisconnected(new F.Callback0() { @Override public void invoke() throws Throwable { onClose(); } }); out.write(padding2K); if (isAndroid) { out.write(padding2K); } out.write("\n"); onOpen(); } }; res.setContentType(streamContentType() + "; charset=utf-8"); }
private Result postFile(String sid) { // System.out.println("POST FILE CALLED: " + sid); Http.MultipartFormData body = request().body().asMultipartFormData(); List<Http.MultipartFormData.FilePart> fileParts = body.getFiles(); if (fileParts.size() <= 0) { // System.out.println("**** NO FILE ****"); return badRequest("No file uploaded"); } Http.MultipartFormData.FilePart uploadedFile = fileParts.get(0); String fileName = uploadedFile.getFilename(); String contentType = uploadedFile.getContentType(); File file = uploadedFile.getFile(); List<String> lines; try { lines = FileUtils.readLines(file); } catch (IOException e) { // System.out.println("**** COULD NOT READ FILE ****"); return badRequest("Could not read from uploaded file"); } knownSessionIDs.put(sid, lines); return ok("File uploaded: " + fileName + ", " + contentType + ", " + file); } }
public WsSocket(Request req) { this.params = params(req.queryString()); this.webSocket = new WebSocket<String>() { @Override public void onReady(WebSocket.In<String> in, Out<String> oout) { out = oout; in.onClose(new F.Callback0() { @Override public void invoke() throws Throwable { onClose(); } }); in.onMessage(new F.Callback<String>() { @Override public void invoke(String message) throws Throwable { fire(message); } }); onOpen(); } }; }
@Override public void onReady(Chunks.Out<String> oout) { out = oout; out.onDisconnected(new F.Callback0() { @Override public void invoke() throws Throwable { if (!open && out != null) { onClose(); } } }); if (open) { out.close(); onOpen(); } else { String[] value = req.queryString().get("lastEventIds"); retrieveCache(value != null ? value[0] : null); } } };
protected <T> F.Promise<Result> withJsonBody(Class<T> klass, ToPromiseResult<T> f) { JsonNode jsonNode = request().body().asJson(); if (jsonNode == null) { return pure(badRequest("could not parse body to JSON")); } else { try { T obj = play.libs.Json.fromJson(jsonNode, klass); F.Tuple<Integer, Object> r = f.apply(obj); return pure(Results.status(r._1, toJson(r._2))); } catch (ConstraintViolationException ex) { return pure((badRequest(createJsonResponse(ex)))); } catch (RuntimeException e) { if (e.getCause() instanceof JsonProcessingException) { return pure(badRequest(handleProcessingException((JsonProcessingException) e.getCause()))); } else { throw e; } } } }
@Override public Map<String, String[]> getRequestParameters() { final Map<String, String[]> parameters = new HashMap<>(); if (formParameters != null) { parameters.putAll(formParameters); } else if (request.hasBody()) { final Http.RequestBody body = request.body(); if (body != null) { final Map<String, String[]> p = body.asFormUrlEncoded(); if (p != null) { parameters.putAll(p); } } } final Map<String, String[]> urlParameters = request.queryString(); if (urlParameters != null) { parameters.putAll(urlParameters); } return parameters; }
@Override public Map<String, String[]> getRequestParameters() { final Map<String, String[]> parameters = new HashMap<>(); if (formParameters != null) { parameters.putAll(formParameters); } else if (request.hasBody()) { final Http.RequestBody body = request.body(); if (body != null) { final Map<String, String[]> p = body.asFormUrlEncoded(); if (p != null) { parameters.putAll(p); } } } final Map<String, String[]> urlParameters = request.queryString(); if (urlParameters != null) { parameters.putAll(urlParameters); } return parameters; }
Chunks<String> openHttp(Request req, Response res) { String when = req.queryString().get("when")[0]; String id = req.queryString().get("id")[0]; String transport = req.queryString().get("transport")[0]; HttpSocket socket = null; if (transport.equals("sse") || transport.startsWith("stream")) { socket = new StreamSocket(req, res); sockets.put(id, socket); } else if (transport.startsWith("longpoll")) { if (when.equals("open")) { socket = new LongPollSocket(req, res); sockets.put(id, socket); } else if (when.equals("poll")) { socket = (LongPollSocket) sockets.get(id); ((LongPollSocket) socket).refresh(req, res, false); } } return socket.chunks; }
private Result uploadFile() { Http.MultipartFormData body = request().body().asMultipartFormData(); List<Http.MultipartFormData.FilePart> fileParts = body.getFiles(); if (fileParts.size() <= 0) { return badRequest("No file uploaded"); } Http.MultipartFormData.FilePart uploadedFile = fileParts.get(0); String fileName = uploadedFile.getFilename(); String contentType = uploadedFile.getContentType(); File file = uploadedFile.getFile(); try { uploadedFileLines = FileUtils.readLines(file); } catch (IOException e) { return badRequest("Could not read from uploaded file"); } return ok("File uploaded: " + fileName + ", " + contentType + ", " + file); }
@Override public Map<String, String[]> getRequestParameters() { final Http.RequestBody body = request.body(); final Map<String, String[]> formParameters; if (body != null) { formParameters = body.asFormUrlEncoded(); } else { formParameters = new HashMap<String, String[]>(); } final Map<String, String[]> urlParameters = request.queryString(); final Map<String, String[]> parameters = new HashMap<String, String[]>(); if (formParameters != null) { parameters.putAll(formParameters); } if (urlParameters != null) { parameters.putAll(urlParameters); } return parameters; }
private Result uploadFile() { Http.MultipartFormData body = request().body().asMultipartFormData(); List<Http.MultipartFormData.FilePart> fileParts = body.getFiles(); if (fileParts.size() <= 0) { return badRequest("No file uploaded"); } Http.MultipartFormData.FilePart uploadedFile = fileParts.get(0); String fileName = uploadedFile.getFilename(); String contentType = uploadedFile.getContentType(); File file = uploadedFile.getFile(); try { uploadedFileLines = FileUtils.readLines(file); } catch (IOException e) { return badRequest("Could not read from uploaded file"); } return ok("File uploaded: " + fileName + ", " + contentType + ", " + file); }
private Result uploadFile() { Http.MultipartFormData body = request().body().asMultipartFormData(); List<Http.MultipartFormData.FilePart> fileParts = body.getFiles(); if (fileParts.size() <= 0) { return badRequest("No file uploaded"); } Http.MultipartFormData.FilePart uploadedFile = fileParts.get(0); String fileName = uploadedFile.getFilename(); String contentType = uploadedFile.getContentType(); File file = uploadedFile.getFile(); try { uploadedFileLines = FileUtils.readLines(file); } catch (IOException e) { return badRequest("Could not read from uploaded file"); } return ok("File uploaded: " + fileName + ", " + contentType + ", " + file); }