SAS 9.1.3 Integration Technologies » SAS Web Infrastructure Kit: Developer's Guide


Developing Custom Portlets
Development Steps
Creating a Deployment Descriptor
Creating Display Resources Files
Developing the Presentation JSP Page
Creating Action Classes
Implementing Portlet Help
Creating a PAR File
Use Cases
Simple Display Portlet
Localized Portlet
Portlet Template (Editable Portlet)
Remote Portlet
Tips and Best Practices
Using the Portlet API
Sample Portlets
Localized Display Portlet (Welcome)
Interactive Form Portlet (FormExample)
Portlet Template, or Editable Portlet (DisplayURL)
Web Application (HelloUserWikExample)
Remote Portlet (HelloUserRemote
Portlet
Sample: Portlet Template, or Editable Portlet (DisplayURL)

Error Handler Action

The source code for the DisplayURL portlet's ErrorHandler action class follows.


/** Copyright (c) 2003 by SAS Institute Inc., Cary, NC 27513.
 *  All Rights Reserved.
 */
package com.sas.portal.portlets.displayurl;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.sas.apps.portal.PortalException;
import com.sas.portal.Logger;
import com.sas.portal.portlet.ErrorHandlerInterface;
import com.sas.portal.portlet.NavigationUtil;
import com.sas.portal.portlet.PortletContext;

/**
 * Error handler for this portlet. It logs the exception and 
 * returns ErrorPage.jsp for the portlet to display.
 */
public class ErrorHandler implements ErrorHandlerInterface
{
  private final String _loggingContext = 
    this.getClass().getName();

  /**
   * Returns the URL for the portlet controller to call. 
   * This is the name of
   * the error page JSP.
   * @param request the HttpServletRequest
   * @param response the HttpServeltResponse
   * @param context the PortletContext
   * @param exception the exception thrown by a portlet action
   * @return the URL to call
   */
  public String service(HttpServletRequest request,
                        HttpServletResponse response,
                        PortletContext context,
                        Exception thrownException)
  {
      //prepare the localized resources for use by the jsp.
      try {
          NavigationUtil.prepareLocalizedResources(
             "com.sas.portal.portlets.displayurl.Resources",
             request, context);
      }
      catch (java.io.IOException ioe) {
          Logger.error(ioe.getMessage(), _loggingContext, ioe);
      }

      //send error to server log in default locale.
      Logger.error(thrownException.getMessage(), _loggingContext, 
          thrownException);

      //get msg in user's locale.
      String msg = null;
      try {
         PortalException ourException = (PortalException)
            thrownException;
         msg = 
		    ourException.getMessage(request.getLocale());
      }
      catch (ClassCastException cce){
         msg= "";
      }

      if (msg == null) {
         //prevent showing the word null in a JSP
         msg = "";
      }

      //make msg available for display on error jsp.
      context.setAttribute("Exception_message", msg);

      return (String)context.getAttribute("error-page");
  }
}