RE: calling request.getInputStream() within Action
Hi Craig Filter, hmm. Thanks, I'll look into that :) Rob -Original Message- From: Craig McClanahan [mailto:[EMAIL PROTECTED] Sent: Fri 16/07/2004 00:34 To: Struts Users Mailing List Cc: Subject: Re: calling request.getInputStream() within Action Robert Shields wrote: >Hi Craig, > >Yes, I thought as much. >Do you know if it's possible to exactly recreate the binary HTTP body from parsed parameters, or elsewhere for that matter? > > The only thing I could think of would be a Filter -- requires Servlet 2.3 or later -- that would be able to scrape a copy of the incoming request, yet still provide getInputStream() and getReader() methods that would let the actual servlet being invoked operate normally. You'll also have to watch out for the complexity that a servlet can call *either* getInputStream() *or* getReader() on any single call. That means, among other things, that you'll need to create a request wrapper that not only does the copying for you, but also fakes the getReader() method, since you'll have already called getInputStream() on the real underlying request. > >I'm writing a web proxy using HttpClient - currently I've only tested a urlencoded form, which works fine with the following code. I wonder how it will fair when it encounters a mutipart form - I haven't tested yet. > >HttpClient client = new HttpClient(); > >... > >if (request.getMethod().toLowerCase().equals("post")) > >{ > >HttpMethod method = new PostMethod(url.toString()); > >PostMethod postMethod = (PostMethod)method; > >// populate form parameters > >Enumeration paramEnum = request.getParameterNames(); > >while (paramEnum.hasMoreElements()) { > >String key = ((String) paramEnum.nextElement()); > >String[] values = request.getParameterValues(key); > >String value = ""; > >for(int i=0; i >value += values[i] + (i < values.length-1 ? "," : ""); > >postMethod.addParameter(key, value); > >} > >} > > Sorry for the HTML email - outlook web access :) > > > You'll probably need to check the content type as well ... the above works for how browsers normally post data (content type "application/x-www-form-urlencoded"), but multipart uploads use something different. On the other hand, if you never call getParameterXxx() type methods in your servlet, you should have been able to copy the input stream as in your original approach -- you might want to double check that you're not doing that. >Rob > > Craig > -Original Message----- > From: Craig McClanahan [mailto:[EMAIL PROTECTED] > Sent: Thu 15/07/2004 17:50 > To: Struts Users Mailing List > Cc: > Subject: Re: calling request.getInputStream() within Action > > > > Robert Shields wrote: > > >Hi Bill > > > >Thanks for your response. > >I've tried with a servlet too, but even without calling > >request.getParameter the stream is still empty: > > > > > > > One way to paint yourself into this particular corner is if you're > trying this on a POST request. As soon as you call a method like > request.getParameter(), the container has to read the content of the > input stream (since that is where the key/value pairs are), so trying to > read the stream after that is not going to work. > > Craig > > > - > To unsubscribe, e-mail: [EMAIL PROTECTED] >
Re: calling request.getInputStream() within Action
Robert Shields wrote: Hi Craig, Yes, I thought as much. Do you know if it's possible to exactly recreate the binary HTTP body from parsed parameters, or elsewhere for that matter? The only thing I could think of would be a Filter -- requires Servlet 2.3 or later -- that would be able to scrape a copy of the incoming request, yet still provide getInputStream() and getReader() methods that would let the actual servlet being invoked operate normally. You'll also have to watch out for the complexity that a servlet can call *either* getInputStream() *or* getReader() on any single call. That means, among other things, that you'll need to create a request wrapper that not only does the copying for you, but also fakes the getReader() method, since you'll have already called getInputStream() on the real underlying request. I'm writing a web proxy using HttpClient - currently I've only tested a urlencoded form, which works fine with the following code. I wonder how it will fair when it encounters a mutipart form - I haven't tested yet. HttpClient client = new HttpClient(); ... if (request.getMethod().toLowerCase().equals("post")) { HttpMethod method = new PostMethod(url.toString()); PostMethod postMethod = (PostMethod)method; // populate form parameters Enumeration paramEnum = request.getParameterNames(); while (paramEnum.hasMoreElements()) { String key = ((String) paramEnum.nextElement()); String[] values = request.getParameterValues(key); String value = ""; for(int i=0; i value += values[i] + (i < values.length-1 ? "," : ""); postMethod.addParameter(key, value); } } Sorry for the HTML email - outlook web access :) You'll probably need to check the content type as well ... the above works for how browsers normally post data (content type "application/x-www-form-urlencoded"), but multipart uploads use something different. On the other hand, if you never call getParameterXxx() type methods in your servlet, you should have been able to copy the input stream as in your original approach -- you might want to double check that you're not doing that. Rob Craig -Original Message- From: Craig McClanahan [mailto:[EMAIL PROTECTED] Sent: Thu 15/07/2004 17:50 To: Struts Users Mailing List Cc: Subject: Re: calling request.getInputStream() within Action Robert Shields wrote: >Hi Bill > >Thanks for your response. >I've tried with a servlet too, but even without calling >request.getParameter the stream is still empty: > > > One way to paint yourself into this particular corner is if you're trying this on a POST request. As soon as you call a method like request.getParameter(), the container has to read the content of the input stream (since that is where the key/value pairs are), so trying to read the stream after that is not going to work. Craig - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] This e-mail has been scanned for all viruses by Star Internet. The service is powered by MessageLabs. For more information on a proactive anti-virus service working around the clock, around the globe, visit: http://www.star.net.uk _ This e-mail has been scanned for all viruses by Star Internet. The service is powered by MessageLabs. For more information on a proactive anti-virus service working around the clock, around the globe, visit: http://www.star.net.uk _ - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
RE: calling request.getInputStream() within Action
Hi Craig, Yes, I thought as much. Do you know if it's possible to exactly recreate the binary HTTP body from parsed parameters, or elsewhere for that matter? I'm writing a web proxy using HttpClient - currently I've only tested a urlencoded form, which works fine with the following code. I wonder how it will fair when it encounters a mutipart form - I haven't tested yet. HttpClient client = new HttpClient(); ... if (request.getMethod().toLowerCase().equals("post")) { HttpMethod method = new PostMethod(url.toString()); PostMethod postMethod = (PostMethod)method; // populate form parameters Enumeration paramEnum = request.getParameterNames(); while (paramEnum.hasMoreElements()) { String key = ((String) paramEnum.nextElement()); String[] values = request.getParameterValues(key); String value = ""; for(int i=0; imailto:[EMAIL PROTECTED] Sent: Thu 15/07/2004 17:50 To: Struts Users Mailing List Cc: Subject: Re: calling request.getInputStream() within Action Robert Shields wrote: >Hi Bill > >Thanks for your response. >I've tried with a servlet too, but even without calling >request.getParameter the stream is still empty: > > > One way to paint yourself into this particular corner is if you're trying this on a POST request. As soon as you call a method like request.getParameter(), the container has to read the content of the input stream (since that is where the key/value pairs are), so trying to read the stream after that is not going to work. Craig - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] This e-mail has been scanned for all viruses by Star Internet. The service is powered by MessageLabs. For more information on a proactive anti-virus service working around the clock, around the globe, visit: http://www.star.net.uk _ This e-mail has been scanned for all viruses by Star Internet. The service is powered by MessageLabs. For more information on a proactive anti-virus service working around the clock, around the globe, visit: http://www.star.net.uk _ - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: calling request.getInputStream() within Action
Robert Shields wrote: Hi Bill Thanks for your response. I've tried with a servlet too, but even without calling request.getParameter the stream is still empty: One way to paint yourself into this particular corner is if you're trying this on a POST request. As soon as you call a method like request.getParameter(), the container has to read the content of the input stream (since that is where the key/value pairs are), so trying to read the stream after that is not going to work. Craig - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
RE: calling request.getInputStream() within Action
Hi Paul I'm trying to pass it to HttpClient - I'm writing a screen scraper (proxy). At the moment I have to enumerate the request parameters and pass them one by one to HttpClient - I have a feeling this won't work when I test it with a multipart encoded form (although I may be able to make use of the struts multipart stuff). It's a whole lot easier to write: HttpClient client = new HttpClient(); HttpMethod method = new PostMethod(url.toString()); postMethod.setRequestBody(request.getInputStream()); Rob -Original Message- From: Paul McCulloch [mailto:[EMAIL PROTECTED] Sent: 15 July 2004 17:00 To: 'Struts Users Mailing List' Subject: RE: calling request.getInputStream() within Action Can I ask why you are after the binary content of the request? Paul > -Original Message- > From: Robert Shields [mailto:[EMAIL PROTECTED] > Sent: 15 July 2004 16:02 > To: Struts Users Mailing List > Subject: RE: calling request.getInputStream() within Action > > > Hi Bill > > Thanks for your response. > I've tried with a servlet too, but even without calling > request.getParameter the stream is still empty: > > > public class TestServlet extends HttpServlet { >ResourceBundle rb = ResourceBundle.getBundle("LocalStrings"); > >public void doGet(HttpServletRequest request, > HttpServletResponse response) >throws IOException, ServletException >{ > int length = request.getContentLength(); // 18 > String contentType= request.getContentType(); > // application/x-www-form-urlencoded > > InputStream inputStream = request.getInputStream(); > int bytesAvailable = inputStream.available(); // 0 > > byte[] buf = new byte[1024]; > int bytesread; > > // bytesread is always -1 > while((bytesread = inputStream.read(buf)) != -1) > { > // do stuff > } >} > >public void doPost(HttpServletRequest request, > HttpServletResponse response) >throws IOException, ServletException >{ >doGet(request, response); >} > } > > > This should work, right? > I'm starting to think something is wrong with my Tomcat! > > Regards, > Rob > > -Original Message- > From: news [mailto:[EMAIL PROTECTED] On Behalf Of Bill Siggelkow > Sent: 15 July 2004 14:16 > To: [EMAIL PROTECTED] > Subject: Re: calling request.getInputStream() within Action > > Robert, I found the following link that discusses this: > > http://archives.java.sun.com/cgi-bin/wa?A2=ind0106&L=jsp-inter > est&F=&S=& > P=49196 > > It sounds like you are correct -- if request.getParameter() has been > called you cannot get the input stream -- have you considered using a > Servlet instead? > > Robert Shields wrote: > > > Hi Struts Users > > > > I'm trying to get the InputStream to read the binary contents of the > > request within an Action's execute method. E.g. > > > > > > > > public ActionForward execute( > > ActionMapping mapping, > > ActionForm form, > > HttpServletRequest request, > > HttpServletResponse response) > > throws IOException, ServletException { > > > > InputStream inputStream = request.getInputStream(); > > > > // bytesAvailable is always 0 > > int bytesAvailable = inputStream.available(); > > > > int bytesRead; > > byte[] buf = new byte[1024]; > > while((bytesRead = inputStream.read(buf)) != -1) > > { > > // do stuff > > } > > } > > > > So bytesAvailable is always 0, and inputStream.read(buf) immediately > > returns -1. > > > > I have heard that calling request.getParamter() before trying to > access > > the input stream can affect accessing the input stream. I believe > struts > > calls request.getParamter (it must do to populate the form bean). > > > > My question is, how can I get access to the entire binary > contents of > > the HTTP body? > > > > I'm using Tomcat 5.0.25 BTW > > > > TIA > > Rob > > > > > > This e-mail has been scanned for all viruses by Star Internet. The > > service is powered by MessageLabs. For more information on > a proactive > > anti-virus service working around the clock, around the > globe, visit: > > http://www.star.net.uk > > > _ > &
RE: calling request.getInputStream() within Action
Can I ask why you are after the binary content of the request? Paul > -Original Message- > From: Robert Shields [mailto:[EMAIL PROTECTED] > Sent: 15 July 2004 16:02 > To: Struts Users Mailing List > Subject: RE: calling request.getInputStream() within Action > > > Hi Bill > > Thanks for your response. > I've tried with a servlet too, but even without calling > request.getParameter the stream is still empty: > > > public class TestServlet extends HttpServlet { >ResourceBundle rb = ResourceBundle.getBundle("LocalStrings"); > >public void doGet(HttpServletRequest request, > HttpServletResponse response) >throws IOException, ServletException >{ > int length = request.getContentLength(); // 18 > String contentType= request.getContentType(); > // application/x-www-form-urlencoded > > InputStream inputStream = request.getInputStream(); > int bytesAvailable = inputStream.available(); // 0 > > byte[] buf = new byte[1024]; > int bytesread; > > // bytesread is always -1 > while((bytesread = inputStream.read(buf)) != -1) > { > // do stuff > } >} > >public void doPost(HttpServletRequest request, > HttpServletResponse response) >throws IOException, ServletException >{ >doGet(request, response); >} > } > > > This should work, right? > I'm starting to think something is wrong with my Tomcat! > > Regards, > Rob > > -Original Message----- > From: news [mailto:[EMAIL PROTECTED] On Behalf Of Bill Siggelkow > Sent: 15 July 2004 14:16 > To: [EMAIL PROTECTED] > Subject: Re: calling request.getInputStream() within Action > > Robert, I found the following link that discusses this: > > http://archives.java.sun.com/cgi-bin/wa?A2=ind0106&L=jsp-inter > est&F=&S=& > P=49196 > > It sounds like you are correct -- if request.getParameter() has been > called you cannot get the input stream -- have you considered using a > Servlet instead? > > Robert Shields wrote: > > > Hi Struts Users > > > > I'm trying to get the InputStream to read the binary contents of the > > request within an Action's execute method. E.g. > > > > > > > > public ActionForward execute( > > ActionMapping mapping, > > ActionForm form, > > HttpServletRequest request, > > HttpServletResponse response) > > throws IOException, ServletException { > > > > InputStream inputStream = request.getInputStream(); > > > > // bytesAvailable is always 0 > > int bytesAvailable = inputStream.available(); > > > > int bytesRead; > > byte[] buf = new byte[1024]; > > while((bytesRead = inputStream.read(buf)) != -1) > > { > > // do stuff > > } > > } > > > > So bytesAvailable is always 0, and inputStream.read(buf) immediately > > returns -1. > > > > I have heard that calling request.getParamter() before trying to > access > > the input stream can affect accessing the input stream. I believe > struts > > calls request.getParamter (it must do to populate the form bean). > > > > My question is, how can I get access to the entire binary > contents of > > the HTTP body? > > > > I'm using Tomcat 5.0.25 BTW > > > > TIA > > Rob > > > > > > This e-mail has been scanned for all viruses by Star Internet. The > > service is powered by MessageLabs. For more information on > a proactive > > anti-virus service working around the clock, around the > globe, visit: > > http://www.star.net.uk > > > _ > > > - > To unsubscribe, e-mail: [EMAIL PROTECTED] > For additional commands, e-mail: [EMAIL PROTECTED] > > > This e-mail has been scanned for all viruses by Star Internet. The > service is powered by MessageLabs. For more information on a proactive > anti-virus service working around the clock, around the globe, visit: > http://www.star.net.uk > _ > > > This e-mail has been scanned for all viruses by Star Internet. The > service is powered by MessageLabs. For more information on a proactive > anti-virus service working around the clock, arou
RE: calling request.getInputStream() within Action
Hi Bill Thanks for your response. I've tried with a servlet too, but even without calling request.getParameter the stream is still empty: public class TestServlet extends HttpServlet { ResourceBundle rb = ResourceBundle.getBundle("LocalStrings"); public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { int length = request.getContentLength(); // 18 String contentType= request.getContentType(); // application/x-www-form-urlencoded InputStream inputStream = request.getInputStream(); int bytesAvailable = inputStream.available(); // 0 byte[] buf = new byte[1024]; int bytesread; // bytesread is always -1 while((bytesread = inputStream.read(buf)) != -1) { // do stuff } } public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { doGet(request, response); } } This should work, right? I'm starting to think something is wrong with my Tomcat! Regards, Rob -Original Message- From: news [mailto:[EMAIL PROTECTED] On Behalf Of Bill Siggelkow Sent: 15 July 2004 14:16 To: [EMAIL PROTECTED] Subject: Re: calling request.getInputStream() within Action Robert, I found the following link that discusses this: http://archives.java.sun.com/cgi-bin/wa?A2=ind0106&L=jsp-interest&F=&S=&; P=49196 It sounds like you are correct -- if request.getParameter() has been called you cannot get the input stream -- have you considered using a Servlet instead? Robert Shields wrote: > Hi Struts Users > > I'm trying to get the InputStream to read the binary contents of the > request within an Action's execute method. E.g. > > > > public ActionForward execute( > ActionMapping mapping, > ActionForm form, > HttpServletRequest request, > HttpServletResponse response) > throws IOException, ServletException { > > InputStream inputStream = request.getInputStream(); > > // bytesAvailable is always 0 > int bytesAvailable = inputStream.available(); > > int bytesRead; > byte[] buf = new byte[1024]; > while((bytesRead = inputStream.read(buf)) != -1) > { > // do stuff > } > } > > So bytesAvailable is always 0, and inputStream.read(buf) immediately > returns -1. > > I have heard that calling request.getParamter() before trying to access > the input stream can affect accessing the input stream. I believe struts > calls request.getParamter (it must do to populate the form bean). > > My question is, how can I get access to the entire binary contents of > the HTTP body? > > I'm using Tomcat 5.0.25 BTW > > TIA > Rob > > > This e-mail has been scanned for all viruses by Star Internet. The > service is powered by MessageLabs. For more information on a proactive > anti-virus service working around the clock, around the globe, visit: > http://www.star.net.uk > _ - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] This e-mail has been scanned for all viruses by Star Internet. The service is powered by MessageLabs. For more information on a proactive anti-virus service working around the clock, around the globe, visit: http://www.star.net.uk _ This e-mail has been scanned for all viruses by Star Internet. The service is powered by MessageLabs. For more information on a proactive anti-virus service working around the clock, around the globe, visit: http://www.star.net.uk _ - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: calling request.getInputStream() within Action
Robert, I found the following link that discusses this: http://archives.java.sun.com/cgi-bin/wa?A2=ind0106&L=jsp-interest&F=&S=&P=49196 It sounds like you are correct -- if request.getParameter() has been called you cannot get the input stream -- have you considered using a Servlet instead? Robert Shields wrote: Hi Struts Users I'm trying to get the InputStream to read the binary contents of the request within an Action's execute method. E.g. public ActionForward execute( ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { InputStream inputStream = request.getInputStream(); // bytesAvailable is always 0 int bytesAvailable = inputStream.available(); int bytesRead; byte[] buf = new byte[1024]; while((bytesRead = inputStream.read(buf)) != -1) { // do stuff } } So bytesAvailable is always 0, and inputStream.read(buf) immediately returns -1. I have heard that calling request.getParamter() before trying to access the input stream can affect accessing the input stream. I believe struts calls request.getParamter (it must do to populate the form bean). My question is, how can I get access to the entire binary contents of the HTTP body? I'm using Tomcat 5.0.25 BTW TIA Rob This e-mail has been scanned for all viruses by Star Internet. The service is powered by MessageLabs. For more information on a proactive anti-virus service working around the clock, around the globe, visit: http://www.star.net.uk _ - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]