You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@cocoon.apache.org by nanomonk <br...@inbox.ru> on 2008/02/16 12:32:45 UTC

POST .xml

I need to POST .xml file to server for further its processing...
so, I have a simple upload.html for test:
-----
<html>
  <body>
    <form
action="http://localhost:8888/setSettings?paramone=12345&paramtwo=blablabla"
method="post" enctype="text/xml">
      File:  <input type="file" name="uploadfile">
      <input type="submit"/>
    </form>
  </body>
</html>
-----
and I have pipeline with StreamGenerator:
-----
<map:match pattern="upload/setSettings">
	<map:generate type="stream">
		<map:parameter name="form-name" value="uploadfile"/>
		<map:parameter name="defaultContentType" value="text/xml"/>
	</map:generate>
	<map:serialize type="xml" />
</map:match>
-----
all ok and in source of
"http://localhost:8888/setSettings?paramone=12345&paramtwo=blablabla" I see
my .xml.
But.. I need to get from it some data and process it... I think to do that
in .xsp. I need to take params of request and according them to do some
actions... But I don't know how to get DOM Document from this recieved
.xml...
-- 
View this message in context: http://www.nabble.com/POST-.xml-tp15517307p15517307.html
Sent from the Cocoon - Users mailing list archive at Nabble.com.


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


Re: POST .xml

Posted by nanomonk <br...@inbox.ru>.
So, who know how to use StreamGenerator? how can I put result stream to DOM
Document? or even to String ?
Or there is another way to POST file (without saving it to disk, only to DOM
Document)?
-- 
View this message in context: http://www.nabble.com/POST-.xml-tp15517307p15540285.html
Sent from the Cocoon - Users mailing list archive at Nabble.com.


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


Re: POST .xml

Posted by so...@apache.org.
["uploadfile" is the name of the HTML file upload field.]
1. To upload a file, the field needs to be type "file":
   <input type="file" name="uploadfile"/>
Sending the filename with:
   <input type="text" name="uploadfile">
does not send the file to the server, just a string.  You can
immediately see the difference as all browsers show the "Browse"
button when the control is type "file".

2. Cocoon saves the file to the upload directory.  To read the file in
Java or XSP:
   import org.apache.cocoon.servlet.multipart.Part;
   Part part = (Part) request.get("uploadfile");
   InputStream inputStream = part.getInputStream();
Then you can use the DocumentBuilder or this method:
      DOMParser parser =
manager.lookup(org.apache.excalibur.xml.dom.DOMParser.ROLE);
      Document uploadeddoc = parser.parseDocument(inputStream);
Now you can work with an XML Document.

solprovider


On 2/19/08, nanomonk <br...@inbox.ru> wrote:
>  hmm.. I did that. But not like a file...not like a stream... just a text in
>  POST parameter.
>  pipeline:
>  <map:match pattern="upload/myxsp">
>         <map:generate src="upload/myxsp.xsp" type="serverpages"/>
>
>         <map:serialize type="xml"/>
>  </map:match>
> -----
>  for test myxsp.html:
>  <html>
>   <body>
>     <form action="http://localhost:80/upload/myxsp" method="post"
>  enctype="multipart/form-data">
>       File:  <input type="text" name="uploadfile">
>       <input type="submit"/>
>   </body>
>  </html>
>  -----
>  as example... myxsp.xsp:
>  <?xml version="1.0"?>
>  <xsp:page language="java" xmlns:xsp="http://apache.org/xsp">
>  <root>
>  <xsp:logic>
>  try {
>  String foo = request.getParameter("uploadfile");
>  System.out.println(foo);
>  javax.xml.parsers.DocumentBuilderFactory factory =
>  javax.xml.parsers.DocumentBuilderFactory.newInstance();
>  javax.xml.parsers.DocumentBuilder builder = factory.newDocumentBuilder();
>  org.w3c.dom.Document doc = builder.parse(new
>  java.io.ByteArrayInputStream(foo.getBytes()));
>  org.w3c.dom.NodeList catlist =
>  doc.getDocumentElement().getElementsByTagName("category");
>  for (int i = 0; i &lt; catlist.getLength(); i++)
>  {
>         org.w3c.dom.Node boo = catlist.item(i);
>         <xsp:content>
>                 <xsp:expr>boo</xsp:expr>
>         </xsp:content>
>  }
>  }catch(java.lang.Exception e)
>         {
>                 System.out.print(e.toString());
>         }
>  </xsp:logic>
>  </root>
>  </xsp:page>
>  -------------
>  but.......but I don't need to put xml to parameter.
>  I need to take a Content of request. But cocoon's HttpRequest hasn't
>  .getContent(). There is getInputStream, but I can't to understand how to use
>  that.

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


Re: POST .xml

Posted by nanomonk <br...@inbox.ru>.
hmm.. I did that. But not like a file...not like a stream... just a text in
POST parameter.
pipeline:
<map:match pattern="upload/myxsp">
        <map:generate src="upload/myxsp.xsp" type="serverpages"/>
        <map:serialize type="xml"/>
</map:match>

-----
for test myxsp.html:
<html>
  <body>
    <form action="http://localhost:80/upload/myxsp" method="post"
enctype="multipart/form-data">
      File:  <input type="text" name="uploadfile">
      <input type="submit"/>
  </body>
</html>
-----
as example... myxsp.xsp:
<?xml version="1.0"?>
<xsp:page language="java" xmlns:xsp="http://apache.org/xsp">
<root>
<xsp:logic>
try {
String foo = request.getParameter("uploadfile");
System.out.println(foo);
javax.xml.parsers.DocumentBuilderFactory factory =
javax.xml.parsers.DocumentBuilderFactory.newInstance();
javax.xml.parsers.DocumentBuilder builder = factory.newDocumentBuilder();
org.w3c.dom.Document doc = builder.parse(new
java.io.ByteArrayInputStream(foo.getBytes()));
org.w3c.dom.NodeList catlist =
doc.getDocumentElement().getElementsByTagName("category");
for (int i = 0; i &lt; catlist.getLength(); i++)
{
	org.w3c.dom.Node boo = catlist.item(i); 
	<xsp:content>
		<xsp:expr>boo</xsp:expr> 
	</xsp:content>
} 
}catch(java.lang.Exception e) 
	{
		System.out.print(e.toString());
	}
</xsp:logic>
</root>
</xsp:page>
-------------
but.......but I don't need to put xml to parameter.
I need to take a Content of request. But cocoon's HttpRequest hasn't
.getContent(). There is getInputStream, but I can't to understand how to use
that.
-- 
View this message in context: http://www.nabble.com/POST-.xml-tp15517307p15561431.html
Sent from the Cocoon - Users mailing list archive at Nabble.com.


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


Re: POST .xml

Posted by Suzan Foster <su...@nerocmediaware.nl>.
nanomonk:
>
> hmm.. I was thinking about it... but I'm not so good in xslt. I don't know
> can I call java methods from xslt, or can I work with database in it... 
> btw, I have a ready java class with all methods need for processing this
> .xml... All I need is to take POSTed xml as DOM Document(even variant as
> String is good) and call some method.
>   

Why not process the request to a DOM document in javaflow or flowscript:

package scratch;

import java.io.InputStream;
import javax.servlet.http.HttpServletRequest;

import org.apache.avalon.framework.CascadingRuntimeException;
import org.apache.cocoon.components.flow.java.AbstractContinuable;
import org.apache.cocoon.environment.ObjectModelHelper;
import org.apache.cocoon.environment.Request;
import org.apache.cocoon.environment.http.HttpEnvironment;
import org.apache.cocoon.util.PostInputStream;
import org.apache.cocoon.xml.dom.DOMBuilder;
import org.apache.excalibur.xml.sax.SAXParser;
import org.w3c.dom.Document;
import org.xml.sax.InputSource;

public class DOMFromRequestFlow extends AbstractContinuable {
    public void doDom() {
        SAXParser parser = (SAXParser) getComponent(SAXParser.ROLE);       
        try {
            Request request = 
ObjectModelHelper.getRequest(getObjectModel());
            int len = request.getContentLength();
            HttpServletRequest httpRequest = (HttpServletRequest) 
getObjectModel().get(HttpEnvironment.HTTP_REQUEST_OBJECT);
            InputStream inputStream = new 
PostInputStream(httpRequest.getInputStream(), len);
            DOMBuilder builder = new DOMBuilder();
            InputSource inputSource = new InputSource(inputStream);
            parser.parse(inputSource, builder);
            Document document = builder.getDocument();
           
            /* TODO: something with the document */
        } catch (Exception e) {
            throw new CascadingRuntimeException("Parsing of request 
failed", e);
        } finally {
            releaseComponent(parser);
        }
    }
}

Grtz, Suzan.

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


Re: POST .xml

Posted by nanomonk <br...@inbox.ru>.

Alexander Daniel wrote:
> 
> 
> Maybe the XSLT Transformer can help you: The StreamGenerator reads XML  
> from an HttpRequest InputStream and generates SAX Events [1]. After  
> the StreamGenerator you can use an XSLT Transformer to transform the  
> generated SAX Stream:
> 
> <map:match pattern="upload/setSettings">
> 	<map:generate type="stream">
> 		<map:parameter name="form-name" value="uploadfile"/>
> 		<map:parameter name="defaultContentType" value="text/xml"/>
> 	</map:generate>
>          <map:transform src="transform.xslt">
>                  <map:parameter name="paramone" value="{request- 
> param:paramone}"/>
>                  <map:parameter name="paramtwo" value="{request- 
> param:paramtwo}"/>
> 	</map:transform>
> 	<map:serialize type="xml" />
> </map:match>
> 
hmm.. I was thinking about it... but I'm not so good in xslt. I don't know
can I call java methods from xslt, or can I work with database in it... 
btw, I have a ready java class with all methods need for processing this
.xml... All I need is to take POSTed xml as DOM Document(even variant as
String is good) and call some method.
-- 
View this message in context: http://www.nabble.com/POST-.xml-tp15517307p15560347.html
Sent from the Cocoon - Users mailing list archive at Nabble.com.


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


Re: POST .xml

Posted by Alexander Daniel <al...@gmx.at>.
On 16.02.2008, at 12:32, nanomonk wrote:

> and I have pipeline with StreamGenerator:
> -----
> <map:match pattern="upload/setSettings">
> 	<map:generate type="stream">
> 		<map:parameter name="form-name" value="uploadfile"/>
> 		<map:parameter name="defaultContentType" value="text/xml"/>
> 	</map:generate>
> 	<map:serialize type="xml" />
> </map:match>
> -----
> all ok and in source of
> "http://localhost:8888/setSettings? 
> paramone=12345&paramtwo=blablabla" I see
> my .xml.
> But.. I need to get from it some data and process it... I think to  
> do that
> in .xsp. I need to take params of request and according them to do  
> some
> actions... But I don't know how to get DOM Document from this recieved
> .xml...


Maybe the XSLT Transformer can help you: The StreamGenerator reads XML  
from an HttpRequest InputStream and generates SAX Events [1]. After  
the StreamGenerator you can use an XSLT Transformer to transform the  
generated SAX Stream:

<map:match pattern="upload/setSettings">
	<map:generate type="stream">
		<map:parameter name="form-name" value="uploadfile"/>
		<map:parameter name="defaultContentType" value="text/xml"/>
	</map:generate>
         <map:transform src="transform.xslt">
                 <map:parameter name="paramone" value="{request- 
param:paramone}"/>
                 <map:parameter name="paramtwo" value="{request- 
param:paramtwo}"/>
	</map:transform>
	<map:serialize type="xml" />
</map:match>

Alex

[1] http://cocoon.apache.org/2.1/userdocs/stream-generator.html


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


Re: POST .xml

Posted by Joerg Heinicke <jo...@gmx.de>.
On 16.02.2008 06:32, nanomonk wrote:

> I need to POST .xml file to server for further its processing...
> so, I have a simple upload.html for test:

> and I have pipeline with StreamGenerator:

> all ok and in source of
> "http://localhost:8888/setSettings?paramone=12345&paramtwo=blablabla" I see
> my .xml.
> But.. I need to get from it some data and process it... I think to do that
> in .xsp. I need to take params of request and according them to do some
> actions... But I don't know how to get DOM Document from this recieved
> .xml...

You have one problem with your approach: You can't have 2 generators. 
XSP is a generator and so is the StreamGenerator. The easiest way for 
you might be using flowscript [1] as somebody already mentioned, even if 
it is not the most straight-forward:

<map:match pattern="upload/setSettings">
   <map:call function="handleUploadSettings"/>
</map:match>

import Packages.org.apache.cocoon.components.flow.util.PipelineUtil;

function handleUploadSettings() {
   var util = cocoon.createObject(PipelineUtil);
   try {
     var dom = util.processToDom("upload/getStream");
     // call your own Java classes here
   } finally {
     cocoon.disposeObject(util);
   }

   // eventually display new page
   cocoon.sendPage("upload/showResult");
}

<map:match pattern="upload/getStream">
   <map:generate type="stream">
     <map:parameter name="form-name" value="uploadfile"/>
     <map:parameter name="defaultContentType" value="text/xml"/>
   </map:generate>
   <map:serialize type="xml" />
</map:match>

<map:match pattern="upload/showResult">
   <!-- do whatever you want -->
</map:match>

Flowscript gives you this controller kind of thing if you are familiar 
with MVC model. It only bloats the sitemap a bit ...

Joerg

[1] http://cocoon.apache.org/2.1/userdocs/flow/index.html

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