Seam SVN: r13325 - in modules/resteasy/trunk: impl/src/main/java/org/jboss/seam/resteasy/configuration and 2 other directories.
by seam-commits@lists.jboss.org
Author: jharting
Date: 2010-07-01 03:29:24 -0400 (Thu, 01 Jul 2010)
New Revision: 13325
Added:
modules/resteasy/trunk/api/src/main/java/org/jboss/seam/resteasy/configuration/ExceptionMapping.java
modules/resteasy/trunk/impl/src/main/java/org/jboss/seam/resteasy/configuration/BasicExceptionMapper.java
modules/resteasy/trunk/impl/src/main/java/org/jboss/seam/resteasy/configuration/SeamExceptionMapper.java
Removed:
modules/resteasy/trunk/impl/src/main/java/org/jboss/seam/resteasy/configuration/GenericExceptionMapper.java
Modified:
modules/resteasy/trunk/api/src/main/java/org/jboss/seam/resteasy/configuration/SeamResteasyConfiguration.java
modules/resteasy/trunk/impl/src/main/java/org/jboss/seam/resteasy/configuration/ConfigurationListener.java
modules/resteasy/trunk/impl/src/test/java/org/jboss/seam/resteasy/test/SeamResteasyClientTest.java
modules/resteasy/trunk/impl/src/test/java/org/jboss/seam/resteasy/test/configuration/ConfigurationTest.java
modules/resteasy/trunk/impl/src/test/java/org/jboss/seam/resteasy/test/configuration/CustomSeamResteasyConfiguration.java
modules/resteasy/trunk/impl/src/test/java/org/jboss/seam/resteasy/test/configuration/EntityNotFoundException.java
modules/resteasy/trunk/impl/src/test/java/org/jboss/seam/resteasy/test/configuration/TestResource.java
Log:
exception mapping
Added: modules/resteasy/trunk/api/src/main/java/org/jboss/seam/resteasy/configuration/ExceptionMapping.java
===================================================================
--- modules/resteasy/trunk/api/src/main/java/org/jboss/seam/resteasy/configuration/ExceptionMapping.java (rev 0)
+++ modules/resteasy/trunk/api/src/main/java/org/jboss/seam/resteasy/configuration/ExceptionMapping.java 2010-07-01 07:29:24 UTC (rev 13325)
@@ -0,0 +1,79 @@
+package org.jboss.seam.resteasy.configuration;
+
+import javax.ws.rs.core.MediaType;
+
+/**
+ * Represents a mapping of an exception to an HTTP status code and response body.
+ *
+ * @author Jozef Hartinger
+ *
+ */
+public class ExceptionMapping
+{
+ private Class<? extends Throwable> exceptionType;
+ private int statusCode;
+ private String messageBody;
+ private MediaType mediaType = MediaType.TEXT_PLAIN_TYPE;
+
+ public ExceptionMapping()
+ {
+ }
+
+ public ExceptionMapping(Class<? extends Throwable> exceptionType, int statusCode)
+ {
+ this.exceptionType = exceptionType;
+ this.statusCode = statusCode;
+ }
+
+ public ExceptionMapping(Class<? extends Throwable> exceptionType, int statusCode, String messageBody, MediaType mediaType)
+ {
+ this(exceptionType, statusCode);
+ this.messageBody = messageBody;
+ this.mediaType = mediaType;
+ }
+
+ public ExceptionMapping(Class<? extends Throwable> exceptionType, int statusCode, String messageBody, String mediaType)
+ {
+ this(exceptionType, statusCode, messageBody, MediaType.valueOf(mediaType));
+ }
+
+ public Class<? extends Throwable> getExceptionType()
+ {
+ return exceptionType;
+ }
+
+ public void setExceptionType(Class<? extends Throwable> exceptionType)
+ {
+ this.exceptionType = exceptionType;
+ }
+
+ public int getStatusCode()
+ {
+ return statusCode;
+ }
+
+ public void setStatusCode(int statusCode)
+ {
+ this.statusCode = statusCode;
+ }
+
+ public String getMessageBody()
+ {
+ return messageBody;
+ }
+
+ public void setMessageBody(String messageBody)
+ {
+ this.messageBody = messageBody;
+ }
+
+ public MediaType getMediaType()
+ {
+ return mediaType;
+ }
+
+ public void setMediaType(MediaType mediaType)
+ {
+ this.mediaType = mediaType;
+ }
+}
Modified: modules/resteasy/trunk/api/src/main/java/org/jboss/seam/resteasy/configuration/SeamResteasyConfiguration.java
===================================================================
--- modules/resteasy/trunk/api/src/main/java/org/jboss/seam/resteasy/configuration/SeamResteasyConfiguration.java 2010-06-30 22:07:23 UTC (rev 13324)
+++ modules/resteasy/trunk/api/src/main/java/org/jboss/seam/resteasy/configuration/SeamResteasyConfiguration.java 2010-07-01 07:29:24 UTC (rev 13325)
@@ -8,16 +8,18 @@
import javax.enterprise.context.ApplicationScoped;
/**
- * Holds configuration options for seam-resteasy extension. It can be used to configure the extension via XML descriptor
- * using seam-xml extension. Alternatively, you can configure the extension programatically by providing an
- * {@link @Alternative} subclass of SeamResteasyConfiguration.
+ * Holds configuration options for seam-resteasy extension. It can be used to
+ * configure the extension via XML descriptor using seam-xml extension.
+ * Alternatively, you can configure the extension programatically by providing
+ * an {@link @Alternative} subclass of SeamResteasyConfiguration.
*
- * This class allows declarative exception mapping to be used. The way exceptions are treated in the application is based on
- * what {@link #getExceptionMappings()} returns. Override this method or use seam-xml module to set up exception mapping
- * declaratively.
+ * This class allows declarative exception mapping to be used. The way
+ * exceptions are treated in the application is based on what
+ * {@link #getExceptionMappings()} returns. Override this method or use seam-xml
+ * module to set up exception mapping declaratively.
*
* @author <a href="mailto:jharting@redhat.com">Jozef Hartinger</a>
- *
+ *
*/
@ApplicationScoped
public class SeamResteasyConfiguration
@@ -29,12 +31,13 @@
private Map<String, String> mediaTypeMappings = new HashMap<String, String>();
private Map<String, String> languageMappings = new HashMap<String, String>();
- private Map<Class<? extends Throwable>, Integer> exceptionMappings = new HashMap<Class<? extends Throwable>, Integer>();
-
+ private Map<Class<? extends Throwable>, Integer> basicExceptionMappings = new HashMap<Class<? extends Throwable>, Integer>();
+ private Set<ExceptionMapping> exceptionMappings = new HashSet<ExceptionMapping>();
+
private boolean registerValidationExceptionMapper = true;
/**
- * Returns a set of resource classes to be registered.
+ * Returns a set of resource classes to be registered.
*/
public Set<Class<?>> getResources()
{
@@ -46,8 +49,13 @@
this.resources = resources;
}
+ public void addResource(Class<?> resource)
+ {
+ this.resources.add(resource);
+ }
+
/**
- * Returns a set of resource classes that will be excluded from deployment.
+ * Returns a set of resource classes that will be excluded from deployment.
*/
public Set<Class<?>> getExcludedResources()
{
@@ -58,9 +66,14 @@
{
this.excludedResources = excludedResources;
}
+
+ public void addExcludedResource(Class<?> excludedResource)
+ {
+ this.excludedResources.add(excludedResource);
+ }
/**
- * Returns a set of provider classes to be registered.
+ * Returns a set of provider classes to be registered.
*/
public Set<Class<?>> getProviders()
{
@@ -71,23 +84,32 @@
{
this.providers = providers;
}
+
+ public void addProvider(Class<?> provider)
+ {
+ this.providers.add(provider);
+ }
/**
- * Returns a map of media type mappings to be registered.
+ * Returns a map of media type mappings to be registered.
*/
public Map<String, String> getMediaTypeMappings()
{
return mediaTypeMappings;
}
-
public void setMediaTypeMappings(Map<String, String> mediaTypeMappings)
{
this.mediaTypeMappings = mediaTypeMappings;
}
+
+ public void addMediaTypeMapping(String key, String value)
+ {
+ this.mediaTypeMappings.put(key, value);
+ }
/**
- * Returns a map of language mappings to be registered.
+ * Returns a map of language mappings to be registered.
*/
public Map<String, String> getLanguageMappings()
{
@@ -98,22 +120,46 @@
{
this.languageMappings = languageMappings;
}
+
+ public void addLanguageMapping(String key, String value)
+ {
+ this.languageMappings.put(key, value);
+ }
- /**
- * Returns a map of exception mappings.
- */
- public Map<Class<? extends Throwable>, Integer> getExceptionMappings()
+ public Map<Class<? extends Throwable>, Integer> getBasicExceptionMappings()
{
+ return basicExceptionMappings;
+ }
+
+ public void setBasicExceptionMappings(Map<Class<? extends Throwable>, Integer> basicExceptionMappings)
+ {
+ this.basicExceptionMappings = basicExceptionMappings;
+ }
+
+ public void addBasicExceptionMapping(Class<? extends Throwable> exceptionType, int statusCode)
+ {
+ this.basicExceptionMappings.put(exceptionType, statusCode);
+ }
+
+ public Set<ExceptionMapping> getExceptionMappings()
+ {
return exceptionMappings;
}
- public void setExceptionMappings(Map<Class<? extends Throwable>, Integer> exceptionMapping)
+ public void setExceptionMappings(Set<ExceptionMapping> exceptionMappings)
{
- this.exceptionMappings = exceptionMapping;
+ this.exceptionMappings = exceptionMappings;
}
+
+ public void addExceptionMapping(ExceptionMapping mapping)
+ {
+ this.exceptionMappings.add(mapping);
+ }
/**
- * If set to true, the default exception mapper for {@link org.jboss.seam.resteasy.validation.ValidationException} will be registered.
+ * If set to true, the default exception mapper for
+ * {@link org.jboss.seam.resteasy.validation.ValidationException} will be
+ * registered.
*/
public boolean isRegisterValidationExceptionMapper()
{
Copied: modules/resteasy/trunk/impl/src/main/java/org/jboss/seam/resteasy/configuration/BasicExceptionMapper.java (from rev 13323, modules/resteasy/trunk/impl/src/main/java/org/jboss/seam/resteasy/configuration/GenericExceptionMapper.java)
===================================================================
--- modules/resteasy/trunk/impl/src/main/java/org/jboss/seam/resteasy/configuration/BasicExceptionMapper.java (rev 0)
+++ modules/resteasy/trunk/impl/src/main/java/org/jboss/seam/resteasy/configuration/BasicExceptionMapper.java 2010-07-01 07:29:24 UTC (rev 13325)
@@ -0,0 +1,25 @@
+package org.jboss.seam.resteasy.configuration;
+
+import javax.ws.rs.core.Response;
+import javax.ws.rs.ext.ExceptionMapper;
+
+/**
+ * GenericExceptionMapper allows exceptions to be mapped to HTTP status codes declaratively (at runtime).
+ * @author Jozef Hartinger
+ *
+ * @param <T> exception
+ */
+public class BasicExceptionMapper<T extends Throwable> implements ExceptionMapper<T>
+{
+ private int status;
+
+ public BasicExceptionMapper(int status)
+ {
+ this.status = status;
+ }
+
+ public Response toResponse(T exception)
+ {
+ return Response.status(status).build();
+ }
+}
Modified: modules/resteasy/trunk/impl/src/main/java/org/jboss/seam/resteasy/configuration/ConfigurationListener.java
===================================================================
--- modules/resteasy/trunk/impl/src/main/java/org/jboss/seam/resteasy/configuration/ConfigurationListener.java 2010-06-30 22:07:23 UTC (rev 13324)
+++ modules/resteasy/trunk/impl/src/main/java/org/jboss/seam/resteasy/configuration/ConfigurationListener.java 2010-07-01 07:29:24 UTC (rev 13325)
@@ -109,14 +109,15 @@
*/
protected void registerExceptionMappings()
{
- for (Entry<Class<? extends Throwable>, Integer> item : configuration.getExceptionMappings().entrySet())
+ // basic exception mapping (exception -> status code)
+ for (Entry<Class<? extends Throwable>, Integer> item : configuration.getBasicExceptionMappings().entrySet())
{
if (Throwable.class.isAssignableFrom(item.getKey()))
{
Class<? extends Throwable> exceptionType = item.getKey();
int status = item.getValue();
- ExceptionMapper<? extends Throwable> provider = new GenericExceptionMapper<Throwable>(status);
- log.info("Adding exception mapping {} -> {}", exceptionType, status);
+ ExceptionMapper<? extends Throwable> provider = new BasicExceptionMapper<Throwable>(status);
+ log.info("Adding basic exception mapping {} -> {}", exceptionType, status);
try
{
factory.addExceptionMapper(provider, exceptionType);
@@ -131,6 +132,22 @@
log.warn("{} is not an exception. Skipping mapping of the exception to {} status code.", item.getKey(), item.getValue());
}
}
+
+ // exception mapping (exception -> status code, http body, mime type)
+ for (ExceptionMapping mapping : configuration.getExceptionMappings())
+ {
+ SeamExceptionMapper provider = new SeamExceptionMapper(mapping);
+ log.info("Adding exception mapping {} -> {}", mapping.getExceptionType(), mapping.getStatusCode());
+ try
+ {
+ factory.addExceptionMapper(provider, mapping.getExceptionType());
+ }
+ catch (NoSuchMethodError e)
+ {
+ log.warn("You are using old version of RESTEasy. Exception mapper for {} will not be registered.", mapping.getExceptionType());
+ }
+ }
+
// register ExceptionMapper for Bean Validation integration
if (configuration.isRegisterValidationExceptionMapper())
Deleted: modules/resteasy/trunk/impl/src/main/java/org/jboss/seam/resteasy/configuration/GenericExceptionMapper.java
===================================================================
--- modules/resteasy/trunk/impl/src/main/java/org/jboss/seam/resteasy/configuration/GenericExceptionMapper.java 2010-06-30 22:07:23 UTC (rev 13324)
+++ modules/resteasy/trunk/impl/src/main/java/org/jboss/seam/resteasy/configuration/GenericExceptionMapper.java 2010-07-01 07:29:24 UTC (rev 13325)
@@ -1,25 +0,0 @@
-package org.jboss.seam.resteasy.configuration;
-
-import javax.ws.rs.core.Response;
-import javax.ws.rs.ext.ExceptionMapper;
-
-/**
- * GenericExceptionMapper allows exceptions to be mapped to HTTP status codes declaratively (at runtime).
- * @author Jozef Hartinger
- *
- * @param <T> exception
- */
-public class GenericExceptionMapper<T extends Throwable> implements ExceptionMapper<T>
-{
- private int status;
-
- public GenericExceptionMapper(int status)
- {
- this.status = status;
- }
-
- public Response toResponse(T exception)
- {
- return Response.status(status).build();
- }
-}
Copied: modules/resteasy/trunk/impl/src/main/java/org/jboss/seam/resteasy/configuration/SeamExceptionMapper.java (from rev 13323, modules/resteasy/trunk/impl/src/main/java/org/jboss/seam/resteasy/configuration/GenericExceptionMapper.java)
===================================================================
--- modules/resteasy/trunk/impl/src/main/java/org/jboss/seam/resteasy/configuration/SeamExceptionMapper.java (rev 0)
+++ modules/resteasy/trunk/impl/src/main/java/org/jboss/seam/resteasy/configuration/SeamExceptionMapper.java 2010-07-01 07:29:24 UTC (rev 13325)
@@ -0,0 +1,25 @@
+package org.jboss.seam.resteasy.configuration;
+
+import javax.ws.rs.core.Response;
+import javax.ws.rs.core.Response.ResponseBuilder;
+import javax.ws.rs.ext.ExceptionMapper;
+
+public class SeamExceptionMapper implements ExceptionMapper<Throwable>
+{
+ private ResponseBuilder response;
+
+ public SeamExceptionMapper(ExceptionMapping mapping)
+ {
+ response = Response.status(mapping.getStatusCode());
+ if (mapping.getMessageBody() != null)
+ {
+ response.entity(mapping.getMessageBody());
+ response.header("Content-Type", mapping.getMediaType().toString());
+ }
+ }
+
+ public Response toResponse(Throwable exception)
+ {
+ return response.clone().build();
+ }
+}
Modified: modules/resteasy/trunk/impl/src/test/java/org/jboss/seam/resteasy/test/SeamResteasyClientTest.java
===================================================================
--- modules/resteasy/trunk/impl/src/test/java/org/jboss/seam/resteasy/test/SeamResteasyClientTest.java 2010-06-30 22:07:23 UTC (rev 13324)
+++ modules/resteasy/trunk/impl/src/test/java/org/jboss/seam/resteasy/test/SeamResteasyClientTest.java 2010-07-01 07:29:24 UTC (rev 13325)
@@ -8,17 +8,13 @@
import org.jboss.arquillian.api.Run;
import org.jboss.arquillian.api.RunModeType;
import org.jboss.arquillian.testng.Arquillian;
-import org.jboss.seam.resteasy.configuration.ConfigurationListener;
-import org.jboss.seam.resteasy.configuration.GenericExceptionMapper;
import org.jboss.seam.resteasy.configuration.SeamResteasyConfiguration;
-import org.jboss.seam.resteasy.configuration.SeamResteasyBootstrap;
import org.jboss.seam.resteasy.test.configuration.CustomSeamResteasyConfiguration;
import org.jboss.seam.resteasy.test.configuration.EntityNotFoundException;
import org.jboss.seam.resteasy.test.configuration.ExcludedResource;
import org.jboss.seam.resteasy.test.configuration.TestProvider;
import org.jboss.seam.resteasy.test.configuration.TestResource;
-import org.jboss.seam.resteasy.validation.ValidationException;
-import org.jboss.seam.resteasy.validation.ValidationExceptionMapper;
+import org.jboss.seam.resteasy.validation.ValidateRequest;
import org.jboss.shrinkwrap.api.ArchivePaths;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
@@ -43,13 +39,9 @@
JavaArchive jar = ShrinkWrap.create("seam-resteasy.jar", JavaArchive.class);
jar.addManifestResource("META-INF/web-fragment.xml", "web-fragment.xml");
jar.addManifestResource("META-INF/beans.xml", ArchivePaths.create("beans.xml"));
- jar.addClass(SeamResteasyConfiguration.class);
- jar.addClass(SeamResteasyBootstrap.class);
- jar.addClass(ConfigurationListener.class);
- jar.addClass(GenericExceptionMapper.class);
- jar.addClass(ValidationExceptionMapper.class);
- jar.addClass(ValidationException.class);
- jar.addClass(SeamResteasyClientTest.class); // ARQ-165
+ jar.addPackage(SeamResteasyConfiguration.class.getPackage());
+ jar.addPackage(ValidateRequest.class.getPackage());
+ jar.addPackage(SeamResteasyClientTest.class.getPackage());
return jar;
}
Modified: modules/resteasy/trunk/impl/src/test/java/org/jboss/seam/resteasy/test/configuration/ConfigurationTest.java
===================================================================
--- modules/resteasy/trunk/impl/src/test/java/org/jboss/seam/resteasy/test/configuration/ConfigurationTest.java 2010-06-30 22:07:23 UTC (rev 13324)
+++ modules/resteasy/trunk/impl/src/test/java/org/jboss/seam/resteasy/test/configuration/ConfigurationTest.java 2010-07-01 07:29:24 UTC (rev 13325)
@@ -27,10 +27,22 @@
}
@Test
- public void testExceptionMapping() throws Exception
+ public void testExceptionMapping1() throws Exception
{
- test("http://localhost:8080/test/foo/notFound", 410, null);
+ test("http://localhost:8080/test/foo/exception1", 410, null);
}
+
+ @Test
+ public void testExceptionMapping2() throws Exception
+ {
+ test("http://localhost:8080/test/foo/exception2", 410, "You should not call methods on a null reference.");
+ }
+
+ @Test
+ public void testBasicExceptionMapping() throws Exception
+ {
+ test("http://localhost:8080/test/foo/exception3", 410, null);
+ }
@Test
public void testMediaTypeMapping() throws Exception
Modified: modules/resteasy/trunk/impl/src/test/java/org/jboss/seam/resteasy/test/configuration/CustomSeamResteasyConfiguration.java
===================================================================
--- modules/resteasy/trunk/impl/src/test/java/org/jboss/seam/resteasy/test/configuration/CustomSeamResteasyConfiguration.java 2010-06-30 22:07:23 UTC (rev 13324)
+++ modules/resteasy/trunk/impl/src/test/java/org/jboss/seam/resteasy/test/configuration/CustomSeamResteasyConfiguration.java 2010-07-01 07:29:24 UTC (rev 13325)
@@ -1,19 +1,23 @@
package org.jboss.seam.resteasy.test.configuration;
+import javax.annotation.PostConstruct;
import javax.enterprise.inject.Specializes;
+import org.jboss.seam.resteasy.configuration.ExceptionMapping;
import org.jboss.seam.resteasy.configuration.SeamResteasyConfiguration;
@Specializes
public class CustomSeamResteasyConfiguration extends SeamResteasyConfiguration
{
-
- public CustomSeamResteasyConfiguration()
+ @PostConstruct
+ public void setup()
{
- getResources().add(TestResource.class);
- getExcludedResources().add(ExcludedResource.class);
- getProviders().add(TestProvider.class);
- getExceptionMappings().put(EntityNotFoundException.class, 410);
- getMediaTypeMappings().put("xml", "application/xml");
+ addResource(TestResource.class);
+ addExcludedResource(ExcludedResource.class);
+ addProvider(TestProvider.class);
+ addBasicExceptionMapping(IllegalArgumentException.class, 410);
+ addExceptionMapping(new ExceptionMapping(EntityNotFoundException.class, 410));
+ addExceptionMapping(new ExceptionMapping(NullPointerException.class, 410, "You should not call methods on a null reference.", "text/plain"));
+ addMediaTypeMapping("xml", "application/xml");
}
}
Modified: modules/resteasy/trunk/impl/src/test/java/org/jboss/seam/resteasy/test/configuration/EntityNotFoundException.java
===================================================================
--- modules/resteasy/trunk/impl/src/test/java/org/jboss/seam/resteasy/test/configuration/EntityNotFoundException.java 2010-06-30 22:07:23 UTC (rev 13324)
+++ modules/resteasy/trunk/impl/src/test/java/org/jboss/seam/resteasy/test/configuration/EntityNotFoundException.java 2010-07-01 07:29:24 UTC (rev 13325)
@@ -3,4 +3,23 @@
public class EntityNotFoundException extends RuntimeException
{
private static final long serialVersionUID = 1L;
+
+ public EntityNotFoundException()
+ {
+ }
+
+ public EntityNotFoundException(String message, Throwable cause)
+ {
+ super(message, cause);
+ }
+
+ public EntityNotFoundException(String message)
+ {
+ super(message);
+ }
+
+ public EntityNotFoundException(Throwable cause)
+ {
+ super(cause);
+ }
}
Modified: modules/resteasy/trunk/impl/src/test/java/org/jboss/seam/resteasy/test/configuration/TestResource.java
===================================================================
--- modules/resteasy/trunk/impl/src/test/java/org/jboss/seam/resteasy/test/configuration/TestResource.java 2010-06-30 22:07:23 UTC (rev 13324)
+++ modules/resteasy/trunk/impl/src/test/java/org/jboss/seam/resteasy/test/configuration/TestResource.java 2010-07-01 07:29:24 UTC (rev 13325)
@@ -32,9 +32,21 @@
}
@GET
- @Path("notFound")
- public void notFound()
+ @Path("exception1")
+ public void exception1()
{
- throw new EntityNotFoundException();
+ throw new EntityNotFoundException("Entity is gone.");
}
+ @GET
+ @Path("exception2")
+ public void exception2()
+ {
+ throw new NullPointerException("null");
+ }
+ @GET
+ @Path("exception3")
+ public void exception3()
+ {
+ throw new IllegalArgumentException();
+ }
}
14 years, 5 months