@Security.Authenticated(UserAuthenticator.class) public Promise<Result> customPost(String moduleId, String path) { return custom(moduleId, path, (r) -> { RequestBody body = request().body(); String postBody = null; if (body.asText() != null) { postBody = body.asText(); } else if (body.asJson() != null) { r.setContentType("application/json"); postBody = body.asJson().toString(); } else if (body.asXml() != null) { r.setContentType("application/xml"); postBody = body.asXml().toString(); } return r.post(postBody); }); }
public static Result doPost() throws Exception { RequestBody requestBody = request().body(); byte[] requestContent = null; RawBuffer rawBuffer = requestBody.asRaw(); if (rawBuffer != null) { requestContent = rawBuffer.asBytes(); } else { requestContent = requestBody.asText().getBytes(Constants.UTF8); } InputStream in = new ByteArrayInputStream(requestContent); ByteArrayOutputStream out = new ByteArrayOutputStream(); TTransport transport = new TIOStreamTransport(in, out); TProtocol inProtocol = protocolFactory.getProtocol(transport); TProtocol outProtocol = protocolFactory.getProtocol(transport); processor.process(inProtocol, outProtocol); response().setHeader(CONTENT_TYPE, "application/x-thrift"); return ok(out.toByteArray()); }
@SuppressWarnings("unchecked") private static Map<String, Object> parseRequest() { RequestBody requestBody = request().body(); String requestContent = null; JsonNode jsonNode = requestBody.asJson(); if (jsonNode != null) { requestContent = jsonNode.toString(); } else { RawBuffer rawBuffer = requestBody.asRaw(); if (rawBuffer != null) { requestContent = new String(rawBuffer.asBytes(), Constants.UTF8); } else { requestContent = requestBody.asText(); } } return SerializationUtils.fromJsonString(requestContent, Map.class); }
public <U> U extractModel(RequestBody body, Class<U> modelClass) { U model = null; if (isBodyValidJson(body)) { try { if(body.asJson().elements().next().isContainerNode()){ model = mapper.treeToValue(body.asJson().elements().next(),modelClass); } else { model = mapper.treeToValue(body.asJson(), modelClass); } } catch (JsonProcessingException jsonException) { Logger.error("Error processing json: ", jsonException); } } return model; }
public Result login(){ RequestBody body = request().body(); LoginCredentials credentials = jsonHelper.extractModel(body, LoginCredentials.class); if (credentials == null) { return jsonHelper.getInvalidJsonMessage(body); } return jsonHelper.getResponse(service.login(credentials)); }
protected void setHttpContext(Map<String, String[]> requestBody) { RequestHeader reqHeader = mock(RequestHeader.class); Request req = mock(Request.class); if (requestBody != null) { RequestBody reqBody = mock(RequestBody.class); when(req.body()).thenReturn(reqBody); when(reqBody.asFormUrlEncoded()).thenReturn(requestBody); } Map<String, String> session = new HashMap<String, String>(); Map<String, String> flash = new HashMap<String, String>(); Map<String, Object> args = new HashMap<String, Object>(); Context.current.set(new Context(1L, reqHeader, req, session, flash, args)); }
protected void setHttpContext(JsonNode requestBody) { RequestHeader reqHeader = mock(RequestHeader.class); Request req = mock(Request.class); if (requestBody != null) { RequestBody reqBody = mock(RequestBody.class); when(req.body()).thenReturn(reqBody); when(reqBody.asJson()).thenReturn(requestBody); } Map<String, String> session = new HashMap<String, String>(); Map<String, String> flash = new HashMap<String, String>(); Map<String, Object> args = new HashMap<String, Object>(); Context.current.set(new Context(1L, reqHeader, req, session, flash, args)); }
@BodyParser.Of(value = BodyParser.FormUrlEncoded.class, maxLength = 1024 * 1024) public static Result update(Long id) { Logger.debug("update"); RequestBody body = request().body(); if(body.isMaxSizeExceeded()) { return badRequest("Too much data!"); } else { DynamicForm requestData = form().bindFromRequest(); Logger.debug("requestData: " + requestData); Form<Instance> filledForm = form(Instance.class).bindFromRequest(); Logger.debug("hasGlobalErrors: " + filledForm.hasGlobalErrors()); Logger.debug("errors: " + filledForm.errors()); String action = requestData.get("action"); Logger.debug("action: " + action); if (StringUtils.isNotEmpty(action)) { if (action.equals("save")) { if (filledForm.hasErrors()) { Logger.debug("hasErrors: " + filledForm.errors()); return info(filledForm, id); } //Hidden flag if (filledForm.get().hidden == null) { filledForm.get().hidden = Boolean.FALSE; } filledForm.get().update(id); flash("message", "Instance " + filledForm.get().title + " has been updated"); return redirect(routes.InstanceController.view(filledForm.get().id)); } else if (action.equals("delete")) { Instance instance = Instance.findById(id); flash("message", "Instance " + filledForm.get().title + " has been deleted"); instance.delete(); return redirect(routes.InstanceController.index()); } } } return null; }
private boolean isBodyValidJson(RequestBody body) { return body != null && body.asJson() != null; }
public Result create() { RequestBody body = request().body(); return jsonHelper.getResponse(simple.create(jsonHelper.extractModel(body, UserDTO.class)), UserDTO.class); }
public Result update(String id) { RequestBody body = request().body(); return jsonHelper.getResponse(simple.update(jsonHelper.extractModel(body, UserDTO.class), id), UserDTO.class); }
public Result update(String id) { RequestBody body = request().body(); return jsonHelper.getResponse(simple.update(jsonHelper.extractModel(body,BlogDTO.class),id),BlogDTO.class); }
public Result create() { RequestBody body = request().body(); return jsonHelper.getResponse(simple.create(jsonHelper.extractModel(body, PostDTO.class)), PostDTO.class); }
public Result update(String id) { RequestBody body = request().body(); return jsonHelper.getResponse(simple.update(jsonHelper.extractModel(body, PostDTO.class), id), PostDTO.class); }
public Result create() { RequestBody body = request().body(); return jsonHelper.getResponse(simple.create(jsonHelper.extractModel(body, CategoryDTO.class)), CategoryDTO.class); }
public Result update(String id) { RequestBody body = request().body(); return jsonHelper.getResponse(simple.update(jsonHelper.extractModel(body, CategoryDTO.class), id), CategoryDTO.class); }
public Result create() { RequestBody body = request().body(); return jsonHelper.getResponse(simple.create(jsonHelper.extractModel(body, KeywordDTO.class)), KeywordDTO.class); }
public Result update(String id) { RequestBody body = request().body(); return jsonHelper.getResponse(simple.update(jsonHelper.extractModel(body, KeywordDTO.class), id), KeywordDTO.class); }