Forum: JRuby Getting HttpServletRequest from rails to Java class

Posted by Prachi Tripathi (yani_engico)
on 2010-08-25 16:08
Hi,

We have a rails app, from which i want to send the form post parameters
to a java class.

I am using

request.env['java.servlet_request']

to get the HttpServletRequest obj and pass it to the java class.

However it is unable to get the request parameters which are sent
through post.

If I pass the parameters as query parameters I am able to get them,
however I
am unable to get the form Post parameters

I also tried using
servlet_request
but that doesnt help either.

Is there any way aroung it?

Thank you.
Posted by Nick Sieger (Guest)
on 2010-08-25 19:37
(Received via mailing list)
On Wed, Aug 25, 2010 at 7:08 AM, Prachi Tripathi <lists@ruby-forum.com> 
wrote:
>
>
> Is there any way aroung it?

What version of JRuby-Rack are you using? This sounds like a variation
on a earlier bug where once we read the request post body, it's no
longer available to others. It may still exist in your case because
JRuby-Rack reads the post body, tries to rewind the Ruby IO object
wrapping the input stream, but perhaps the servlet input stream cannot
be rewound at all.

In this case I suggest you pass a servlet request wrapper to the Java
code as follows (note, untested):


java_import javax.http.servlet.HttpServletRequestWrapper
class RubyRequestWrapper < HttpServletRequestWrapper
  class ArrayEnumeration
    include java.util.Enumeration

    def hasMoreElements

  def initialize(request)
    super(request.env['java.servlet_request'])
    @request = request
  end
  def getParameter(key)
    request.params[key]
  end
  def getParameterNames
    request
  end
end

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email
Posted by Nick Sieger (Guest)
on 2010-08-25 19:45
(Received via mailing list)
Sorry, my email got cut off accidentally as I was writing the code.
Should've written the whole thing in a proper text editor :)

On Wed, Aug 25, 2010 at 10:37 AM, Nick Sieger <nicksieger@gmail.com> 
wrote:
>> to get the HttpServletRequest obj and pass it to the java class.
>> but that doesnt help either.
> In this case I suggest you pass a servlet request wrapper to the Java
> code as follows (note, untested):

Here's the code, hopefully it gives you an idea:

http://gist.github.com/549950

/Nick

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email
Posted by James Abley (Guest)
on 2010-08-25 22:46
(Received via mailing list)
On 25 August 2010 18:45, Nick Sieger <nicksieger@gmail.com> wrote:

> >> to a java class.
> >> If I pass the parameters as query parameters I am able to get them,
> >> however I
> >> am unable to get the form Post parameters
> >>
> >> I also tried using
> >> servlet_request
> >> but that doesnt help either.
> >>
> >> Is there any way aroung it?
>

I think it might just be a limitation described in the servlet spec.

http://download.oracle.com/javaee/1.3/api/javax/servlet/ServletRequest.html#getParameter(java.lang.String)
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.