private String getMediaType() { return matchMediaType(getMediaTypes()[0], getMediaTypes()[1]); }
@GET @Path("/{id}") @Produces({ BASE_JSON_MEDIA_TYPE, BASE_XML_MEDIA_TYPE }) public Response get(@PathParam("id") String id) { DOMAIN entity = getRepository().get(id); if (entity == null) { return Response.status(Status.NOT_FOUND).type(getMediaType()).build(); } return Response.ok(getConverter().from(uriInfo, entity)) .type(getMediaType()) .lastModified(entity.getLastModified()) .build(); }
@POST @Consumes({ BASE_JSON_MEDIA_TYPE, BASE_XML_MEDIA_TYPE }) public Response create(REP representation) { DOMAIN entity = getConverter().to(uriInfo, representation); getRepository().store(entity); return Response.created( UriBuilder.fromResource(getResourceClass()).segment("{id}").build(entity.getId())).build(); }
@PUT @Path("/{id}") @Consumes({ BASE_JSON_MEDIA_TYPE, BASE_XML_MEDIA_TYPE }) public Response update(@PathParam("id") String id, REP representation) { DOMAIN entity = getRepository().get(id); if (entity == null) { return Response.status(Status.BAD_REQUEST).build(); // TODO: Need Business Exception type to explain why? } DOMAIN updatedEntity = getConverter().update(uriInfo, representation, entity); getRepository().store(updatedEntity); return Response.noContent().build(); }
@DELETE @Path("/{id}") public Response delete(@PathParam("id") String id) { DOMAIN entity = getRepository().get(id); if (entity == null) { return Response.status(Status.NOT_FOUND).build(); } getRepository().remove(entity); return Response.noContent().build(); }