Using the Web Publishing API
Table of Contents | Previous | Next |

Web Publishing Client API Guide


Chapter 2
Using the Web Publishing API

This chapter has the following sections:

The Main Classes

The Web Publishing API has three primary classes for manipulating resources, several utility classes, and one interface.

The primary classes for manipulating resources are:

The utility classes WPAttributeValue, WPAuthentication, WPIndexResource, and WPVersionInfo store particular information about a resource.

The WPStatus interface stores return information from all methods of the WebPubResource, WebPubContainer, and WebPubComponent objects.

To manipulate a physical resource, you create a resource object of the appropriate type, based on the URL of the physical resource you wish to manipulate. You may also need an authentication string to allow access to the resource. Once you have an appropriate object, you can use the methods of that resource object to perform actions on the associated physical resource. The object exists only as long as the application that created it. The physical resource exists independently of this object.

You can think of a resource object as a "handle" for the physical resource that exists on the remote server. This is analogous to the notion of a file handle in a file system.

You should remember this distinction between the physical resource and the resource object you use to manipulate that resource. The resource object is ephemeral; you create it to perform specific manipulations on the physical resource with which it is associated. The existence of the physical resource is separate from that of the object you use to manipulate it. For example, creating a container object does not automatically create the corresponding physical container. To create the physical container, you must call the container object's create method.

Some of the actions you can perform on physical resources using resource objects are:

For a complete list of actions, see the API reference for the appropriate class:

Preparing to Use the Classes

To use the Web Publishing classes, you must have installed Enterprise Server 3.x, you must have installed the Web Publisher classes locally, and you must have enabled Web Publishing.

To enable Web Publishing in the Enterprise Server Administration interface, go to the Web Publishing page and set the Web Publishing state to on. For detailed instructions, refer to your server Administrator's Guide.

For information on installing the Web Publisher classes, see "Installing the Web Publisher Classes Locally". (Installing the Web Publisher classes is a one-time task usually done by the Enterprise Server administrator.)

The Web Publishing classes you use with Navigator 3.01 and later can be found on the server at:

$nshome/plugins/content_mgr/client/WebPubProtocol.jar
where $nshome is the directory in which you installed the server. You can use these classes as part of a Java application or Java applet. These classes work with JDK 1.02 (or later). The examples in "Programming Examples" are valid both for applets and applications.

NOTE: If you are using an earlier version of Navigator (that is, earlier than Navigator 3.01), the Web Publishing classes are on the server at $nshome/plugins/content_mgr/ client/WebPubArchive30.zip. Note that this zip file does not contain all the Web Publisher problem solutions that have been resolved since the initial product release. To get all recent solutions, you must use the jar file.

In a Java Application

To use the Web Publishing classes as part of a Java application, copy WebPubProtocol.jar (or WebPubArchive30.zip) to a directory of your choosing on your client machine (that is, the machine that will run the Java application). Once you've put this file in an appropriate directory, change your CLASSPATH variable to include it. For example, if you copy the file to the c:\javastuff directory, add the following to your CLASSPATH:

c:\javastuff\WebPubProtocol.jar
Or (for older browsers only):

c:\javastuff\WebPubArchive30.zip

In a Java Applet

When compiling a Java applet that uses the Web Publishing classes, the WebPubProtocol.jar file must be in the compiler's CLASSPATH. To ensure this is true, either copy the WebPubProtocol.jar file to a path that is already in the compiler's CLASSPATH, or add the following directory to the CLASSPATH:

$nshome/plugins/content_mgr/client/WebPubProtocol.jar
When you put the compiled class in the root directory (or a a subdirectory thereof) on the Enterprise Server, there is no need to copy the WebPubProtocol.jar file over too.

To run a Java applet that uses Web Publishing classes, you must use Netscape Navigator 3.01 (or a later version). Earlier versions of the Navigator browser cannot access your local disk.

With Navigator 3.01, you need to install the Web Publisher plugin and the older WebPubArchive30.zip file before the server can run applets that use the Web Publishing classes. With Navigator 4.0 (or later), you do not need to install this plugin.

To install the Web Publisher Plugin, go to the Enterprise Server 3.x home page for your installation. From that page, follow the link to the Web Publisher home page. On the Web Publisher home page, follow the instructions to download and install the Web Publisher Plugin on your client. (Before you can access the Web Publisher home page, you must have enabled Web Publishing through the Enterprise Administration interface.)

Troubleshooting

After copying a compiled applet to the root directory (or a subdirectory of the root directory), if the applet cannot find the Web Publishing classes, it may be because the classes have not been installed locally.

Installing the Web Publisher Classes Locally
To install the Web Publisher Java classes, follow these steps:

  1. In Communicator, choose Open Page from the File menu.
  2. Click Choose File and navigate to the following path:
  3. $nshome/plugins/content_mgr/client/bin 

  4. Change the dialog box to display all files, not just HTML files, and double-click WPCommLC-ALL.jar.
  5. Click Open to open the file in the Navigator browser window.
  6. In the Java Security dialog box that appears, click Grant.
  7. In the SmartUpdate dialog box that appears, click Install.
  8. The Installation Complete dialog box appears. Click OK.

Programming Examples

This section provides simple examples of using the Web Publishing classes.

Copying a Resource

Since copying is available to all resources, the copy method is defined for WebPubResource. If the only thing you want to do with the original resource is to copy it, you can create a WebPubResource object to perform the copy. Alternatively, if you want to do something else to the original resource that is specific to the type of resource (such as editing or locking a component or indexing a container), you can create a WebPubComponent or WebPubContainer object instead and use its copy method.

In this example, the client application creates a WebPubResource object to copy the resource (in this case, a file) at:

http://aceindustry/draft/apidoc 
to a new location at:

http://aceindustry/release/apidoc.htm.
In this case, the user gilligan with password 3HourTour is known to the Enterprise Server either through an associated Directory Server or because he has already been added as a user to the server.

The example code has steps which are explained in a discussion following the code.

// java.net.URL provides the URL class
// netscape.net.* provides WPAuthentication and WebPubResource classes
import java.net.URL;
import netscape.net.*;
/* Step 1. Create an authentication object.*/
WPAuthentication auth = new WPAuthentication ("gilligan", "3HourTour");
/* Step 2. Get the URLs for the originating and destination locations.*/
URL origURL=null;
URL destURL=null;
try
{
   origURL = new URL ("http://aceindustry/draft/apidoc");
   destURL = new URL("http://aceindustry/release/apidoc");
}
catch(Exception e)
{
   System.out.println("Exception while creating URL: "+e.toString());
   return;
}
/* Step 3. Create a resource object. */
WebPubResource resource = new WebPubResource (origURL, auth);
/* Step 4 Copy the resource.*/
WPStatus status = resource.copy (destURL);
/* Step 5. Check the status of the copy operation. */
if (status.getStatusCode() == 200)
   System.out.println("Copied " + origURL + " to " + destURL);
else
   System.out.println("Copied failed because: " +
      status.getStatusString());

Code Explanation

  1. Before you can create a resource object, you should create an authorization object for it, specifying the user and password authorized to manipulate that resource. The user must be known to the Enterprise Server. In this example, the user ID for this operation is "gilligan" and the password is "3HourTour". This step creates a WPAuthentication object used to establish authenticated access to the server on behalf of the client.
  2. WPAuthentication auth = new WPAuthentication ("gilligan", "3HourTour");

  3. You also need URL objects corresponding to the original and new locations for the resource. You must create the URL objects inside a try block in case an exception is signalled when creating them.
  4. URL origURL=null;
    URL destURL=null;
    try
    {
       origURL = new URL ("http://aceindustry/draft/apidoc");
       destURL = new URL("http://aceindustry/release/apidoc");
    }
    catch(Exception e)
    {
       System.out.println("Exception while creating URL: "
          + e.toString());
       return;
    }

  5. Once you have the WPAuthentication object and the URL objects, create the WebPubResource object. You need to use a URL object that points to the resource and an authentication object that indicates a valid user and password.
  6. WebPubResource resource = new WebPubResource (origURL, auth);

  7. Now that the WebPubResource object exists, this step copies it. WPStatus is the return type for all methods of the WebPubResource, WebPubContainer, and WebPubComponent objects. If the client needs to know the return status of the copy call, it can invoke the getStatusCode or getStatusString methods of the WPStatus object.
  8. WPStatus status = resource.copy (destURL);

  9. This step prints a message in the Java console window indicating whether the copy operation succeeded or not. The status code for a successful operation is usually 200. The getStatusString method of a WPStatus object returns a string explaining the status, such as "OK" if the operation was successful, or "Unauthorized" if the operation failed because the client was not authorized to perform it.
  10. if (status.getStatusCode() == 200)
       System.out.println("Copied " + origURL + " to " + destURL);
    else
       System.out.println("Copied failed because: " +
       status.getStatusString());

Editing a Resource

The Web Publishing classes provide a mechanism for downloading a resource to a file or buffer, and for uploading the changed contents of the file or buffer back to the resource. You can use other Java classes to change the content of files and streams.

To download a resource, you need an authorization object. If the resource is under version control, you also need a version information object that indicates which version to download and whether or not to lock the resource on the server so that other users cannot edit it at the same time.

In this example, the client application locks and downloads a resource under version control at http://aceindustry/release/apidoc.htm.

The example code has steps which are explained in a discussion following the code.

// java.net.URL provides the URL class
// netscape.net.* provides WPAuthentication and WebPubResource classes
// java.io.File provides the File class
import java.net.URL;
import netscape.net.*;
import java.io.File;
/* Step 1. Create the authentication object. */
WPAuthentication auth = new WPAuthentication ("gilligan", "3HourTour");
/* Step 2. Create the version info object. */
WPVersionInfo versionInfo = new WPVersionInfo ("head", null,
   WPVersionInfo.locked, null);
/* Step 3. Create the URL for the component. */
URL url;
try
{
   url = new URL ("http://aceindustry/release/apidoc");
}
catch(Exception e)
{
   System.out.println("Exception while creating URL: "+e.toString());
   return;
}
/* Step 4. Create the WebPubComponent object. */
WebPubComponent component = new WebPubComponent (url, auth, versionInfo);
/* Step 5. */
File file = new File ("some Filename");
WPStatus status = component.edit (file);
/* Step 6. Edit the file. */
// Make changes to the content of the file.
// Code omitted here.
/* Step 7. Upload the changed content. */
component.save(file);

Code Explanation

  1. Once again, before you can create a resource object, you must have an authorization object for it, specifying the user and password authorized to manipulate that resource.
  2. WPAuthentication auth = new WPAuthentication ("gilligan", "3HourTour");

  3. A WPVersionInfo object contains version information, such as which version of a resource to get. You specify several parameters when you create a WPVersionInfo object.
  4. WPVersionInfo versionInfo = new WPVersionInfo ("head", null, WPVersionInfo.locked, null);

  5. As with the previous example, create a URL object inside a try block in case an error occurs creating the object.
  6. URL url;
    try
    {   url = new URL ("http://aceindustry/release/apidoc");
    }
    catch(Exception e)
    {   System.out.println("Exception while creating URL: "
          + e.toString());
       return;
    }

  7. Once you have created the authorization and version information objects, you can create the WebPubComponent object. The url argument is the URL for the component. The auth argument is an authentication object indicating a valid user and password. The versionInfo argument is a version info object indicating which version of the component we are interested in.
  8. WebPubComponent component = new WebPubComponent (url, auth, versionInfo);

  9. You can now invoke the edit method. This method downloads the component to a file or buffer, and locks it if appropriate. Here we download to a file. The file parameter is an object of class File which refers to the local file on the client in which to put a copy of the resource while editing it. The file must exist before the download occurs. Here we create a new file.
  10. File file = new File ("someFilename");
    WPStatus status = component.edit (file);

  11. Edit the file as desired.
  12. // Make changes to the content of the file.
    // Code omitted here.

  13. Save the changes back to the server. The save method saves the content of the indicated file or buffer (in this case it is a file) back to the component on the server. When the component was created, it was given a version info object indicating which version it pertains to. The save method saves the content back to that version. If the component was locked, it is unlocked after the save operation.
  14. component.save(file);

Getting the Versions of a Resource

This example prints all revision numbers for a particular resource. In this example, the server name is MyServer and the path of the versioned file is help.htm in the directory apps. The user gilligan with password 3HourTour is known to the Enterprise Server.

The example code has steps which are explained in a discussion following the code.

import netscape.net.*;
import java.net.*;
import java.util.*;
/* Step 1. Create the authentication object */
String userID = new String ("gilligan");
String password = new String ("3HourTour");
WPAuthentication auth = new WPAuthentication (userID, password);
/* Step 2. Get the URL for the component */
URL url;
try
{
   url = new URL ("http://MyServer/apps/help.htm");
} 
catch(Exception e)
{
   System.out.println("Exception while creating URL:
      "+e.toString());
   return;
}
/* Step 3. Create a WebPubComponent object for the component */
WebPubComponent component = new WebPubComponent (url, auth);
/* Step 4. Get the component's revision numbers. */
Vector vec = new Vector();
WPStatus status = component.revNum (vec);
/* Step 5. If the component has revision numbers, print them. */
if (status.getStatusCode() == 200)
{   Enumeration enum = vec.elements();
   System.out.println("The component at " + url +
      " has the following version numbers: ");
   while (enum.hasMoreElements())
   {   System.out.println ("Rev Number: " + enum.nextElement());
   }
}
else
{   System.out.println("The component at " + url +
         " is not under version control.");
}

Code Explanation

  1. Get the authentication object.
  2. String userID = new String ("gilligan");
    String password = new String ("3HourTour");
    WPAuthentication auth = new WPAuthentication (userID, password);

  3. Get the URL for the component.
  4. URL url;
    try
    {   
       url = new URL ("http://MyServer/apps/help.htm");
    }
    catch(Exception e)
    {
       System.out.println("Exception while creating URL:
          "+e.toString());
       return;
    }

  5. Get a WebPubComponent object that represents the component.
  6. WebPubComponent component = new WebPubComponent (url, auth);

  7. Get a Vector object that holds the version numbers. If the component is under version control, the revNum method puts the version numbers in the vector and sets the status to 200 (OK). If the component is not under version control, there are no revision numbers and the status is set to some other value such as 404 (Not Found).
  8. Vector vec = new Vector();
    WPStatus status = component.revNum (vec);

  9. Test the status code to see if it is 200 (OK) or not. If so, print the revision numbers. If not, print a message indicating that this component is not under version control.
  10. if (status.getStatusCode() == 200)
    {   Enumeration enum = vec.elements();
       System.out.println("The component at " + url +
          " has the following version numbers: ");
       while (enum.hasMoreElements())
       {   System.out.println ("Rev Number: " + enum.nextElement());
       }
    }
    else
    {   System.out.println("The component at " + url +
          " is not under version control.");
    }

Table of Contents | Previous | Next | Index

Last Updated: 03/10/99 13:42:01