Return-Path: Delivered-To: apmail-xml-cocoon-users-archive@xml.apache.org Received: (qmail 5822 invoked by uid 500); 8 Feb 2002 14:37:33 -0000 Mailing-List: contact cocoon-users-help@xml.apache.org; run by ezmlm Precedence: bulk list-help: list-unsubscribe: list-post: Reply-To: cocoon-users@xml.apache.org Delivered-To: mailing list cocoon-users@xml.apache.org Received: (qmail 5803 invoked from network); 8 Feb 2002 14:37:32 -0000 From: "Joseph Jupin" Subject: AW: Having Servlet call Cocoon and then capturing results... To: X-Mailer: CommuniGate Pro Web Mailer v.3.5.1 Date: Fri, 08 Feb 2002 06:37:13 -0800 Message-ID: MIME-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1"; format="flowed" Content-Transfer-Encoding: 8bit X-Spam-Rating: daedalus.apache.org 1.6.2 0/1000/N 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.) 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. To unsubscribe, e-mail: For additional commands, e-mail: