From 1bf0fedd2232de8ea1c03b445ccc86cf8bd12ae7 Mon Sep 17 00:00:00 2001 From: LightGuard Date: Wed, 17 Nov 2010 16:37:44 -0700 Subject: [PATCH] Adding a JAX-RS example Doesn't deploy currently (testing glassfish first) but the example is here. --- examples/jaxrs/pom.xml | 265 ++++++++++++++++++ .../example/jaxrs/LibraryApplication.java | 46 +++ .../example/jaxrs/db/InitDatabaseBean.java | 95 +++++++ .../jaxrs/db/InitDatabaseListener.java | 45 +++ .../db/ManagedPeristenceContextProducer.java | 39 +++ .../control/example/jaxrs/entity/Author.java | 95 +++++++ .../control/example/jaxrs/entity/Book.java | 96 +++++++ .../example/jaxrs/handler/CatchBridge.java | 58 ++++ .../example/jaxrs/handler/RestCatch.java | 36 +++ .../jaxrs/handler/RestCatchLiteral.java | 32 +++ .../jaxrs/handler/RestHandlerContainer.java | 57 ++++ .../jaxrs/resource/AuthorResource.java | 59 ++++ .../example/jaxrs/resource/BookResource.java | 61 ++++ .../main/resources/META-INF/persistence.xml | 35 +++ .../jaxrs/src/main/webapp/WEB-INF/beans.xml | 48 ++++ .../jaxrs/src/main/webapp/WEB-INF/web.xml | 28 ++ examples/jaxrs/web/WEB-INF/web.xml | 8 + impl/pom.xml | 5 - pom.xml | 2 +- 19 files changed, 1104 insertions(+), 6 deletions(-) create mode 100644 examples/jaxrs/pom.xml create mode 100644 examples/jaxrs/src/main/java/org/jboss/seam/exception/control/example/jaxrs/LibraryApplication.java create mode 100644 examples/jaxrs/src/main/java/org/jboss/seam/exception/control/example/jaxrs/db/InitDatabaseBean.java create mode 100644 examples/jaxrs/src/main/java/org/jboss/seam/exception/control/example/jaxrs/db/InitDatabaseListener.java create mode 100644 examples/jaxrs/src/main/java/org/jboss/seam/exception/control/example/jaxrs/db/ManagedPeristenceContextProducer.java create mode 100644 examples/jaxrs/src/main/java/org/jboss/seam/exception/control/example/jaxrs/entity/Author.java create mode 100644 examples/jaxrs/src/main/java/org/jboss/seam/exception/control/example/jaxrs/entity/Book.java create mode 100644 examples/jaxrs/src/main/java/org/jboss/seam/exception/control/example/jaxrs/handler/CatchBridge.java create mode 100644 examples/jaxrs/src/main/java/org/jboss/seam/exception/control/example/jaxrs/handler/RestCatch.java create mode 100644 examples/jaxrs/src/main/java/org/jboss/seam/exception/control/example/jaxrs/handler/RestCatchLiteral.java create mode 100644 examples/jaxrs/src/main/java/org/jboss/seam/exception/control/example/jaxrs/handler/RestHandlerContainer.java create mode 100644 examples/jaxrs/src/main/java/org/jboss/seam/exception/control/example/jaxrs/resource/AuthorResource.java create mode 100644 examples/jaxrs/src/main/java/org/jboss/seam/exception/control/example/jaxrs/resource/BookResource.java create mode 100644 examples/jaxrs/src/main/resources/META-INF/persistence.xml create mode 100644 examples/jaxrs/src/main/webapp/WEB-INF/beans.xml create mode 100644 examples/jaxrs/src/main/webapp/WEB-INF/web.xml create mode 100644 examples/jaxrs/web/WEB-INF/web.xml diff --git a/examples/jaxrs/pom.xml b/examples/jaxrs/pom.xml new file mode 100644 index 0000000..05537f0 --- /dev/null +++ b/examples/jaxrs/pom.xml @@ -0,0 +1,265 @@ + + + 4.0.0 + + + org.jboss.seam.catch + seam-catch-parent + 3.0.0-SNAPSHOT + ../../pom.xml + + + org.jboss.seam.catch + catch-jaxrs-example + war + 3.0.0-SNAPSHOT + Seam Catch JAX-RS example + + + 2.0.1.GA + 3.0.0.Alpha1 + 3.0.0.Alpha1 + 3.0.0.Alpha3 + 1.0.0.Beta2 + 1.1.1 + 1.0.0.Final + 1.0.0.Final + 3.0.0.Beta4 + + + + + + org.jboss.logging + jboss-logging + ${jboss.logging.version} + + + javax.ws.rs + jsr311-api + ${javax.ws.rs.version} + + + org.jboss.seam.rest + seam-rest-api + ${seam.rest.version} + + + org.jboss.seam.rest + seam-rest + ${seam.rest.version} + + + org.jboss.resteasy + resteasy-jaxrs + ${resteasy.version} + + + javax.activation + activation + + + commons-httpclient + commons-httpclient + + + org.apache.httpcomponents + httpclient + + + commons-codec + commons-codec + + + + + org.jboss.resteasy + resteasy-cdi + ${resteasy.version} + + + org.jboss.seam.catch + seam-catch-api + ${project.parent.version} + + + org.jboss.seam.catch + seam-catch-impl + ${project.parent.version} + + + org.hibernate.javax.persistence + hibernate-jpa-2.0-api + ${javax.persistence.version} + + + org.jboss.spec.javax.ejb + jboss-ejb-api_3.1_spec + ${javax.ejb.version} + + + org.jboss.spec.javax.servlet + jboss-servlet-api_3.0_spec + ${javax.servlet.version} + + + org.jboss.seam.persistence + seam-persistence-api + ${seam.persistence.version} + + + org.jboss.seam.persistence + seam-persistence-impl + ${seam.persistence.version} + + + + + + + org.jboss.spec.javax.ejb + jboss-ejb-api_3.1_spec + provided + + + org.jboss.logging + jboss-logging + runtime + + + javax.validation + validation-api + provided + + + org.jboss.spec.javax.servlet + jboss-servlet-api_3.0_spec + provided + + + org.hibernate.javax.persistence + hibernate-jpa-2.0-api + provided + + + javax.enterprise + cdi-api + provided + + + org.jboss.spec.javax.interceptor + jboss-interceptors-api_1.1_spec + provided + + + org.jboss.seam.rest + seam-rest-api + compile + + + javax.ws.rs + jsr311-api + provided + + + org.jboss.seam.catch + seam-catch-api + compile + + + org.jboss.seam.catch + seam-catch-impl + runtime + + + org.jboss.seam.xml + seam-xml-config + runtime + + + org.jboss.seam.rest + seam-rest + compile + + + org.jboss.seam.persistence + seam-persistence-api + compile + + + org.jboss.seam.persistence + seam-persistence-impl + runtime + + + org.jboss.weld + weld-extensions + + + + org.slf4j + slf4j-api + runtime + + + org.slf4j + slf4j-simple + runtime + + + + + + jboss-public-repository + + + + jboss-public-repository + !false + + + + + jboss-public-repository-group + JBoss Public Maven Repository Group + http://repository.jboss.org/nexus/content/groups/public + default + + true + never + + + false + never + + + + + + jboss-public-repository-group + JBoss Public Maven Repository Group + http://repository.jboss.org/nexus/content/groups/public + default + + true + never + + + true + never + + + + + + + + scm:git:git://github.com/seam/catch.git + scm:git:git@github.com:seam/catch.git + https://github.com/seam/catch + + + + diff --git a/examples/jaxrs/src/main/java/org/jboss/seam/exception/control/example/jaxrs/LibraryApplication.java b/examples/jaxrs/src/main/java/org/jboss/seam/exception/control/example/jaxrs/LibraryApplication.java new file mode 100644 index 0000000..3da519b --- /dev/null +++ b/examples/jaxrs/src/main/java/org/jboss/seam/exception/control/example/jaxrs/LibraryApplication.java @@ -0,0 +1,46 @@ +/* + * JBoss, Home of Professional Open Source + * Copyright 2010, Red Hat, Inc., and individual contributors + * by the @authors tag. See the copyright.txt 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.seam.exception.control.example.jaxrs; + +import org.jboss.seam.exception.control.example.jaxrs.handler.CatchBridge; +import org.jboss.seam.exception.control.example.jaxrs.resource.AuthorResource; +import org.jboss.seam.exception.control.example.jaxrs.resource.BookResource; + +import javax.ws.rs.ApplicationPath; +import javax.ws.rs.core.Application; +import java.util.Arrays; +import java.util.HashSet; +import java.util.Set; + +@ApplicationPath("/api/*") +public class LibraryApplication extends Application +{ + @Override + public Set> getClasses() + { + final Set> classes = new HashSet>(); + classes.addAll(Arrays.asList(CatchBridge.class, AuthorResource.class, BookResource.class)); + + return classes; + } +} diff --git a/examples/jaxrs/src/main/java/org/jboss/seam/exception/control/example/jaxrs/db/InitDatabaseBean.java b/examples/jaxrs/src/main/java/org/jboss/seam/exception/control/example/jaxrs/db/InitDatabaseBean.java new file mode 100644 index 0000000..053e24a --- /dev/null +++ b/examples/jaxrs/src/main/java/org/jboss/seam/exception/control/example/jaxrs/db/InitDatabaseBean.java @@ -0,0 +1,95 @@ +/* + * JBoss, Home of Professional Open Source + * Copyright 2010, Red Hat, Inc., and individual contributors + * by the @authors tag. See the copyright.txt 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.seam.exception.control.example.jaxrs.db; + +import org.jboss.seam.exception.control.example.jaxrs.entity.Author; +import org.jboss.seam.exception.control.example.jaxrs.entity.Book; + +import javax.ejb.TransactionAttribute; +import javax.ejb.TransactionAttributeType; +import javax.inject.Inject; +import javax.persistence.EntityManager; + +public class InitDatabaseBean +{ + @Inject private EntityManager em; + + @TransactionAttribute(TransactionAttributeType.REQUIRED) + public void seedDatabase() + { + Author hal_fulton = createAuthor("Hal Fulton"); + Author max_katz = createAuthor("Max Katz"); + Author gavin_king = createAuthor("Gavin King"); + Author christian_bauer = createAuthor("Christian Bauer"); + Author dan_allen = createAuthor("Dan Allen"); + + Book ruby_way = createBook("The Ruby Way"); + Book practical_richfaces = createBook("Practical RichFaces"); + Book java_persistence = createBook("Java Persistence with Hibernate"); + Book seam_in_action = createBook("Seam in Action"); + + hal_fulton.addBook(ruby_way); + ruby_way.addAuthor(hal_fulton); + em.persist(hal_fulton); + em.persist(ruby_way); + + max_katz.addBook(practical_richfaces); + practical_richfaces.addAuthor(max_katz); + em.persist(max_katz); + em.persist(practical_richfaces); + + java_persistence.addAuthor(gavin_king); + java_persistence.addAuthor(christian_bauer); + gavin_king.addBook(java_persistence); + christian_bauer.addBook(java_persistence); + em.persist(java_persistence); + em.persist(gavin_king); + em.persist(christian_bauer); + + seam_in_action.addAuthor(dan_allen); + dan_allen.addBook(seam_in_action); + em.persist(dan_allen); + em.persist(seam_in_action); + } + + @TransactionAttribute(TransactionAttributeType.REQUIRED) + public void clear() + { + this.em.createQuery("delete from Book").executeUpdate(); + this.em.createQuery("delete from Author").executeUpdate(); + } + + private Author createAuthor(String authorName) + { + final Author a = new Author(); + a.setName(authorName); + return a; + } + + private Book createBook(String bookTitle) + { + final Book b = new Book(); + b.setTitle(bookTitle); + return b; + } +} diff --git a/examples/jaxrs/src/main/java/org/jboss/seam/exception/control/example/jaxrs/db/InitDatabaseListener.java b/examples/jaxrs/src/main/java/org/jboss/seam/exception/control/example/jaxrs/db/InitDatabaseListener.java new file mode 100644 index 0000000..3488380 --- /dev/null +++ b/examples/jaxrs/src/main/java/org/jboss/seam/exception/control/example/jaxrs/db/InitDatabaseListener.java @@ -0,0 +1,45 @@ +/* + * JBoss, Home of Professional Open Source + * Copyright 2010, Red Hat, Inc., and individual contributors + * by the @authors tag. See the copyright.txt 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.seam.exception.control.example.jaxrs.db; + +import javax.inject.Inject; +import javax.servlet.ServletContextEvent; +import javax.servlet.ServletContextListener; +import javax.servlet.annotation.WebListener; + +@SuppressWarnings({"ManagedBeanInconsistencyInspection"}) +@WebListener +public class InitDatabaseListener implements ServletContextListener +{ + @Inject InitDatabaseBean bean; + + public void contextInitialized(ServletContextEvent sce) + { + bean.seedDatabase(); + } + + public void contextDestroyed(ServletContextEvent sce) + { + bean.clear(); + } +} diff --git a/examples/jaxrs/src/main/java/org/jboss/seam/exception/control/example/jaxrs/db/ManagedPeristenceContextProducer.java b/examples/jaxrs/src/main/java/org/jboss/seam/exception/control/example/jaxrs/db/ManagedPeristenceContextProducer.java new file mode 100644 index 0000000..759fc76 --- /dev/null +++ b/examples/jaxrs/src/main/java/org/jboss/seam/exception/control/example/jaxrs/db/ManagedPeristenceContextProducer.java @@ -0,0 +1,39 @@ +/* + * JBoss, Home of Professional Open Source + * Copyright 2010, Red Hat, Inc., and individual contributors + * by the @authors tag. See the copyright.txt 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.seam.exception.control.example.jaxrs.db; + +import org.jboss.seam.persistence.SeamManaged; + +import javax.enterprise.context.RequestScoped; +import javax.enterprise.inject.Produces; +import javax.persistence.EntityManagerFactory; +import javax.persistence.PersistenceUnit; + +public class ManagedPeristenceContextProducer +{ + @PersistenceUnit(unitName = "libraryDatabase") + @RequestScoped + @Produces + @SeamManaged + EntityManagerFactory emf; +} diff --git a/examples/jaxrs/src/main/java/org/jboss/seam/exception/control/example/jaxrs/entity/Author.java b/examples/jaxrs/src/main/java/org/jboss/seam/exception/control/example/jaxrs/entity/Author.java new file mode 100644 index 0000000..ac20903 --- /dev/null +++ b/examples/jaxrs/src/main/java/org/jboss/seam/exception/control/example/jaxrs/entity/Author.java @@ -0,0 +1,95 @@ +/* + * JBoss, Home of Professional Open Source + * Copyright 2010, Red Hat, Inc., and individual contributors + * by the @authors tag. See the copyright.txt 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.seam.exception.control.example.jaxrs.entity; + +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.Id; +import javax.persistence.ManyToMany; +import javax.persistence.NamedQueries; +import javax.persistence.NamedQuery; +import javax.validation.constraints.NotNull; +import javax.validation.constraints.Size; +import javax.xml.bind.annotation.XmlAttribute; +import javax.xml.bind.annotation.XmlRootElement; +import java.util.ArrayList; +import java.util.List; + +@SuppressWarnings({"ReturnOfCollectionOrArrayField"}) +@Entity +@XmlRootElement +@NamedQueries({ + @NamedQuery(name = "authorsByName", query = "select a from Author a where a.name = :name"), + @NamedQuery(name = "authors", query = "select a from Author a") +}) +public class Author +{ + private Long id; + private String name; + private List books; + + public void addBook(Book newBook) + { + if (this.books == null) + { + this.books = new ArrayList(); + } + this.books.add(newBook); + } + + @ManyToMany + public List getBooks() + { + return books; + } + + public void setBooks(List books) + { + this.books = books; + } + + @XmlAttribute + @GeneratedValue + @Id + public Long getId() + { + return id; + } + + public void setId(Long id) + { + this.id = id; + } + + @NotNull + @Size(min = 1, max = 100) + public String getName() + { + return name; + } + + public void setName(String name) + { + this.name = name; + } +} diff --git a/examples/jaxrs/src/main/java/org/jboss/seam/exception/control/example/jaxrs/entity/Book.java b/examples/jaxrs/src/main/java/org/jboss/seam/exception/control/example/jaxrs/entity/Book.java new file mode 100644 index 0000000..7cc5e97 --- /dev/null +++ b/examples/jaxrs/src/main/java/org/jboss/seam/exception/control/example/jaxrs/entity/Book.java @@ -0,0 +1,96 @@ +/* + * JBoss, Home of Professional Open Source + * Copyright 2010, Red Hat, Inc., and individual contributors + * by the @authors tag. See the copyright.txt 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.seam.exception.control.example.jaxrs.entity; + +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.Id; +import javax.persistence.ManyToMany; +import javax.persistence.NamedQueries; +import javax.persistence.NamedQuery; +import javax.validation.constraints.NotNull; +import javax.validation.constraints.Size; +import javax.xml.bind.annotation.XmlAttribute; +import javax.xml.bind.annotation.XmlRootElement; +import java.util.ArrayList; +import java.util.List; + +@SuppressWarnings({"ReturnOfCollectionOrArrayField"}) +@Entity +@XmlRootElement +@NamedQueries({ + @NamedQuery(name = "booksByTitle", query = "select b from Book b where b.title = :title"), + @NamedQuery(name = "books", query = "select b from Book b") +}) + +public class Book +{ + private Long id; + private String title; + private List authors; + + public void addAuthor(Author newAuthor) + { + if (this.authors == null) + { + this.authors = new ArrayList(); + } + this.authors.add(newAuthor); + } + + @ManyToMany + public List getAuthors() + { + return authors; + } + + public void setAuthors(List authors) + { + this.authors = authors; + } + + @XmlAttribute + @GeneratedValue + @Id + public Long getId() + { + return id; + } + + public void setId(Long id) + { + this.id = id; + } + + @NotNull + @Size(min = 1, max = 100) + public String getTitle() + { + return title; + } + + public void setTitle(String title) + { + this.title = title; + } +} diff --git a/examples/jaxrs/src/main/java/org/jboss/seam/exception/control/example/jaxrs/handler/CatchBridge.java b/examples/jaxrs/src/main/java/org/jboss/seam/exception/control/example/jaxrs/handler/CatchBridge.java new file mode 100644 index 0000000..290b66e --- /dev/null +++ b/examples/jaxrs/src/main/java/org/jboss/seam/exception/control/example/jaxrs/handler/CatchBridge.java @@ -0,0 +1,58 @@ +/* + * JBoss, Home of Professional Open Source + * Copyright 2010, Red Hat, Inc., and individual contributors + * by the @authors tag. See the copyright.txt 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.seam.exception.control.example.jaxrs.handler; + +import org.jboss.seam.exception.control.ExceptionToCatchEvent; + +import javax.enterprise.context.ApplicationScoped; +import javax.enterprise.context.RequestScoped; +import javax.enterprise.event.Event; +import javax.enterprise.inject.Produces; +import javax.inject.Inject; +import javax.ws.rs.core.Response; +import javax.ws.rs.ext.ExceptionMapper; +import javax.ws.rs.ext.Provider; + +@Provider +@ApplicationScoped +public class CatchBridge implements ExceptionMapper +{ + @Inject @RestCatch + private Response.ResponseBuilder responseBuilder; + + @Inject Event event; + + @Produces + @RequestScoped + @RestCatch + public Response.ResponseBuilder createErrorResponseBuilder() + { + return Response.serverError(); + } + + public Response toResponse(Throwable exception) + { + event.fire(new ExceptionToCatchEvent(exception, RestCatchLiteral.INSTANCE)); + return this.responseBuilder.build(); + } +} diff --git a/examples/jaxrs/src/main/java/org/jboss/seam/exception/control/example/jaxrs/handler/RestCatch.java b/examples/jaxrs/src/main/java/org/jboss/seam/exception/control/example/jaxrs/handler/RestCatch.java new file mode 100644 index 0000000..18e18a2 --- /dev/null +++ b/examples/jaxrs/src/main/java/org/jboss/seam/exception/control/example/jaxrs/handler/RestCatch.java @@ -0,0 +1,36 @@ +/* + * JBoss, Home of Professional Open Source + * Copyright 2010, Red Hat, Inc., and individual contributors + * by the @authors tag. See the copyright.txt 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.seam.exception.control.example.jaxrs.handler; + +import javax.inject.Qualifier; +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Qualifier +@Retention(RetentionPolicy.RUNTIME) +@Target({ElementType.PARAMETER, ElementType.TYPE, ElementType.METHOD, ElementType.FIELD}) +public @interface RestCatch +{ +} diff --git a/examples/jaxrs/src/main/java/org/jboss/seam/exception/control/example/jaxrs/handler/RestCatchLiteral.java b/examples/jaxrs/src/main/java/org/jboss/seam/exception/control/example/jaxrs/handler/RestCatchLiteral.java new file mode 100644 index 0000000..c68236b --- /dev/null +++ b/examples/jaxrs/src/main/java/org/jboss/seam/exception/control/example/jaxrs/handler/RestCatchLiteral.java @@ -0,0 +1,32 @@ +/* + * JBoss, Home of Professional Open Source + * Copyright 2010, Red Hat, Inc., and individual contributors + * by the @authors tag. See the copyright.txt 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.seam.exception.control.example.jaxrs.handler; + +import javax.enterprise.util.AnnotationLiteral; + +public class RestCatchLiteral extends AnnotationLiteral implements RestCatch +{ + private static final long serialVersionUID = 1437877200809906569L; + + public static final RestCatch INSTANCE = new RestCatchLiteral(); +} diff --git a/examples/jaxrs/src/main/java/org/jboss/seam/exception/control/example/jaxrs/handler/RestHandlerContainer.java b/examples/jaxrs/src/main/java/org/jboss/seam/exception/control/example/jaxrs/handler/RestHandlerContainer.java new file mode 100644 index 0000000..5e544f3 --- /dev/null +++ b/examples/jaxrs/src/main/java/org/jboss/seam/exception/control/example/jaxrs/handler/RestHandlerContainer.java @@ -0,0 +1,57 @@ +/* + * JBoss, Home of Professional Open Source + * Copyright 2010, Red Hat, Inc., and individual contributors + * by the @authors tag. See the copyright.txt 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.seam.exception.control.example.jaxrs.handler; + +import org.jboss.seam.exception.control.CatchEvent; +import org.jboss.seam.exception.control.Handles; +import org.jboss.seam.exception.control.HandlesExceptions; +import org.jboss.seam.rest.exceptions.ErrorMessageWrapper; +import org.jboss.seam.rest.exceptions.ExceptionMapping; +import org.jboss.seam.rest.exceptions.ExceptionMappingConfiguration; + +import javax.ws.rs.core.Response; + +@HandlesExceptions +public class RestHandlerContainer +{ + public void genericRestHandler(@Handles(precedence = -100) @RestCatch CatchEvent event, + @RestCatch Response.ResponseBuilder responseBuilder, + ExceptionMappingConfiguration exceptionMapping) + { + final Class exceptionClass = event.getException().getClass(); + + for (ExceptionMapping mapping : exceptionMapping.getExceptionMappings()) + { + if (exceptionClass.equals(mapping.getExceptionType())) + { + responseBuilder.status(mapping.getStatusCode()); + + if (mapping.getMessage() != null) + { + responseBuilder.entity(new ErrorMessageWrapper(mapping.getMessage())); + } + break; + } + } + } +} diff --git a/examples/jaxrs/src/main/java/org/jboss/seam/exception/control/example/jaxrs/resource/AuthorResource.java b/examples/jaxrs/src/main/java/org/jboss/seam/exception/control/example/jaxrs/resource/AuthorResource.java new file mode 100644 index 0000000..30a951d --- /dev/null +++ b/examples/jaxrs/src/main/java/org/jboss/seam/exception/control/example/jaxrs/resource/AuthorResource.java @@ -0,0 +1,59 @@ +/* + * JBoss, Home of Professional Open Source + * Copyright 2010, Red Hat, Inc., and individual contributors + * by the @authors tag. See the copyright.txt 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.seam.exception.control.example.jaxrs.resource; + +import org.jboss.seam.exception.control.example.jaxrs.entity.Author; + +import javax.ejb.TransactionAttribute; +import javax.ejb.TransactionAttributeType; +import javax.inject.Inject; +import javax.persistence.EntityManager; +import javax.ws.rs.Consumes; +import javax.ws.rs.GET; +import javax.ws.rs.Path; +import javax.ws.rs.PathParam; +import javax.ws.rs.Produces; +import javax.ws.rs.core.MediaType; +import java.util.List; + +@Produces(MediaType.APPLICATION_XML) +@Consumes(MediaType.APPLICATION_XML) +@Path("author") +@TransactionAttribute(TransactionAttributeType.REQUIRED) +public class AuthorResource +{ + @Inject private EntityManager em; + + @GET + public List getAllAuthors() + { + return this.em.createNamedQuery("authors", Author.class).getResultList(); + } + + @GET + @Path("{name:[A-Za-z]+}") + public Author getAuthorByName(@PathParam("name") String authorName) + { + return this.em.createNamedQuery("authorsByName", Author.class).setParameter("name", authorName).getSingleResult(); + } +} diff --git a/examples/jaxrs/src/main/java/org/jboss/seam/exception/control/example/jaxrs/resource/BookResource.java b/examples/jaxrs/src/main/java/org/jboss/seam/exception/control/example/jaxrs/resource/BookResource.java new file mode 100644 index 0000000..2701854 --- /dev/null +++ b/examples/jaxrs/src/main/java/org/jboss/seam/exception/control/example/jaxrs/resource/BookResource.java @@ -0,0 +1,61 @@ +/* + * JBoss, Home of Professional Open Source + * Copyright 2010, Red Hat, Inc., and individual contributors + * by the @authors tag. See the copyright.txt 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.seam.exception.control.example.jaxrs.resource; + +import org.jboss.seam.exception.control.example.jaxrs.entity.Book; +import org.jboss.seam.rest.validation.ValidateRequest; + +import javax.ejb.TransactionAttribute; +import javax.ejb.TransactionAttributeType; +import javax.inject.Inject; +import javax.persistence.EntityManager; +import javax.ws.rs.Consumes; +import javax.ws.rs.GET; +import javax.ws.rs.Path; +import javax.ws.rs.PathParam; +import javax.ws.rs.Produces; +import javax.ws.rs.core.MediaType; +import java.util.List; + +@Produces(MediaType.APPLICATION_XML) +@Consumes(MediaType.APPLICATION_XML) +@Path("book") +@TransactionAttribute(TransactionAttributeType.REQUIRED) +@ValidateRequest +public class BookResource +{ + @Inject private EntityManager em; + + @GET + public List getAllAuthors() + { + return this.em.createNamedQuery("books", Book.class).getResultList(); + } + + @GET + @Path("{title:[A-Za-z]+}") + public Book getAuthorByName(@PathParam("title") String authorName) + { + return this.em.createNamedQuery("booksByTitle", Book.class).setParameter("title", authorName).getSingleResult(); + } +} diff --git a/examples/jaxrs/src/main/resources/META-INF/persistence.xml b/examples/jaxrs/src/main/resources/META-INF/persistence.xml new file mode 100644 index 0000000..1fade34 --- /dev/null +++ b/examples/jaxrs/src/main/resources/META-INF/persistence.xml @@ -0,0 +1,35 @@ + + + + + + jdbc/__default + + + + + + diff --git a/examples/jaxrs/src/main/webapp/WEB-INF/beans.xml b/examples/jaxrs/src/main/webapp/WEB-INF/beans.xml new file mode 100644 index 0000000..5a51848 --- /dev/null +++ b/examples/jaxrs/src/main/webapp/WEB-INF/beans.xml @@ -0,0 +1,48 @@ + + + + + org.jboss.seam.rest.validation.ValidationInterceptor + org.jboss.seam.persistence.transaction.TransactionInterceptor + + + + + + + + Requested resource does not exist. + + + + + Illegal value. + + + + + diff --git a/examples/jaxrs/src/main/webapp/WEB-INF/web.xml b/examples/jaxrs/src/main/webapp/WEB-INF/web.xml new file mode 100644 index 0000000..4ade90e --- /dev/null +++ b/examples/jaxrs/src/main/webapp/WEB-INF/web.xml @@ -0,0 +1,28 @@ + + + + + diff --git a/examples/jaxrs/web/WEB-INF/web.xml b/examples/jaxrs/web/WEB-INF/web.xml new file mode 100644 index 0000000..17ac0d2 --- /dev/null +++ b/examples/jaxrs/web/WEB-INF/web.xml @@ -0,0 +1,8 @@ + + + + diff --git a/impl/pom.xml b/impl/pom.xml index 88f1bb4..47e0672 100644 --- a/impl/pom.xml +++ b/impl/pom.xml @@ -28,11 +28,6 @@ provided - - joda-time - joda-time - - org.jboss.arquillian arquillian-junit diff --git a/pom.xml b/pom.xml index c0fd4bb..d1ae179 100644 --- a/pom.xml +++ b/pom.xml @@ -81,7 +81,7 @@ dist docs - + examples/jaxrs