Re: Change request
Jancsi Farkas <[email protected]>
| Newsgroups | gmane.comp.java.enhydra.barracuda.general |
|---|---|
| Message-ID | <[email protected]> |
Yes, but what if the form has some other fields you require, along with
the uploaded files?
Will barracuda form mapping still work?
This is what I did in my screen, in order to allow file upload together
with form mapping (I had to use form mapping, as the screen
generator uses form mapping)
List items = new ArrayList();
FormMap fm = new DefaultFormMap();
try
{
// Parse the request
/* FileItem */ items = upload.parseRequest(req,
1024, 1024*1024*3, req.getRealPath("images/upload"));
}
catch(Exception ex)
{
ex.printStackTrace();
}
Iterator it = items.iterator();
while(it.hasNext())
{
FileItem item = (FileItem) it.next();
if (item.isFormField())
{
String name = item.getFieldName();
String value = item.getString();
fm.putState(name,value);
}
if (!item.isFormField())
{
//process the file
}
}
try
{
gf.map(fm).validate(true);
}
catch(ValidationException e)
{
ve = e;
}
// from here everything goes as without file upload
Kirk Daries wrote:
>Hi,
>
>Also implemented Jakarta Commons Upload.
>
>Here's what I did...
>Wrote my own RequestWrapper...
>
>package sitawc.barraframework.utils.fileupload;
>
>import org.apache.commons.fileupload.DefaultFileItemFactory;
>import org.apache.commons.fileupload.FileUpload;
>import org.apache.commons.fileupload.FileUploadException;
>
>import javax.servlet.http.HttpServletRequest;
>import javax.servlet.http.HttpServletRequestWrapper;
>import java.io.File;
>import java.util.List;
>
>/**
> * A simple upload wrapper for the Servlet Request
> */
>public class FileUploadMultipartRequest extends
>HttpServletRequestWrapper implements HttpServletRequest {
> private FileUpload fileUpload;
> private String destinationDirString;
> private File destinationDir;
> private HttpServletRequest httpServletRequest;
> private List fileItems;
> private DefaultFileItemFactory defaultFileItemFactory;
>
> public FileUploadMultipartRequest(String destinationDir,
>HttpServletRequest httpServletRequest) {
> super(httpServletRequest);
> this.fileUpload = new FileUpload();
> this.destinationDirString = destinationDir;
> this.httpServletRequest = httpServletRequest;
> this.defaultFileItemFactory = new DefaultFileItemFactory();
> this.destinationDir = new File(this.destinationDirString);
> }
>
> /**
> * @return Returns a refenrence to the file upload object
> */
> public FileUpload getFileUpload() {
> return fileUpload;
> }
>
> /**
> * The actual processing method
> * @throws FileUploadException
> */
> public void parseRequest() throws FileUploadException {
> this.fileItems = null;
>
> this.defaultFileItemFactory.setRepository(this.destinationDir);
> this.defaultFileItemFactory.setSizeThreshold(4096);
>
> this.fileUpload.setFileItemFactory(this.defaultFileItemFactory);
> this.fileUpload.setSizeMax(1000000);
>
> // maximum size that will be stored in memory
> this.fileItems =
>this.fileUpload.parseRequest(this.httpServletRequest);
> }
>
> /**
> * Returns a list of fileItems that were found in the request...
> * @return
> */
> public List getFileItems() {
> return this.fileItems;
> }
>}
>
>
>Installed my wrapper in my application gateway,
>Note: installHttpRequestWrapper is called in initializeLocal.
>
> private void installHttpRequestWrapper() {
> File tempDir;
> tempDir =
>(File)getServletConfig().getServletContext().getAttribute("javax.servlet.context.tempdir");
> REQUEST_WRAPPER = new
>FileUploadWrapper(tempDir.getAbsolutePath());
> }
>
>
> public static class FileUploadWrapper implements RequestWrapper {
> private String destinationDir;
> public FileUploadWrapper(String destinationDir) {
> this.destinationDir = destinationDir;
> }
> public HttpServletRequestWrapper wrap(HttpServletRequest
>httpServletRequest) {
> return new HttpServletRequestWrapper(new
>FileUploadMultipartRequest(destinationDir, httpServletRequest));
> }
> }
>
>
>In my top-most RequestEvent_Listener, I have the following code,
>
>
> if (FileUpload.isMultipartContent(req)) {
> FileUploadMultipartRequest fileUploadMultipartRequest =
>(FileUploadMultipartRequest) ((HttpServletRequestWrapper)
>req).getCoreRequest();
>
> try {
> fileUploadMultipartRequest.parseRequest();
> } catch (FileUploadException fue) {
> if (logger.isEnabledFor(Priority.ERROR))
>logger.error("FileUploadException: " + fue.getMessage());
> }
>
> List fileItems = null;
> Iterator i;
> FileItem fileItem;
>
>
> fileItems = fileUploadMultipartRequest.getFileItems();
> i = fileItems.iterator();
>
> while (i.hasNext()) {
> fileItem = (FileItem)i.next();
>
> if (fileItem.isFormField()) {
>
> parms.put(fileItem.getFieldName() ,
>processSpecialUrlCharacters(fileItem.getString(), false));
>
> if (logger.isDebugEnabled()) logger.debug("parm - " +
>fileItem.getFieldName() + " : " + (String)
>parms.get(fileItem.getFieldName()));
>
> } else {
> if (logger.isDebugEnabled()) logger.debug("File - " +
>fileItem.getName());
> }
> }
> }
>
>Basically, if the request Content is MultiPart, Cast to
>FileUploadMultipartRequest and call 'parseReqest'.
>Also logs the param and files to the log file.
>
>Further down the event tree I simply just cast to the Wrapper Again,
>and call the 'getFileItems' method.
>This is the actual class that does the 'processing'
>E.g.
>
> FileUploadMultipartRequest fileUploadMultipartRequest =
>(FileUploadMultipartRequest) ((HttpServletRequestWrapper)
>req).getCoreRequest();
> fileUploadMultipartRequest.getFileItems();
>
> List fileItems = null;
> Iterator i;
> FileItem lfileItem;
> lfileItem = null;
>
> fileItems = fileUploadMultipartRequest.getFileItems();
> i = fileItems.iterator();
>
> while (i.hasNext()) {
> lfileItem = (FileItem)i.next();
>
> if (!lfileItem.isFormField()) {
> //Do your processing here!
> }
> }
>
>
>Hope that Helps
>Regards
>KD
>
>
>
>>>>[email protected] 2003/11/14 16:40:32 >>>
>>>>
>>>>
>Anyway is pretty easy to integrate. Only have to be careful, if is
>multipart request, put manually param requests back to context, and
>everything works like charm. (or at least worked for me :D)
>I can send some samples later, when i'll be home, if anybody is
>interested.
>
>Jancsi
>
>Diez B. Roggisch wrote:
>
>
>
>>Jancsi Farkas wrote:
>>
>>
>>
>>>Yes I have used with barracuda, it is very easy to integrate.
>>>(I was about to ask why is not used :) )
>>>
>>>
>>
>>Most probably because I didn't find it back two years ago :-)
>>
>>Regards,
>>
>>Diez
>>
>>
>>
>>_______________________________________________
>>Barracuda mailing list
>>[email protected]
>>http://barracudamvc.org/lists/listinfo/barracuda
>>
>>
>>
>
>_______________________________________________
>Barracuda mailing list
>[email protected]
>http://barracudamvc.org/lists/listinfo/barracuda
>---------------------------------------------------------------
>“All views or opinions expressed in this electronic message
>and its attachments are the view of the sender and do not
>necessarily reflect the views and opinions of the
>Provincial Government Western Cape (“the PGWCâ€).
>No employee of the WCPG is entitled to conclude a
>binding contract on behalf of the PGWC unless he/she
>is an Accounting Officer of the PGWC, or his or her
>authorised representative.
>
>The information contained in this message and its attachments
>may be confidential or privileged and is for the use of the
>named recipient only, except where the sender specifically
>states otherwise. If you are not the intended recipient you
>may not copy or deliver this message to anyone.â€
>
>_______________________________________________
>Barracuda mailing list
>[email protected]
>http://barracudamvc.org/lists/listinfo/barracuda
>
>
>