Author: shameera Date: Wed Jun 25 18:41:42 2014 New Revision: 1605543 URL: http://svn.apache.org/r1605543 Log: Fixed possible nul exceptions from latest JSON implementation and add logs
Modified: axis/axis2/java/core/trunk/modules/json/pom.xml axis/axis2/java/core/trunk/modules/json/src/org/apache/axis2/json/gson/JSONMessageHandler.java axis/axis2/java/core/trunk/modules/json/src/org/apache/axis2/json/gson/JsonBuilder.java Modified: axis/axis2/java/core/trunk/modules/json/pom.xml URL: http://svn.apache.org/viewvc/axis/axis2/java/core/trunk/modules/json/pom.xml?rev=1605543&r1=1605542&r2=1605543&view=diff ============================================================================== --- axis/axis2/java/core/trunk/modules/json/pom.xml (original) +++ axis/axis2/java/core/trunk/modules/json/pom.xml Wed Jun 25 18:41:42 2014 @@ -71,6 +71,10 @@ <artifactId>gson</artifactId> </dependency> <dependency> + <groupId>commons-logging</groupId> + <artifactId>commons-logging</artifactId> + </dependency> + <dependency> <groupId>org.apache.axis2</groupId> <artifactId>axis2-adb-codegen</artifactId> <version>${project.version}</version> Modified: axis/axis2/java/core/trunk/modules/json/src/org/apache/axis2/json/gson/JSONMessageHandler.java URL: http://svn.apache.org/viewvc/axis/axis2/java/core/trunk/modules/json/src/org/apache/axis2/json/gson/JSONMessageHandler.java?rev=1605543&r1=1605542&r2=1605543&view=diff ============================================================================== --- axis/axis2/java/core/trunk/modules/json/src/org/apache/axis2/json/gson/JSONMessageHandler.java (original) +++ axis/axis2/java/core/trunk/modules/json/src/org/apache/axis2/json/gson/JSONMessageHandler.java Wed Jun 25 18:41:42 2014 @@ -23,19 +23,23 @@ import org.apache.axiom.om.OMElement; import org.apache.axiom.om.impl.builder.StAXOMBuilder; import org.apache.axis2.AxisFault; import org.apache.axis2.context.MessageContext; +import org.apache.axis2.description.AxisOperation; import org.apache.axis2.engine.MessageReceiver; import org.apache.axis2.handlers.AbstractHandler; +import org.apache.axis2.json.gson.factory.JsonConstant; import org.apache.axis2.json.gson.rpc.JsonInOnlyRPCMessageReceiver; import org.apache.axis2.json.gson.rpc.JsonRpcMessageReceiver; -import org.apache.axis2.json.gson.factory.JsonConstant; import org.apache.axis2.wsdl.WSDLConstants; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; import org.apache.ws.commons.schema.XmlSchema; import javax.xml.namespace.QName; import java.util.List; - public class JSONMessageHandler extends AbstractHandler { + Log log = LogFactory.getLog(JSONMessageHandler.class); + /** * This method will be called on each registered handler when a message * needs to be processed. If the message processing is paused by the @@ -53,33 +57,44 @@ public class JSONMessageHandler extends * @param msgContext the <code>MessageContext</code> to process with this * <code>Handler</code>. * @return An InvocationResponse that indicates what - * the next step in the message processing should be. + * the next step in the message processing should be. * @throws org.apache.axis2.AxisFault if the handler encounters an error */ public InvocationResponse invoke(MessageContext msgContext) throws AxisFault { - MessageReceiver messageReceiver = msgContext.getAxisOperation().getMessageReceiver(); - if (messageReceiver instanceof JsonRpcMessageReceiver || messageReceiver instanceof JsonInOnlyRPCMessageReceiver) { - // do not need to parse XMLSchema list, as this message receiver will not use GsonXMLStreamReader to read the inputStream. - } else { - Object tempObj = msgContext.getProperty(JsonConstant.IS_JSON_STREAM); - if (tempObj != null) { - boolean isJSON = Boolean.valueOf(tempObj.toString()); - Object o = msgContext.getProperty(JsonConstant.GSON_XML_STREAM_READER); - if (o != null) { - GsonXMLStreamReader gsonXMLStreamReader = (GsonXMLStreamReader) o; - QName elementQname = msgContext.getAxisOperation().getMessage(WSDLConstants.MESSAGE_LABEL_IN_VALUE).getElementQName(); - List<XmlSchema> schemas = msgContext.getAxisService().getSchema(); - gsonXMLStreamReader.initXmlStreamReader(elementQname, schemas, msgContext.getConfigurationContext()); - StAXOMBuilder stAXOMBuilder = new StAXOMBuilder(gsonXMLStreamReader); - OMElement omElement = stAXOMBuilder.getDocumentElement(); - msgContext.getEnvelope().getBody().addChild(omElement); + AxisOperation axisOperation = msgContext.getAxisOperation(); + if (axisOperation != null) { + MessageReceiver messageReceiver = axisOperation.getMessageReceiver(); + if (messageReceiver instanceof JsonRpcMessageReceiver || messageReceiver instanceof JsonInOnlyRPCMessageReceiver) { + // do not need to parse XMLSchema list, as this message receiver will not use GsonXMLStreamReader to read the inputStream. + } else { + Object tempObj = msgContext.getProperty(JsonConstant.IS_JSON_STREAM); + if (tempObj != null) { + boolean isJSON = Boolean.valueOf(tempObj.toString()); + Object o = msgContext.getProperty(JsonConstant.GSON_XML_STREAM_READER); + if (o != null) { + GsonXMLStreamReader gsonXMLStreamReader = (GsonXMLStreamReader) o; + QName elementQname = msgContext.getAxisOperation().getMessage(WSDLConstants.MESSAGE_LABEL_IN_VALUE).getElementQName(); + List<XmlSchema> schemas = msgContext.getAxisService().getSchema(); + gsonXMLStreamReader.initXmlStreamReader(elementQname, schemas, msgContext.getConfigurationContext()); + StAXOMBuilder stAXOMBuilder = new StAXOMBuilder(gsonXMLStreamReader); + OMElement omElement = stAXOMBuilder.getDocumentElement(); + msgContext.getEnvelope().getBody().addChild(omElement); + } else { + if (log.isDebugEnabled()) { + log.debug("GsonXMLStreamReader is null"); + } + throw new AxisFault("GsonXMLStreamReader should not be null"); + } } else { - throw new AxisFault("GsonXMLStreamReader should not be null"); + // request is not a JSON request so don't need to initialize GsonXMLStreamReader } - } else { - // request is not a JSON request so don't need to initialize GsonXMLStreamReader } + } else { + if (log.isDebugEnabled()) { + log.debug("Axis operation is null"); + } + // message hasn't been dispatched to operation, ignore it } return InvocationResponse.CONTINUE; } Modified: axis/axis2/java/core/trunk/modules/json/src/org/apache/axis2/json/gson/JsonBuilder.java URL: http://svn.apache.org/viewvc/axis/axis2/java/core/trunk/modules/json/src/org/apache/axis2/json/gson/JsonBuilder.java?rev=1605543&r1=1605542&r2=1605543&view=diff ============================================================================== --- axis/axis2/java/core/trunk/modules/json/src/org/apache/axis2/json/gson/JsonBuilder.java (original) +++ axis/axis2/java/core/trunk/modules/json/src/org/apache/axis2/json/gson/JsonBuilder.java Wed Jun 25 18:41:42 2014 @@ -22,35 +22,42 @@ package org.apache.axis2.json.gson; import com.google.gson.stream.JsonReader; import org.apache.axiom.om.OMAbstractFactory; import org.apache.axiom.om.OMElement; -import org.apache.axiom.soap.SOAPEnvelope; import org.apache.axiom.soap.SOAPFactory; import org.apache.axis2.AxisFault; import org.apache.axis2.Constants; import org.apache.axis2.builder.Builder; import org.apache.axis2.context.MessageContext; import org.apache.axis2.json.gson.factory.JsonConstant; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; import java.io.InputStream; import java.io.InputStreamReader; import java.io.UnsupportedEncodingException; public class JsonBuilder implements Builder { + Log log = LogFactory.getLog(JsonBuilder.class); public OMElement processDocument(InputStream inputStream, String s, MessageContext messageContext) throws AxisFault { messageContext.setProperty(JsonConstant.IS_JSON_STREAM , true); JsonReader jsonReader; String charSetEncoding=null; - try { - charSetEncoding = (String) messageContext.getProperty(Constants.Configuration.CHARACTER_SET_ENCODING); - jsonReader = new JsonReader(new InputStreamReader(inputStream , charSetEncoding)); - GsonXMLStreamReader gsonXMLStreamReader = new GsonXMLStreamReader(jsonReader); - messageContext.setProperty(JsonConstant.GSON_XML_STREAM_READER , gsonXMLStreamReader); - // dummy envelop - SOAPFactory soapFactory = OMAbstractFactory.getSOAP11Factory(); - SOAPEnvelope soapEnvelope = soapFactory.getDefaultEnvelope(); - return soapEnvelope; - } catch (UnsupportedEncodingException e) { - throw new AxisFault(charSetEncoding + " encoding is may not supported by json inputStream ", e); - } + if (inputStream != null) { + try { + charSetEncoding = (String) messageContext.getProperty(Constants.Configuration.CHARACTER_SET_ENCODING); + jsonReader = new JsonReader(new InputStreamReader(inputStream, charSetEncoding)); + GsonXMLStreamReader gsonXMLStreamReader = new GsonXMLStreamReader(jsonReader); + messageContext.setProperty(JsonConstant.GSON_XML_STREAM_READER, gsonXMLStreamReader); + } catch (UnsupportedEncodingException e) { + throw new AxisFault(charSetEncoding + " encoding is may not supported by json inputStream ", e); + } + } else { + if (log.isDebugEnabled()) { + log.debug("Inputstream is null, This is possible with GET request"); + } + } + // dummy envelop + SOAPFactory soapFactory = OMAbstractFactory.getSOAP11Factory(); + return soapFactory.getDefaultEnvelope(); } }