From 6f2b9897f49c0dce6d69df5ce788ad37a783fb38 Mon Sep 17 00:00:00 2001 From: Alessio Soldano Date: Tue, 12 Jan 2016 23:44:04 +0100 Subject: [PATCH] More tests... --- ...sourceTest.java => OrderResourceTest.java} | 4 +- .../examples/ex11_1/CustomerResourceTest.java | 99 ++++++++++ .../examples/ex11_1/domain/Customer.java | 169 ++++++++++++++++++ .../ex11_1/services/CustomerResource.java | 154 ++++++++++++++++ .../ex11_1/services/ShoppingApplication.java | 44 +++++ .../examples/ex12_1/CustomerResourceTest.java | 100 +++++++++++ .../examples/ex12_1/domain/Customer.java | 142 +++++++++++++++ .../ex12_1/features/CacheControlFilter.java | 50 ++++++ .../examples/ex12_1/features/MaxAge.java | 36 ++++ .../ex12_1/features/MaxAgeFeature.java | 42 +++++ .../ex12_1/services/CustomerResource.java | 92 ++++++++++ .../ex12_1/services/ShoppingApplication.java | 55 ++++++ .../examples/ex12_2/CustomerResourceTest.java | 101 +++++++++++ .../examples/ex12_2/domain/Customer.java | 143 +++++++++++++++ .../ex12_2/features/ContentMD5Writer.java | 75 ++++++++ .../ex12_2/services/CustomerResource.java | 90 ++++++++++ .../ex12_2/services/ShoppingApplication.java | 55 ++++++ .../jaxrs/examples/ex11_x/WEB-INF/web.xml | 6 + .../jaxrs/examples/ex12_x/WEB-INF/web.xml | 6 + 19 files changed, 1461 insertions(+), 2 deletions(-) rename modules/testsuite/shared-rest-tests/src/test/java/org/jboss/test/jaxrs/examples/ex10_2/{CustomerResourceTest.java => OrderResourceTest.java} (98%) create mode 100644 modules/testsuite/shared-rest-tests/src/test/java/org/jboss/test/jaxrs/examples/ex11_1/CustomerResourceTest.java create mode 100644 modules/testsuite/shared-rest-tests/src/test/java/org/jboss/test/jaxrs/examples/ex11_1/domain/Customer.java create mode 100644 modules/testsuite/shared-rest-tests/src/test/java/org/jboss/test/jaxrs/examples/ex11_1/services/CustomerResource.java create mode 100644 modules/testsuite/shared-rest-tests/src/test/java/org/jboss/test/jaxrs/examples/ex11_1/services/ShoppingApplication.java create mode 100644 modules/testsuite/shared-rest-tests/src/test/java/org/jboss/test/jaxrs/examples/ex12_1/CustomerResourceTest.java create mode 100644 modules/testsuite/shared-rest-tests/src/test/java/org/jboss/test/jaxrs/examples/ex12_1/domain/Customer.java create mode 100644 modules/testsuite/shared-rest-tests/src/test/java/org/jboss/test/jaxrs/examples/ex12_1/features/CacheControlFilter.java create mode 100644 modules/testsuite/shared-rest-tests/src/test/java/org/jboss/test/jaxrs/examples/ex12_1/features/MaxAge.java create mode 100644 modules/testsuite/shared-rest-tests/src/test/java/org/jboss/test/jaxrs/examples/ex12_1/features/MaxAgeFeature.java create mode 100644 modules/testsuite/shared-rest-tests/src/test/java/org/jboss/test/jaxrs/examples/ex12_1/services/CustomerResource.java create mode 100644 modules/testsuite/shared-rest-tests/src/test/java/org/jboss/test/jaxrs/examples/ex12_1/services/ShoppingApplication.java create mode 100644 modules/testsuite/shared-rest-tests/src/test/java/org/jboss/test/jaxrs/examples/ex12_2/CustomerResourceTest.java create mode 100644 modules/testsuite/shared-rest-tests/src/test/java/org/jboss/test/jaxrs/examples/ex12_2/domain/Customer.java create mode 100644 modules/testsuite/shared-rest-tests/src/test/java/org/jboss/test/jaxrs/examples/ex12_2/features/ContentMD5Writer.java create mode 100644 modules/testsuite/shared-rest-tests/src/test/java/org/jboss/test/jaxrs/examples/ex12_2/services/CustomerResource.java create mode 100644 modules/testsuite/shared-rest-tests/src/test/java/org/jboss/test/jaxrs/examples/ex12_2/services/ShoppingApplication.java create mode 100644 modules/testsuite/shared-rest-tests/src/test/resources/jaxrs/examples/ex11_x/WEB-INF/web.xml create mode 100644 modules/testsuite/shared-rest-tests/src/test/resources/jaxrs/examples/ex12_x/WEB-INF/web.xml diff --git a/modules/testsuite/shared-rest-tests/src/test/java/org/jboss/test/jaxrs/examples/ex10_2/CustomerResourceTest.java b/modules/testsuite/shared-rest-tests/src/test/java/org/jboss/test/jaxrs/examples/ex10_2/OrderResourceTest.java similarity index 98% rename from modules/testsuite/shared-rest-tests/src/test/java/org/jboss/test/jaxrs/examples/ex10_2/CustomerResourceTest.java rename to modules/testsuite/shared-rest-tests/src/test/java/org/jboss/test/jaxrs/examples/ex10_2/OrderResourceTest.java index c566823d5..01edaf71e 100644 --- a/modules/testsuite/shared-rest-tests/src/test/java/org/jboss/test/jaxrs/examples/ex10_2/CustomerResourceTest.java +++ b/modules/testsuite/shared-rest-tests/src/test/java/org/jboss/test/jaxrs/examples/ex10_2/OrderResourceTest.java @@ -53,7 +53,7 @@ * @author Alessio Soldano */ @RunWith(Arquillian.class) -public class CustomerResourceTest extends JBossWSTest +public class OrderResourceTest extends JBossWSTest { @ArquillianResource private URL baseURL; @@ -78,7 +78,7 @@ public static WebArchive createDeployments() { @Test @RunAsClient - public void testQueryCustomers() throws Exception + public void testCreateCancelPurge() throws Exception { Client client = ClientBuilder.newClient(); try { diff --git a/modules/testsuite/shared-rest-tests/src/test/java/org/jboss/test/jaxrs/examples/ex11_1/CustomerResourceTest.java b/modules/testsuite/shared-rest-tests/src/test/java/org/jboss/test/jaxrs/examples/ex11_1/CustomerResourceTest.java new file mode 100644 index 000000000..8c529fb0c --- /dev/null +++ b/modules/testsuite/shared-rest-tests/src/test/java/org/jboss/test/jaxrs/examples/ex11_1/CustomerResourceTest.java @@ -0,0 +1,99 @@ +/* + * JBoss, Home of Professional Open Source. + * Copyright 2015, Red Hat Middleware LLC, and individual contributors + * as indicated by the @author tags. See the copyright.txt file in the + * distribution for a full listing of individual contributors. + * + * This 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 software 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 software; if not, write to the Free + * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA + * 02110-1301 USA, or see the FSF site: http://www.fsf.org. + */ +package org.jboss.test.jaxrs.examples.ex11_1; + +import java.io.File; +import java.net.URL; + +import javax.ws.rs.client.Client; +import javax.ws.rs.client.ClientBuilder; +import javax.ws.rs.client.Entity; +import javax.ws.rs.client.WebTarget; +import javax.ws.rs.core.EntityTag; +import javax.ws.rs.core.Response; + +import org.jboss.arquillian.container.test.api.Deployment; +import org.jboss.arquillian.container.test.api.RunAsClient; +import org.jboss.arquillian.junit.Arquillian; +import org.jboss.arquillian.test.api.ArquillianResource; +import org.jboss.shrinkwrap.api.ShrinkWrap; +import org.jboss.shrinkwrap.api.spec.WebArchive; +import org.jboss.test.jaxrs.examples.ex10_2.domain.Customer; +import org.jboss.wsf.test.JBossWSTest; +import org.jboss.wsf.test.JBossWSTestHelper; +import org.junit.Assert; +import org.junit.Test; +import org.junit.runner.RunWith; + +/** + * @author Bill Burke + * @author Alessio Soldano + */ +@RunWith(Arquillian.class) +public class CustomerResourceTest extends JBossWSTest +{ + @ArquillianResource + private URL baseURL; + + @Deployment(testable = false) + public static WebArchive createDeployments() { + WebArchive archive = ShrinkWrap.create(WebArchive.class, "jaxrs-examples-ex11_1.war"); + archive + .addManifest() + .addClass(org.jboss.test.jaxrs.examples.ex11_1.domain.Customer.class) + .addClass(org.jboss.test.jaxrs.examples.ex11_1.services.CustomerResource.class) + .addClass(org.jboss.test.jaxrs.examples.ex11_1.services.ShoppingApplication.class) + .setWebXML(new File(JBossWSTestHelper.getTestResourcesDir() + "/jaxrs/examples/ex11_x/WEB-INF/web.xml")); + return archive; + } + + @Test + @RunAsClient + public void testCustomerResource() throws Exception + { + Client client = ClientBuilder.newClient(); + try { + WebTarget customerTarget = client.target(baseURL + "services/customers/1"); + Response response = customerTarget.request().get(); + Assert.assertEquals(200, response.getStatus()); + Customer cust = response.readEntity(Customer.class); + + EntityTag etag = response.getEntityTag(); + response.close(); + + response = customerTarget.request() + .header("If-None-Match", etag).get(); + Assert.assertEquals(304, response.getStatus()); + response.close(); + + // Update and send a bad etag with conditional PUT + cust.setCity("Bedford"); + response = customerTarget.request() + .header("If-Match", "JUNK") + .put(Entity.xml(cust)); + Assert.assertEquals(412, response.getStatus()); + response.close(); + } finally { + client.close(); + } + } +} diff --git a/modules/testsuite/shared-rest-tests/src/test/java/org/jboss/test/jaxrs/examples/ex11_1/domain/Customer.java b/modules/testsuite/shared-rest-tests/src/test/java/org/jboss/test/jaxrs/examples/ex11_1/domain/Customer.java new file mode 100644 index 000000000..418f297c6 --- /dev/null +++ b/modules/testsuite/shared-rest-tests/src/test/java/org/jboss/test/jaxrs/examples/ex11_1/domain/Customer.java @@ -0,0 +1,169 @@ +/* + * JBoss, Home of Professional Open Source. + * Copyright 2015, Red Hat Middleware LLC, and individual contributors + * as indicated by the @author tags. See the copyright.txt file in the + * distribution for a full listing of individual contributors. + * + * This 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 software 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 software; if not, write to the Free + * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA + * 02110-1301 USA, or see the FSF site: http://www.fsf.org. + */ +package org.jboss.test.jaxrs.examples.ex11_1.domain; + +import javax.xml.bind.annotation.XmlAttribute; +import javax.xml.bind.annotation.XmlElement; +import javax.xml.bind.annotation.XmlRootElement; +import java.util.Date; + +@XmlRootElement(name = "customer") +public class Customer +{ + private int id; + private String firstName; + private String lastName; + private String street; + private String city; + private String state; + private String zip; + private String country; + private String lastViewed = new Date().toString(); + + @XmlAttribute + public int getId() + { + return id; + } + + public void setId(int id) + { + this.id = id; + } + + @XmlElement(name = "first-name") + public String getFirstName() + { + return firstName; + } + + public void setFirstName(String firstName) + { + this.firstName = firstName; + } + + @XmlElement(name = "last-name") + public String getLastName() + { + return lastName; + } + + public void setLastName(String lastName) + { + this.lastName = lastName; + } + + @XmlElement + public String getStreet() + { + return street; + } + + public void setStreet(String street) + { + this.street = street; + } + + @XmlElement + public String getCity() + { + return city; + } + + public void setCity(String city) + { + this.city = city; + } + + @XmlElement + public String getState() + { + return state; + } + + public void setState(String state) + { + this.state = state; + } + + @XmlElement + public String getZip() + { + return zip; + } + + public void setZip(String zip) + { + this.zip = zip; + } + + @XmlElement + public String getCountry() + { + return country; + } + + public void setCountry(String country) + { + this.country = country; + } + + public String getLastViewed() + { + return lastViewed; + } + + public void setLastViewed(String lastViewed) + { + this.lastViewed = lastViewed; + } + + @Override + public String toString() + { + return "Customer{" + + "id=" + id + + ", firstName='" + firstName + '\'' + + ", lastName='" + lastName + '\'' + + ", street='" + street + '\'' + + ", city='" + city + '\'' + + ", state='" + state + '\'' + + ", zip='" + zip + '\'' + + ", country='" + country + '\'' + + ", lastViewed='" + lastViewed + '\'' + + '}'; + } + + @Override + public int hashCode() + { + int result = id; + result = 31 * result + (firstName != null ? firstName.hashCode() : 0); + result = 31 * result + (lastName != null ? lastName.hashCode() : 0); + result = 31 * result + (street != null ? street.hashCode() : 0); + result = 31 * result + (city != null ? city.hashCode() : 0); + result = 31 * result + (state != null ? state.hashCode() : 0); + result = 31 * result + (zip != null ? zip.hashCode() : 0); + result = 31 * result + (country != null ? country.hashCode() : 0); + return result; + } +} \ No newline at end of file diff --git a/modules/testsuite/shared-rest-tests/src/test/java/org/jboss/test/jaxrs/examples/ex11_1/services/CustomerResource.java b/modules/testsuite/shared-rest-tests/src/test/java/org/jboss/test/jaxrs/examples/ex11_1/services/CustomerResource.java new file mode 100644 index 000000000..5b1028aa4 --- /dev/null +++ b/modules/testsuite/shared-rest-tests/src/test/java/org/jboss/test/jaxrs/examples/ex11_1/services/CustomerResource.java @@ -0,0 +1,154 @@ +/* + * JBoss, Home of Professional Open Source. + * Copyright 2015, Red Hat Middleware LLC, and individual contributors + * as indicated by the @author tags. See the copyright.txt file in the + * distribution for a full listing of individual contributors. + * + * This 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 software 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 software; if not, write to the Free + * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA + * 02110-1301 USA, or see the FSF site: http://www.fsf.org. + */ +package org.jboss.test.jaxrs.examples.ex11_1.services; + +import javax.ws.rs.Consumes; +import javax.ws.rs.GET; +import javax.ws.rs.HeaderParam; +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.Context; +import javax.ws.rs.core.EntityTag; +import javax.ws.rs.core.Request; +import javax.ws.rs.core.Response; + +import org.jboss.test.jaxrs.examples.ex11_1.domain.Customer; + +import java.net.URI; +import java.util.Date; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.atomic.AtomicInteger; + +@Path("/customers") +public class CustomerResource +{ + private Map customerDB = new ConcurrentHashMap(); + private AtomicInteger idCounter = new AtomicInteger(); + + public CustomerResource() + { + Customer customer; + int id = 1; + + customer = new Customer(); + customer.setId(id); + 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(id++, 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") + public Response getCustomer(@PathParam("id") int id, + @HeaderParam("If-None-Match") String sent, + @Context Request request) + { + Customer cust = customerDB.get(id); + if (cust == null) + { + throw new WebApplicationException(Response.Status.NOT_FOUND); + } + + if (sent == null) System.out.println("No If-None-Match sent by client"); + + EntityTag tag = new EntityTag(Integer.toString(cust.hashCode())); + + CacheControl cc = new CacheControl(); + cc.setMaxAge(5); + + + Response.ResponseBuilder builder = request.evaluatePreconditions(tag); + if (builder != null) + { + System.out.println("** revalidation on the server was successful"); + builder.cacheControl(cc); + return builder.build(); + } + + + // Preconditions not met! + + cust.setLastViewed(new Date().toString()); + builder = Response.ok(cust, "application/xml"); + builder.cacheControl(cc); + builder.tag(tag); + return builder.build(); + } + + + @Path("{id}") + @PUT + @Consumes("application/xml") + public Response updateCustomer(@PathParam("id") int id, + @Context Request request, + Customer update) + { + Customer cust = customerDB.get(id); + if (cust == null) throw new WebApplicationException(Response.Status.NOT_FOUND); + EntityTag tag = new EntityTag(Integer.toString(cust.hashCode())); + + Response.ResponseBuilder builder = + request.evaluatePreconditions(tag); + + if (builder != null) + { + // Preconditions not met! + return builder.build(); + } + + // Preconditions met, perform update + + cust.setFirstName(update.getFirstName()); + cust.setLastName(update.getLastName()); + cust.setStreet(update.getStreet()); + cust.setState(update.getState()); + cust.setZip(update.getZip()); + cust.setCountry(update.getCountry()); + + + builder = Response.noContent(); + return builder.build(); + } +} \ No newline at end of file diff --git a/modules/testsuite/shared-rest-tests/src/test/java/org/jboss/test/jaxrs/examples/ex11_1/services/ShoppingApplication.java b/modules/testsuite/shared-rest-tests/src/test/java/org/jboss/test/jaxrs/examples/ex11_1/services/ShoppingApplication.java new file mode 100644 index 000000000..d59250229 --- /dev/null +++ b/modules/testsuite/shared-rest-tests/src/test/java/org/jboss/test/jaxrs/examples/ex11_1/services/ShoppingApplication.java @@ -0,0 +1,44 @@ +/* + * JBoss, Home of Professional Open Source. + * Copyright 2015, Red Hat Middleware LLC, and individual contributors + * as indicated by the @author tags. See the copyright.txt file in the + * distribution for a full listing of individual contributors. + * + * This 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 software 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 software; if not, write to the Free + * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA + * 02110-1301 USA, or see the FSF site: http://www.fsf.org. + */ +package org.jboss.test.jaxrs.examples.ex11_1.services; + +import javax.ws.rs.ApplicationPath; +import javax.ws.rs.core.Application; +import java.util.HashSet; +import java.util.Set; + +@ApplicationPath("/services") +public class ShoppingApplication extends Application +{ + private Set singletons = new HashSet(); + + public ShoppingApplication() + { + singletons.add(new CustomerResource()); + } + + @Override + public Set getSingletons() + { + return singletons; + } +} \ No newline at end of file diff --git a/modules/testsuite/shared-rest-tests/src/test/java/org/jboss/test/jaxrs/examples/ex12_1/CustomerResourceTest.java b/modules/testsuite/shared-rest-tests/src/test/java/org/jboss/test/jaxrs/examples/ex12_1/CustomerResourceTest.java new file mode 100644 index 000000000..8a8883db1 --- /dev/null +++ b/modules/testsuite/shared-rest-tests/src/test/java/org/jboss/test/jaxrs/examples/ex12_1/CustomerResourceTest.java @@ -0,0 +1,100 @@ +/* + * JBoss, Home of Professional Open Source. + * Copyright 2015, Red Hat Middleware LLC, and individual contributors + * as indicated by the @author tags. See the copyright.txt file in the + * distribution for a full listing of individual contributors. + * + * This 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 software 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 software; if not, write to the Free + * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA + * 02110-1301 USA, or see the FSF site: http://www.fsf.org. + */ +package org.jboss.test.jaxrs.examples.ex12_1; + +import java.io.File; +import java.net.URL; + +import javax.ws.rs.client.Client; +import javax.ws.rs.client.ClientBuilder; +import javax.ws.rs.client.Entity; +import javax.ws.rs.core.CacheControl; +import javax.ws.rs.core.HttpHeaders; +import javax.ws.rs.core.Response; + +import org.jboss.arquillian.container.test.api.Deployment; +import org.jboss.arquillian.container.test.api.RunAsClient; +import org.jboss.arquillian.junit.Arquillian; +import org.jboss.arquillian.test.api.ArquillianResource; +import org.jboss.shrinkwrap.api.ShrinkWrap; +import org.jboss.shrinkwrap.api.spec.WebArchive; +import org.jboss.test.jaxrs.examples.ex10_2.domain.Customer; +import org.jboss.wsf.test.JBossWSTest; +import org.jboss.wsf.test.JBossWSTestHelper; +import org.junit.Assert; +import org.junit.Test; +import org.junit.runner.RunWith; + +/** + * @author Bill Burke + * @author Alessio Soldano + */ +@RunWith(Arquillian.class) +public class CustomerResourceTest extends JBossWSTest +{ + @ArquillianResource + private URL baseURL; + + @Deployment(testable = false) + public static WebArchive createDeployments() { + WebArchive archive = ShrinkWrap.create(WebArchive.class, "jaxrs-examples-ex12_1.war"); + archive + .addManifest() + .addClass(org.jboss.test.jaxrs.examples.ex12_1.domain.Customer.class) + .addClass(org.jboss.test.jaxrs.examples.ex12_1.features.MaxAge.class) + .addClass(org.jboss.test.jaxrs.examples.ex12_1.features.MaxAgeFeature.class) + .addClass(org.jboss.test.jaxrs.examples.ex12_1.features.CacheControlFilter.class) + .addClass(org.jboss.test.jaxrs.examples.ex12_1.services.CustomerResource.class) + .addClass(org.jboss.test.jaxrs.examples.ex12_1.services.ShoppingApplication.class) + .setWebXML(new File(JBossWSTestHelper.getTestResourcesDir() + "/jaxrs/examples/ex12_x/WEB-INF/web.xml")); + return archive; + } + + @Test + @RunAsClient + public void testCustomerResource() throws Exception + { + Client client = ClientBuilder.newClient(); + try { + Customer newCustomer = new Customer(); + newCustomer.setFirstName("Bill"); + newCustomer.setLastName("Burke"); + newCustomer.setStreet("256 Clarendon Street"); + newCustomer.setCity("Boston"); + newCustomer.setState("MA"); + newCustomer.setZip("02115"); + newCustomer.setCountry("USA"); + + Response response = client.target(baseURL + "services/customers") + .request().post(Entity.xml(newCustomer)); + if (response.getStatus() != 201) throw new RuntimeException("Failed to create"); + String location = response.getLocation().toString(); + response.close(); + + response = client.target(location).request().get(); + CacheControl cc = CacheControl.valueOf(response.getHeaderString(HttpHeaders.CACHE_CONTROL)); + Assert.assertEquals(500, cc.getMaxAge()); + } finally { + client.close(); + } + } +} diff --git a/modules/testsuite/shared-rest-tests/src/test/java/org/jboss/test/jaxrs/examples/ex12_1/domain/Customer.java b/modules/testsuite/shared-rest-tests/src/test/java/org/jboss/test/jaxrs/examples/ex12_1/domain/Customer.java new file mode 100644 index 000000000..d75499676 --- /dev/null +++ b/modules/testsuite/shared-rest-tests/src/test/java/org/jboss/test/jaxrs/examples/ex12_1/domain/Customer.java @@ -0,0 +1,142 @@ +/* + * JBoss, Home of Professional Open Source. + * Copyright 2015, Red Hat Middleware LLC, and individual contributors + * as indicated by the @author tags. See the copyright.txt file in the + * distribution for a full listing of individual contributors. + * + * This 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 software 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 software; if not, write to the Free + * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA + * 02110-1301 USA, or see the FSF site: http://www.fsf.org. + */ +package org.jboss.test.jaxrs.examples.ex12_1.domain; + +import javax.xml.bind.annotation.XmlAttribute; +import javax.xml.bind.annotation.XmlElement; +import javax.xml.bind.annotation.XmlRootElement; + +@XmlRootElement(name = "customer") +public class Customer +{ + private int id; + private String firstName; + private String lastName; + private String street; + private String city; + private String state; + private String zip; + private String country; + + @XmlAttribute + public int getId() + { + return id; + } + + public void setId(int id) + { + this.id = id; + } + + @XmlElement(name = "first-name") + public String getFirstName() + { + return firstName; + } + + public void setFirstName(String firstName) + { + this.firstName = firstName; + } + + @XmlElement(name = "last-name") + public String getLastName() + { + return lastName; + } + + public void setLastName(String lastName) + { + this.lastName = lastName; + } + + @XmlElement + public String getStreet() + { + return street; + } + + public void setStreet(String street) + { + this.street = street; + } + + @XmlElement + public String getCity() + { + return city; + } + + public void setCity(String city) + { + this.city = city; + } + + @XmlElement + public String getState() + { + return state; + } + + public void setState(String state) + { + this.state = state; + } + + @XmlElement + public String getZip() + { + return zip; + } + + public void setZip(String zip) + { + this.zip = zip; + } + + @XmlElement + public String getCountry() + { + return country; + } + + public void setCountry(String country) + { + this.country = country; + } + + @Override + public String toString() + { + return "Customer{" + + "id=" + id + + ", firstName='" + firstName + '\'' + + ", lastName='" + lastName + '\'' + + ", street='" + street + '\'' + + ", city='" + city + '\'' + + ", state='" + state + '\'' + + ", zip='" + zip + '\'' + + ", country='" + country + '\'' + + '}'; + } +} \ No newline at end of file diff --git a/modules/testsuite/shared-rest-tests/src/test/java/org/jboss/test/jaxrs/examples/ex12_1/features/CacheControlFilter.java b/modules/testsuite/shared-rest-tests/src/test/java/org/jboss/test/jaxrs/examples/ex12_1/features/CacheControlFilter.java new file mode 100644 index 000000000..39a18a009 --- /dev/null +++ b/modules/testsuite/shared-rest-tests/src/test/java/org/jboss/test/jaxrs/examples/ex12_1/features/CacheControlFilter.java @@ -0,0 +1,50 @@ +/* + * JBoss, Home of Professional Open Source. + * Copyright 2015, Red Hat Middleware LLC, and individual contributors + * as indicated by the @author tags. See the copyright.txt file in the + * distribution for a full listing of individual contributors. + * + * This 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 software 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 software; if not, write to the Free + * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA + * 02110-1301 USA, or see the FSF site: http://www.fsf.org. + */ +package org.jboss.test.jaxrs.examples.ex12_1.features; + +import javax.ws.rs.container.ContainerRequestContext; +import javax.ws.rs.container.ContainerResponseContext; +import javax.ws.rs.container.ContainerResponseFilter; +import javax.ws.rs.core.CacheControl; +import java.io.IOException; + +/** + * @author Bill Burke + */ +public class CacheControlFilter implements ContainerResponseFilter +{ + private int maxAge; + + public CacheControlFilter(int maxAge) { + this.maxAge = maxAge; + } + + public void filter(ContainerRequestContext req, ContainerResponseContext res) + throws IOException + { + if (req.getMethod().equals("GET")) { + CacheControl cc = new CacheControl(); + cc.setMaxAge(this.maxAge); + res.getHeaders().add("Cache-Control", cc); + } + } +} \ No newline at end of file diff --git a/modules/testsuite/shared-rest-tests/src/test/java/org/jboss/test/jaxrs/examples/ex12_1/features/MaxAge.java b/modules/testsuite/shared-rest-tests/src/test/java/org/jboss/test/jaxrs/examples/ex12_1/features/MaxAge.java new file mode 100644 index 000000000..2324e3226 --- /dev/null +++ b/modules/testsuite/shared-rest-tests/src/test/java/org/jboss/test/jaxrs/examples/ex12_1/features/MaxAge.java @@ -0,0 +1,36 @@ +/* + * JBoss, Home of Professional Open Source. + * Copyright 2015, Red Hat Middleware LLC, and individual contributors + * as indicated by the @author tags. See the copyright.txt file in the + * distribution for a full listing of individual contributors. + * + * This 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 software 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 software; if not, write to the Free + * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA + * 02110-1301 USA, or see the FSF site: http://www.fsf.org. + */ +package org.jboss.test.jaxrs.examples.ex12_1.features; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * @author Bill Burke + */ +@Target(ElementType.METHOD) +@Retention(RetentionPolicy.RUNTIME) +public @interface MaxAge { + int value(); +} \ No newline at end of file diff --git a/modules/testsuite/shared-rest-tests/src/test/java/org/jboss/test/jaxrs/examples/ex12_1/features/MaxAgeFeature.java b/modules/testsuite/shared-rest-tests/src/test/java/org/jboss/test/jaxrs/examples/ex12_1/features/MaxAgeFeature.java new file mode 100644 index 000000000..688282b57 --- /dev/null +++ b/modules/testsuite/shared-rest-tests/src/test/java/org/jboss/test/jaxrs/examples/ex12_1/features/MaxAgeFeature.java @@ -0,0 +1,42 @@ +/* + * JBoss, Home of Professional Open Source. + * Copyright 2015, Red Hat Middleware LLC, and individual contributors + * as indicated by the @author tags. See the copyright.txt file in the + * distribution for a full listing of individual contributors. + * + * This 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 software 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 software; if not, write to the Free + * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA + * 02110-1301 USA, or see the FSF site: http://www.fsf.org. + */ +package org.jboss.test.jaxrs.examples.ex12_1.features; + +import javax.ws.rs.container.DynamicFeature; +import javax.ws.rs.container.ResourceInfo; +import javax.ws.rs.core.FeatureContext; +import javax.ws.rs.ext.Provider; + +/** + * @author Bill Burke + */ +@Provider +public class MaxAgeFeature implements DynamicFeature +{ + + public void configure(ResourceInfo ri, FeatureContext ctx) { + MaxAge max = ri.getResourceMethod().getAnnotation(MaxAge.class); + if (max == null) return; + CacheControlFilter filter = new CacheControlFilter(max.value()); + ctx.register(filter); + } +} \ No newline at end of file diff --git a/modules/testsuite/shared-rest-tests/src/test/java/org/jboss/test/jaxrs/examples/ex12_1/services/CustomerResource.java b/modules/testsuite/shared-rest-tests/src/test/java/org/jboss/test/jaxrs/examples/ex12_1/services/CustomerResource.java new file mode 100644 index 000000000..9ce981f2d --- /dev/null +++ b/modules/testsuite/shared-rest-tests/src/test/java/org/jboss/test/jaxrs/examples/ex12_1/services/CustomerResource.java @@ -0,0 +1,92 @@ +/* + * JBoss, Home of Professional Open Source. + * Copyright 2015, Red Hat Middleware LLC, and individual contributors + * as indicated by the @author tags. See the copyright.txt file in the + * distribution for a full listing of individual contributors. + * + * This 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 software 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 software; if not, write to the Free + * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA + * 02110-1301 USA, or see the FSF site: http://www.fsf.org. + */ +package org.jboss.test.jaxrs.examples.ex12_1.services; + +import javax.ws.rs.Consumes; +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.Response; + +import org.jboss.test.jaxrs.examples.ex12_1.domain.Customer; +import org.jboss.test.jaxrs.examples.ex12_1.features.MaxAge; + +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 customerDB = new ConcurrentHashMap(); + private AtomicInteger idCounter = new AtomicInteger(); + + public CustomerResource() + { + } + + @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") + @MaxAge(500) + public Customer getCustomer(@PathParam("id") int id) + { + Customer customer = customerDB.get(id); + if (customer == null) + { + throw new WebApplicationException(Response.Status.NOT_FOUND); + } + return customer; + } + + @PUT + @Path("{id}") + @Consumes("application/xml") + public void updateCustomer(@PathParam("id") int id, Customer update) + { + Customer current = customerDB.get(id); + if (current == null) throw new WebApplicationException(Response.Status.NOT_FOUND); + + current.setFirstName(update.getFirstName()); + current.setLastName(update.getLastName()); + current.setStreet(update.getStreet()); + current.setState(update.getState()); + current.setZip(update.getZip()); + current.setCountry(update.getCountry()); + } +} \ No newline at end of file diff --git a/modules/testsuite/shared-rest-tests/src/test/java/org/jboss/test/jaxrs/examples/ex12_1/services/ShoppingApplication.java b/modules/testsuite/shared-rest-tests/src/test/java/org/jboss/test/jaxrs/examples/ex12_1/services/ShoppingApplication.java new file mode 100644 index 000000000..57b1e67a1 --- /dev/null +++ b/modules/testsuite/shared-rest-tests/src/test/java/org/jboss/test/jaxrs/examples/ex12_1/services/ShoppingApplication.java @@ -0,0 +1,55 @@ +/* + * JBoss, Home of Professional Open Source. + * Copyright 2015, Red Hat Middleware LLC, and individual contributors + * as indicated by the @author tags. See the copyright.txt file in the + * distribution for a full listing of individual contributors. + * + * This 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 software 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 software; if not, write to the Free + * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA + * 02110-1301 USA, or see the FSF site: http://www.fsf.org. + */ +package org.jboss.test.jaxrs.examples.ex12_1.services; + +import javax.ws.rs.ApplicationPath; +import javax.ws.rs.core.Application; + +import org.jboss.test.jaxrs.examples.ex12_1.features.MaxAgeFeature; + +import java.util.HashSet; +import java.util.Set; + +@ApplicationPath("/services") +public class ShoppingApplication extends Application +{ + private Set singletons = new HashSet(); + private Set> classes = new HashSet>(); + + public ShoppingApplication() + { + singletons.add(new CustomerResource()); + classes.add(MaxAgeFeature.class); + } + + @Override + public Set> getClasses() + { + return classes; + } + + @Override + public Set getSingletons() + { + return singletons; + } +} \ No newline at end of file diff --git a/modules/testsuite/shared-rest-tests/src/test/java/org/jboss/test/jaxrs/examples/ex12_2/CustomerResourceTest.java b/modules/testsuite/shared-rest-tests/src/test/java/org/jboss/test/jaxrs/examples/ex12_2/CustomerResourceTest.java new file mode 100644 index 000000000..6e213026a --- /dev/null +++ b/modules/testsuite/shared-rest-tests/src/test/java/org/jboss/test/jaxrs/examples/ex12_2/CustomerResourceTest.java @@ -0,0 +1,101 @@ +/* + * JBoss, Home of Professional Open Source. + * Copyright 2015, Red Hat Middleware LLC, and individual contributors + * as indicated by the @author tags. See the copyright.txt file in the + * distribution for a full listing of individual contributors. + * + * This 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 software 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 software; if not, write to the Free + * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA + * 02110-1301 USA, or see the FSF site: http://www.fsf.org. + */ +package org.jboss.test.jaxrs.examples.ex12_2; + +import java.io.File; +import java.net.URL; + +import javax.ws.rs.client.Client; +import javax.ws.rs.client.ClientBuilder; +import javax.ws.rs.client.Entity; +import javax.ws.rs.core.CacheControl; +import javax.ws.rs.core.HttpHeaders; +import javax.ws.rs.core.Response; + +import org.jboss.arquillian.container.test.api.Deployment; +import org.jboss.arquillian.container.test.api.RunAsClient; +import org.jboss.arquillian.junit.Arquillian; +import org.jboss.arquillian.test.api.ArquillianResource; +import org.jboss.shrinkwrap.api.ShrinkWrap; +import org.jboss.shrinkwrap.api.asset.StringAsset; +import org.jboss.shrinkwrap.api.spec.WebArchive; +import org.jboss.test.jaxrs.examples.ex10_2.domain.Customer; +import org.jboss.wsf.test.JBossWSTest; +import org.jboss.wsf.test.JBossWSTestHelper; +import org.junit.Assert; +import org.junit.Test; +import org.junit.runner.RunWith; + +/** + * @author Bill Burke + * @author Alessio Soldano + */ +@RunWith(Arquillian.class) +public class CustomerResourceTest extends JBossWSTest +{ + @ArquillianResource + private URL baseURL; + + @Deployment(testable = false) + public static WebArchive createDeployments() { + WebArchive archive = ShrinkWrap.create(WebArchive.class, "jaxrs-examples-ex12_2.war"); + archive + .setManifest(new StringAsset("Manifest-Version: 1.0\n" + "Dependencies: org.jboss.ws.common\n")) + .addClass(org.jboss.test.jaxrs.examples.ex12_2.domain.Customer.class) + .addClass(org.jboss.test.jaxrs.examples.ex12_2.features.ContentMD5Writer.class) + .addClass(org.jboss.test.jaxrs.examples.ex12_2.services.CustomerResource.class) + .addClass(org.jboss.test.jaxrs.examples.ex12_2.services.ShoppingApplication.class) + .setWebXML(new File(JBossWSTestHelper.getTestResourcesDir() + "/jaxrs/examples/ex12_x/WEB-INF/web.xml")); + return archive; + } + + @Test + @RunAsClient + public void testCustomerResource() throws Exception + { + Client client = ClientBuilder.newClient(); + try { + Customer newCustomer = new Customer(); + newCustomer.setFirstName("Bill"); + newCustomer.setLastName("Burke"); + newCustomer.setStreet("256 Clarendon Street"); + newCustomer.setCity("Boston"); + newCustomer.setState("MA"); + newCustomer.setZip("02115"); + newCustomer.setCountry("USA"); + + Response response = client.target(baseURL + "services/customers") + .request().post(Entity.xml(newCustomer)); + if (response.getStatus() != 201) throw new RuntimeException("Failed to create"); + String location = response.getLocation().toString(); + System.out.println("Location: " + location); + response.close(); + + System.out.println("*** GET Created Customer **"); + response = client.target(location).request().get(); + String md5 = response.getHeaderString("Content-MD5"); + Assert.assertEquals("uPYTHd3xAeXEk8NMp7A6hw==", md5); + } finally { + client.close(); + } + } +} diff --git a/modules/testsuite/shared-rest-tests/src/test/java/org/jboss/test/jaxrs/examples/ex12_2/domain/Customer.java b/modules/testsuite/shared-rest-tests/src/test/java/org/jboss/test/jaxrs/examples/ex12_2/domain/Customer.java new file mode 100644 index 000000000..2c39f936f --- /dev/null +++ b/modules/testsuite/shared-rest-tests/src/test/java/org/jboss/test/jaxrs/examples/ex12_2/domain/Customer.java @@ -0,0 +1,143 @@ +/* + * JBoss, Home of Professional Open Source. + * Copyright 2015, Red Hat Middleware LLC, and individual contributors + * as indicated by the @author tags. See the copyright.txt file in the + * distribution for a full listing of individual contributors. + * + * This 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 software 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 software; if not, write to the Free + * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA + * 02110-1301 USA, or see the FSF site: http://www.fsf.org. + */ +package org.jboss.test.jaxrs.examples.ex12_2.domain; + + +import javax.xml.bind.annotation.XmlAttribute; +import javax.xml.bind.annotation.XmlElement; +import javax.xml.bind.annotation.XmlRootElement; + +@XmlRootElement(name = "customer") +public class Customer +{ + private int id; + private String firstName; + private String lastName; + private String street; + private String city; + private String state; + private String zip; + private String country; + + @XmlAttribute + public int getId() + { + return id; + } + + public void setId(int id) + { + this.id = id; + } + + @XmlElement(name = "first-name") + public String getFirstName() + { + return firstName; + } + + public void setFirstName(String firstName) + { + this.firstName = firstName; + } + + @XmlElement(name = "last-name") + public String getLastName() + { + return lastName; + } + + public void setLastName(String lastName) + { + this.lastName = lastName; + } + + @XmlElement + public String getStreet() + { + return street; + } + + public void setStreet(String street) + { + this.street = street; + } + + @XmlElement + public String getCity() + { + return city; + } + + public void setCity(String city) + { + this.city = city; + } + + @XmlElement + public String getState() + { + return state; + } + + public void setState(String state) + { + this.state = state; + } + + @XmlElement + public String getZip() + { + return zip; + } + + public void setZip(String zip) + { + this.zip = zip; + } + + @XmlElement + public String getCountry() + { + return country; + } + + public void setCountry(String country) + { + this.country = country; + } + + @Override + public String toString() + { + return "Customer{" + + "id=" + id + + ", firstName='" + firstName + '\'' + + ", lastName='" + lastName + '\'' + + ", street='" + street + '\'' + + ", city='" + city + '\'' + + ", state='" + state + '\'' + + ", zip='" + zip + '\'' + + ", country='" + country + '\'' + + '}'; + } +} \ No newline at end of file diff --git a/modules/testsuite/shared-rest-tests/src/test/java/org/jboss/test/jaxrs/examples/ex12_2/features/ContentMD5Writer.java b/modules/testsuite/shared-rest-tests/src/test/java/org/jboss/test/jaxrs/examples/ex12_2/features/ContentMD5Writer.java new file mode 100644 index 000000000..166b82648 --- /dev/null +++ b/modules/testsuite/shared-rest-tests/src/test/java/org/jboss/test/jaxrs/examples/ex12_2/features/ContentMD5Writer.java @@ -0,0 +1,75 @@ +/* + * JBoss, Home of Professional Open Source. + * Copyright 2015, Red Hat Middleware LLC, and individual contributors + * as indicated by the @author tags. See the copyright.txt file in the + * distribution for a full listing of individual contributors. + * + * This 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 software 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 software; if not, write to the Free + * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA + * 02110-1301 USA, or see the FSF site: http://www.fsf.org. + */ +package org.jboss.test.jaxrs.examples.ex12_2.features; + +import javax.ws.rs.WebApplicationException; +import javax.ws.rs.ext.WriterInterceptor; +import javax.ws.rs.ext.WriterInterceptorContext; + +import org.jboss.ws.common.utils.Base64; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.OutputStream; +import java.security.DigestOutputStream; +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; + +/** + * @author Bill Burke + */ +public class ContentMD5Writer implements WriterInterceptor +{ + @Override + public void aroundWriteTo(WriterInterceptorContext context) throws IOException, WebApplicationException + { + MessageDigest digest = null; + try + { + digest = MessageDigest.getInstance("MD5"); + } + catch (NoSuchAlgorithmException e) + { + throw new IllegalArgumentException(e); + } + ByteArrayOutputStream buffer = new ByteArrayOutputStream(); + DigestOutputStream digestStream = new DigestOutputStream(buffer, digest); + OutputStream old = context.getOutputStream(); + context.setOutputStream(digestStream); + + try + { + context.proceed(); + + byte[] hash = digest.digest(); + String encodedHash = Base64.encodeBytes(hash); + context.getHeaders().putSingle("Content-MD5", encodedHash); + + byte[] content = buffer.toByteArray(); + old.write(content); + } + finally + { + context.setOutputStream(old); + } + } +} \ No newline at end of file diff --git a/modules/testsuite/shared-rest-tests/src/test/java/org/jboss/test/jaxrs/examples/ex12_2/services/CustomerResource.java b/modules/testsuite/shared-rest-tests/src/test/java/org/jboss/test/jaxrs/examples/ex12_2/services/CustomerResource.java new file mode 100644 index 000000000..cf90ea9bf --- /dev/null +++ b/modules/testsuite/shared-rest-tests/src/test/java/org/jboss/test/jaxrs/examples/ex12_2/services/CustomerResource.java @@ -0,0 +1,90 @@ +/* + * JBoss, Home of Professional Open Source. + * Copyright 2015, Red Hat Middleware LLC, and individual contributors + * as indicated by the @author tags. See the copyright.txt file in the + * distribution for a full listing of individual contributors. + * + * This 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 software 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 software; if not, write to the Free + * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA + * 02110-1301 USA, or see the FSF site: http://www.fsf.org. + */ +package org.jboss.test.jaxrs.examples.ex12_2.services; + +import javax.ws.rs.Consumes; +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.Response; + +import org.jboss.test.jaxrs.examples.ex12_2.domain.Customer; + +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 customerDB = new ConcurrentHashMap(); + private AtomicInteger idCounter = new AtomicInteger(); + + public CustomerResource() + { + } + + @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") + public Customer getCustomer(@PathParam("id") int id) + { + Customer customer = customerDB.get(id); + if (customer == null) + { + throw new WebApplicationException(Response.Status.NOT_FOUND); + } + return customer; + } + + @PUT + @Path("{id}") + @Consumes("application/xml") + public void updateCustomer(@PathParam("id") int id, Customer update) + { + Customer current = customerDB.get(id); + if (current == null) throw new WebApplicationException(Response.Status.NOT_FOUND); + + current.setFirstName(update.getFirstName()); + current.setLastName(update.getLastName()); + current.setStreet(update.getStreet()); + current.setState(update.getState()); + current.setZip(update.getZip()); + current.setCountry(update.getCountry()); + } +} \ No newline at end of file diff --git a/modules/testsuite/shared-rest-tests/src/test/java/org/jboss/test/jaxrs/examples/ex12_2/services/ShoppingApplication.java b/modules/testsuite/shared-rest-tests/src/test/java/org/jboss/test/jaxrs/examples/ex12_2/services/ShoppingApplication.java new file mode 100644 index 000000000..b329e27c7 --- /dev/null +++ b/modules/testsuite/shared-rest-tests/src/test/java/org/jboss/test/jaxrs/examples/ex12_2/services/ShoppingApplication.java @@ -0,0 +1,55 @@ +/* + * JBoss, Home of Professional Open Source. + * Copyright 2015, Red Hat Middleware LLC, and individual contributors + * as indicated by the @author tags. See the copyright.txt file in the + * distribution for a full listing of individual contributors. + * + * This 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 software 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 software; if not, write to the Free + * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA + * 02110-1301 USA, or see the FSF site: http://www.fsf.org. + */ +package org.jboss.test.jaxrs.examples.ex12_2.services; + +import javax.ws.rs.ApplicationPath; +import javax.ws.rs.core.Application; + +import org.jboss.test.jaxrs.examples.ex12_2.features.ContentMD5Writer; + +import java.util.HashSet; +import java.util.Set; + +@ApplicationPath("/services") +public class ShoppingApplication extends Application +{ + private Set singletons = new HashSet(); + private Set> classes = new HashSet>(); + + public ShoppingApplication() + { + singletons.add(new CustomerResource()); + classes.add(ContentMD5Writer.class); + } + + @Override + public Set> getClasses() + { + return classes; + } + + @Override + public Set getSingletons() + { + return singletons; + } +} \ No newline at end of file diff --git a/modules/testsuite/shared-rest-tests/src/test/resources/jaxrs/examples/ex11_x/WEB-INF/web.xml b/modules/testsuite/shared-rest-tests/src/test/resources/jaxrs/examples/ex11_x/WEB-INF/web.xml new file mode 100644 index 000000000..0f80d8e06 --- /dev/null +++ b/modules/testsuite/shared-rest-tests/src/test/resources/jaxrs/examples/ex11_x/WEB-INF/web.xml @@ -0,0 +1,6 @@ + + + diff --git a/modules/testsuite/shared-rest-tests/src/test/resources/jaxrs/examples/ex12_x/WEB-INF/web.xml b/modules/testsuite/shared-rest-tests/src/test/resources/jaxrs/examples/ex12_x/WEB-INF/web.xml new file mode 100644 index 000000000..0f80d8e06 --- /dev/null +++ b/modules/testsuite/shared-rest-tests/src/test/resources/jaxrs/examples/ex12_x/WEB-INF/web.xml @@ -0,0 +1,6 @@ + + +