Java Code Examples for play.mvc.Results#status()
The following examples show how to use
play.mvc.Results#status() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BaseController.java From sunbird-lms-service with MIT License | 6 votes |
/** * Common exception response handler method. * * @param e Exception * @param request play.mvc.Http.Request * @return Result */ public Result createCommonExceptionResponse(Exception e, Request request) { Request req = request; ProjectCommonException exception = null; if (e instanceof ProjectCommonException) { exception = (ProjectCommonException) e; } else { exception = new ProjectCommonException( ResponseCode.internalError.getErrorCode(), ResponseCode.internalError.getErrorMessage(), ResponseCode.SERVER_ERROR.getResponseCode()); } generateExceptionTelemetry(request, exception); // cleaning request info ... return Results.status( exception.getResponseCode(), Json.toJson(createResponseOnException(req, exception))); }
Example 2
Source File: Utils.java From NationStatesPlusPlus with MIT License | 6 votes |
public static Result handleDefaultGetHeaders(Request request, Response response, String calculatedEtag, String seconds) { response.setHeader("Access-Control-Allow-Origin", "*"); response.setHeader("Access-Control-Allow-Methods", "POST, GET, HEAD"); response.setHeader("Access-Control-Max-Age", seconds); response.setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept"); response.setHeader("Cache-Control", "public, max-age=" + seconds); response.setHeader("Expires", new DateTime().plusHours(1).plusSeconds(Integer.parseInt(seconds)).toString(Utils.HTTP_DATE_TIME)); response.setHeader("Last-Modified", new DateTime().plusHours(-1).plusSeconds(-Integer.parseInt(seconds)).toString(Utils.HTTP_DATE_TIME)); response.setHeader("Vary", "Accept-Encoding"); if (calculatedEtag != null) { response.setHeader("ETag", calculatedEtag); String eTag = request.getHeader("If-None-Match"); if (calculatedEtag != null && calculatedEtag.equals(eTag)) { return Results.status(304); } } return null; }
Example 3
Source File: BaseController.java From sunbird-lms-service with MIT License | 5 votes |
private Result createClientErrorResponse(Request httpReq, ClientErrorResponse response) { ClientErrorResponse errorResponse = response; generateExceptionTelemetry(httpReq, errorResponse.getException()); Response responseObj = createResponseOnException(httpReq, errorResponse.getException()); responseObj.getResult().putAll(errorResponse.getResult()); return Results.status(errorResponse.getException().getResponseCode(), Json.toJson(responseObj)); }
Example 4
Source File: RMBController.java From NationStatesPlusPlus with MIT License | 5 votes |
public Result addComment(int rmbPost) throws SQLException { Result invalid = Utils.validateRequest(request(), response(), getAPI(), getDatabase()); if (invalid != null) { return invalid; } final String comment = Utils.getPostValue(request(), "comment"); if (comment == null) { Utils.handleDefaultGetHeaders(request(), response(), null, "0"); return Results.badRequest(); } final String nation = Utils.getPostValue(request(), "nation"); final int nationId = getDatabase().getNationId(nation); Connection conn = getConnection(); try { PreparedStatement select = conn.prepareStatement("SELECT timestamp FROM assembly.rmb_comments WHERE rmb_message_id = ? AND nation_id = ? AND timestamp > ?"); select.setInt(1, rmbPost); select.setInt(2, nationId); select.setLong(3, System.currentTimeMillis() - Duration.standardSeconds(10).getMillis()); ResultSet result = select.executeQuery(); if (result.next()) { Utils.handleDefaultGetHeaders(request(), response(), null, "0"); return Results.status(429); } DbUtils.closeQuietly(result); DbUtils.closeQuietly(select); PreparedStatement insert = conn.prepareStatement("INSERT INTO assembly.rmb_comments (rmb_message_id, timestamp, comment, nation_id) VALUES (?, ?, ?, ?)"); insert.setInt(1, rmbPost); insert.setLong(2, System.currentTimeMillis()); insert.setString(3, comment); insert.setInt(4, nationId); DbUtils.closeQuietly(insert); } finally { DbUtils.closeQuietly(conn); } Utils.handleDefaultGetHeaders(request(), response(), null, "0"); return Results.ok(); }
Example 5
Source File: HappeningsController.java From NationStatesPlusPlus with MIT License | 5 votes |
public Result parseHappenings() throws SQLException { String region = Utils.getPostValue(request(), "region"); String xml = Utils.getPostValue(request(), "xml"); if (region != null && xml != null) { return doParseHappenings(region, xml); } return Results.status(BAD_REQUEST); }
Example 6
Source File: HappeningsController.java From NationStatesPlusPlus with MIT License | 5 votes |
private Result doParseHappenings(String region, String xml) throws SQLException { NationStates api = new NationStates(); Connection conn = null; ArrayList<HappeningData> happenings = new ArrayList<HappeningData>(); try { RegionData regionData = api.getRegionInfo(api.getInfo(IOUtils.toInputStream(xml)), region); conn = getConnection(); for (RegionHappening happening : regionData.happenings) { String text = Utils.formatHappeningText(happening.text, conn, ""); HappeningData data = new HappeningData(); data.happening = text; data.timestamp = happening.timestamp; happenings.add(data); } } catch (Exception e) { Logger.error("Unable to parse happenings for region [" + region + "]. Data: " + xml, e); return Results.status(INTERNAL_SERVER_ERROR); } finally { DbUtils.closeQuietly(conn); } String calculatedEtag = String.valueOf(happenings.hashCode()); Result result = Utils.handleDefaultGetHeaders(request(), response(), calculatedEtag); if (happenings.isEmpty()) { return Results.noContent(); } else if (result != null) { return result; } return ok(Json.toJson(happenings)).as("application/json"); }