protected JSONObject getCustomerData(Customer customer) { JSONObject jsonObject = new JSONObject(); jsonObject.put(Constants.FORM_CUSTOMER_ID, customer.getId()); jsonObject.put(Constants.FORM_FIRST_NAME, customer.getFirstName()); jsonObject.put(Constants.FORM_LAST_NAME, customer.getLastName()); jsonObject.put(Constants.FORM_STREET, customer.getStreet()); jsonObject.put(Constants.FORM_CITY, customer.getCity()); jsonObject.put(Constants.FORM_STATE, customer.getState()); jsonObject.put(Constants.FORM_ZIP, customer.getZip()); jsonObject.put(Constants.FORM_COUNTRY, customer.getCountry()); return jsonObject; }
@POST // @RolesAllowed("rest-jersey-examples") // not ideal because each change requires the code to be // re-compiled....use web.xml @Path("insert") @Produces("application/json") // TODO Make a constant string class @Consumes("application/x-www-form-urlencoded") // TODO Make a constant string class public Response createCustomer( @FormParam(Constants.FORM_FIRST_NAME) String firstName, // the FormParam annotation is used to access // "application/x-www-form-urlencoded" request bodies @FormParam(Constants.FORM_LAST_NAME) String lastName, @FormParam(Constants.FORM_STREET) String street, @FormParam(Constants.FORM_CITY) String city, @FormParam(Constants.FORM_STATE) String state, @FormParam(Constants.FORM_ZIP) String zip, @FormParam(Constants.FORM_COUNTRY) String country, @HeaderParam("CustomHeader") String CustomHeader, @CookieParam("customerId") int customerId, @Context HttpHeaders allHeaders) { // http://localhost:8080/RestJerseyExamples/customers/insert // http://barsc09-u122264:8080/RestJerseyExamples/customers/insert // insert the form data into the table int id = idCounter.incrementAndGet(); Customer customer = new Customer(id, firstName, lastName, street, city, state, zip, country); customerDb.put(id, customer); // create JSON object response body JSONObject jsonObject = new JSONObject(); jsonObject.put(Constants.CUSTOMERS, customerDb.size()); jsonObject.put(Constants.MESSAGE, Constants.SUCCESS); // Create a cookie to send to the client NewCookie cookie1 = new NewCookie("CookieKey", "CookieValue"); NewCookie cookie2 = new NewCookie("CookieName", "CookieValue"); // // Set cache information // CacheControl cache = new CacheControl(); // cache.setMaxAge(300); // 300 seconds // cache.setPrivate(true); // no caching on the shared proxy, only the final client // cache.setNoStore(true); // client cannot persist to disk, only in-memory. // Log information System.out.println(customer.toString()); System.out.println(CustomHeader); System.out.println(allHeaders.getRequestHeader("content-length").get(0)); for (String header : allHeaders.getRequestHeaders().keySet()) { System.out.println( "This header was set: " + header + " = " + allHeaders.getRequestHeader(header).get(0)); } System.out.println("Cookie: customerId = " + customerId); for (String name : allHeaders.getCookies().keySet()) { Cookie cookie = allHeaders.getCookies().get(name); System.out.println("This cookie was set: " + name + " = " + cookie.getValue()); } // Return the response to the client return Response.status( Status .CREATED) // Check the status Enum for all the constants. Range: 200-299 Success, // 300-399 Success, but redirection, 400-499 client errors, 500-599 server // errors .cookie(cookie1) .cookie(cookie2) .cacheControl(cache) .entity(jsonObject.toJSONString()) .build(); }