12-Oct-04 (Created: 12-Oct-04) | More in 'Howto'

How to use com.ai.parts.URLStringReaderPart

Using URLStringReader to read a relative URL

You can use this part to read the contents of a local or remote url. Here is an example properties file that reads a local url. Notice how the url line is broken for clarity. In your properties file you don't have to break this.


# USing the URLStringReader to retrieve a url as a string
#
request.GetBodyText.classname=com.ai.parts.URLStringReaderPart
request.GetBodyText.URL=\
/avada/display?url=DisplayURL&ordid={ordid}&useid1={useid1}
request.GetBodyText.resultName=bodyText

Using URLStringReader to read an absolute URL

This release of build 21 has a problem with local urls. This will be fixed in the next release. To correct this you can do the following in this release


# USing the URLStringReader to retrieve a url as a string
request.GetBodyText.classname=com.ai.parts.URLStringReaderPart

request.GetBodyText.URL=\
http://localhost:port/avada/display?\
url=DisplayURL&ordid={ordid}&useid1={useid1}

request.GetBodyText.resultName=bodyText

Notice how an absolute url is mentioned instead. You can also use your explicit host name instead of the local host if you would like. The port is needed only if you are running on a port other than 80.

Source code of URLStringReader part ver 1

package com.ai.parts;

import com.ai.application.interfaces.*;
import com.ai.application.utils.AppObjects;
import com.ai.aspire.utils.TransformUtils;
import com.ai.common.*;
import java.io.*;
import java.util.Hashtable;
import java.util.Map;
import com.ai.servletutils.*;
/**
 * Takes a URL and reads its contents as string and returns it
 *
 * Additional property file arguments
 * 1. URL=Any internal or external url
 *
 * Output
 * 1.resultName: The content at the specified url as string
 *
 */

public class URLStringReaderPart extends AFactoryPart
{
    protected Object executeRequestForPart(String requestName, Map inArgs)
        throws RequestExecutionException
    {
       String substUrlString = null;
        try
        {
        //mandatory args
            String urlString = AppObjects.getValue(requestName + ".URL");
            substUrlString = ServletUtils.getSubstitutedURLUsingAMap(urlString,inArgs);
            java.net.URL url = new java.net.URL(substUrlString);
            InputStream is = url.openStream();
            String os = FileUtils.readStreamAsString(is);
            return os;
        }
        catch(ConfigException x)
        {
            throw new RequestExecutionException("Error: ConfigException. See the embedded exception for details", x);
        }
        catch(IOException x)
        {
            throw new RequestExecutionException("Error: reading url:" + substUrlString);
        }
    }
}