Skip to content

Commit

Permalink
update catch integration
Browse files Browse the repository at this point in the history
- update to Catch 3.0.0.Alpha2
- change scope of catch dependency to required
- rename/package ServletWebRequest annotation to WebRequest
- provide enable detection for Catch exception filter
- log when Catch exception filter is enabled
- document that WebRequest is required
- add default constructors for ServletRequestContext types
  • Loading branch information
mojavelinux committed Dec 7, 2010
1 parent 46458c8 commit 93930d2
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 24 deletions.
@@ -1,5 +1,6 @@
package org.jboss.seam.servlet;

import javax.enterprise.inject.Typed;
import javax.servlet.ServletContext;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
Expand All @@ -12,12 +13,16 @@
*
* @author <a href="http://community.jboss.org/people/dan.j.allen">Dan Allen</a>
*/
@Typed()
public class ServletRequestContext
{
private ServletRequest request;

private ServletResponse response;

// required for scoped producer
public ServletRequestContext() {}

public ServletRequestContext(ServletRequest request, ServletResponse response)
{
this.request = request;
Expand Down
Expand Up @@ -14,7 +14,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jboss.seam.servlet.http;
package org.jboss.seam.servlet;

import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
Expand All @@ -39,6 +39,6 @@
@Target({ TYPE, METHOD, PARAMETER, FIELD })
@Retention(RUNTIME)
@Documented
public @interface ServletWebRequest
public @interface WebRequest
{
}
@@ -1,5 +1,6 @@
package org.jboss.seam.servlet.http;

import javax.enterprise.inject.Typed;
import javax.servlet.ServletContext;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
Expand All @@ -17,8 +18,12 @@
*
* @author <a href="http://community.jboss.org/people/dan.j.allen">Dan Allen</a>
*/
@Typed()
public class HttpServletRequestContext extends ServletRequestContext
{
// required for scoped producer
public HttpServletRequestContext() {}

public HttpServletRequestContext(ServletRequest request, ServletResponse response)
{
super(enforceType(request, HttpServletRequest.class), enforceType(response, HttpServletResponse.class));
Expand Down
Expand Up @@ -14,17 +14,17 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jboss.seam.servlet.http.literal;
package org.jboss.seam.servlet.literal;

import javax.enterprise.util.AnnotationLiteral;

import org.jboss.seam.servlet.http.ServletWebRequest;
import org.jboss.seam.servlet.WebRequest;

/*
* @author <a href="http://community.jboss.org/people/dan.j.allen">Dan Allen</a>
*/
public class ServletWebRequestLiteral extends AnnotationLiteral<ServletWebRequest> implements ServletWebRequest
public class WebRequestLiteral extends AnnotationLiteral<WebRequest> implements WebRequest
{
private static final long serialVersionUID = -6004283843896130532L;
public static final ServletWebRequest INSTANCE = new ServletWebRequestLiteral();
public static final WebRequest INSTANCE = new WebRequestLiteral();
}
16 changes: 11 additions & 5 deletions docs/reference/src/main/docbook/en-US/exception_handling.xml
Expand Up @@ -49,22 +49,28 @@ public class ExceptionHandlers {
}
}]]></programlisting>
<para>
That's all there is to it! If you only want this handler to be used for exceptions
processed by the Servlet integration (excluding web service requests like JAX-RS),
then you can add the <literal>@ServletWebRequest</literal> qualifier to the handler:
That's all there is to it! If you only want this handler to be used for exceptions raised by a web request
(excluding web service requests like JAX-RS), then you can add the <literal>@WebRequest</literal> qualifier to
the handler:
</para>
<programlisting role="JAVA"><![CDATA[@HandlesExceptions
public class ExceptionHandlers {
void handleAll(@Handles @ServletWebRequest
void handleAll(@Handles @WebRequest
CaughtException<Throwable> caught, HttpServletResponse response) {
response.sendError(500, "You've been caught by Catch!");
}
}]]></programlisting>
<note>
<para>
Currently, <literal>@WebRequest</literal> is required to catch exceptions initiated by the Servlet
integration because of a bug in Catch.
</para>
</note>
<para>
Let's consider another example. When the custom <literal>AccountNotFound</literal> exception is thrown,
we'll send a 404 response using this handler.
</para>
<programlisting role="JAVA"><![CDATA[void handleAccountNotFound(@Handles @ServletWebRequest
<programlisting role="JAVA"><![CDATA[void handleAccountNotFound(@Handles @WebRequest
CaughtException<AccountNotFound> caught, HttpServletResponse response) {
response.sendError(404, "Account not found: " + caught.getException().getAccountId());
}]]></programlisting>
Expand Down
11 changes: 2 additions & 9 deletions impl/pom.xml
Expand Up @@ -48,15 +48,8 @@
<dependency>
<groupId>org.jboss.seam.catch</groupId>
<artifactId>seam-catch-api</artifactId>
<version>3.0.0.Alpha1</version>
<scope>compile</scope>
<exclusions>
<!-- workaround for seam catch marking this compile scope -->
<exclusion>
<groupId>javax.enterprise</groupId>
<artifactId>cdi-api</artifactId>
</exclusion>
</exclusions>
<version>3.0.0.Alpha2</version>
<scope>provided</scope>
</dependency>

<dependency>
Expand Down
Expand Up @@ -27,19 +27,23 @@
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

import org.jboss.seam.exception.control.ExceptionToCatchEvent;
import org.jboss.seam.servlet.http.literal.ServletWebRequestLiteral;
import org.jboss.seam.exception.control.ExceptionToCatch;
import org.jboss.seam.servlet.literal.WebRequestLiteral;
import org.jboss.weld.extensions.beanManager.BeanManagerAware;
import org.jboss.weld.extensions.core.Requires;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* A bridge that forwards unhandled exceptions to the Seam exception handling facility (Seam Catch).
*
* @author <a href="http://community.jboss.org/people/dan.j.allen">Dan Allen</a>
*/
@Requires("org.jboss.seam.exception.control.CatchExtension")
@Requires("org.jboss.seam.exception.control.extension.CatchExtension")
public class CatchExceptionFilter extends BeanManagerAware implements Filter
{
private transient Logger log = LoggerFactory.getLogger(CatchExceptionFilter.class);

@Inject
private BeanManager beanManager;

Expand All @@ -55,13 +59,15 @@ public void init(FilterConfig config) throws ServletException
}
catch (IllegalStateException e)
{
log.info("Could not locate BeanManager. Catch integration for Servlet disabled (even if present on the classpath)");
return;
}
}

if (!beanManager.getBeans(CatchExceptionFilter.class).isEmpty())
{
enabled = true;
log.info("Catch integration for Servlet enabled");
}
}

Expand All @@ -79,7 +85,7 @@ public void doFilter(ServletRequest request, ServletResponse response, FilterCha
}
catch (Exception e)
{
ExceptionToCatchEvent catchEvent = new ExceptionToCatchEvent(e, ServletWebRequestLiteral.INSTANCE);
ExceptionToCatch catchEvent = new ExceptionToCatch(e, WebRequestLiteral.INSTANCE);
beanManager.fireEvent(catchEvent);
// QUESTION should catch handle rethrowing?
if (!catchEvent.isHandled())
Expand Down

0 comments on commit 93930d2

Please sign in to comment.