cocoon-docs mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From stev...@outerthought.org
Subject [WIKI-UPDATE] FileUploadsWithCocoon2.1 FileUploadsInCustomActions FileUploadsWithFlow Fri Feb 13 02:00:06 2004
Date Fri, 13 Feb 2004 01:00:07 GMT
Page: http://wiki.cocoondev.org/Wiki.jsp?page=FileUploadsWithCocoon2.1 , version: 3 on Fri
Feb 13 01:00:11 2004 by MarkLundquist

- 
- __Technical Overview__\\
- ''If you are uninterested in the technical details, just skim this part.''
- 
- If uploads are "turned on", when the Cocoon servlet receives a request, org.apache.cocoon.servlet.multipart.RequestFactory
parses out the multipart data which comprises the file data, and places an [org.apache.cocoon.servlet.multipart.Part|http://cocoon.apache.org/2.1/apidocs/org/apache/cocoon/servlet/multipart/Part.html]
object in the request using the name of the multipart file parameter (the name of the <input
type="file" ...> from the html).  This object provides access methods for later Cocoon
steps to deal with the file.  It handles multiple files by placing multiple objects in the
request.
- 
- Part is abstract, but has two concrete subclasses: [PartOnDisk|http://cocoon.apache.org/2.1/apidocs/org/apache/cocoon/servlet/multipart/PartOnDisk.html]
and [PartInMemory|http://cocoon.apache.org/2.1/apidocs/org/apache/cocoon/servlet/multipart/PartInMemory.html]
which are the actual objects placed in the request as described above, depending on configuration
options to be described below.  PartOnDisk is a file already saved to disk and provides an
additional method getFile(), not defined in Part, which returns the java.io.File object for
the file on disk.  PartInMemory holds the contents of the uploaded file in a byte array in
memory.  Both PartOnDisk and PartInMemory are only temporary and will be cleaned up (deleted/dereferenced)
at the end of the request.  They will not persist after the request is serviced unless otherwise
acted upon.
- 
- What this means is that a form constructed to post a file to any Cocoon page will result
in that file being parsed and either kept in memory or saved automatically (though temporarily)
to disk.  Without this knowledge, the file upload sample shipping with Cocoon ([here in Tomcat|http://localhost:8080/cocoon/samples/xsp/upload]
[here in Jetty|http://localhost:8888/samples/xsp/upload]) seems confusingly simple to some
- the code in the xsp does not upload the file (nor does any action as some may suspect) but
merely lists information about it...  In fact, the sample has nothing to do with xsp and should
probably be moved.
- __Examples__
+ __Examples__\\
?             ++

+ There are several approaches to handling uploads in Cocoon:
- See [FileUploadsWithFlow].
? ^^^                      -

+ * [FileUploadsWithFlow]
? ^

+ * [FileUploadWithAction] (careful, written for 2.0 and needs to be rewritten) for an action
which retrieves a FilePartFile (see!) object by name from the request (requires autosave-uploads=true)
for further processing (i.e., move to different directory, save to Blob in database, etc.)
 If you want to create an action in 2.1 and need help, there is a tip at the end of the flow
example.
+ * [FileUploadsInCustomActions]
- See also [FileUploadWithAction] (careful, written for 2.0 and needs to be rewritten) for
an action which retrieves a FilePartFile (see!) object by name from the request (requires
autosave-uploads=true) for further processing (i.e., move to different directory, save to
Blob in database, etc.)  If you want to create an action in 2.1 and need help, there is a
tip at the end of the flow example.
+ __Technical Overview__\\
+ ''..for those who are interested...''
+ If uploads are "turned on", when the Cocoon servlet receives a request, org.apache.cocoon.servlet.multipart.RequestFactory
parses out the multipart data which comprises the file data, and places an [org.apache.cocoon.servlet.multipart.Part|http://cocoon.apache.org/2.1/apidocs/org/apache/cocoon/servlet/multipart/Part.html]
object in the request using the name of the multipart file parameter (the name of the <input
type="file" ...> from the html).  This object provides access methods for later Cocoon
steps to deal with the file.  It handles multiple files by placing multiple objects in the
request.
+ 
+ Part is abstract, but has two concrete subclasses: [PartOnDisk|http://cocoon.apache.org/2.1/apidocs/org/apache/cocoon/servlet/multipart/PartOnDisk.html]
and [PartInMemory|http://cocoon.apache.org/2.1/apidocs/org/apache/cocoon/servlet/multipart/PartInMemory.html]
which are the actual objects placed in the request as described above, depending on configuration
options to be described below.  PartOnDisk is a file already saved to disk and provides an
additional method getFile(), not defined in Part, which returns the java.io.File object for
the file on disk.  PartInMemory holds the contents of the uploaded file in a byte array in
memory.  Both PartOnDisk and PartInMemory are only temporary and will be cleaned up (deleted/dereferenced)
at the end of the request.  They will not persist after the request is serviced unless otherwise
acted upon.
+ 
+ What this means is that a form constructed to post a file to any Cocoon page will result
in that file being parsed and either kept in memory or saved automatically (though temporarily)
to disk.  Without this knowledge, the file upload sample shipping with Cocoon ([here in Tomcat|http://localhost:8080/cocoon/samples/xsp/upload]
[here in Jetty|http://localhost:8888/samples/xsp/upload]) seems confusingly simple to some
- the code in the xsp does not upload the file (nor does any action as some may suspect) but
merely lists information about it...  In fact, the sample has nothing to do with xsp and should
probably be moved.
+ 
+ 


Page: http://wiki.cocoondev.org/Wiki.jsp?page=FileUploadsInCustomActions , version: 1 on Fri
Feb 13 01:07:09 2004 by MarkLundquist

New page created:
+ ''Note, this code is reportedly ([Geoff Howard]) __wrong__, but I don't know what's wrong
with it.  This came from an earlier version of [FileUploadsWithFlow].  I moved it to its own
page and added a link from [FileUploadsWithCocoon2.1], because it had nothing to do with flow.
-- [MarkLundquist]
+ ----
+ If you have a custom sitemap component that you want to handle an upload request, you can
include the following Java code:
+ {{{
+ 
+   import org.apache.cocoon.servlet.multipart.*;
+ 
+   ...
+ 
+   Request request = ObjectModelHelper.getRequest(obj);
+   if (request instanceof MultipartHttpServletRequest) {
+     Part part = (Part) request.get("upload-file");
+     if (part != null) {
+       // do something with it
+     } else {
+       // parameter not found
+     }
+   } else {
+     // upload is disabled
+   }
+ }}}
+ 


Page: http://wiki.cocoondev.org/Wiki.jsp?page=FileUploadsWithFlow , version: 17 on Fri Feb
13 00:53:44 2004 by MarkLundquist

+ This functionality was introduced in April 4, 2003 and is available in all 2.1 releases.
 See [FileUploadsWithCocoon2.1] for a general overview of uploading, and links to Wiki pages
explaining how to upload other than using flow.
- ''This is taken from [this thread|http://marc.theaimsgroup.com/?t=104946920900004&r=1&w=2]
in which [Stefanomazzocchi] describes the changes to the file upload system.''
- 
- ''I'm not sure if this totally replaces the old file upload stuff at [FileUploadsWithCocoon]
and [FileUploadWithAction], so once this gets straightened out, expect a centralized file
uploads page.'' -- [Tony Collen]
- ----
- 
- This functionality was introduced in April 4, 2003 and is available in all 2.1 releases.
- To get this example to work as written, you will need a FileUploadManager component that
does what the flowscript is asking it to do.  So first, grab the cocoon-upload.jar file attached
to this page (see below), and drop it into your WEB-INF/lib directory.
+ To get this example to work as written, you will need a FileUploadManager component that
does what the flowscript is asking it to do.  Here are the steps to set this up:
- Next, you need to register this as a managed component.  Create WEB-INF/user.xroles with
these contents:
+ 1) Grab the cocoon-upload.jar file attached to this page (see below), and drop it into your
WEB-INF/lib directory.
+ 
+ 2) Create WEB-INF/user.xroles with these contents:
- ...and change the <cocoon> element in WEB-INF/cocoon.xconf to read:
? ^^^^^^ ^

+ 3) Change the <cocoon> element in WEB-INF/cocoon.xconf to read:
? ^^ ^

+ 4) ...and add this to your cocoon.xconf:
+ 
+ {{{
+ <upload_manager>
+     <uploadfolder>/some/where</uploadfolder>
+ </upload_manager>
+ }}}
+ 
- Finally, set the __enable-uploads__ parameter in WEB-INF/web.xml to __true__ (this is already
done for you if you built Cocoon with 'cocoon.enable-uploads=true' in build.properties or
local.build.properties).
? ^^^^^^^^ ^

+ 5) Set the __enable-uploads__ parameter in WEB-INF/web.xml to __true__ (this is already
done for you if you built Cocoon with 'cocoon.enable-uploads=true' in build.properties or
local.build.properties).
? ^^ ^

- This example calls the upload() method of the component... but this FileUploadManager component
also has some other methods, for controlling the directory into which files are uploaded,
etc.  Take a look at the interface in FileUploadManager.java to learn about those.
+ This example calls the upload() method of the component... but this FileUploadManager component
also has some other methods, for controlling the directory into which files are uploaded,
etc.  Take a look at the interface in FileUploadManager.java (from the .jar file) to learn
about those.
?                                                                                        
                                                                                         
                                                                     +++++++++++++++++++++

- \\
- ----
- Alternatively, you can write the following java code in your sitemap components:
- {{{
-   import org.apache.cocoon.servlet.multipart.*;
- 
-   ...
- 
-   Request request = ObjectModelHelper.getRequest(obj);
-   if (request instanceof MultipartHttpServletRequest) {
-     Part part = (Part) request.get("upload-file");
-     if (part != null) {
-       // do something with it
-     } else {
-       // parameter not found
-     }
-   } else {
-     // upload is disabled
-   }
- }}}
- 



Mime
View raw message