Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

calling getInputStream after getParameter

 
Rajagopal Manohar
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I have a requirment where I need to do something like this



The problem is that the inputstream get reset to null after calling the getParameter

the getParameter values are used at 3 places, only at one place I am required to build the value from the inputstream

Any pointers on how to solve it arre welcome. So far I can think of writing getParameter like funtinality myself but any other non complicated ways will be very helpfull

PS: Can we assume that the & symbol will always mean the start of a new parameter

Thanks in advance,
Raj
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13061
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since getParameter triggers the reading of the input stream to the end, there is no way you can do both. You may have to get the input stream and do the parsing yourself. The information missing from your post:
1. Is the request a GET or a POST?
2. Where does the request come from? An HTML form? Javascript? whatever?

Bill
 
Rajagopal Manohar
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply

Its a POST request

The request comes from multiple locations

1. Java Batch programs
2. JSP application (Both HTML forms and javascript)
3. 3rd part applications

Basically I can make no assumptions about the front end neither can I change them.

Any pointers on how to parse the inputstream would be very helpfull

is it just key1=val1&key2=val2&key3=val3 and so on or are there some pittfalls I should avoid

Thanks again,
Raj
 
Rajagopal Manohar
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
looks like HttpUtils class will do what I was looking for. But its deprecated any idea why and alternatives if any will be helpfull

Thanks in advance,
Raj
 
ak pillai
author
Ranch Hand
Posts: 288
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How about using

public java.util.Map getParameterMap()

to store your parameters in an immutable HashMap and subsequently build it from the map as opposed to InputStream.
 
Rajagopal Manohar
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the thing is there might be a scenario where there are no parameters but I still want to read the inputstream

do I make sense or am I complicating a simple solution
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64833
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why do you need the input stream in the first place?
 
Rajagopal Manohar
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
actually the inputstream contains a xml document. The document some times comes as one of the parameters "xmlDoc" or the inputstream contains only the xml document.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64833
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So this is a file upload?
 
Rajagopal Manohar
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think no, The xml may be generated by a batch program, other servlets or even javascript. Actually I am not confident on how when where the request is generated and have no control over it. For all you know it could be a file upload.

ps: some times the xmlDoc is just one of the request parameters, other times it is the post body
[ March 02, 2006: Message edited by: Rajagopal Manohar ]
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13061
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since you can't change the way requests are created, it looks to me like you will have to grab the contents of the input stream to a byte[], grab all the header information to some sort of Map, and then decide what to do with it.
I say byte[] because that leaves you the flexibility to handle data as binary or as characters.
If there was something in the request headers that could let you decide how to treat the body of the POST you could use a Filter to look at the headers and direct the request to the appropriate servlet.
Bill
 
Rajagopal Manohar
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Rajagopal Manohar:
looks like HttpUtils class will do what I was looking for. But its deprecated any idea why and alternatives if any will be helpfull

Thanks in advance,
Raj


Thanks for the replys,

I am using this class as it seems to do what I want read a string and return a hashtable of parameters or throw a exception. Is there any thing that can go wrong with this approach?

-Raj
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by William Brogden:

If there was something in the request headers that could let you decide how to treat the body of the POST you could use a Filter to look at the headers and direct the request to the appropriate servlet.
Bill


Yes, if the clients are at all well written then they will adhere to the HTTP spec and send you a "Content-Type" request header.
If it's application/x-www-form-urlencoded then you should look for the data in a request parameter.
If it's text/xml then you would know to parse the body of the message as XML.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic