Author: keith Date: Wed Sep 3 09:28:50 2008 New Revision: 21449 URL: http://wso2.org/svn/browse/wso2?view=rev&revision=21449
Log: Fixing handling of JSON to deal with XML as well as text Modified: trunk/mashup/java/modules/hostobjects/src/org/wso2/mashup/hostobjects/wsrequest/WSRequestHostImpl.java Modified: trunk/mashup/java/modules/hostobjects/src/org/wso2/mashup/hostobjects/wsrequest/WSRequestHostImpl.java URL: http://wso2.org/svn/browse/wso2/trunk/mashup/java/modules/hostobjects/src/org/wso2/mashup/hostobjects/wsrequest/WSRequestHostImpl.java?rev=21449&r1=21448&r2=21449&view=diff ============================================================================== --- trunk/mashup/java/modules/hostobjects/src/org/wso2/mashup/hostobjects/wsrequest/WSRequestHostImpl.java (original) +++ trunk/mashup/java/modules/hostobjects/src/org/wso2/mashup/hostobjects/wsrequest/WSRequestHostImpl.java Wed Sep 3 09:28:50 2008 @@ -17,12 +17,14 @@ import org.apache.axiom.om.OMElement; import org.apache.axiom.om.OMNode; +import org.apache.axiom.om.OMDataSource; import org.apache.axiom.om.impl.builder.StAXOMBuilder; import org.apache.axiom.om.impl.llom.OMSourcedElementImpl; import org.apache.axiom.soap.SOAP11Constants; import org.apache.axiom.soap.SOAP12Constants; import org.apache.axis2.AxisFault; import org.apache.axis2.Constants; +import org.apache.axis2.json.JSONDataSource; import org.apache.axis2.addressing.AddressingConstants; import org.apache.axis2.addressing.EndpointReference; import org.apache.axis2.client.Options; @@ -850,7 +852,7 @@ * Getter for the responseText property. The raw text representing the XML (or non-XML) response. * If the responseXML property is empty, you can check the responseText property to see if a non-XML response was received. */ - public String jsGet_responseText() throws MashupFault { + public String jsGet_responseText() { if (responseText == null && responseXML != null) { // If the response is something like JSON calling toString on it wont work. We need to @@ -858,12 +860,10 @@ // condition. if (responseXML instanceof OMSourcedElementImpl) { OMSourcedElementImpl sourcedElement = (OMSourcedElementImpl) responseXML; - if (sourcedElement.getDataSource() != null) { - try { - responseText = sourcedElement.toStringWithConsume(); - } catch (XMLStreamException e) { - throw new MashupFault(e); - } + OMDataSource dataSource = sourcedElement.getDataSource(); + if (dataSource != null && dataSource instanceof JSONDataSource) { + JSONDataSource jsonDataSource = (JSONDataSource)dataSource; + responseText = jsonDataSource.getCompleteJOSNString(); } } else { responseText = responseXML.toString(); @@ -872,14 +872,30 @@ return responseText; } + private Scriptable getJSONAsXML() throws MashupFault { + OMSourcedElementImpl sourcedElement = (OMSourcedElementImpl) responseXML; + if (sourcedElement.getDataSource() != null) { + try { + String jsonString = sourcedElement.toStringWithConsume(); + while (jsonString.indexOf("<?") == 0) { + jsonString = jsonString.substring(jsonString.indexOf("?>")+2); + } + Object[] objects = { jsonString }; + return context.newObject(this, "XML", objects); + } catch (XMLStreamException e) { + throw new MashupFault(e); + } + } + return null; + } + /** * Getter for the responseE4X property. * The parsed E4X XML message representing the response from the service. */ public Scriptable jsGet_responseE4X() throws MashupFault { if (responseXML instanceof OMSourcedElementImpl) { - throw new MashupFault("The response is not in an XML format. Please try using " + - "responseText instead"); + return getJSONAsXML(); } if (responseXML != null) { Object[] objects = { responseXML }; @@ -895,8 +911,7 @@ */ public Scriptable jsGet_responseXML() throws MashupFault { if (responseXML instanceof OMSourcedElementImpl) { - throw new MashupFault("The response is not in an XML format. Please try using " + - "responseText instead"); + return getJSONAsXML(); } if (responseXML != null) { Object[] objects = { responseXML }; _______________________________________________ Mashup-dev mailing list [email protected] http://mailman.wso2.org/cgi-bin/mailman/listinfo/mashup-dev
