Developing Custom Portlets
Tips and Best PracticesThis 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 ProblemsTo avoid namespace problems, do the following:
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 Bundling Multiple Portlets into a Single PAR FileWhen 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 PortletsTo 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 ContextThe Session Context provides a means of passing information from one portlet to another. You can use methods that are specified in the
Obtaining the User's LocaleIf you want to obtain the user's locale from within a portlet initializer or action class, you can obtain this information from the 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 Note:
In order for this sample code to run, the following JAR files must be present in the classpath of the portlet's build environment:
These files should not be distributed with the portlet's PAR file. Obtaining the User's NameYou can use the 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 Note: Remote portlets can obtain the user's name by using the remote Session Context. |