Re: upload fail occasionally [also Re: File upload fails first time, then works after page reload]

2007-04-11 Thread Eric Rank
I ran into the same problem with inconsistent behavior when uploading  
files. Sometimes i get a RequestFacade, and sometimes I get the right  
MultiPartRequestWrapper.


I dug into the struts source code a little and I think I found the  
culprit.


in org.apache.struts2.dispatcher.FilterDispatcher there's a method  
named "prepareDispatcherAndWrapRequest" where the RequestFacade  
becomes the right kind of Request object. In the code, the wrapping  
is within a conditional block where it will only be wrapped when the  
Dispatcher instance is null. The problem when uploading a file is  
that it's not always null.


Is that the expected behavior of this method? I've never dug this deep.

The good news is that the contextCleanup filter seems to take care of  
making that Dispatcher instance null.


Finally, I hesitate suggesting a change in a class at such a  
foundational point in the framework, but what would happen if the  
request hits the wrapping part of the code outside when the  
Dispatcher instance is not null? My file upload problems are fixed,  
but what other problems would this cause?



=== Existing code for FilterDispatcher 

protected HttpServletRequest prepareDispatcherAndWrapRequest(
HttpServletRequest request,
HttpServletResponse response)
throws ServletException {

Dispatcher du = Dispatcher.getInstance();

// Prepare and wrap the request if the cleanup filter hasn't  
already, cleanup filter should be
// configured first before struts2 dispatcher filter, hence  
when its cleanup filter's turn,

// static instance of Dispatcher should be null.
if (du == null) {

Dispatcher.setInstance(dispatcher);

// prepare the request no matter what - this ensures  
that the proper character encoding

// is used before invoking the mapper (see WW-9127)
dispatcher.prepare(request, response);

try {
// Wrap request first, just in case it is multipart/ 
form-data
// parameters might not be accessible through before  
encoding (ww-1278)
request = dispatcher.wrapRequest(request,  
getServletContext());

} catch (IOException e) {
String message = "Could not wrap servlet request  
with MultipartRequestWrapper!";

LOG.error(message, e);
throw new ServletException(message, e);
}
} // <--
//THE END OF THE LOGiC BLOCK HERE MEANS
//THAT THE Request ONLY GETS WRAPPED WHEN THE Dispatcher
//INSTANCE IS NULL. WHEN UPLOADING, IT'S SOMETIMES NOT null
else {
dispatcher = du;
}
return request;
}

=== Moving the wrapping outside of the logic block 
=== wraps the request even if the Dispatcher is not null ==


protected HttpServletRequest prepareDispatcherAndWrapRequest(
HttpServletRequest request,
HttpServletResponse response)
throws ServletException {

Dispatcher du = Dispatcher.getInstance();

// Prepare and wrap the request if the cleanup filter hasn't  
already, cleanup filter should be
// configured first before struts2 dispatcher filter, hence  
when its cleanup filter's turn,

// static instance of Dispatcher should be null.
if (du == null) {

Dispatcher.setInstance(dispatcher);

}
else {
dispatcher = du;
}

//MOVING THE WRAPPING CODE HERE
//SO THE Request ALWAYS GETS WRAPPED

// prepare the request no matter what - this ensures  
that the proper character encoding

// is used before invoking the mapper (see WW-9127)
dispatcher.prepare(request, response);

try {
// Wrap request first, just in case it is multipart/ 
form-data
// parameters might not be accessible through before  
encoding (ww-1278)
request = dispatcher.wrapRequest(request,  
getServletContext());

} catch (IOException e) {
String message = "Could not wrap servlet request  
with MultipartRequestWrapper!";

LOG.error(message, e);
throw new ServletException(message, e);
}
return request;
}



Thanks,

Eric Rank




Dave Newton wrote:

Did either of you try specifying the context cleanup
filter?


  contextCleanup
  
org.apache.struts2.dispatcher.ActionContextCleanUp
  


and put it first in the filter mapping.

I have yet to have any issues w/ file upload since
including that and I still think I vaguely recall a
thread about this a long time ago but can no longer
find any references to it, so I could be way off-base.








-
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional comm

Re: upload fail occasionally

2007-04-05 Thread torben
I have now done some more studies by making my own fileUploadInterceptor 
class. It is complety the same as 
org.apache.struts2.interceptor.FileUploadInterceptor, except that I 
write in the log the class name of the HttpServletRequest I get in the  
intercept method.


If I bypass apache as frontend (that is calling jboss on port 8080) I 
get the org.apache.st ruts2.dispatcher.multipart.MultiPartRequestWrapper 
class in the inetercept method. And everything works fine.


If I use apache as frontend (Using mod_jk 1.2.x with JBoss/Tomcat and 
Apache2, link 
http://www.jboss.org/wiki/Wiki.jsp?page=UsingMod_jk1.2WithJBoss) I get 
the org.apache.ca talina.connector.RequestFacade Class in the intercept 
method.


Now the question is why do I not get the MultiPartRequestWrapper class ?

Best regards
Torben Frøberg


Dave Newton wrote:

Did either of you try specifying the context cleanup
filter?


  contextCleanup
  
org.apache.struts2.dispatcher.ActionContextCleanUp
  


and put it first in the filter mapping.

I have yet to have any issues w/ file upload since
including that and I still think I vaguely recall a
thread about this a long time ago but can no longer
find any references to it, so I could be way off-base.

  



  


-
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Re: upload fail occasionally

2007-04-05 Thread Dave Newton
--- nordland <[EMAIL PROTECTED]> wrote:
> I had the same problem too. The invocation of the
> ActionContextCleanUp-filter was the right choice. 
> Fileupload works now without any errors.

That's great!

I or someone else will add something to the wiki page,
although I sure wish I could find the thread that
backs this up instead of invoking the Rule of
Presumptive Configuration (tm) ;)

d.



 

Get your own web address.  
Have a HUGE year through Yahoo! Small Business.
http://smallbusiness.yahoo.com/domains/?p=BESTDEAL

-
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Re: upload fail occasionally

2007-04-05 Thread nordland
="redirect-action">show_item
>> >  > name="input">/WEB-INF/jsp/add_item.jsp
>> > 
>> > --struts.xml
>> end--
>> >
>> > This is my action file
>> >
>> > --action
>> begin--
>> > package com.shoutloud.action.user;
>> >
>> > import java.io.File;
>> > import java.io.FileInputStream;
>> > import java.io.FileOutputStream;
>> >
>> > import javax.servlet.http.HttpServletRequest;
>> >
>> > import org.apache.struts2.ServletActionContext;
>> > import
>> org.apache.struts2.interceptor.ServletRequestAware;
>> >
>> > import sun.util.logging.resources.logging;
>> >
>> > import com.opensymphony.xwork2.ActionSupport;
>> > import com.shoutloud.model.Item;
>> > import com.shoutloud.model.MusicItem;
>> > import com.shoutloud.service.AddItemService;
>> > import
>> com.shoutloud.service.impl.LoginServiceImpl;
>> >
>> > public class AddItemAction extends ActionSupport
>> implements
>> > ServletRequestAware {
>> >
>> > private static final long MAX_UPLOAD_SIZE =
>> 0x200L;
>> >
>> > private String itemType;
>> >
>> > private String description;
>> >
>> > private Double price;
>> >
>> > private String url;
>> >
>> > private String name;
>> >
>> > private AddItemService addItemService;
>> >
>> > private HttpServletRequest request;
>> > 
>> > private String contentType;
>> >
>> > private File upload;
>> >
>> > private String fileName;
>> > 
>> >
>> > public void setAddItemService(AddItemService
>> addItemService) {
>> > this.addItemService = addItemService;
>> > }
>> >
>> > public void
>> setServletRequest(HttpServletRequest 
>> > httpServletRequest) {
>> > this.request = httpServletRequest;
>> > }
>> >
>> 
> === message truncated ===
> 
> 
> 
>  
> 
> No need to miss a message. Get email on-the-go 
> with Yahoo! Mail for Mobile. Get started.
> http://mobile.yahoo.com/mail 
> 
> -
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
> 
> 
> 

-- 
View this message in context: 
http://www.nabble.com/upload-fail-occasionally-tf3467096.html#a9854357
Sent from the Struts - User mailing list archive at Nabble.com.


-
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Re: upload fail occasionally

2007-04-04 Thread torben

Thanks for the tip. But it does not work in my case.

best regards
Torben Frøberg

Dave Newton wrote:

Did either of you try specifying the context cleanup
filter?


  contextCleanup
  
org.apache.struts2.dispatcher.ActionContextCleanUp
  


and put it first in the filter mapping.

I have yet to have any issues w/ file upload since
including that and I still think I vaguely recall a
thread about this a long time ago but can no longer
find any references to it, so I could be way off-base.

d.
  



-
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Re: upload fail occasionally

2007-04-04 Thread Dave Newton
Did either of you try specifying the context cleanup
filter?


  contextCleanup
  
org.apache.struts2.dispatcher.ActionContextCleanUp
  


and put it first in the filter mapping.

I have yet to have any issues w/ file upload since
including that and I still think I vaguely recall a
thread about this a long time ago but can no longer
find any references to it, so I could be way off-base.

d.

--- torben <[EMAIL PROTECTED]> wrote:

> I cannot help you. But I have exactly the same
> problem. Sometimes it 
> seems that, the execute method is called before the
> HTTP client has 
> finished uploading the file. This results in a null
> value for the File 
> object upload.
> 
> But the file is uploaded, the problem is just, he
> the execute methd is 
> called to fast.
> 
> I try to use Apache webserver as frontend (by the
> use of the mod_jk 
> module), and here I have the problem almost the
> time. Is there some 
> time-out parameters one can set?
> 
> I am using JBOSS as Application server (version
> 4.0.5) and struts 2.0.6.
> 
> 
> 
> best regards
> 
> Torben Frøberg
> Fasanvænget 484
> Tlf. privat 49 14 05 85
> Tlf. arbejde 45 17 12 97
> 
> 
> Kurapica wrote:
> > Hi all,
> >
> > I am using struts 2.0.6 and encountered a submit
> problem.
> >
> > I have a form with enctype="multipart/form-data"
> property. there're
> > other text fields beside . when I submit,
> at most time it
> > works fine, but I found that occasionally all
> fields are null. I have
> > replaced commons-fileupload, commons-io and
> commons-collections with
> > latest versions and the problem still exists.
> >
> > struts version is 2.0.6
> > jdk version is 1.5.0_11
> > tomcat version is 5.5.12
> > commons-fileupload version is 1.2
> > commons-collections version is 3.2
> > commons-io version is 1.3.1
> >
> > I provided jspfile, struts.xml and action class
> below. If any
> > infomation required, please reply.
> >
> > Any suggestion is welcome and thanks for your
> advice~
> >
> > This is my jsp file:
> >
> > --jsp file
> begin--
> >  > "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd";>
> > <%@ page contentType="text/html; charset=UTF-8" %>
> > <%@ taglib prefix="s" uri="/struts-tags" %>
> > <%@ include file="/WEB-INF/jsp/inc/head_code.jsp"
> %>
> >
>
<%if(!isLoggedIn)response.sendRedirect("./Login.jsp");%>
> > 
> > 
> >  rel="stylesheet" 
> > type="text/css">
> >  > rel="stylesheet" type="text/css">
> >Shoutloud Add Item Page
> >
> > 
> >
> > 
> > 
> >  > enctype="multipart/form-data">
> > 
> >  name="com.shoutloud.ui.beans.ItemTypesBean"
> id="types">
> >  name="itemType" value="%{1}"
> > list="#types" required="true"
> requiredposition="left"  />
> > 
> >  required="true"
> > requiredposition="left"/>
> >  key="description"/>
> >  key="price"
> > required="true" requiredposition="left"/>
> >  required="true" 
> > requiredposition="left"/>
> > 
> >
> >
> > 
> > 
> > 
> > 
> > --jsp file
> end--
> >
> > This is part of my struts.xml
> >
> > --struts.xml
> begin--
> > 
> >   name="success">/WEB-INF/jsp/add_item.jsp
> > 
> >
> > 
> >   type="redirect-action">show_item
> >   name="input">/WEB-INF/jsp/add_item.jsp
> > 
> > --struts.xml
> end--
> >
> > This is my action file
> >
> > --action
> begin--
> > package com.shoutloud.action.user;
> >
> > import java.io.File;
> > import java.io.FileInputStream;
> > import java.io.FileOutputStream;
> >
> > import javax.servlet.http.HttpServletRequest;
> >
> > import org.apache.struts2.ServletActionContext;
> > import
> org.apache.struts2.interceptor.ServletRequestAware;
> >
> > import sun.util.logging.resources.logging;
> >
> > import com.opensymphony.xwork2.ActionSupport;
> > import com.shoutloud.model.Item;
> > import com.shoutloud.model.MusicItem;
> > import com.shoutloud.service.AddItemService;
> > import
> com.shoutloud.service.impl.LoginServiceImpl;
> >
> > public class AddItemAction extends ActionSupport
> implements
> > ServletRequestAware {
> >
> > private static final long MAX_UPLOAD_SIZE =
> 0x200L;
> >
> > private String itemType;
> >
> > private String description;
> >
> > private Double price;
> >
> > private String url;
> >
> > private String name;
> >
> > private AddItemService addItemService;
> >
> > private HttpServletRequest request;
> > 
> > private String contentType;
> >
> > private File upload;
> >
> > private String fileName;
> > 
> >
> > public void setAddItemService(AddItemService
> addItemService) {
> > this.addItemService = addItemService;
> > }
> >
> > public void
> setServletRequest(HttpServletRequest 
> > httpServletRequest) {
> > this.request = httpServletRequest;
> > }
> >
> 
=== message truncated ===



 

Re: upload fail occasionally

2007-04-04 Thread torben
I cannot help you. But I have exactly the same problem. Sometimes it 
seems that, the execute method is called before the HTTP client has 
finished uploading the file. This results in a null value for the File 
object upload.


But the file is uploaded, the problem is just, he the execute methd is 
called to fast.


I try to use Apache webserver as frontend (by the use of the mod_jk 
module), and here I have the problem almost the time. Is there some 
time-out parameters one can set?


I am using JBOSS as Application server (version 4.0.5) and struts 2.0.6.



best regards

Torben Frøberg
Fasanvænget 484
Tlf. privat 49 14 05 85
Tlf. arbejde 45 17 12 97


Kurapica wrote:

Hi all,

I am using struts 2.0.6 and encountered a submit problem.

I have a form with enctype="multipart/form-data" property. there're
other text fields beside . when I submit, at most time it
works fine, but I found that occasionally all fields are null. I have
replaced commons-fileupload, commons-io and commons-collections with
latest versions and the problem still exists.

struts version is 2.0.6
jdk version is 1.5.0_11
tomcat version is 5.5.12
commons-fileupload version is 1.2
commons-collections version is 3.2
commons-io version is 1.3.1

I provided jspfile, struts.xml and action class below. If any
infomation required, please reply.

Any suggestion is welcome and thanks for your advice~

This is my jsp file:

--jsp file begin--
http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd";>
<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<%@ include file="/WEB-INF/jsp/inc/head_code.jsp" %>
<%if(!isLoggedIn)response.sendRedirect("./Login.jsp");%>


type="text/css">


   Shoutloud Add Item Page





enctype="multipart/form-data">








requiredposition="left"/>


   
   




--jsp file end--

This is part of my struts.xml

--struts.xml begin--

 /WEB-INF/jsp/add_item.jsp



 show_item
 /WEB-INF/jsp/add_item.jsp

--struts.xml end--

This is my action file

--action begin--
package com.shoutloud.action.user;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.ServletRequestAware;

import sun.util.logging.resources.logging;

import com.opensymphony.xwork2.ActionSupport;
import com.shoutloud.model.Item;
import com.shoutloud.model.MusicItem;
import com.shoutloud.service.AddItemService;
import com.shoutloud.service.impl.LoginServiceImpl;

public class AddItemAction extends ActionSupport implements
ServletRequestAware {

private static final long MAX_UPLOAD_SIZE = 0x200L;

private String itemType;

private String description;

private Double price;

private String url;

private String name;

private AddItemService addItemService;

private HttpServletRequest request;

private String contentType;


private File upload;

private String fileName;



public void setAddItemService(AddItemService addItemService) {
this.addItemService = addItemService;
}

public void setServletRequest(HttpServletRequest 
httpServletRequest) {

this.request = httpServletRequest;
}

@Override
public String execute() throws Exception {
if (!LoginServiceImpl.isLoggedIn(request))
return LOGIN;
if (itemType == null)
return INPUT;
int itemTypeInt = Integer.parseInt(itemType);
if (itemTypeInt == 1) {
// This is a Music Item
Item item = new Item();
item.setName(getName());
item.setDescription(getDescription());
item.setItemType(itemTypeInt);
item.setPrice(getPrice());
item.setUrl(getUrl());
MusicItem musicItem = new MusicItem();
Integer uid = (Integer) 
request.getSession().getAttribute("uid");

String relativePath=uid.toString();
//use url field to save the relative path of item file.
//this url generating arthmetic may be changed in the future.
item.setUrl(relativePath+"/"+this.fileName);
// set properties of Music Item

// save item and music item
addItemService.addMusicItem(uid, item, musicItem);
request.getSession().setAttribute("itemId", item.getId());
// add uploader

String rootPath =
ServletActionContext.getServletContext().getRealPath("/resources");
String uploadDir = (new 
StringBuilder()).append(rootPath).append("/").append(

relativePath).append("/").toString();

  File dirPath = new File(uplo

upload fail occasionally

2007-03-26 Thread Kurapica

Hi all,

I am using struts 2.0.6 and encountered a submit problem.

I have a form with enctype="multipart/form-data" property. there're
other text fields beside . when I submit, at most time it
works fine, but I found that occasionally all fields are null. I have
replaced commons-fileupload, commons-io and commons-collections with
latest versions and the problem still exists.

struts version is 2.0.6
jdk version is 1.5.0_11
tomcat version is 5.5.12
commons-fileupload version is 1.2
commons-collections version is 3.2
commons-io version is 1.3.1

I provided jspfile, struts.xml and action class below. If any
infomation required, please reply.

Any suggestion is welcome and thanks for your advice~

This is my jsp file:

--jsp file begin--
http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd";>
<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<%@ include file="/WEB-INF/jsp/inc/head_code.jsp" %>
<%if(!isLoggedIn)response.sendRedirect("./Login.jsp");%>




   Shoutloud Add Item Page















   
   




--jsp file end--

This is part of my struts.xml

--struts.xml begin--

 /WEB-INF/jsp/add_item.jsp



 show_item
 /WEB-INF/jsp/add_item.jsp

--struts.xml end--

This is my action file

--action begin--
package com.shoutloud.action.user;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.ServletRequestAware;

import sun.util.logging.resources.logging;

import com.opensymphony.xwork2.ActionSupport;
import com.shoutloud.model.Item;
import com.shoutloud.model.MusicItem;
import com.shoutloud.service.AddItemService;
import com.shoutloud.service.impl.LoginServiceImpl;

public class AddItemAction extends ActionSupport implements
ServletRequestAware {

private static final long MAX_UPLOAD_SIZE = 0x200L;

private String itemType;

private String description;

private Double price;

private String url;

private String name;

private AddItemService addItemService;

private HttpServletRequest request;

private String contentType;

private File upload;

private String fileName;


public void setAddItemService(AddItemService addItemService) {
this.addItemService = addItemService;
}

public void setServletRequest(HttpServletRequest httpServletRequest) {
this.request = httpServletRequest;
}

@Override
public String execute() throws Exception {
if (!LoginServiceImpl.isLoggedIn(request))
return LOGIN;
if (itemType == null)
return INPUT;
int itemTypeInt = Integer.parseInt(itemType);
if (itemTypeInt == 1) {
// This is a Music Item
Item item = new Item();
item.setName(getName());
item.setDescription(getDescription());
item.setItemType(itemTypeInt);
item.setPrice(getPrice());
item.setUrl(getUrl());
MusicItem musicItem = new MusicItem();
Integer uid = (Integer) 
request.getSession().getAttribute("uid");
String relativePath=uid.toString();
//use url field to save the relative path of item file.
//this url generating arthmetic may be changed in the 
future.
item.setUrl(relativePath+"/"+this.fileName);
// set properties of Music Item

// save item and music item
addItemService.addMusicItem(uid, item, musicItem);
request.getSession().setAttribute("itemId", 
item.getId());
// add uploader

String rootPath =
ServletActionContext.getServletContext().getRealPath("/resources");
String uploadDir = (new 
StringBuilder()).append(rootPath).append("/").append(
relativePath).append("/").toString();

 File dirPath = new File(uploadDir);
 if (!dirPath.exists()) {
 dirPath.mkdirs();
 }
 if (upload == null) {
 addActionError(getText("maxLengthExceeded"));