How to get the status of a request by another request? - java

Let me explain what I am doing first:
I have a servlet that handles some GET, POST and PUT requests.
Now on my PUT request I am saving a file from the request.inputStream. Now I want to do some things like I issue another GET request that can give me the status of reading the input stream of that previous request. I can issue an PUT request that can put some binary data with range for that file I am saving previously. Or I can send a DELETE request that will cancel the upload.
How can I do that? How can I access one request from another?

You could use the HttpSession object on the request and save the bytes read on the InputStream.
The session attribute will be accessible on the following request.

Related

Capturing Response Size - HttpServlet

I have an application where the users are accessing a servlet file which will read a .pdf file conditionally and send the .pdf file as response to the users request. The servlet code does not sets the content length on the response header.
In the above scenario I want to capture the Response Size of the each user request without making any changes in the servlet code.
At the same time I do not want to use the HttpServletResponseWrapper for some other reasons.
Please suggest me on the possible ways to achieve this.
Thanks in advance.
Every container supports Web Access Logging. Look for that and enable it. It will log the response content length along with other request and response parameters.

Reading Jira Webhook POST data

I am trying to read Jira issue data using a webhook that posts the data to my servlet.
When I travserve the request parameters map, I don't find anything in it.
But the content lenght shows as "8876" which means webhook is sending the data. Somehow I am not able to read/retrieve the data in my servlet.
Also checked, content-type returns as "application/json".
Does anyone know how to read Jira webhook post data?
You have to read the response body, not the parameters map. For that purpose you can use
request.getInputStream();
or
request.getReader();
method.
PS: You can configure the web hook to post data to http://requestb.in/ so you can easily analyze the request parameters, the request body, the headers, etc.

Java - send HTTP POST request without downloading all the content

Is it possible to send HTTP POST request to a webserver and retrieve just headers of response or read just few bytes of the body, so the rest won't be downloaded at all (so it won't consume traffic)? If yes, how?
I know that there is a HEAD method for this, but I need to achieve it by POST method .. well, I am not sure if I need the POST method, I just need to post the data. Maybe if the webserver isn't secured well enough (it doesn't check what method it's used - it's just directly access the post data), is it possible to send "post data" by HEAD request?
There is no built-in HTTP mechanism for this, and HTTP HEAD requests do not allow content in the body. If however you are the one writing the server code then anything is possible.
If this is the case, I would suggest a URL parameter that triggers this behavior. For example:
POST /myURL - This would return the whole response
POST /myURL?body=minimal - Returns the reduced size response that you are looking for.
And you would have to code your server method to construct and return the appropriate response based on the URL parameter.

how does doPost request works? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Servlets: doGet and doPost
I know doGet() request is appended to the request URL in a query string.But I don't know the concept of doPost() request.how does doPost request posting information to the server.
Please Guide me to get the working concept of doPost request...
Post requests are used usually for sending data to Server, and get request for reading data from server. In Post request data is sent in http request body, so data size can be very large compared to Get. If a browser fires an POST request (usually a form submit) doPost of the mapping Servlet will be called. There is another overloaded method (service()) which is called for both GET and POST
In doPost() the data is not appended in the URL.
It can handle large amount of data compared to the doGet() method.
Filling of form and submitting is done through doPost(), it's secure to use doPost() during submission of the username and password.
There is also differnce in the doGet() and doPost() header and body structure.
The main conceptual difference GET and POST is that, GET is used for getting the data from the server, and POST is used for updating the data to the server.
In general POST has the following properties:
The data is x-www-form-urlencoded . Which means, the request parameters are sent as request body. And the server has to parse the request body for parameters.
By default, when no content-length header is present, the default value for GET is 0 whereas for POST it is till end of stream.
GET is Idempotent whereas POST is Non-Idempotent. i.e, Proxies on failures for GET they retry. But, for POST they do not retry.
doGet() can be used when client request doesn't intend to change stored data.

How can the servlet determine when an upload has been cancelled?

Here is some code on the javascript side for form-based uploads:
iframe.setAttribute('src', 'javascript:false;');
I'm using the code above to cancel an in-progress upload associated with an input element placed in an iframe.
I'm using the code below to cancel an in-progress upload sent via XHR:
myxhr.abort();
In both cases, no more bytes are sent to the servlet. The part I'm struggling with is on the servlet side. Currently, I can't figure out a way for the servlet instance to determine if the user has cancelled the upload. This is critical, otherwise the servlet will go on and process the partially uploaded file as if it is valid.
How can I determine, via the HttpServletRequest, if a user has cancelled the upload?
The POST request with the data contains the Content-Length header which tells you the size of the data that is going to be uploaded.
So when the data stops coming to your server and the size of the data received is less than expected - it would mean that the user (or some network glitch) has canceled the upload.
If the upload has been cancelled the browser will close the connection, resulting in a an IO exception on the servlet side. For example, in Tomcat it will say "Connection reset by peer" and this is a ClientAbortException. Other servers wrap the IOException differently. Point is just catch the IOException and you should be able to handle it as you wish.
Using content-length is not reliable because the HTTP spec does not require content-length headers for POSTs - or for GETs for the matter. Point is, unless you are sure your javascript XHR sets the header explicitly, this method won't work.
Alternatively you could calculate it yourself and set it to be sure, or even better append your own character stream to the end of a the posted data in the XHR, some unique string of characters, e.g. "jh923k49sk$2#%'. In the servlet, snip off the last 14 characters of the inbound message and check it against the string. If it is the same you know they didn't cancel.
I don't see how you can tell, just because a request stream has ended, whether it's done or cancelled. There would have to be a separate HTTP request to indicate cancellation that would have to include some token or ID associated with the upload, since HTTP is stateless and idempotent.

Categories

Resources