users@jersey.java.net

How to handle exceptions when we need to display error msg in the same page that was served by GET

From: Dinesh Narayanan <ndchandar_at_gmail.com>
Date: Wed, 21 Oct 2009 02:04:06 -0700

Hello Paul,

Question 1:

 I was trying to implement exception handling for my project based on the
following email thread
http://markmail.org/message/vicjp4eyqqwx7brd#query:ExceptionMapper%20and%20Viewable+page:1+mid:zxuftddvp7ugltnw+state:results

And I was wondering if there is a better way to do this. I have a jsp
(siteAccessPartial.jsp) which displays some entity info and also has some
form fields. Now the user enters some form fields (say some textbox fields
in this case) and clicked on submit. I do a POST to the appropriate
resource, and one of my service threw an application exception (due to some
business validation failure in this case), I need to return back a 500 (with
an appropriate error message) and I need to show this error message in the
same jsp(siteAccessPartial.jsp).


The Question is that is it OK to pass my resource instance to the Exception
class or is there a better way to do it?


Here is the sample code snippet:


public class SiteAccessResource {

    private static final Logger logger =
Logger.getLogger(SiteAccessResource.class

    private static final String JSP_PAGE =
"/jsp/myAccount/siteAccessPartial.jsp";

    // this entity info be displayed in the jsp

    private List<UserAccess> userAccessList = new ArrayList<UserAccess>();

    private final UserAccessDAO userAccessDAO;


    @Inject

    public SiteAccessResource(UserAccessDAO userAccessDAO){

        this.userAccessDAO = userAccessDAO;

    }


    @GET

    @Produces("text/html")

    public Viewable get(@QueryParam("userName) String userName) {

        userAccessList = userAccessDAO.getUserAccess(userName);

        return new Viewable(JSP_PAGE, this);

    }


    @POST

    @Produces("text/html")

    public Viewable post(@FormParam("key1") String siteAccessCode ){

// this will throw MyBrocadeException if siteAccessCode does not exist in DB

            if( userAccessDAO.checkIfCodeisValid(siteAccessCode)) {

//TODO - Paul Is this correct?

            throw new MyBrocadeException("error.siteAccessCode", "Invalid
site access code", JSP_PAGE, this);

} else {

// store the entry in db

userAccessDAO.createUserAcces(siteAccessCode);

}

        return new Viewable(JSP_PAGE, this);

    }

}


MyBrocadeException class:


public class MyBrocadeException extends RuntimeException{

    private final String errorMessage;

    private final String errorCode;

    private final String jspName;

    private final Object o;


    public MyBrocadeException(String errorCode, String errorMessage, String
jspName, Object obj) {

        this.errorCode = errorCode;

        this.errorMessage = errorMessage;

        this.jspName = jspName;

        this.o = obj;

    }


    public String getErrorMessage() {

        return errorMessage;

    }


    public String getErrorCode() {

        return errorCode;

    }


    public String getJspName() {

        return jspName;

    }


    public Object getO() {

        return o;

    }

}


MyProvider class:

@Provider

public class MyBrocadeExceptionMapper implements
javax.ws.rs.ext.ExceptionMapper<MyBrocadeException>{

    public Response toResponse(MyBrocadeException exception) {

      return Response.serverError().entity(new
Viewable(exception.getJspName(),exception.getO())).build();

    }

}



Question no 2: Are there any performance overhead if we use excessive
subResource locators or is this negligible.

Thanks
Dinesh