You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@cocoon.apache.org by Martin Kalén <ma...@curalia.se> on 2003/10/13 10:57:19 UTC

[newbie] UrlExistsSelector

Greetings,
  I was under the impression that the selectors included in the standard 
Cocoon distro was somewhat dev-related, but after reading up a bit on 
the lists I guess this is 100% users@cocoon material. Sorry for the 
incorrect post, I'd be happy if someone could give me some feeback here 
instead.

--- original post follows ---

  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?
TIA,
  Martin

-- 
Martin Kalén
Curalia AB              Web:  http://www.curalia.se
Orrspelsvägen 2B        Mail: info@curalia.se
SE-182 79  Stocksund    Tel:  +46-8-410 064 40

Re: [newbie] UrlExistsSelector

Posted by Geoff Howard <co...@leverageweb.com>.
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 {@link org.apache.cocoon.selection.ResourceExistsSelector}, * but 
> works with |java.net.URL| instead of using the servlet * containter's 
> context resolving. * * @author Martin Kalén 
> <ma...@curalia.se> * @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: users-unsubscribe@cocoon.apache.org
> For additional commands, e-mail: users-help@cocoon.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@cocoon.apache.org
For additional commands, e-mail: users-help@cocoon.apache.org