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
Developing Custom Portlets

Tips and Best Practices

This page provides tips and best practices for developing portlets, including the following:

Note: In order for the code samples on this page to work, you must include the appropriate import statements at the top of your program. For example:

import com.sas.preferences.SASProfileInterface;
import com.sas.services.information.metadata.PersonInterface;
import com.sas.services.session.SessionContextInterface;
import com.sas.services.user.UserContextInterface;
import com.sas.services.user.UserServiceInterface;
 

Avoiding Namespace Problems

To avoid namespace problems, do the following:

  • Use a standard naming convention for portlet paths.

  • Avoid using the _SAS namespace.

The portlet namespace is comprised of the path (with leading underscores in place of slashes) and the portlet's name. For example, a portlet with the name simpleJSP and a path of /mycompany/portlets would be deployed as _mycompany_portlets_simpleJSP.


Bundling Multiple Portlets into a Single PAR File

When you need to deploy multiple portlets, define the portlets in a single portlet deployment descriptor (XML) file and bundle the portlets into a single portlet archive (PAR) file if it is feasible to do so. This practice improves performance, since only one PAR file needs to be opened and only one XML file needs to be read.


Testing Portlets

To test and debug a local portlet that you have developed, deploy it into a staging area (that is, a test installation of the portal Web application). After the portlet has been verified and tested, deploy it into the production environment.

For remote portlets, test and debug the Web application that is called by the portlet by using the application's direct URL. After the application has been verified and tested, deploy the remote portlet into the portal Web application's production environment.


Obtaining a Session Context

The Session Context provides a means of passing information from one portlet to another. You can use methods that are specified in the PortletContext interface to obtain either the local Session Context or the remote Session Context, as follows:

  • Local Session Context. From a local portlet, use the getHTTPSession() method to obtain the HttpSession object. From the HttpSession object, you can access the local Session Context.

    The following code obtains the local Session Context and then obtains the User Context from the local Session Context:

    HttpSession session = portletContext.getHttpSession();
    SessionContextInterface sessionContext =
     (SessionContextInterface)
     session.getAttribute(com.sas.web.keys.CommonKeys.SESSION_CONTEXT);
    UserContextInterface userContext =
     sessionContext.getUserContext();
    

    If you obtain the User Context from the local Session Context, then you will have access to all of the repositories that are defined in the local services deployment. Therefore, it is recommended that you use the local Session Context instead of the remote Session Context whenever possible .

  • Remote Session Context. Both local and remote portlets can call the getSessionContext() method. This method returns a remote Session Context object from the remote services.

    The following code obtains the remote Session Context and obtains the User Context from the remote Session Context:

    UserContextInterface ucf = 
       portletContext.getSessionContext().getUserContext(); 
    

    If you obtain the User Context from the remote Session Context, then you can connect only to the repositories that are defined in the remote services deployment. Since additional repositories might be defined in the local services deployment, it is recommended that you use the local Session Context whenever possible.


Obtaining the User's Locale

If you want to obtain the user's locale from within a portlet initializer or action class, you can obtain this information from the PortletContext as shown in the following sample code:

HttpSession session = portletContext.getHttpSession(); 
SessionContextInterface sessionContext =
  (SessionContextInterface) 
   session.getAttribute(com.sas.web.keys.CommonKeys.SESSION_CONTEXT);
UserContextInterface userContext = sessionContext.getUserContext();
ProfileInterface profile = userContext.getProfile(); 
   com.sas.preferences.SASProfileInterface sasProfile =  
      (com.sas.preferences.SASProfileInterface) 
profile.getProfile("SAS");
   Locale locale = sasProfile.getLocale();

This code first obtains the User Context from the local Session Context. Then it obtains the desired profile from the User Context. Finally, it gets the locale from the instance of the SASProfileInterface.

Note:

  • The locale may be null if no value was available from the SAS profile. In this case, use the HttpServletRequest.getLocale() method.

  • Remote portlets can obtain the user's locale by using the remote Session Context.

In order for this sample code to run, the following JAR files must be present in the classpath of the portlet's build environment:

  • sas.common.framework.jar
  • sas.core.jar
  • sas.entities.jar
  • sas.portal.metadata.jar
  • sas.svc.core.jar.

These files should not be distributed with the portlet's PAR file.


Obtaining the User's Name

You can use the PortletContext interface to obtain the name of the person who is logged on to the portal Web application, as shown in the following sample code:

HttpSession session = portletContext.getHttpSession(); 
SessionContextInterface sessionContext = 
 (SessionContextInterface)
 session.getAttribute(com.sas.web.keys.CommonKeys.SESSION_CONTEXT); 
UserContextInterface userContext = sessionContext.getUserContext(); 
UserServiceInterface userService = UserContext.getUserService(); 
SASProfileInterface sasProfile = (SASProfileInterface) 
  userService.loadProfile(userContext, "SAS");
PersonInterface aPerson = sasProfile.getUser();
String name = aPerson.getName();

This code first obtains the User Context from the local Session Context. Then it uses the UserService to load the desired profile for the User Context. Finally, it gets the name from the instance of the SASProfileInterface.

Note: Remote portlets can obtain the user's name by using the remote Session Context.