users@jersey.java.net

RE: [Jersey] ExceptionMapper and Viewable

From: Jordi Domingo <noseya_at_hotmail.com>
Date: Fri, 2 Oct 2009 17:22:41 +0200

Hi again,

I disabled the mapper, trying to let all exceptions reach the servlet container.

I deployed 4 times, 2 restarts and always the exception get dumped.

web.xml:
<error-page>
        <exception-type>java.lang.Throwable</exception-type>
        <location>/jsp/ejbException.jsp</location>
    </error-page>
    <error-page>
        <exception-type>com.sun.jersey.api.container.MappableContainerException</exception-type>
        <location>/jsp/ejbException.jsp</location>
    </error-page>
    <error-page>
        <error-code>500</error-code>
        <location>/jsp/ejbException.jsp</location>
    </error-page>

My throwing exception test resource:

@GET
    @Produces({"text/html","application/xhtml+xml","application/xaml+xml"})
    public Viewable listCompaniesHtml(@QueryParam("page") @DefaultValue("1") int page) throws Exception{
        securityService.checkAccess("admin");
        CompaniesTO c = null;
        try{
            if(true)throw new SecurityException();
            
            c = companiesService.listCompanies(page,Constants.REGISTRES_PAGINA);
        }catch(DaoException e){
            throw new WebApplicationException(Status.INTERNAL_SERVER_ERROR);
        }
        return new Viewable("/companiesList",c);
    }

What gets dumped is just the stack trace (like gf logs) without any html tag.

Thanks,

JOrdi
From: noseya_at_hotmail.com
To: users_at_jersey.dev.java.net
Date: Fri, 2 Oct 2009 17:10:30 +0200
Subject: RE: [Jersey] ExceptionMapper and Viewable








Hi again Paul!

I saw that sometimes (to change behavior the server must be restarted) the exception goes through the mapper and sometimes not and it gets dumped. Maybe is a glassfish bug :S In any case, do you know wich component may be dumping the exception (its an stacktrace like the one in glassfish logs).
----
I tried using HttpContext:
@Context HttpContext httpContext;
And in the toResponse(..)
MediaType m = httpContext.getResponse().getMediaType();
 MediaType selected = httpContext.getRequest().getMediaType();
I get null. 
---
The first thing i tried was using the servlet exception rules (im using that yet with the mapper for EJBException) but happens the same with glassfish dumping the exception:
<error-page>
        <exception-type>com.sun.jersey.api.container.MappableContainerException</exception-type>
        <location>/jsp/ejbException.jsp</location>
    </error-page>
    <error-page>
        <error-code>500</error-code>
        <location>/jsp/ejbException.jsp</location>
    </error-page>
---
When a user sends a request, jersey selects wich method must attend the user and jersey know which one will be the response type by default. Is there any method for me, to know wich one will be? (httpContext.getResponse().getMediaType() returns null)
Thanks Paul!
> Date: Thu, 1 Oct 2009 10:33:48 +0200
> From: Paul.Sandoz_at_Sun.COM
> To: users_at_jersey.dev.java.net
> Subject: Re: [Jersey] ExceptionMapper and Viewable
> 
> On Sep 30, 2009, at 1:44 PM, Jordi Domingo wrote:
> 
> >
> > Hi :)
> >
> > I'm trying to make an ExceptionMapper that returns the response  
> > expected by the client. I dont know how i can return a Viewable or a  
> > JSON response because i dont have access to any object with that  
> > information
> >
> >  public Response toResponse(EJBException exception) {}
> >
> 
> If a resource method was selected for invocation then the Content-Type  
> of the response will have been selected if the @Produces specifies a  
> concrete (non-wild card) media type. So if the Content-Type is present  
> you can look at that.
> 
> You can inject Jersey's HttpContext on the ExceptionMapper and in the  
> toResponse method you can do:
> 
>    MediaType m = httpContext.getResponse().getMediaType();
> 
> You can also get access to the acceptable headers by doing:
> 
>    httpContext.getRequest().getAcceptableMediaTypes()
> 
> (That method is a method on javax.ws.rs.core.HttpHeaders, which can be  
> injected if required rather than referring to something Jersey  
> specific).
> 
> 
> > On another side, when Jersey finds an Exception (not Runtime) that  
> > is not mapped, forwards it to the ServletContainer, thats fine.
> 
> Any RuntimeException that is not mapped will be re-thrown and it will  
> pass through Jersey's Servlet layer to the Servlet framework from  
> where it can be mapped using Servlet-based rules.
> 
> Any checked exception that is not mapped will be wrapped around a  
> Jersey runtime exception (ContainerException) and that will be passed  
> to Jersey's Servlet layer  and wrapped in a ServletException exception  
> and re-thrown.
> 
> 
> > When i do the same with an EJBException (SystemException) I dont  
> > know how to achieve the same behavior. This is my code for testing:
> >
> > public Response toResponse(EJBException exception) {
> >          final Exception cause = exception.getCausedByException();
> >
> >          return new WebApplicationException(cause).getResponse();
> >      }
> >
> 
> I am not sure i understand what you want to achieve.
> 
> Do you want the EJBException to be passed through to the Servlet  
> framework?
> 
> Paul.
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
> For additional commands, e-mail: users-help_at_jersey.dev.java.net
> 
 		 	   		  
Charlas más divertidas con el nuevo Windows Live Messenger 		 	   		  
_________________________________________________________________
Nuevo Windows Live, un mundo lleno de posibilidades. Descúbrelo.
http://www.microsoft.com/windows/windowslive/default.aspx
--_283f2440-7e11-4223-a3fe-e18658b5ba64_
Content-Type: text/html; charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
<html>
<head>
<style><!--
.hmmessage P
{
margin:0px;
padding:0px
}
body.hmmessage
{
font-size: 10pt;
font-family:Verdana
}
--></style>
</head>
<body class='hmmessage'>
Hi again,<br><br>I disabled the mapper, trying to let all exceptions reach the servlet container.<br><br>I deployed 4 times, 2 restarts and always the exception get dumped.<br><br>web.xml:<br>&lt;error-page&gt;<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &lt;exception-type&gt;java.lang.Throwable&lt;/exception-type&gt;<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &lt;location&gt;/jsp/ejbException.jsp&lt;/location&gt;<br>&nbsp;&nbsp;&nbsp; &lt;/error-page&gt;<br>&nbsp;&nbsp;&nbsp; &lt;error-page&gt;<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &lt;exception-type&gt;com.sun.jersey.api.container.MappableContainerException&lt;/exception-type&gt;<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &lt;location&gt;/jsp/ejbException.jsp&lt;/location&gt;<br>&nbsp;&nbsp;&nbsp; &lt;/error-page&gt;<br>&nbsp;&nbsp;&nbsp; &lt;error-page&gt;<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &lt;error-code&gt;500&lt;/error-code&gt;<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &lt;location&gt;/jsp/ejbException.jsp&lt;/location&gt;<br>&nbsp;&nbsp;&nbsp; &lt;/error-page&gt;<br><br>My throwing exception test resource:<br><br>@GET<br>&nbsp;&nbsp;&nbsp; @Produces({"text/html","application/xhtml+xml","application/xaml+xml"})<br>&nbsp;&nbsp;&nbsp; public Viewable listCompaniesHtml(@QueryParam("page") @DefaultValue("1") int page) throws Exception{<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; securityService.checkAccess("admin");<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; CompaniesTO c = null;<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; try{<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; if(true)throw new SecurityException();<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; c = companiesService.listCompanies(page,Constants.REGISTRES_PAGINA);<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }catch(DaoException e){<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; throw new WebApplicationException(Status.INTERNAL_SERVER_ERROR);<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; return new Viewable("/companiesList",c);<br>&nbsp;&nbsp;&nbsp; }<br><br>What gets dumped is just the stack trace (like gf logs) without any html tag.<br><br>Thanks,<br><br>JOrdi<br><hr id="stopSpelling">From: noseya_at_hotmail.com<br>To: users_at_jersey.dev.java.net<br>Date: Fri, 2 Oct 2009 17:10:30 +0200<br>Subject: RE: [Jersey] ExceptionMapper and Viewable<br><br>
<style>
.ExternalClass .ecxhmmessage P
{padding:0px;}
.ExternalClass body.ecxhmmessage
{font-size:10pt;font-family:Verdana;}
</style>
Hi again Paul!<br><br>I saw that sometimes (to change behavior the server must be restarted) the exception goes through the mapper and sometimes not and it gets dumped. Maybe is a glassfish bug :S In any case, do you know wich component may be dumping the exception (its an stacktrace like the one in glassfish logs).<br>----<br><br>I tried using HttpContext:<br><br>@Context HttpContext httpContext;<br><br>And in the toResponse(..)<br><br>MediaType m = httpContext.getResponse().getMediaType();<br> MediaType selected = httpContext.getRequest().getMediaType();<br><br>I get null. <br>---<br>The first thing i tried was using the servlet exception rules (im using that yet with the mapper for EJBException) but happens the same with glassfish dumping the exception:<br><br>&lt;error-page&gt;<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &lt;exception-type&gt;com.sun.jersey.api.container.MappableContainerException&lt;/exception-type&gt;<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &lt;location&gt;/jsp/ejbException.jsp&lt;/location&gt;<br>&nbsp;&nbsp;&nbsp; &lt;/error-page&gt;<br>&nbsp;&nbsp;&nbsp; &lt;error-page&gt;<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &lt;error-code&gt;500&lt;/error-code&gt;<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &lt;location&gt;/jsp/ejbException.jsp&lt;/location&gt;<br>&nbsp;&nbsp;&nbsp; &lt;/error-page&gt;<br><br>---<br>When a user sends a request, jersey selects wich method must attend the user and jersey know which one will be the response type by default. Is there any method for me, to know wich one will be? (httpContext.getResponse().getMediaType() returns null)<br><br>Thanks Paul!<br><br>&gt; Date: Thu, 1 Oct 2009 10:33:48 +0200<br>&gt; From: Paul.Sandoz@Sun.COM<br>&gt; To: users@jersey.dev.java.net<br>&gt; Subject: Re: [Jersey] ExceptionMapper and Viewable<br>&gt; <br>&gt; On Sep 30, 2009, at 1:44 PM, Jordi Domingo wrote:<br>&gt; <br>&gt; &gt;<br>&gt; &gt; Hi :)<br>&gt; &gt;<br>&gt; &gt; I'm trying to make an ExceptionMapper that returns the response  <br>&gt; &gt; expected by the client. I dont know how i can return a Viewable or a  <br>&gt; &gt; JSON response because i dont have access to any object with that  <br>&gt; &gt; information<br>&gt; &gt;<br>&gt; &gt;  public Response toResponse(EJBException exception) {}<br>&gt; &gt;<br>&gt; <br>&gt; If a resource method was selected for invocation then the Content-Type  <br>&gt; of the response will have been selected if the @Produces specifies a  <br>&gt; concrete (non-wild card) media type. So if the Content-Type is present  <br>&gt; you can look at that.<br>&gt; <br>&gt; You can inject Jersey's HttpContext on the ExceptionMapper and in the  <br>&gt; toResponse method you can do:<br>&gt; <br>&gt;    MediaType m = httpContext.getResponse().getMediaType();<br>&gt; <br>&gt; You can also get access to the acceptable headers by doing:<br>&gt; <br>&gt;    httpContext.getRequest().getAcceptableMediaTypes()<br>&gt; <br>&gt; (That method is a method on javax.ws.rs.core.HttpHeaders, which can be  <br>&gt; injected if required rather than referring to something Jersey  <br>&gt; specific).<br>&gt; <br>&gt; <br>&gt; &gt; On another side, when Jersey finds an Exception (not Runtime) that  <br>&gt; &gt; is not mapped, forwards it to the ServletContainer, thats fine.<br>&gt; <br>&gt; Any RuntimeException that is not mapped will be re-thrown and it will  <br>&gt; pass through Jersey's Servlet layer to the Servlet framework from  <br>&gt; where it can be mapped using Servlet-based rules.<br>&gt; <br>&gt; Any checked exception that is not mapped will be wrapped around a  <br>&gt; Jersey runtime exception (ContainerException) and that will be passed  <br>&gt; to Jersey's Servlet layer  and wrapped in a ServletException exception  <br>&gt; and re-thrown.<br>&gt; <br>&gt; <br>&gt; &gt; When i do the same with an EJBException (SystemException) I dont  <br>&gt; &gt; know how to achieve the same behavior. This is my code for testing:<br>&gt; &gt;<br>&gt; &gt; public Response toResponse(EJBException exception) {<br>&gt; &gt;          final Exception cause = exception.getCausedByException();<br>&gt; &gt;<br>&gt; &gt;          return new WebApplicationException(cause).getResponse();<br>&gt; &gt;      }<br>&gt; &gt;<br>&gt; <br>&gt; I am not sure i understand what you want to achieve.<br>&gt; <br>&gt; Do you want the EJBException to be passed through to the Servlet  <br>&gt; framework?<br>&gt; <br>&gt; Paul.<br>&gt; <br>&gt; ---------------------------------------------------------------------<br>&gt; To unsubscribe, e-mail: users-unsubscribe@jersey.dev.java.net<br>&gt; For additional commands, e-mail: users-help@jersey.dev.java.net<br>&gt; <br> 		 	   		  <br><hr>Charlas más divertidas con el nuevo <a href="http://download.live.com">Windows Live Messenger</a> 		 	   		  <br /><hr />Nuevo Windows Live, un mundo lleno de posibilidades <a href='http://www.microsoft.com/windows/windowslive/default.aspx' target='_new'>Descúbrelo.</a></body>
</html>
--_283f2440-7e11-4223-a3fe-e18658b5ba64_--