Re: [newbie] UrlExistsSelector

2003-10-13 Thread Martin Kalén
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% [EMAIL PROTECTED] material. Sorry for the 
incorrect post, I'd be happy if someone could give me some feeback there 
instead.

--
Martin Kalén
Curalia AB  Web:  http://www.curalia.se
Orrspelsvägen 2BMail: [EMAIL PROTECTED]
SE-182 79  StocksundTel:  +46-8-410 064 40


[newbie] UrlExistsSelector

2003-10-10 Thread Martin Kalén
Greetings developers,

 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:
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 2BMail: [EMAIL PROTECTED]
SE-182 79  StocksundTel:  +46-8-410 064 40
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
 * @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 = "" 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;
}
}

}