Author: keith Date: Thu Feb 12 02:03:04 2009 New Revision: 30712 URL: http://wso2.org/svn/browse/wso2?view=rev&revision=30712
Log: puuting in fix for 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: branches/mashup/java/1.5/java/modules/hostobjects/src/org/wso2/mashup/hostobjects/wsrequest/WSRequestHostImpl.java Modified: branches/mashup/java/1.5/java/modules/hostobjects/src/org/wso2/mashup/hostobjects/wsrequest/WSRequestHostImpl.java URL: http://wso2.org/svn/browse/wso2/branches/mashup/java/1.5/java/modules/hostobjects/src/org/wso2/mashup/hostobjects/wsrequest/WSRequestHostImpl.java?rev=30712&r1=30711&r2=30712&view=diff ============================================================================== --- branches/mashup/java/1.5/java/modules/hostobjects/src/org/wso2/mashup/hostobjects/wsrequest/WSRequestHostImpl.java (original) +++ branches/mashup/java/1.5/java/modules/hostobjects/src/org/wso2/mashup/hostobjects/wsrequest/WSRequestHostImpl.java Thu Feb 12 02:03:04 2009 @@ -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,19 +852,17 @@ * 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 // call toStringWithConsume instead, the following checks are checking for this // 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(); @@ -871,14 +871,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 }; @@ -894,8 +910,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] https://wso2.org/cgi-bin/mailman/listinfo/mashup-dev
