/* * This library is free software; you can redistribute it and/or modify it under the terms of * the GNU Lesser General Public License as published by the Free Software Foundation; * either version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR * PURPOSE. See the GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License along with this * library; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, * Boston, MA 02110-1301 USA */ package com.restfully.shop.services; import com.restfully.shop.domain.Customer; import javax.ws.rs.Consumes; import javax.ws.rs.GET; import javax.ws.rs.POST; 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.Response; import java.net.URI; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.atomic.AtomicInteger; @Path("/customers") public class CustomerResource { private Map<Integer, Customer> customerDB = new ConcurrentHashMap<Integer, Customer>(); private AtomicInteger idCounter = new AtomicInteger(); public CustomerResource() { Customer customer = new Customer(); customer.setId(1); customer.setFirstName("Bill"); customer.setLastName("Burke"); customer.setStreet("263 Clarendon Street"); customer.setCity("Boston"); customer.setState("MA"); customer.setZip("02115"); customer.setCountry("USA"); customerDB.put(1, customer); } @POST @Consumes("application/xml") public Response createCustomer(Customer customer) { customer.setId(idCounter.incrementAndGet()); customerDB.put(customer.getId(), customer); System.out.println("Created customer " + customer.getId()); return Response.created(URI.create("/customers/" + customer.getId())).build(); } @GET @Path("{id}") @Produces({"application/xml", "application/json"}) public Customer getCustomer(@PathParam("id") int id) { Customer customer = customerDB.get(id); if (customer == null) { throw new WebApplicationException(Response.Status.NOT_FOUND); } return customer; } @GET @Path("{id}") @Produces("text/plain") public String getCustomerString(@PathParam("id") int id) { return getCustomer(id).toString(); } @GET @Path("{id}") @Produces("text/html") public String getCustomerHtml(@PathParam("id") int id) { return "<h1>Customer As HTML</h1><pre>" + getCustomer(id).toString() + "</pre>"; } }