@GET @Path("/{id}") public Response get(@PathParam("id") int id, @Context Request request) { // Create cache control header CacheControl cc = new CacheControl(); // Set max age to one day cc.setMaxAge(86400); Response.ResponseBuilder rb = null; // Calculate the ETag on last modified date of user resource EntityTag etag = new EntityTag(UserDao.getLastModifiedById(id).hashCode() + ""); // Verify if it matched with etag available in http request rb = request.evaluatePreconditions(etag); // If ETag matches the rb will be non-null; // Use the rb to return the response without any further processing if (rb != null) { return rb.cacheControl(cc).tag(etag).build(); } // If rb is null then either it is first time request; or resource is modified // Get the updated representation and return with Etag attached to it rb = Response.ok(UserDao.get(id).get()).cacheControl(cc).tag(etag); return rb.build(); }
private Keyword getKeyword(final Request request) { if (request.isParam(Command.HELP)) { return Keyword.HELP; } if (request.isParam(Command.NEW)) { return Keyword.NEW; } if (request.isParam(Command.DIFF)) { return Keyword.DIFF; } return Keyword.NONE; }
private Response doSyncUpdate(final Request request) { loggerContext.init(getRequestId(request.getRemoteAddress())); try { if (!sourceMatchesContext(request.getSource())) { return Response.status(Response.Status.BAD_REQUEST) .entity("Invalid source specified: " + request.getSource()) .build(); } boolean notificationsEnabled = true; if (request.isParam(Command.REDIRECT)) { if (!ipRanges.isTrusted(IpInterval.parse(request.getRemoteAddress()))) { return Response.status(Response.Status.FORBIDDEN) .entity("Not allowed to disable notifications: " + request.getRemoteAddress()) .build(); } notificationsEnabled = false; } if (!request.hasParam(Command.DATA) && request.isParam(Command.NEW)) { return Response.status(Response.Status.BAD_REQUEST) .entity("DATA parameter is missing") .build(); } if (!request.hasParam(Command.DATA) && !request.isParam(Command.HELP)) { return Response.status(Response.Status.BAD_REQUEST).entity("Invalid request").build(); } loggerContext.log("msg-in.txt", new SyncUpdateLogCallback(request.toString())); final UpdateContext updateContext = new UpdateContext(loggerContext); final String content = request.hasParam("DATA") ? request.getParam("DATA") : ""; final UpdateRequest updateRequest = new UpdateRequest( new SyncUpdate(dateTimeProvider, request.getRemoteAddress()), getKeyword(request), content, updatesParser.parse( updateContext, Lists.newArrayList(new ContentWithCredentials(content))), notificationsEnabled); final UpdateResponse updateResponse = updateRequestHandler.handle(updateRequest, updateContext); loggerContext.log("msg-out.txt", new SyncUpdateLogCallback(updateResponse.getResponse())); return getResponse(updateResponse); } finally { loggerContext.remove(); } }