/* * Copyright 2012 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.github.carlomicieli.rest.resources; import java.net.URI; import javax.inject.Inject; import javax.ws.rs.Consumes; import javax.ws.rs.DELETE; import javax.ws.rs.GET; import javax.ws.rs.POST; import javax.ws.rs.PUT; import javax.ws.rs.Path; import javax.ws.rs.PathParam; import javax.ws.rs.Produces; import javax.ws.rs.WebApplicationException; import javax.ws.rs.core.CacheControl; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.MultivaluedMap; import javax.ws.rs.core.Response; import javax.ws.rs.core.Response.Status; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.context.annotation.Scope; import org.springframework.dao.DataAccessException; import com.github.carlomicieli.entities.Brand; import com.github.carlomicieli.rest.representations.BrandRepresentation; import com.github.carlomicieli.services.BrandService; import com.sun.jersey.api.spring.Autowire; import static com.github.carlomicieli.rest.representations.BrandsArrayRepresentation.*; /** * The REST resource class for a brand. * @author Carlo Micieli * */ @Autowire @Scope("request") @Path("/brands") public class BrandsResource { private static final Logger log = LoggerFactory.getLogger("rest"); private @Inject BrandService brandService; private CacheControl getCacheControl() { CacheControl cache = new CacheControl(); cache.setMaxAge(10); cache.setPrivate(true); return cache; } /** * Return the list of all brands. * <p> * <code>GET /brands</code> * </p> * * @return the list of brands. */ @GET @Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML}) public Response getBrands() { log.info("GET /brands"); try { final BrandRepresentation[] brands = createArray(brandService.getAllBrands()); return Response.ok(brands) .cacheControl(getCacheControl()) .build(); } catch( DataAccessException ex ) { log.error(ex.getMessage()); throw new WebApplicationException(Status.INTERNAL_SERVER_ERROR); } } /** * Return the list of all brands. * <p> * <code>GET /brands/{brandId}</code> * </p> * * @param brandId the brand unique id. * @return the list of brands. */ @GET @Path("/{brandId}") @Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML}) public Response getBrand(@PathParam("brandId") long brandId) { log.info("GET /brands/{}", brandId); try { final BrandRepresentation brand = new BrandRepresentation(brandService.getBrand(brandId)); return Response.ok(brand) .tag(brand.tag()) .cacheControl(getCacheControl()) .build(); } catch( DataAccessException ex ) { log.error(ex.getMessage()); throw new WebApplicationException(Status.INTERNAL_SERVER_ERROR); } } @POST @Consumes(MediaType.APPLICATION_FORM_URLENCODED) public Response post(MultivaluedMap<String, String> formValues) { return post(new BrandRepresentation(formValues)); } @POST @Consumes({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML}) public Response post(BrandRepresentation brandRep) { log.info("POST /brands"); final Brand brand = brandRep.getInner(); try { long id = brandService.create(brand); // the response contains the ETag for the resource just created. return Response.created(URI.create(Long.toString(id))) .tag(brandRep.tag()) .build(); } catch( RuntimeException ex ) { log.error(ex.getMessage()); throw new WebApplicationException(Status.INTERNAL_SERVER_ERROR); } } /** * Update the brand resource. * <p> * <code>PUT /brands</code> * </p> * * @param formValues the brand representation. * @return HTTP response. */ @PUT @Consumes(MediaType.APPLICATION_FORM_URLENCODED) public Response put(MultivaluedMap<String, String> formValues) { return put(new BrandRepresentation(formValues)); } /** * Update the brand resource. * <p> * <code>PUT /brands</code> * </p> * * @param brandRep the brand representation. * @return HTTP response. */ @PUT @Consumes({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML}) public Response put(BrandRepresentation brandRep) { log.info("PUT /brands"); final Brand brand = brandRep.getInner(); try { brandService.save(brand); return Response.noContent() .build(); } catch( RuntimeException ex ) { log.error(ex.getMessage()); throw new WebApplicationException(Status.INTERNAL_SERVER_ERROR); } } /** * * @param brandId */ @DELETE @Path("/{brandId}") public void delete(@PathParam("brandId") long brandId) { log.info("DELETE /brands/{}", brandId); try { brandService.remove(new Brand(brandId)); } catch( RuntimeException ex ) { log.error(ex.getMessage()); throw new WebApplicationException(Response.Status.INTERNAL_SERVER_ERROR); } } /** * * @param brandRep */ @DELETE @Consumes({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML}) public void delete(BrandRepresentation brandRep) { delete(brandRep.getId()); } }