Sun Java System Portal Server 7 Developer's Guide

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

Steps
  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 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 subcommand 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. Access the CustomURLScraperProvider channel inside the JSPTableContainer. To access, log in to the Desktop and type the following URL in your browser:


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