On Oct 2, 2009, at 5:10 PM, Jordi Domingo wrote:
> 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
Quite possibly.
> In any case, do you know wich component may be dumping the exception
> (its an stacktrace like the one in glassfish logs).
Can you send the stacktrace ?
> ----
>
> I tried using HttpContext:
>
> @Context HttpContext httpContext;
>
> And in the toResponse(..)
>
> MediaType m = httpContext.getResponse().getMediaType();
> MediaType selected = httpContext.getRequest().getMediaType();
>
> I get null.
If "m" is null it implies that no resource method was called with
@Produces.
I think i would need to see more of your application to understand
where the EJBException is originating from. In fact if you can send me
a zip file of a maven project that would help.
Recently i modified the behavior of processing EJBExcpetion such that
Jersey registered a mapper for that class (see end of email). This is
in 1.1.2. So actually EJBExcpetion will never be propagated to the
Servlet container. Perhaps that is why things are not working for you.
Note that you can override this with your own implementation.
The mapper will attempt to extract the cause of the exception then
attempt to map that.
Does that give any more clues? as i said an example would help then i
can debug it.
Paul.
public class EJBExceptionMapper implements
ExceptionMapper<EJBException> {
private final Providers providers;
public EJBExceptionMapper(@Context Providers providers) {
this.providers = providers;
}
public Response toResponse(EJBException exception) {
final Exception cause = exception.getCausedByException();
if (cause != null) {
final ExceptionMapper mapper =
providers.getExceptionMapper(cause.getClass());
if (mapper != null) {
return mapper.toResponse(cause);
} else if (cause instanceof WebApplicationException) {
return ((WebApplicationException)cause).getResponse();
}
}
return Response.serverError().build();
}
}
> ---
> 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