pier 2003/02/18 08:21:40
Modified: src/blocks/proxy/java/org/apache/cocoon/generation
HttpProxyGenerator.java
Log:
More javadocs.
Private methods moved at the bottom.
Better exception handling in generate().
Revision Changes Path
1.3 +107 -55
xml-cocoon2/src/blocks/proxy/java/org/apache/cocoon/generation/HttpProxyGenerator.java
Index: HttpProxyGenerator.java
===================================================================
RCS file:
/home/cvs/xml-cocoon2/src/blocks/proxy/java/org/apache/cocoon/generation/HttpProxyGenerator.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- HttpProxyGenerator.java 18 Feb 2003 14:47:43 -0000 1.2
+++ HttpProxyGenerator.java 18 Feb 2003 16:21:39 -0000 1.3
@@ -62,6 +62,7 @@
import org.apache.avalon.framework.parameters.Parameters;
import org.apache.cocoon.ProcessingException;
+import org.apache.cocoon.ResourceNotFoundException;
import org.apache.cocoon.environment.SourceResolver;
import org.apache.commons.httpclient.HttpConnection;
@@ -82,7 +83,7 @@
/**
* The <code>HttpProxyGenerator</code> is a Cocoon generator using the
- * <b>Jakarta Commons HTTPClient Library</code> to access an XML stream
+ * <b>Jakarta Commons HTTPClient Library</b> to access an XML stream
* over HTTP.
*
* @version CVS $Revision$
@@ -104,62 +105,19 @@
private boolean debug = false;
/**
- * Prepare a map of parameters from an array of <code>Configuration</code>
- * items.
- *
- * @param configurations An array of <code>Configuration</code> elements.
- * @return A <code>List</code> of <code>NameValuePair</code> elements.
- * @exception ConfigurationException If a parameter doesn't specify a name.
+ * Default (empty) constructor.
*/
- private ArrayList getParams(Configuration configurations[])
- throws ConfigurationException {
- ArrayList list = new ArrayList();
-
- if (configurations.length < 1) return (list);
-
- for (int x = 0; x < configurations.length; x++) {
- Configuration configuration = configurations[x];
- String name = configuration.getAttribute("name", null);
- if (name == null) {
- throw new ConfigurationException("No name specified for parameter
at "
- + configuration.getLocation());
- }
-
- String value = configuration.getAttribute("value", null);
- if (value != null) list.add(new NameValuePair(name, value));
-
- Configuration subconfigurations[] = configuration.getChildren("value");
- for (int y = 0; y < subconfigurations.length; y++) {
- value = subconfigurations[y].getValue(null);
- if (value != null) list.add(new NameValuePair(name, value));
- }
- }
-
- return (list);
+ public HttpProxyGenerator() {
+ super();
}
/**
- * Override the value for a named parameter in a specfied <code>ArrayList</code>
- * or add it if the parameter was not found.
+ * Set up this <code>Generator</code> instance from its sitemap
<code>Configuration</code>
*
- * @param list The <code>ArrayList</code> where the parameter is stored.
- * @param name The parameter name.
- * @param list The new parameter value.
- * @return The same <code>List</code> of <code>NameValuePair</code> elements.
+ * @param configuration The base <code>Configuration</code> for this
<code>Generator</code>.
+ * @throws ConfigurationException If this instance cannot be configured
properly.
+ * @see #recycle()
*/
- private ArrayList overrideParams(ArrayList list, String name, String value) {
- Iterator iterator = list.iterator();
- while (iterator.hasNext()) {
- NameValuePair param = (NameValuePair) iterator.next();
- if (param.getName().equals(name)) {
- iterator.remove();
- break;
- }
- }
- list.add(new NameValuePair(name, value));
- return (list);
- }
-
public void configure(Configuration configuration)
throws ConfigurationException {
@@ -190,6 +148,19 @@
this.qryParams = this.getParams(configuration.getChildren("query"));
}
+ /**
+ * Setup this <code>Generator</code> with its runtime configurations and
parameters
+ * specified in the sitemap, and prepare it for generation.
+ *
+ * @param sourceResolver The <code>SourceResolver</code> instance resolving
sources by
+ * system identifiers.
+ * @param objectModel The Cocoon "object model" <code>Map</code>
+ * @param parameters The runtime <code>Parameters</code> instance.
+ * @throws ProcessingException If this instance could not be setup.
+ * @throws SAXException If a SAX error occurred during setup.
+ * @throws IOException If an I/O error occurred during setup.
+ * @see #recycle()
+ */
public void setup(SourceResolver sourceResolver, Map objectModel,
String source, Parameters parameters)
throws ProcessingException, SAXException, IOException {
@@ -269,6 +240,14 @@
this.debug = parameters.getParameterAsBoolean("debug", false);
}
+ /**
+ * Recycle this instance, clearing all done during setup and generation, and
reverting
+ * back to what was configured in the sitemap.
+ *
+ * @see #configure(Configuration)
+ * @see #setup(SourceResolver, Map, String, Parameters)
+ * @see #generate()
+ */
public void recycle() {
/* Recycle the method */
this.method.recycle();
@@ -279,8 +258,16 @@
super.recycle();
}
+ /**
+ * Parse the remote <code>InputStream</code> accessed over HTTP.
+ *
+ * @throws ResourceNotFoundException If the remote HTTP resource could not be
found.
+ * @throws ProcessingException If an error occurred processing generation.
+ * @throws SAXException If an error occurred parsing or processing XML in the
pipeline.
+ * @throws IOException If an I/O error occurred accessing the HTTP server.
+ */
public void generate()
- throws IOException, SAXException, ProcessingException {
+ throws ResourceNotFoundException, ProcessingException, SAXException,
IOException {
/* Do the boring stuff in case we have to do a debug output (blablabla) */
if (this.debug) {
this.generateDebugOutput();
@@ -292,12 +279,16 @@
HttpState state = new HttpState();
this.method.setFollowRedirects(true);
int status = this.method.execute(state, connection);
- if ((status < 200) || (status > 299)) {
- throw new ProcessingException("Unable to access HTTP resource at \""
+ if (status == 404) {
+ throw new ResourceNotFoundException("Unable to access \"" +
this.method.getURI()
+ + "\" (HTTP 404 Error)");
+ } else if ((status < 200) || (status > 299)) {
+ throw new IOException("Unable to access HTTP resource at \""
+ this.method.getURI().toString() + "\" (status=" + status +
")");
}
InputStream response = this.method.getResponseBodyAsStream();
+ /* Let's try to set up our InputSource from the response output stream and
to parse it */
SAXParser parser = null;
try {
InputSource inputSource = new InputSource(response);
@@ -312,8 +303,14 @@
}
}
+ /**
+ * Generate debugging output as XML data from the current configuration.
+ *
+ * @throws SAXException If an error occurred parsing or processing XML in the
pipeline.
+ * @throws IOException If an I/O error occurred accessing the HTTP server.
+ */
private void generateDebugOutput()
- throws IOException, SAXException, ProcessingException {
+ throws SAXException, IOException {
super.xmlConsumer.startDocument();
AttributesImpl attributes = new AttributesImpl();
@@ -350,6 +347,61 @@
return;
}
+ /**
+ * Prepare a map of parameters from an array of <code>Configuration</code>
+ * items.
+ *
+ * @param configurations An array of <code>Configuration</code> elements.
+ * @return A <code>List</code> of <code>NameValuePair</code> elements.
+ * @throws ConfigurationException If a parameter doesn't specify a name.
+ */
+ private ArrayList getParams(Configuration configurations[])
+ throws ConfigurationException {
+ ArrayList list = new ArrayList();
+
+ if (configurations.length < 1) return (list);
+ for (int x = 0; x < configurations.length; x++) {
+ Configuration configuration = configurations[x];
+ String name = configuration.getAttribute("name", null);
+ if (name == null) {
+ throw new ConfigurationException("No name specified for parameter
at "
+ + configuration.getLocation());
+ }
+
+ String value = configuration.getAttribute("value", null);
+ if (value != null) list.add(new NameValuePair(name, value));
+
+ Configuration subconfigurations[] = configuration.getChildren("value");
+ for (int y = 0; y < subconfigurations.length; y++) {
+ value = subconfigurations[y].getValue(null);
+ if (value != null) list.add(new NameValuePair(name, value));
+ }
+ }
+
+ return (list);
+ }
+
+ /**
+ * Override the value for a named parameter in a specfied <code>ArrayList</code>
+ * or add it if the parameter was not found.
+ *
+ * @param list The <code>ArrayList</code> where the parameter is stored.
+ * @param name The parameter name.
+ * @param list The new parameter value.
+ * @return The same <code>List</code> of <code>NameValuePair</code> elements.
+ */
+ private ArrayList overrideParams(ArrayList list, String name, String value) {
+ Iterator iterator = list.iterator();
+ while (iterator.hasNext()) {
+ NameValuePair param = (NameValuePair) iterator.next();
+ if (param.getName().equals(name)) {
+ iterator.remove();
+ break;
+ }
+ }
+ list.add(new NameValuePair(name, value));
+ return (list);
+ }
}
----------------------------------------------------------------------
In case of troubles, e-mail: [EMAIL PROTECTED]
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]