Hey, All,
Since I posted that response of having a serlvet talk to
cocoon and capture the results, I've had several requests
on how to do this. First off, let me give the reason why
we chose this way:
We have a state machine that is sending xml documents of
activities across the net. The result can be renderer in
either xml, wml, pager-format or even XUL for swing. The
issue for is that we wanted just a fronting servlet to
handle the request that we all talk to and then separate
the transformations (using cocoon) into it's own separate
process. That why, since the xml is dynamic, we can
easily switch on parameters, etc.
So, on to how I solved this:
first off, I have a sub-sitemap and in it, I have the
following pipeline segment. Notice that my servlet passes
in several parameters, but for now, I'm only interested in
the "xml" parameter and "style" parameter. (this assumes
that you set up the request selector as shown in the
cocoon sitemap.xmap section of selectors.)
<!-- original zodiac translator matcher... -->
<map:match pattern="translate">
<map:generate type="stream">
<map:parameter name="form-name" value="xml"/>
</map:generate>
<map:select type="request">
<map:parameter name="parameter-name"
value="style"/>
<map:when test="fancy">
<map:transform src="stylesheets/fancy.xsl" />
</map:when>
<map:otherwise>
<map:transform src="stylesheets/generic.xsl" />
</map:otherwise>
</map:select>
<map:serialize type="html"/>
</map:match>
So, what does this say? It states that take whatever is
streamed in as the xml parameter and then select the
transform based on the "style" parameter and serialize the
results as html.
In my servlet, I have the following (which calls cocoon -
the results from cocoon are then sent to the httpResponse
writer (note: I have a sub-site called "zodiac" - and no,
I haven't mapped the root of cocoon yet in tomcat - we're
still in development and that will come later when we go
production)...
StringBuffer uri = new
StringBuffer("http://localhost:8080/cocoon/zodiac/request1");
BufferedReader br = null;
try {
//
// setup my url connection stuff to be sent to
cocoon...
//
URL miurl = new URL(uri.toString());
URLConnection con = miurl.openConnection();
con.setDoOutput(true);
con.setDoInput(true);
con.setUseCaches(false);
con.setRequestProperty("Content-
type","application/x-www-form-urlencoded ;
charset=ISO-8859-1");
String params = "&mode=" + mode + "&style=" +
style + "&lang=" + lang + "&action=" + action;
//
// note: here, s is passed in as the string of
// of xml data (properly formed).
// Now, you need to figure the length of
// of the data being sent since the
stream
// won't be able to figure out how to
stop
// reading for parameters! <--- VERY
// important!!!
//
String length =
Integer.toString((URLEncoder.encode(s.toString())+
params).length());
// computed my length, now, set that request
// property and start writing to the stream!
con.setRequestProperty("Content-length",
length);
DataOutputStream dos = new
DataOutputStream(con.getOutputStream());
dos.writeBytes("xml=" +
URLEncoder.encode(s.toString()) + params);
dos.close();
sb = new StringBuffer();
// done.
// read any results.
// and handle them accordingly in your
results.
// I just capture it all in a stringBuffer
// and then do any manipulation in other
// methods...
//
InputStreamReader isr = new
InputStreamReader(con.getInputStream());
br = new BufferedReader(isr);
String aline;
while ((aline=br.readLine()) != null)
sb.append(aline);
br.close();
} catch (MalformedURLException mfe) {
System.out.println("mfe: " + mfe.toString());
} catch (IOException ioe) {
System.out.println("ioe: " + ioe.toString());
} catch (Exception e) {
System.out.println("e: " + e.toString());
}
return sb;
The returned string buffer (sb) is then sent to the writer
for output - it works great as a fronting servlet that I
can change paremeters to and get dynamic xml & html files
back from.
hope this helps...
peace. JOe...
---------------------------------------------------------------------
Please check that your question has not already been answered in the
FAQ before posting. <http://xml.apache.org/cocoon/faqs.html>
To unsubscribe, e-mail: <[EMAIL PROTECTED]>
For additional commands, e-mail: <[EMAIL PROTECTED]>