cocoon-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From o...@kompuart.pl (Leszek Gawron)
Subject Re: XML data via POST request
Date Tue, 20 Aug 2002 15:34:32 GMT
On Tue, Aug 20, 2002 at 01:33:49PM +0200, Boscoe wrote:
>  Hi all,
> 
> I am writing this because the archive isn't accessible at the moment, so 
> I can' t reply to my original mail.
> 
> A while ago, I had the problem that I wanted to send XML data via POST 
> to a XSP. I received some interesting hints from Vadim Gritschenko 
> (thanks again, Vadim), but in the end, nothing seemed to solve my 
> problem, at least in a simple, obvious way. So, here is what I found out:
> 
> Actually, the solution is very simple. I looked for a way to directly 
> access the input stream which is associated with a HttpServletRequest. 
> This can be done via
> 
> public Map act (Redirector redirector,
>            SourceResolver resolver,
>            Map objectModel,
>            String source,
>            Parameters params)
> 
> which is a method of org.apache.cocoon.acting.AbstractAction. Using this 
> approach, the HttpServletRequest instance is accessible via
> 
> objectModel.get(HttpEnvironment.HTTP_REQUEST_OBJECT);
> 
> I developed my own action, passed the request object the

What do you need the action for ? The way I do it is "pure" xsp, an example
below.  I do not quite know though how to make it reusable and put it into
logicsheet so I do not have to make xsp:includes and so I could do:
Document document = <myutils:get-request-body-as-DOM/>;

Could someone help me ?


<?xml version="1.0" encoding="ISO-8859-1"?>
<xsp:page language="java"
          xmlns:xsp="http://apache.org/xsp"
          xmlns:esql="http://apache.org/cocoon/SQL/v2">
<xsp:structure>
	<xsp:include>java.net.*</xsp:include>
	<xsp:include>org.w3c.dom.*</xsp:include>
	<xsp:include>org.apache.cocoon.xml.dom.DOMStreamer</xsp:include>
	<xsp:include>org.apache.cocoon.xml.dom.DOMBuilder</xsp:include>
	<xsp:include>org.apache.cocoon.components.parser.Parser</xsp:include>
	<xsp:include>org.apache.cocoon.util.PostInputStream</xsp:include>
	<xsp:include>org.apache.cocoon.environment.http.HttpEnvironment</xsp:include>
	<xsp:include>javax.servlet.http.HttpServletRequest</xsp:include>
	<xsp:include>org.apache.xpath.XPathAPI</xsp:include>
	<xsp:include>javax.xml.transform.TransformerException</xsp:include>
</xsp:structure>
<xsp:logic>
	Document document = null;
	public Document GetInputStreamDocument() throws IOException {
		// > Node node = XPathAPI.selectNode(parser.parse(request.getParameter("xmlparam"), "kazoo");
    	int contentLength = request.getContentLength();
    	if ( contentLength >= 0 ) {
	    	HttpServletRequest request =
	    			(HttpServletRequest) objectModel.get( HttpEnvironment.HTTP_REQUEST_OBJECT );

	    	PostInputStream anStream = new PostInputStream( request.getInputStream(), contentLength
);
	    	InputSource contentSource = new InputSource( anStream );

			Parser newParser = null;
			DOMBuilder builder = null;
			try {
				newParser = (Parser) this.manager.lookup(Parser.ROLE);
				document = newParser.newDocument();
				builder = new DOMBuilder(newParser);
				newParser.setContentHandler(builder);
				newParser.setLexicalHandler(builder);
				newParser.parse( contentSource );
				
			} catch (Exception e) {
				// ignore - return null
			} finally {
				this.manager.release((Component) newParser);
			}
			return builder.getDocument();
    	}
    	return null;
	}
</xsp:logic>
<content>
	<xsp:logic>
	document = GetInputStreamDocument();
	try {
		<id><xsp:expr>XPathAPI.selectSingleNode(document, "/Orders/OrderID").getFirstChild().getNodeValue()</xsp:expr></id>
   	} catch ( TransformerException e ) {
   	} catch ( DOMException e ) {
   	}
	</xsp:logic>
</content>
</xsp:page>

-- 
            __
         | /  \ |        Leszek Gawron            //  \\
        \_\\  //_/      ouzo@vip.net.pl          _\\()//_
         .'/()\'.     Phone: +48(600)341118     / //  \\ \
          \\  //  recursive: adj; see recursive  | \__/ |


---------------------------------------------------------------------
Please check that your question  has not already been answered in the
FAQ before posting.     <http://xml.apache.org/cocoon/faq/index.html>

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


Mime
View raw message