URLContent Reusuable Component

URLContent Reusuable Component

This project demonstrates the techniques required for reading content from another web site and displaying it on a WebObjects page. As a useful bonus, a reusable component for doing this, taking only a URL binding, is provided.

It is commonly accepted wisdom in the WebObjects mailing lists that WOHTTPConnection is the solution to this problem. However, as you can see from the code, this is actually more complex to implement than using the standard Java Url class. Where WOHTTPConnection will be of use is in the specific situation it was intended for: distributing requests to multiple WebObjects application servers. Having ready built WORequests, and a ready use for WOResponses, will be where this class is of use.

The UrlContent componment follows standard rules for creating reusable components: the html is deliberately minimised. To use it, just include it in a component in the usual way, and give it a url binding; this can be any valid url, and is most effective to act as a kind of include mechanism that can work across sites. Or to include an entire page from another site in your page. I haven't given it any facilities to extract substrings from the extracted html, although this would be a useful addition.

The contentString() method calls either the getHHTPString (uses WOHTTPConnection) or getURLString (uses URL) methods; comment out whichever you don't need.

getURLString

Creates a new URL instance for the passed url string, then opens a BufferedReader on it, and loops to the end of input, accumulating input into a string. That's all.

getHTTPString

WOHTTPConnection requires the host, port and url strings to be separated, so I had to write a simple parser for url strings to break them down as required; this alone needed more lines of code than the URL method above. I store and return the parsed values in a dictionary.

I open a new WOHTTPConnection with the parsed information, then create a WORequest from the url. This has to be sent to the connection, and a WOResponse is read back. The content string can then be returned.

Download source code for this project.