Martin Kal�n wrote:

...

as a newbie to the wonderful world of Cocoon sitemaps I was struggling with the problem of using a selector to determine if an external HTTP URL was available.


* Problem description:


A pipeline matcher with the following steps is triggered:
1. XSP generation
2. cinclude transformation
3. XSLT transformation
4. HTML serialization

The cinclude in step 2 triggers another cocoon pipeline. XSP snippet:
<ci:include src="cocoon:/(name of pipeline #2)" xmlns:ci="http://apache.org/cocoon/include/1.0"/>


Pipeline #2:
1. (what I wanted) map:select and generation based on status of external HTTP URL
2. XSLT transformation
3. XML serialization



* My soloution:


I was first trying the map:select with the org.apache.cocoon.selection.ResourceExistsSelector until I found out that this was only for content within the current servlet context.

I then wrote the attached (trivial) UrlExistsSelector and used this one for step 1 in pipeline #2.

This works, but might have som implications in security/performance (no timeout handling) etc. etc. that I just don't see because I'm new to Cocoon.


Any comments? Other soloutions?

Hmm. You may want to see if the http-client project used elsewhere by Cocoon has some optimized features you could use. Also, it'd be nice to be able to use the connection created in your selector to acquire the remote content. To do this, you could try placing the connection or other useful object into the request as a request attribute (I think this holds general objects but wouldn't swear to it right now). You'd then need a specialized (simple) generator to look up the object from the request attributes and go on its way.


If you are using Cocoon 2.1 this may be a good candidate for using flow. With it you can go about your business of establishing the connection and retrieving the content and if something goes wrong, just do sendPage() for your alternative result.

------------------------------------------------------------------------

import org.apache.avalon.framework.logger.AbstractLogEnabled; import org.apache.avalon.framework.parameters.Parameters; import org.apache.avalon.framework.thread.ThreadSafe; import org.apache.cocoon.selection.Selector; import java.net.MalformedURLException; import java.net.URL; import java.net.URLConnection; import java.util.Map; /** * Selects the first of a set of URLs that can be connected to. *

* Like [EMAIL PROTECTED] org.apache.cocoon.selection.ResourceExistsSelector}, * but works with |java.net.URL| instead of using the servlet * containter's context resolving. * * @author Martin Kal�n <mailto:[EMAIL PROTECTED]> * @version CVS $Id: UrlExistsSelector.java,v 1.1 2003/10/10 09:08:47 martin Exp $ */ public class UrlExistsSelector extends AbstractLogEnabled implements ThreadSafe, Selector { public boolean select(String expression, Map objectModel, Parameters parameters) { try { URL url = new URL(expression); URLConnection connection = url.openConnection(); connection.connect(); return true; } catch (MalformedURLException e) { StringBuffer message = new StringBuffer(); message.append("Selector expression '"); message.append(expression); message.append("' is not a valid URL"); getLogger().warn(message.toString()); return false; } catch (Exception e) { return false; } } }


------------------------------------------------------------------------


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]



Reply via email to