joi, 2 aprilie 2015

[OmniFaces Utilities (2.0)] Send a HTTP response error with the given status and message


[OmniFaces utilities] The responseSendError() method sends a HTTP response error with the given status and message. This will end up in either a custom <error-page> whose <error-code> matches the given status, or in a servlet container specific default error page if there is none. The message will be available in the error page as a request attribute with name javax.servlet.error.message. The FacesContext#responseComplete() will implicitly be called after sending the error. The HTTP response status is supposed to be in the range 4nn-5nn. You can use the constant field values of HttpServletResponse for this.

Note The caller should preferably not catch the IOException thrown by this method,but just re-declare it in the action method.The Servlet container will handle it.

Method:
Usage:
Suppose that we access a page as: shop.xhtml?product=id, where id is the unique product identifier (e.g. a number between 1-100). We have this:

<h:head>
  <title></title>       
  <f:metadata>
    <f:viewParam name="product"
                 value="#{productBean.product}"
                 validator="productValidator"/>                      
  </f:metadata>
</h:head>

 Now, this id must be validated before showing the product, and if it is an invalid product then return a 404 page error. For this, we can use the Faces#responseSendError(), as below:

import org.omnifaces.util.Faces;
...
@FacesValidator(value = "productValidator")
public class ProductValidator implements Validator {

  @Override
  public void validate(FacesContext context, UIComponent component, Object value)
                                                       throws ValidatorException {

    int id = Integer.parseInt((String) value);
    if (check_id) {
        try {
            Faces.responseSendError(404, "The product with unique product identifier, " + id + " cannot be found !");
            // or using, HttpServletResponse#SC_NOT_FOUND
            Faces.responseSendError(HttpServletResponse.SC_NOT_FOUND, 
                "The product with unique product identifier, " + id + " cannot be found !");
        } catch (IOException ex) {
          throw new FacesException(ex);
        }
    }
  }
}

How the user sees the error message ? Well, by default it will see it in a Servlet container specific default error page, as below:
Or, you may provide an error page and configure it in web.xml. A simple error.xhtml page:
...
<h:body>     
        <h2>#{facesContext.externalContext.requestMap.get("javax.servlet.error.status_code")}</h2>       
  <hr/>
  Error Desscription: #{facesContext.externalContext.requestMap.get("javax.servlet.error.message")}<br/>               
  Servlet Name: #{facesContext.externalContext.requestMap.get("javax.servlet.error.servlet_name")}
</h:body>
...

In web.xml, it must be configured as:

<error-page>
  <error-code>404</error-code>
  <location>/faces/error.xhtml</location>
</error-page>

Now, the result of an invalid id will be:

Niciun comentariu:

Trimiteți un comentariu