I'm using Java to call a remote webservice, sometimes it sends me a reply that is a "valid" XML but without header, like this:
<Re>
<Re1>
<code>108</code>
<desc>Error.</desc>
<det>Imposible ejecutar</det>
</Re1>
</Re>
Due to the lack of header I cannot process it, so I'm thinking to add header myself, but before I want to check if it's a valid XML or only garbage.
How can I do that?
Related
In Java, I need to add a SOAP header containing authentication data for a client-side SOAP request to a 3rd party web service that I have no control over. I have set up a SOAPHandler, and am monitoring the actual code sent to the server-side to see what is going on. The authentication is not working.
What I precisely need is:
<soap12:Header>
<AuthenticationHeader xmlns="http://abc.xyz.com/">
<UserName>uname</UserName>
<Password>pwd</Password>
</AuthenticationHeader>
</soap12:Header>
I can create the AuthenticationHeader element and add it with the SOAPHeader.addHeaderElement method without issue, but I can't get the username and password content added properly. What I can get from using the various commands to set the text for a SOAPElement (setValue, setTextContent, addTextNode), and then using SOAPHeaderElement.addChildElement is
<soap12:Header>
<AuthenticationHeader xmlns="http://abc.xyz.com/">
<UserName xmlns="">uname</UserName>
<Password xmlns="">pwd</Password>
</AuthenticationHeader>
</soap12:Header>
The server side can't cope with the extra xmlns="" that all the commands I have tried seem to add to the UserName and Password tag names.
If I try to construct a string and add that text directly to the AutheticationHeader element, the < and > characters are escaped. I've also tried CDATA tags...that prevents escaping of the < and > characters, but the server still doesn't like it.
I found a solution constructing a Document to hold the raw text (which prevents escaping the characters), and then adding that, but unfortunately, it seems that Document can only be added to the SOAP body, not the SOAP header.
Does anyone have any suggestions how to overcome this? The server people are programming in .NET, not Java, so they aren't able to help.
I am getting the proper response back from the server, with all off the correct SOAP output tags, but the error message contained in the tag is "Can't Authenticate".
Thanks!
Following code will generate the correct Authentication Header that you would like to generate.
SOAPHeader header = request.getSOAPHeader();
Document doc = header.getOwnerDocument();
Element el1 = doc.createElementNS("http://abc.xyz.com/","UserName");
el1.setTextContent("MyName");
Element el2 = doc.createElementNS("http://abc.xyz.com/","Password");
el2.setTextContent("pass******");
Element el0 = doc.createElementNS("http://abc.xyz.com/", "AuthenticationHeader");
el0.appendChild(el1);
el0.appendChild(el2);
header.appendChild(el0);
I am working with a web framework (uPortal) that is handling errors by just throwing an exception and then hanging. The framework works by rendering XML into HTML. When there is an exception, the browser recieves rendered content up to the XML template element that is failing, and then the browser just sits and waits for a timeout. Our team's theory is that the content is sent before the error occurs, which surprised me. Other frameworks I've worked with seem to finish rendering before sending content.
My question is, is there a way to redirect the browser after content has already been sent? In this case, we are in the middle of rendering the content of a <script> tag, but the error could occur potentially anywhere in the html.
My only current thought is to inject some javascript at the top of the page, and to try to change the framework's behavior to fail quickly and close the connection and add </body> and </html> tags when an error occurs. Then the above mentioned javascript would run on pageload and detect if the entire page's content was there and do a client-side redirect if not. Maybe it could look for a special hidden div at the bottom of the page.
Are there any examples of frameworks solving this problem differently or of people using similar framework working around this issue?
You must either capture the error, or capture the output in a buffer. If you can handle the exception, you can probably print a simple script tag like
<script> window.location.href = 'some_new_url';</script>
If the browser understands the doctype to be something related to HTML, it will execute that tag.
If you can capture the output in a buffer, when you handle the error you can decide to send an HTTP redirect to the browser and destroy the output buffer up to that point.
As for other frameowrks, in PHP, you can simply enable output buffering with ob_start(), which won't start sending content until the request is fully completed.
I don't know that framework, but
In http, every response has a response-code associated with it. Since the page is already half-way transferred / rendered that status code (usually "200") was sent (and received) already.
There's no way for the browser to accept another response code (like "301" for redirect) for the same response! Also the server is not able to send another response code, because the original response code was already commited and sent to the client.
Your description of the error and knowledge of the http-protocol implies that there is probably some implementation error in the framework / server components used, OR it was done deliberatly, risking the situation that you are in now...
to redirect a page , you need to set redirect information in header. but you can write header once you start writing content ( may be header is already received by client by the time you compete writing whole document )
But, you can do it in different way as below
1.let document loading complete and record if you need to redirect the page while rendering
2. add a unique request-id identifier for each page load
3. invoke ajax call with request-id ( may be rest call) to server asking if page needs to be redirected.
4. if page needs to be redirected , do so, via javascript in browser at client end.
A HTTP response consists of headers and an optional response content.
Once you have started to write the response to the socket connection you can't revert it. In your example: If you run into an error in the middle of content generation you can't add a redirect header - the header section has already be written.
The statement above is not entirely true: in HTTP chunked transfer encoding the response is sent in separate chunks. The last chunk can have an optional trailer containing entity-header fields and theoretically a redirect header. But if you can use these mechanism is a different question. For instance a servlet container may use chunked transfer encoding but does not give you an API to set the trailer.
But writing must not start immediately: For instance HttpServletResponse maintains a buffer for the response content. If you set headers and start writing the content only the buffer is filled and you still can reset the response and start all over. But once the buffer overflows the response is written to the connection and the HttpServletResponse is now committed.
Such a mechanism gives you way to deal with errors during content generation which happen when the response is not yet committed: Just reset the response and send an error message instead. You could examine your framework if it supports such an mechanism. But obviously this is not a solution for larger responses.
A second way to avoid errors during content generation is simply to make sure that they can't happen. First gather all your data needed for the response (e.g. making unsafe database calls), then in a second step generate the response - the second now step should not fail (except if you have bugs in your code).
You already mentioned a third way to handle an error, by having the client sanitize the response and take some action it errors are detected (e.g. by including a script in the generated HTML response).
The only reliable way to do this is to create a proxy HttpServletResponse object that caches the response. You'd need to give the uPortal this proxy instead of the actual HttpServletResponse, and only send the output using the real response once the processing completes / send redirect if the processing fails.
It is HTTP protocol design limitation that you cannot send HTTP redirect once output was started.
Other possible ways rely on HTML or Javascript redirects, but since you write that the error may happen at any moment, it would be difficult to print it out in a way that the browsers would reliably interpret it as redirect.
I'm developing a program that queries and prints out open data from the local transit authority, which is returned in the form of an XML response.
Normally, when there are buses scheduled to run in the next few hours (and in other typical situations), the XML response generated by the page is handled correctly by the java.net.URLConnection.getInputStream() function, and I am able to print the individual results afterwards.
The problem is when the buses are NOT running, or when some other problem with my queries develops after it is sent to the transit authority's web server. When the authority developed their service, they came up with their own unique error response codes, which are also sent as XMLs. For example, one of these error messages might look like this:
<Error xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<Code>3005</Code>
<Message>Sorry, no stop estimates found for given values.</Message>
</Error>
(This code and similar is all that I receive from the transit authority in such situations.)
However, it appears that URLConnection.getInputStream() and some of its siblings are unable to interpret this custom code as a "valid" response that I can handle and print out as an error message. Instead, they give me a more generic HTTP/1.1 404 Not Found error. This problem cascades into my program which then prints out a java.io.FileNotFoundException error pointing to the offending input stream.
My question is therefore two-fold:
1. Is there a way to retrieve, parse, and print a custom XML-formatted error code sent by a web service using the plugins that are available in Java?
2. If the above is not possible, what other tools should I use or develop to handle such custom codes as described?
URLConnection isn't up to the job of REST, in my opinion, and if you're using getInputStream, I'm almost certain you're not handling character encoding correctly.
Check out Spring's RestTemplate - it's really easy to use (just as easy as URLConnection), powerful and flexible. You will need to change the ResponseErrorHandler, because the default one will throw an exception on 404, but it looks like you want it to carry on and parse the XML in the response.
Basically I need to provide REST service that would receive a String param, use that param to fetch a file from another system and then return the fetched file back as the response.
The effect should be the same as when a user clicks on a pdf or any other binary file link and the browser prompts him to save/download that file.
A couple of points:
is it possible to stream the file (to send bytes as I receive them from source system). In other words, how to handle very large files?
also related to streaming, when using regular HttpServletResponse, do I have to wait until a large file is completely read to return response.build()?
How do I go around doing this using Apache Wink?
PS Sorry, this may be trivial for Wink gurus, but I'm just starting to wrap my head around developer guide.
You can just return the java.io.File from your method. You can wrap it with Response if you like. Wink will handle the streaming. The streaming doesn't start when you call to response.build(), but rather when your method finishes.
If you want a correct download dialog, you should return the proper Content-Disposition header. See How to set response header in JAX-RS so that user sees download popup for Excel?
Im working with a server which always sends me XML responds. But sometimes when server is lack or something it reports me about it by sending me back a HTML page (it just a html page informing about the error) but i didn't expect that and my XML parser crashed.
Im using DefaultHttpClient() and I do send header like mHttpRequest.setHeader("Accept", "text/xml");
So what is the proper way to ensure i got XML (or other specific format) response?
As Kristian suggested, see if it provides a different Content-Type when HTML is emitted. Failing that I would check for a <?xml... line, as apposed to a doctype or whatever is on the HTML page.
Can you check the content type header on the response?
Something like (if I understand the Android documentation correctly):
"text/xml".equals(httpResponse.getEntity().getContentType().getValue());
Every XML contains document descriptor <?xml version="1.0" encoding="utf-8"?> in the beginning. You can check for this before parsing it.
However, I think you need some error handling and result validation in your parser. Network is not a safe environment - you can easily get a half-broken, malformed, or even forged XML from the network. Good parser should detect that and report corresponding errors, not just crash.