Sun Java System Portal Server 7.1 Developer's Guide

Chapter 8 Extending the Leaf Providers

This chapter includes detailed instructions for:

Extending the JSPProvider

This sections contains some sample extensions to the JSPProvider.

Example 1

In the following example, the JSPProvider getContent() method is overridden to make a connection to the database to get the address book from the database and use a JSP to display the content of the address book for each user.

ProcedureTo Extend the JSPProvider

  1. Extend the JSPProvider and develop the AddressBookProvider class.

    The AddressBookProvider class overrides the JSPProvider’s getContent() method which reads the user’s address book. The getContent() method gets the address book for the user who is logged in to the Desktop.

    AddressBookProvider.java File


    package custom;
    
    import javax.servlet.http.HttpServletResponse;
    import javax.servlet.http.HttpServletRequest;
    
    import com.sun.portal.providers.ProviderException;
    import com.sun.portal.providers.jsp.JSPProvider;
    
    public class AddressBookProvider extends JSPProvider {
    	public StringBuffer getContent (
    	HttpServletRequest req, HttpServletResponse res
    	) throws ProviderException {
    		AddressBook addrBook = new custom.AddressBook
    		(getProviderContext().getUserID());
    		req.setAttribute("addressBook", addrBook);
    		return super.getContent(req, res);
    	}
    }
  2. Develop the AddressBook class.

    The class AddressBook is responsible for making a connection to the database and getting the user’s address book from the database.

  3. Compile both classes and put them in the provider class base directory.

    The default directory for the class file is PortalServer-DataDir/portals/portal-ID/desktop/classes. That is, build a JAR and copy the JAR into the provider class base directory. Or, just copy the class files into the provider class base directory.

  4. Develop the display profile XML fragment for this provider and this provider’s channel.

    The display profile fragment for the AddressBookProvider’s provider is saved in AddressBookProvider.xml file and the display profile fragment for the AddressBookProvider’s channel is saved in AddressBookChannel.xml file.

    AddressBookProvider.xml File


    <?xml version="1.0" encoding="utf-8" standalone="no"?>
    <!DOCTYPE DisplayProfile SYSTEM "jar://resources/psdp.dtd">
    
    <Provider name="AddressBookProvider" class="custom.AddressBookProvider">
    	<Properties>
    		<String name="title" value="Address Book Provider"/>
    		<String name="description" value="My Addressbook"/>
    		<String name="refreshTime" value="0" advanced="true"/>
    		<Boolean name="isEditable" value="false" advanced="true"/>
    		<String name="editType" value="edit_subset" advanced="true"/>
    		<String name="width" value="thin" advanced="true"/>
    		<String name="helpURL" value="desktop/usedesk.htm" advanced="true"/>
    		<String name="fontFace1" value="Sans-serif"/>
    		<String name="productName" value="Sun Java System Portal Server"/>
    		<String name="contentPage" value="addressBook.jsp"/>
    	</Properties>
    </Provider>

    In AddressBookChannel.xml, note that the AddressBookProvider channel definition is embedded in an existing table container, JSPTableContainer.

    AddressBookChannel.xml File


    <?xml version="1.0" encoding="utf-8" standalone="no"?>
    <!DOCTYPE DisplayProfile SYSTEM "jar://resources/psdp.dtd">
    
    <Container name="JSPTableContainer" provider="JSPTableContainerProvider">
    	<Properties>
    		<Collection name="Personal Channels">
    			<String name="AddressBook"/>
    		</Collection>
    		<Collection name="channelsColumn">
    			<String name="AddressBook" value="3"/>
    		</Collection>
    	</Properties>
    	<Available>
    		<Reference value="AddressBook"/>
    	</Available>
    	<Selected>
    		<Reference value="AddressBook"/>
    	</Selected>
    	<Channels>
    		<Channel name="AddressBook" provider="AddressBookProvider">
    			<Properties>
    			</Properties>
    		</Channel>
    	</Channels>
    </Container>
  5. Use the psadmin command to upload the display profile fragments for this provider.

    For the AddressBookProvider, use the psadmin add-display-profile command to upload the AddressBookProvider.xml file and use the psadmin modify-display-profile command to upload the AddressBookChannel.xml file XML fragments in the display profile. For more information on these sub commands, see the Sun Java System Portal Server 7.1 Command Line Reference.

  6. Develop the JSP file for the provider.

    For the sample AddressBookProvider, the JSP in addressBook.jsp can be used to display the content of the address book for each user. However, the details of displaying the address book are not shown in addressBook.jsp.

    addressBook.jsp File


    <%-- addressBook.jsp --%>
    <%@page import="custom.AddressBookProvider" %>
    <%@page import="custom.AddressBook" %>
    
    <%
    AddressBookProvider addressBookProvider = (AddressBookProvider)
    pageContext.getAttribute("JSPProvider");
    AddressBook addrBook = (AddressBook)pageContext.getAttribute
    ("addressBook",PageContext.REQUEST_SCOPE);
    %>
    
    <%-- Display the address book --%>
  7. Create a channel directory under the template root directory.

    By default, the template root directory is PortalServer-DataDir/portals/portal-ID/desktop/desktoptype. For this example, create an AddressBook directory under PortalServer-DataDir/portals/portal-ID/desktop/desktoptype directory.

  8. Copy all the JSP files over to the newly created directory (in Example 1). For example, type:


    cp addressBook.jsp PortalServer-DataDir/portals/portal-ID/desktop/desktoptype/AddressBook
  9. Login to the Desktop and specify the URL in your browser to access the AddressBook channel inside the JSPTableContainer.


    http://hostname:port/portal-ID/dt?provider=JSPTableContainer

Example 2

In the following example, the SimpleUserInfoProvider overrides the processEdit() method of the JSPProvider. It reads the common name that is input in the edit page JSP, and then it is read from the processEdit() method. The value is set to the backend store. To accomplish this:

ProcedureTo Develop the SimpleUserInfoProvider

  1. Extend the JSPProvider and develop the SimpleUserInfoProvider class.

    The SimpleUserInfoProvider class overrides the JSPProvider processEdit() method as shown in Example 2.


    SimpleUserInfoProvider.java File
    package custom;
    
    import java.net.URL;
    
    import javax.servlet.http.HttpServletResponse;
    import javax.servlet.http.HttpServletRequest;
    
    import com.sun.portal.providers.ProviderException;
    import com.sun.portal.providers.jsp.JSPProvider;
    
    public class SimpleUserInfoProvider extends JSPProvider {
    	public URL processEdit(
    	HttpServletRequest req, HttpServletResponse res
    	) throws ProviderException {
    		//get the common name from the request and set it into the backend store
    		getProviderContext().setStringAttribute("cn",(String)req.getParameter("cn"));
    		return null;
    	}
    }
  2. Compile the class and put it in the provider class base directory.

    The default directory for the class file is PortalServer-DataDir/portals/portal-ID/desktop/classes. That is, build the JAR and copy the JAR into the provider class base directory. Or, just copy the class as a file into the provider class base directory. To compile the SimpleUserInfoProvider.java file, type:


    javac -d PortalServer-DataDir/portals/
    portal-ID/desktop/classes -classpath PortalServer-base/sdk/desktop/desktopsdk.jar:
    AccessManager-base/lib/servlet.jar SimpleUserInfoProvider.java
  3. Develop the display profile XML fragment for this provider and this provider’s channel.

    The display profile fragment for the SimpleUserInfoProvider’s provider is saved in SimpleUIProvider.xml file and the display profile fragment for the SimpleUserInfoProvider’s channel is saved in SimpleUIPChannel.xml file.


    SimpleUIProvider.xml File
    <?xml version="1.0" encoding="utf-8" standalone="no"?>
    <!DOCTYPE DisplayProfile SYSTEM "jar://resources/psdp.dtd">
    
    <Provider name="SimpleUserInfoProvider" class="custom.SimpleUserInfoProvider">
    	<Properties>
    		<String name="title" value="Simple User Information Provider"/>
    		<String name="description" value="My UserInformation"/>
    		<String name="refreshTime" value="0" advanced="true"/>
    		<Boolean name="isEditable" value="false" advanced="true"/>
    		<String name="editType" value="edit_subset" advanced="true"/>
    		<String name="width" value="thin" advanced="true"/>
    		<String name="helpURL" value="desktop/usedesk.htm" advanced="true"/>
    		<String name="fontFace1" value="Sans-serif"/>
    		<String name="productName" value="Sun Java System Portal Server"/>
    		<String name="contentPage" value="simpleUserInfoContent.jsp"/>
    		<String name="editPage" value="simpleUserInfoEdit.jsp"/>
    	</Properties>
    </Provider>

    In SimpleUIPChannel.xml, note that the SimpleUserInfoProvider channel definition is embedded inside JSPTableContainer thus making the channel only visible via the container.


    SimpleUIPChannel.xml File
    <?xml version="1.0" encoding="utf-8" standalone="no"?>
    <!DOCTYPE DisplayProfile SYSTEM "jar://resources/psdp.dtd">
    
    <Container name="JSPTableContainer" provider="JSPTableContainerProvider">
    	<Properties>
    		<Collection name="Personal Channels">
    			<String name="SimpleUserInfo"/>
    		</Collection>
    		<Collection name="channelsColumn">
    			<String name="SimpleUserInfo" value="3"/>
    		</Collection>
    	</Properties>
    	<Available>
    		<Reference value="SimpleUserInfo"/>
    	</Available>
    	<Selected>
    		<Reference value="SimpleUserInfo"/>
    	</Selected>
    	<Channels>
    		<Channel name="SimpleUserInfo" provider="SimpleUserInfoProvider">
    			<Properties>
    				<String name="processPage" value="simpleUserInfoDoedit.jsp"/>
    			</Properties>
    		</Channel>
    	</Channels>
    </Container>
  4. Use the psadmin command to upload the display profile fragments for this provider.

    For the SimpleUserInfoProvider, use the psadmin add-display-profile command to upload the SimpleUIProvider.xml file and use the psadmin modify-display-profile command to upload the SimpleUIPChannel.xml file XML fragments in the display profile. For more information on these sub commands, see the Sun Java System Portal Server 7.1 Command Line Reference.

  5. Create a channel directory under the template root directory.

    By default, the template root directory is PortalServer-DataDir/portals/portal-ID/desktop/desktoptype. For this example, create an SimpleUserInfo directory under PortalServer-DataDir/portals/portal-ID/desktop/desktoptype directory.

  6. Develop and copy all the JSP files over to the newly created directory (in Step 5).

    For example, develop the simpleUserInfoContent.jsp and simpleUserInfoEdit.jsp files and copy them over to the newly created PortalServer-DataDir/portals/portal-ID/desktop/desktoptype/SimpleUserInfo directory.

  7. Login to the Desktop and specify the URL in your browser to access the SimpleUserInfo channel inside the JSPTableContainer.


    http://hostname:port/portal-ID/dt?provider=JSPTableContainer

Extending the URLScraperProvider

The following sample extends the URLScraperProvider and overrides the getURL() method to get a random URL from a pool of URLs defined in the display profile. This sample can be used to display a random image from a pool of images; that is, the following getURL() method selects an URL at random from a pool of URLs where each URL is pointing to a HTML file containing the image.

ProcedureTo Extend the URLScraperProvider

  1. Extend the URLScraperProvider and develop the custom class file.

    The CustomURLScraperProvider class file overrides the URLScraperProvider getURL() method as shown in Extending the URLScraperProvider. The CustomURLScraperProvider gets the URL property for the provider. This is the URL from where the contents are fetched.


    CustomURLScraperProvider.java File
    package custom;
    
    import java.util.List;
    import java.util.Hashtable;
    import java.util.Random;
    
    import com.sun.portal.providers.urlscraper.URLScraperProvider;
    import com.sun.portal.providers.ProviderException;
    import com.sun.portal.providers.context.ProviderContextException;
    
    public class CustomURLScraperProvider
    extends URLScraperProvider {
        protected String getURL() throws ProviderException {
            List urlList = getListProperty("urlPool");
            int size = urlList.size();
            if ( size == 1) {
                return (String)urlList.get(0);
            }
            Random rand = new Random();
            int r = rand.nextInt(size-1);
            return (String)urlList.get(r);
        }
    }
  2. Compile the class and put it in the provider class base directory.

    The default directory for the class file is PortalServer-DataDir/portals/portal-ID/desktop/classes. That is, build the JAR and copy the JAR into the provider class base directory. Or, just copy the class as a file into the provider class base directory. To compile the CustomURLScraperProvider.java file, type:


    javac -d /var/opt/SUNWportal/portals/portal-ID/desktop/classes -classpath
    PortalServer-base/sdk/desktop/desktopsdk.jar:AccessManager-base
    /lib/servlet.jar CustomURLScraperProvider.java
  3. Develop the display profile XML fragment for this provider and this provider’s channel.

    The display profile fragment for the CustomURLScraperProvider’s provider is saved in CustomURLSProvider.xml file and the display profile fragment for the CustomURLScraperProvider’s channel is saved in CustomURLSChannel.xml file.


    CustomURLSProvider.xml File
    <?xml version="1.0" encoding="utf-8" standalone="no"?>
    <!DOCTYPE DisplayProfile SYSTEM "jar://resources/psdp.dtd">
    
    <Provider name="CustomURLScraperProvider" class="custom.CustomURLScraperProvider">
    	<Properties>
    		<String name="title" value=" Custom UrlScraper Channel"/>
    		<String name="description" value="This channel displays the urls at random."/>
    		<Boolean name="isEditable" value="false" advanced="true"/>
    		<String name="urlScraperRulesetID" value="default_ruleset"/>
    		<String name="width" value="thick"/>
    		<String name="refreshTime" value="0" advanced="true"/>
    		<Collection name="urlPool">
    			<String value="http://
    				localhost:
    				port/
    				file1.
    				html"/>
    			<String value="http://
    				localhost:
    				port/
    				file2.html"/>
    		</Collection>
    		<String name="fontFace1" value="Sans-serif"/>
    		<String name="productName" value="Sun Java System Portal Server"/>
    		<Boolean name="cookiesToForwardAll" value="true"/>
    		<String name="inputEncoding" value="UTF-8"/>
    		<Collection name="cookiesToForwardList">
    		</Collection>
    		<Integer name="timeout" value="100"/>
    	</Properties>
    </Provider>

    CustomURLSChannel.xml File


    <?xml version="1.0" encoding="utf-8" standalone="no"?>
    <!DOCTYPE DisplayProfile SYSTEM "jar://resources/psdp.dtd">
    
    <Channel name="CustomURLScraper" provider="CustomURLScraperProvider">
    	<Properties>
    		<String name="refreshTime" value="600" advanced="true"/>
    		<Collection name="urlPool">
    			<String value="http://
    				localhost:
    				port/
    				file1.html"/>
    			<String value="http://
    				localhost:
    				port/
    				file2.html"/>
    		</Collection>
    	</Properties>
    </Channel>
  4. Use the psadmin command to upload the display profile fragments for this provider.

    For the CustomURLScraperProvider, use the psadmin add-display-profile command to upload the CustomURLSProvider.xml and use the psadmin modify-display-profile command to upload the CustomURLSChannel.xml file XML fragments in the display profile. For more information on these commands, see the Sun Java System Portal Server 7.1 Command Line Reference.

  5. Include the new channel in one of the existing containers.

    The CustomURLScraperProvider channel will be displayed in JSPTableContainer. To add the channel in the JSPTableContainer from the administration console, follow instructions in Portal Server administration console online help. To add the channel in the JSPTableContainer manually:

    1. Add the CustomURLScraperProvider channel XML fragment (in the CustomURLSContainer.xml file) for the JSPTableContainerProvider.

      The CustomURLScraperProvider container XML fragment is shown below.


      CustomURLSContainer.xml File
      <?xml version="1.0" encoding="utf-8" standalone="no"?>
      <!DOCTYPE DisplayProfile SYSTEM "jar://resources/psdp.dtd">
      
      <Container name="JSPTableContainer" provider="JSPTableContainerProvider">
      	<Properties>
      		<Collection name="Personal Channels">
      			<String value="CustomURLScraper"/>
      		</Collection>
      		<Collection name="channelsRow">
      			<String name="CustomURLScraper" value="3"/>
      		</Collection>
      	</Properties>
      	<Available>
      			<Reference value="CustomURLSscraper"/>
      	</Available>
      	<Selected>
              <Reference value="CustomURLScraper"/>
      	</Selected>
      	<Channels>
      	</Channels>
      </Container>
    2. Use the psadmin modify-display-profile sub command to add the channel to a container.

      If you do not specify a parent object with the -p option, the channel is added at the root level.

  6. Change directories to PortalServer-DataDir/portals/portal-ID/desktop/classes and copy the properties file for the provider into this directory.

    For example, for the CustomURLScraperProvider:

    1. Change directories to PortalServer-DataDir/portals/portal-ID/desktop/classes.

    2. Type cp URLScraperProvider.properties PortalServer-DataDir/portals/portal-ID/desktop/classes/CustomURLScraperProvider.properties.

  7. Login to the Desktop and specify the URL in your browser to access the CustomURLScraperProvider channel inside the JSPTableContainer.


    http://hostname:port/portal-ID/dt?provider=JSPTableContainer

Extending the XMLProvider

In the following example, the XMLProvider getXML() method is overridden to get the content out of a database. In the CustomXMLProvider, the XML content is retrieved from a database.

ProcedureTo Extend the XMLProvider

  1. Develop the CustomXMLProvider class file and override the getXML() method in the CustomXMLProvider’s class file.

    The CustomXMLProvider class file (see below) extends the XMLProvider and includes only the overriding getXML() method.


    CustomXMLProvider.java File
    package custom;
    
    import com.sun.portal.providers.ProviderException;
    import com.sun.portal.providers.context.ProviderContext;
    import com.sun.portal.providers.xml.XMLProvider;
    
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    public class CustomXMLProvider extends XMLProvider {
        protected StringBuffer getXML(
        HttpServletRequest req,HttpServletResponse res
        ) throws ProviderException {
            String xmlURL = getURL();
            StringBuffer xmlbuf = new StringBuffer();
    
            // It must be http, https or file i.e http://<url>, https://<url>
            // or file:////<path>
            if (xmlURL != null && xmlURL.length() != 0) {
                String proto = xmlURL.substring(0,xmlURL.indexOf(’:’));
                if((proto.equalsIgnoreCase("http")) || (proto.equalsIgnoreCase
    				("https")) || (proto.equalsIgnoreCase("file"))) {
                    try {
                        xmlbuf = super.getXML(req, res);
                    } catch(ProviderException pr) {
                        getProviderContext().debugError("XMLProvider:getXML(): ",pr);
                    }
                    if (xmlbuf != null) {
                        return xmlbuf;
                    } else {
                        throw new ProviderException("XMLProvider.getXML(): 
    							Getting XML failed from source");
                    }
                }
            } else {
                // Open database connection
                // Read the XML contents into a stringbuffer (xmlbuf)
                // Close the database connection
            }
            return xmlbuf;
        }
    }
  2. Compile the class and put it in the provider class base directory.

    That is, build the JAR and copy the JAR into the provider class base directory. Or, just copy the class as a file into the provider class base directory. The default directory for the class file is PortalServer-DataDir/portals/portal-ID/desktop/classes. To compile the CustomXMLProvider.java file, type:

    javac -d PortalServer-DataDir/portals/portal-ID/desktop/classes -classpath PortalServer-base/sdk/desktop/desktopsdk.jar:AccessManager-base/lib/servlet.jar CustomXMLProvider.java

  3. Develop the display profile XML fragment for this provider and this provider’s channel.

    The display profile fragment for the CustomXMLProvider’s provider is saved in CustomXMLProvider.xml file and the display profile fragment for the CustomXMLProvider’s channel is saved in CustomXMLChannel.xml file.


    CustomXMLProvider.xml File
    <?xml version="1.0" encoding="utf-8" standalone="no"?>
    <!DOCTYPE DisplayProfile SYSTEM "jar://resources/psdp.dtd">
    
    <Provider name="CustomXMLProvider" class="custom.CustomXMLProvider">
    	<Properties>
    		<String name="title" value="*** XML Provider ***"/>
    		<String name="description" value="*** DESCRIPTION ***"/>
    		<String name="width" value="thick"/>
    		<String name="refreshTime" value="0" advanced="true"/>
    		<Boolean name="isEditable" value="false" advanced="true"/>
    		<String name="helpURL" value="desktop/xmlchann.htm" advanced="true"/>
    		<String name="fontFace1" value="Sans-serif"/>
    		<String name="productName" value="Sun Java System Portal Server"/>
    		<String name="url" value=""/>
    		<String name="xslFileName" value="html_stockquote.xsl"/>
    		<Integer name="timeout" value="100"/>
    		<String name="inputEncoding" value="iso-8859-1"/>
    		<String name="urlScraperRulesetID" value="default_ruleset"/>
    		<Boolean name="cookiesToForwardAll" value="true"/>
    		<Collection name="cookiesToForwardList">
    		</Collection>
    	</Properties>
    </Provider>

    CustomXMLChannel.xml File


    <?xml version="1.0" encoding="utf-8" standalone="no"?>
    <!DOCTYPE DisplayProfile SYSTEM "jar://resources/psdp.dtd">
    
    <Channel name="CustomXML" provider="CustomXMLProvider">
    	<Properties>
    		<String name="refreshTime" value="600" advanced="true"/>
    		<String name="title" value="XML Test Channel"/>
    		<String name="description" value="This is a test of the CustomXMLProvider"/>
    		<String name="url" value=""/>
    		<String name="xslFileName" value="html_stockquote.xsl"/>
    	</Properties>
    </Channel>
  4. Use the psadmin command to upload the display profile fragments for this provider.

    For the CustomXMLProvider, use the psadmin add-display-profile sub command to upload the CustomXMLProvider.xml file and the CustomXMLChannel.xml file XML fragments in the display profile. For more information on this sub command, see the Sun Java System Portal Server 7.1 Command Line Reference.

  5. Include the new channel in one of the existing containers.

    The CustomXMLProvider channel will be displayed in JSPTableContainer. To add the channel in the JSPTableContainer from the administration console, follow instructions in the Portal Server administration console online help. To add the channel in the JSPTableContainer manually:

    1. Develop the CustomXMLProvider channel XML fragment (in the CustomXMLContainer.xml file) for the JSPTableContainer.

      The CustomXMLProvider container fragment is shown below.


      CustomXMLContainer.xml File
      <?xml version="1.0" encoding="utf-8" standalone="no"?>
      <!DOCTYPE DisplayProfile SYSTEM "jar://resources/psdp.dtd">
      
      <Container name="JSPTableContainer" provider="JSPTableContainerProvider">
      	<Properties>
      		<Collection name="Personal Channels">
      			<String value="CustomXML"/>
      		</Collection>
      		<Collection name="channelsRow">
      			<String name="CustomXML" value="3"/>
      		</Collection>
      	</Properties>
      	<Available>
      			<Reference value="CustomXML"/>
      	</Available>
      	<Selected>
      			<Reference value="CustomXML"/>
      	</Selected>
      	<Channels>
      	</Channels>
      </Container>
    2. Use the psadmin modify-display-profile sub command to add the channel to a container.

      If you do not specify a parent object with the -p option, the channel is added at the root level.

  6. Change directories to PortalServer-DataDir/portals/portal-ID/desktop/classes and copy the properties file for the provider into this directory.

    For example, for the CustomXMLProvider:

    1. Change directories to PortalServer-DataDir/portals/portal-ID/desktop/classes.

    2. Type cp XMLProvider.properties PortalServer-DataDir/portals/portal-ID/desktop/classes/CustomXMLProvider.properties.

  7. Login to the Desktop and specify the URL in your browser to access the CustomXMLProvider channel inside the JSPTableContainer.


    http://hostname:port/portal-ID/dt?provider=JSPTableContainer