/** * Example not restricting media type but at least using double submit * * @param nickName * @param oneLinerJson * @return */ @POST @CsrfProtection public Response putNewOneLinerText( @CookieParam("nickNameSecurish") String nickName, String oneLinerJson) { if (logger.isDebugEnabled()) { logger.debug("In putNewOneLinerText()"); } try { OneLiner oneLiner = oneLinerFactory.createOneLiner(oneLinerJson); if (nickName != null && oneLiner != null) { oneLiner.setNickName( nickName); // Should be real session handling and userid lookup server-side :) oneLinerDb.putOneLiner(oneLiner); if (logger.isDebugEnabled()) { logger.debug("Added one line: " + oneLiner); } return Response.status(Response.Status.CREATED).build(); } else { logger.warn( "One liner data not OK, nicknames don't match. Nickname cookie = " + nickName + (oneLiner != null ? ", nickname in one liner = " + oneLiner.getNickName() : ", one liner = " + oneLiner)); return Response.status(Response.Status.BAD_REQUEST).build(); } } catch (IllegalArgumentException e) { logger.warn("One liner data not OK. Nickname = " + nickName); return Response.status(Response.Status.BAD_REQUEST).build(); } catch (Exception e) { logger.error( "One liner data not OK. Nickname = " + nickName + ". Exception: " + e.toString()); return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build(); } }
@POST @Path("/reset") public void resetOneLinerList() { oneLinerDb.initializeOneLinerDb(); if (logger.isDebugEnabled()) { logger.debug("One liner DB reset"); } }
@GET @Path("/size") @Produces({MediaType.APPLICATION_JSON}) public Response getNumberOfOneLiners(@Context HttpServletResponse response) { setNoCache(response); DbSize dbSize = oneLinerDb.getNumberOfOneliners(); GenericEntity entity = new GenericEntity<DbSize>(dbSize) {}; return Response.ok(entity).build(); }
@GET @Path("/{numberOfOneLiners}") @Produces({MediaType.APPLICATION_JSON}) public Response getLatestOneLiners( @DefaultValue(MAX_NUMBER_OF_ONE_LINERS_TO_RETRIEVE + "") @PathParam("numberOfOneLiners") int numberOfOneLiners, @Context HttpServletResponse response) { setNoCache(response); if (numberOfOneLiners < 1 || numberOfOneLiners > MAX_NUMBER_OF_ONE_LINERS_TO_RETRIEVE) { logger.warn( "Specified number of one liners not OK. Number of one liners = " + numberOfOneLiners); return Response.status(Response.Status.BAD_REQUEST).build(); } else { List<OneLiner> result = oneLinerDb.getLatestOneLiners(numberOfOneLiners); if (logger.isDebugEnabled()) { logger.debug("Returning list of one liners, size = " + result.size()); for (OneLiner oneLiner : result) { logger.debug(oneLiner); } } GenericEntity entity = new GenericEntity<List<OneLiner>>(result) {}; return Response.ok(entity).build(); } }