odd problem with form posts

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

odd problem with form posts

Sean Hogan-2
I have been having a problem for a while, that comes and goes. Recently
it came back and won't go.  :-(  This is Restlet 2.1.4, Java EE edition.

Sometimes when a client posts a non-empty form with
application/x-www-form-urlencoded, the form arrives empty at the @Post
method.  As you might be able to appreciate, this has been very
difficult to debug due to the lazy evaluation techniques employed in
Restlet.

Unfortunately the Restlet application is large, and part of a huge
servlet-based web app.   I have not been able to boil this problem down
to just the essentials that show the problem.  Here is something similar
to the server code:

import org.restlet.Server;
import org.restlet.data.Form;
import org.restlet.data.Protocol;
import org.restlet.resource.Post;
import org.restlet.resource.ServerResource;

public class FormProblem extends ServerResource {
     @Post
     public String testPost(Form form) {
         return form.toString();
     }

     public static void main(String[] args) throws Exception {
         new Server(Protocol.HTTP, 8182, FormProblem.class).start();
     }
}

As I said, this problem does not always happen.  For a time it happened
only on the first post and all subsequent ones were okay until the app
was restarted.  But now it is happening to me consistently (but again,
not in that tiny sample).

Has anyone seen this sort of behaviour?  Can any Restlet developer
suggest a strategy that will allow me to debug this?  What is the
earliest point in Restlet where I will be able to see the request body
in a debugger?

Thanks very much,
Sean

------------------------------------------------------
http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447&dsMessageId=3070111
Reply | Threaded
Open this post in threaded view
|

Re: odd problem with form posts

Thierry Boileau-4
Hello Sean,

what comes to my mind first is an issue where the input stream is consumed before it goes to the "testPost" method.
Is there some filters (servlet or not) that read the input stream or consume the request's representation (for example by printing it?)

You can try to debug by providing your own subclass of ServerServlet, and update your web.xml file
<servlet>
  <servlet-name>MyServlet</servlet-name>
  <servlet-class>path.to.MyServerServlet</servlet-class>
  <init-param>
   <param-name>org.restlet.application</param-name>
   <param-value>path.to.MyApplication</param-value>
  </init-param>
 </servlet>

 <servlet-mapping>
  <servlet-name>MyServlet</servlet-name>
  <url-pattern>/*</url-pattern>
 </servlet-mapping>
 
and see what happen in the "service(HttpServletRequest, HttpServletResponse)" method, especially regarding the "request.getInputStream()".

Best regards,
Thierry Boileau
ps : for a customer we use Restlet 2.1.4 with Tomcat, and it works without problems.


Best regards
Thierry Boilean



2013/12/11 Sean Hogan <[hidden email]>
I have been having a problem for a while, that comes and goes. Recently
it came back and won't go.  :-(  This is Restlet 2.1.4, Java EE edition.

Sometimes when a client posts a non-empty form with
application/x-www-form-urlencoded, the form arrives empty at the @Post
method.  As you might be able to appreciate, this has been very
difficult to debug due to the lazy evaluation techniques employed in
Restlet.

Unfortunately the Restlet application is large, and part of a huge
servlet-based web app.   I have not been able to boil this problem down
to just the essentials that show the problem.  Here is something similar
to the server code:

import org.restlet.Server;
import org.restlet.data.Form;
import org.restlet.data.Protocol;
import org.restlet.resource.Post;
import org.restlet.resource.ServerResource;

public class FormProblem extends ServerResource {
     @Post
     public String testPost(Form form) {
         return form.toString();
     }

     public static void main(String[] args) throws Exception {
         new Server(Protocol.HTTP, 8182, FormProblem.class).start();
     }
}

As I said, this problem does not always happen.  For a time it happened
only on the first post and all subsequent ones were okay until the app
was restarted.  But now it is happening to me consistently (but again,
not in that tiny sample).

Has anyone seen this sort of behaviour?  Can any Restlet developer
suggest a strategy that will allow me to debug this?  What is the
earliest point in Restlet where I will be able to see the request body
in a debugger?

Thanks very much,
Sean

------------------------------------------------------
http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447&dsMessageId=3070111

Reply | Threaded
Open this post in threaded view
|

Re: odd problem with form posts

Sean Hogan-2
Thanks for the hint Thierry.  I found a place where one of the 11 (!) filters in our app was unconditionally calling getParameter on the ServletRequest.  It appears this causes the request body to be consumed, at least when the Content-Type is x-www-form-urlencoded.  I changed it to avoid calling getParameter on non-GET requests and things are now working.

Thanks again,
Sean

On 13-12-16 09:45 AM, Thierry Boileau wrote:
Hello Sean,

what comes to my mind first is an issue where the input stream is consumed before it goes to the "testPost" method.
Is there some filters (servlet or not) that read the input stream or consume the request's representation (for example by printing it?)

You can try to debug by providing your own subclass of ServerServlet, and update your web.xml file
<servlet>
  <servlet-name>MyServlet</servlet-name>
  <servlet-class>path.to.MyServerServlet</servlet-class>
  <init-param>
   <param-name>org.restlet.application</param-name>
   <param-value>path.to.MyApplication</param-value>
  </init-param>
 </servlet>

 <servlet-mapping>
  <servlet-name>MyServlet</servlet-name>
  <url-pattern>/*</url-pattern>
 </servlet-mapping>
 
and see what happen in the "service(HttpServletRequest, HttpServletResponse)" method, especially regarding the "request.getInputStream()".

Best regards,
Thierry Boileau
ps : for a customer we use Restlet 2.1.4 with Tomcat, and it works without problems.


Best regards
Thierry Boilean



2013/12/11 Sean Hogan <[hidden email]>
I have been having a problem for a while, that comes and goes. Recently
it came back and won't go.  :-(  This is Restlet 2.1.4, Java EE edition.

Sometimes when a client posts a non-empty form with
application/x-www-form-urlencoded, the form arrives empty at the @Post
method.  As you might be able to appreciate, this has been very
difficult to debug due to the lazy evaluation techniques employed in
Restlet.

Unfortunately the Restlet application is large, and part of a huge
servlet-based web app.   I have not been able to boil this problem down
to just the essentials that show the problem.  Here is something similar
to the server code:

import org.restlet.Server;
import org.restlet.data.Form;
import org.restlet.data.Protocol;
import org.restlet.resource.Post;
import org.restlet.resource.ServerResource;

public class FormProblem extends ServerResource {
     @Post
     public String testPost(Form form) {
         return form.toString();
     }

     public static void main(String[] args) throws Exception {
         new Server(Protocol.HTTP, 8182, FormProblem.class).start();
     }
}

As I said, this problem does not always happen.  For a time it happened
only on the first post and all subsequent ones were okay until the app
was restarted.  But now it is happening to me consistently (but again,
not in that tiny sample).

Has anyone seen this sort of behaviour?  Can any Restlet developer
suggest a strategy that will allow me to debug this?  What is the
earliest point in Restlet where I will be able to see the request body
in a debugger?

Thanks very much,
Sean

------------------------------------------------------
http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447&dsMessageId=3070111


Reply | Threaded
Open this post in threaded view
|

Re: odd problem with form posts

Thierry Boileau-4
Hello Sean,

that sounds great!
Effectively, getting "parameters" consumes the request's body, as explains here http://docs.oracle.com/javaee/6/api/javax/servlet/ServletRequest.html#getParameter%28java.lang.String%29:
If the parameter data was sent in the request body, such as occurs with an HTTP POST request, then reading the body directly via getInputStream() or getReader() can interfere with the execution of this method.

Best regards,
Thierry Boileau




2013/12/16 Sean Hogan <[hidden email]>
Thanks for the hint Thierry.  I found a place where one of the 11 (!) filters in our app was unconditionally calling getParameter on the ServletRequest.  It appears this causes the request body to be consumed, at least when the Content-Type is x-www-form-urlencoded.  I changed it to avoid calling getParameter on non-GET requests and things are now working.

Thanks again,
Sean


On 13-12-16 09:45 AM, Thierry Boileau wrote:
Hello Sean,

what comes to my mind first is an issue where the input stream is consumed before it goes to the "testPost" method.
Is there some filters (servlet or not) that read the input stream or consume the request's representation (for example by printing it?)

You can try to debug by providing your own subclass of ServerServlet, and update your web.xml file
<servlet>
  <servlet-name>MyServlet</servlet-name>
  <servlet-class>path.to.MyServerServlet</servlet-class>
  <init-param>
   <param-name>org.restlet.application</param-name>
   <param-value>path.to.MyApplication</param-value>
  </init-param>
 </servlet>

 <servlet-mapping>
  <servlet-name>MyServlet</servlet-name>
  <url-pattern>/*</url-pattern>
 </servlet-mapping>
 
and see what happen in the "service(HttpServletRequest, HttpServletResponse)" method, especially regarding the "request.getInputStream()".

Best regards,
Thierry Boileau
ps : for a customer we use Restlet 2.1.4 with Tomcat, and it works without problems.


Best regards
Thierry Boilean



2013/12/11 Sean Hogan <[hidden email]>
I have been having a problem for a while, that comes and goes. Recently
it came back and won't go.  :-(  This is Restlet 2.1.4, Java EE edition.

Sometimes when a client posts a non-empty form with
application/x-www-form-urlencoded, the form arrives empty at the @Post
method.  As you might be able to appreciate, this has been very
difficult to debug due to the lazy evaluation techniques employed in
Restlet.

Unfortunately the Restlet application is large, and part of a huge
servlet-based web app.   I have not been able to boil this problem down
to just the essentials that show the problem.  Here is something similar
to the server code:

import org.restlet.Server;
import org.restlet.data.Form;
import org.restlet.data.Protocol;
import org.restlet.resource.Post;
import org.restlet.resource.ServerResource;

public class FormProblem extends ServerResource {
     @Post
     public String testPost(Form form) {
         return form.toString();
     }

     public static void main(String[] args) throws Exception {
         new Server(Protocol.HTTP, 8182, FormProblem.class).start();
     }
}

As I said, this problem does not always happen.  For a time it happened
only on the first post and all subsequent ones were okay until the app
was restarted.  But now it is happening to me consistently (but again,
not in that tiny sample).

Has anyone seen this sort of behaviour?  Can any Restlet developer
suggest a strategy that will allow me to debug this?  What is the
earliest point in Restlet where I will be able to see the request body
in a debugger?

Thanks very much,
Sean

------------------------------------------------------
http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447&dsMessageId=3070111