Handling different XML response documents with one SAX Handler - java

I am developing a Java application that makes an HTTP Request to a web service, and XML is returned. If the response code is 200, then a requestSucceeded() callback method will send the XML to a SAXParser with a different SAX Handler, depending on what web service is being called. If the response code is not 200, then a requestFailed() callback method is being called.
The web service that I am calling will return two types of XML documents (with a response code of 200): an XML document containing the successful response information, or an XML error document containing error information (for example, if one of the request parameters wasn't formatted correctly).
My question is this: Given my current setup, what is the best way to look for / handle both kinds of XML documents (a successful XML response or an XML error document)? The SAX Handler is looking for all of the relevant response information and it is storing that information into an object, which is then processed by my application. Is there a better solution than just always first looking for the unique XML Error tags?
Thanks!

Option #1 - Change Respose Code
Why are you returning an error with response code 200? 400 (Bad Request) or another error code might be a better option. Then you could process the XML based on the response code.
Option #2 - Swap Content Handlers
Below is a link to one of my previous answers where I explain how to swap content handlers while processing the document. You could have one content handler that determines if the response is content or error, and then swaps in the appropriate content handler to process the rest.
Using SAX to parse common XML elements
Option #3 - Use JAXB
If the end result is that the XML will be converted to an object, have you considered using JAXB? It will build an object based on the XML based on what is returned.

Related

How to take xml input from postman request body

I am creating a java server with Core java and want to create an Document object. Currently I am using InputStreamReader. I am already entring xml into the request body, Please tell me how to store the request body in The Document obj using core java, No frameworks

#WebService jaxws - how to get namespace on method not soap envelope

I am trying to create a #WebService with java 8 and jax-ws. However, I can't seem to find combination that will move the xmls "http://com.test.mymethod/mymethod" off of the envelope and onto the method. Instead every method I try adds it to the Envbelope and then puts ser: prefix before the MyWebmethod. (which would be good if I was creating and sending out wsdl but I have to match request format already in use elsewhere)
desired request format:
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Body>
<MyWebmethod xmlns="http://com.test.mymethod/mymethod">
I think the only feasible way to to this (apart from asking: "Why is a semantically identical response not desired?" - but I am afraid of the answer :-) ) would be to "post-process" the response.
The obvious solution would be to define a SOAPHandlerfor the Service-Endpoint. This should be a straight-forward process:
Grab a sample response from your service (let's call this the "input XML" for the following steps).
Create a XSL Template to transform your input XML to the desired output form.
Annotate your #Webservice with a #HandlerChain annotation.
Create the handler chain .xml file to define your SOAP handler (there is a basic example of the whole process of defining handlers over at mkyong: http://www.mkyong.com/webservices/jax-ws/jax-ws-soap-handler-in-server-side/)
Use your XSLT from step 2 in the SOAP handler to transfrom the response's DOM.
That being said - I am not 100% positive that you will be able to sufficiently influence the output of the handler chain. There is a very real chance that the resulting textual representation after the application server has serialized your DOM is still non-satisfactory.
The second option would be to define a ServletFilterand have it rewrite the textual output of your webservice endpoint. That will most certainly give you complete control over the output form - but at the (significant) cost of regenerating a DOM to feed into you XSL template.
All in all: Why would you go through all the pain? I am seriously curious.

SOAP to XML conversion and sending it in response object

I am new to webservices,some how i managed to host a service with metro.With the help of SOAP UI and also httpUrlConnection object i am able to get SOAP response.But my next task is to send a response with content type "application/xml".So i used httpServletResponse,but i am not getting how to extract only XML part(without SOAP envolope and SOAP header) and also how to send XML inside respose object.Whether the way in which i am doing is rite?If yes,how to proceed with next step.
There are various ways to send xml content within SOAP response. To keep wsdl and xsd simple, you may use CDATA for your xml content and parse like a string on the receiving end.
<![CDATA[YOUR XML GOES HERE]]>
If you want to parse SOAP message and extract XML then you may want to use Java API SAAJ

Send error message as JSON object

I have two servlet: first servlet is similar to a client and creates an HttpURLConnection to call the second servlet.
I would like send a special error, formatted like a JSON object, so I call sendError method in this way:
response.sendError(code, "{json-object}")
But in the first servlet when I read error with getResponseMessage method I just get standard HTTP message and not my json object as a string.
How I can get my json string?
From the HttpServletResponse#sendError() javadoc:
The server defaults to creating the response to look like an HTML-formatted server error page containing the specified message, setting the content type to "text/html", leaving cookies and other headers unmodified. If an error-page declaration has been made for the web application corresponding to the status code passed in, it will be served back in preference to the suggested msg parameter.
So with this approach you have no other option than extracting the message from the HTML response yourself. JSoup may however be useful in this.
To achieve what you want, you need to set the error code and write the response yourself, e.g.
response.setStatus(code);
response.setContentType("application/json");
response.setCharacterEncoding("UTF-8");
response.getWriter().write(json);
Instead of code you could by the way also use one of the HttpServletResponse.SC_XXX constants for this.

AS2: Does xml.sendAndLoad use POST or GET?

All,
I'm trying to find out, unambiguously, what method (GET or POST) Flash/AS2 uses with XML.sendAndLoad.
Here's what the help/docs (http://livedocs.adobe.com/flash/9.0/main/wwhelp/wwhimpl/common/html/wwhelp.htm?context=LiveDocs_Parts&file=00002340.html) say about the function
Encodes the specified XML object into
an XML document, sends it to the
specified URL using the POST method,
downloads the server's response, and
loads it into the resultXMLobject
specified in the parameters.
However, I'm using this method to send XML data to a Java Servlet developed and maintained by another team of developers. And they're seeing log entries that look like this:
GET /portal/delegate/[someService]?svc=setPayCheckInfo&XMLStr=[an encoded version of the XML I send]
After a Google search to figure out why the POST shows up as a GET in their log, I found this Adobe technote (http://kb2.adobe.com/cps/159/tn_15908.html). Here's what it says:
When loadVariables or getURL actions are
used to send data to Java servlets it
can appear that the data is being sent
using a GET request, when the POST
method was specified in the Flash
movie.
This happens because Flash sends the
data in a GET/POST hybrid format. If
the data were being sent using a GET
request, the variables would appear in
a query string appended to the end of
the URL. Flash uses a GET server
request, but the Name/Value pairs
containing the variables are sent in a
second transmission using POST.
Although this causes the servlet to
trigger the doGet() method, the
variables are still available in the
server request.
I don't really understand that. What is a "GET/POST hybrid format"?
Why does the method Flash uses (POST or GET) depend on whether the data is sent to a Java servlet or elsewhere (e.g., a PHP page?)
Can anyone make sense of this? Many thanks in advance!
Cheers,
Matt
Have you try doing something like that :
var sendVar=new LoadVars();
var xml=new XML("<r>test</r>");
sendVar.xml=xml;
sendVar.svc="setPayCheckInfo";
var receiveXML=new XML();
function onLoad(success) {
if (success) {
trace("receive:"+receiveXML);
} else {
trace('error');
}
}
receiveXML.onLoad=onLoad;
sendVar.sendAndLoad("http://mywebserver", receiveXML, "POST");
The hybrid format is just a term Macromedia invented to paint over its misuse of HTTP.
HTTP is very vague on what you can do with GET and POST. But the convention is that no message body is used in GET. Adobe violates this convention by sending parameters in the message body.
Flash sends the same request regardless of the server. You have problem in Servlet because most implementation (like Tomcat) ignores message body for GET. PHP doesn't care the verb and it processes the message body for GET too.

Categories