Added: axis/axis2/java/core/trunk/modules/json/src/org/apache/axis2/json/gson/factory/XmlNodeGenerator.java URL: http://svn.apache.org/viewvc/axis/axis2/java/core/trunk/modules/json/src/org/apache/axis2/json/gson/factory/XmlNodeGenerator.java?rev=1381640&view=auto ============================================================================== --- axis/axis2/java/core/trunk/modules/json/src/org/apache/axis2/json/gson/factory/XmlNodeGenerator.java (added) +++ axis/axis2/java/core/trunk/modules/json/src/org/apache/axis2/json/gson/factory/XmlNodeGenerator.java Thu Sep 6 15:24:44 2012 @@ -0,0 +1,181 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.axis2.json.gson.factory; + + +import org.apache.ws.commons.schema.XmlSchema; +import org.apache.ws.commons.schema.XmlSchemaComplexType; +import org.apache.ws.commons.schema.XmlSchemaElement; +import org.apache.ws.commons.schema.XmlSchemaObjectCollection; +import org.apache.ws.commons.schema.XmlSchemaParticle; +import org.apache.ws.commons.schema.XmlSchemaSequence; +import org.apache.ws.commons.schema.XmlSchemaSimpleType; +import org.apache.ws.commons.schema.XmlSchemaType; + +import javax.xml.namespace.QName; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; + +public class XmlNodeGenerator { + + List<XmlSchema> xmlSchemaList; + + QName elementQname; + + private XmlNode mainXmlNode; + + Queue<JsonObject> queue = new LinkedList<JsonObject>(); + + public XmlNodeGenerator(List<XmlSchema> xmlSchemaList, QName elementQname) { + this.xmlSchemaList = xmlSchemaList; + this.elementQname = elementQname; + } + + public XmlNodeGenerator() { + } + + private void processSchemaList() { + // get the operation schema and process. + XmlSchema operationSchema = getXmlSchema(elementQname); + XmlSchemaElement messageElement = operationSchema.getElementByName(elementQname.getLocalPart()); + mainXmlNode = new XmlNode(elementQname.getLocalPart(), elementQname.getNamespaceURI() , false, (messageElement.getMaxOccurs() == 1 ? false : true) , ""); + + QName messageSchemaTypeName = messageElement.getSchemaTypeName(); + XmlSchemaType schemaType = null; + XmlSchema schemaOfType = null; + if (messageSchemaTypeName != null) { + schemaType = operationSchema.getTypeByName(messageSchemaTypeName); + if (schemaType == null) { + schemaOfType = getXmlSchema(messageSchemaTypeName); + schemaType = schemaOfType.getTypeByName(messageSchemaTypeName.getLocalPart()); + } else { + schemaOfType = operationSchema; + } + } else { + schemaType = messageElement.getSchemaType(); + schemaOfType = operationSchema; + } + + if (schemaType != null) { + processSchemaType(schemaType, mainXmlNode, schemaOfType); + } else { + // nothing to do + } + } + + private void processElement(XmlSchemaElement element, XmlNode parentNode , XmlSchema schema) { + String targetNamespace = schema.getTargetNamespace(); + XmlNode xmlNode; + QName schemaTypeName = element.getSchemaTypeName(); + XmlSchemaType schemaType = element.getSchemaType(); + if (schemaTypeName != null) { + xmlNode = new XmlNode(element.getName(), targetNamespace, false, (element.getMaxOccurs() == 1 ? false : true), schemaTypeName.getLocalPart()); + parentNode.addChildtoList(xmlNode); + if (("http://www.w3.org/2001/XMLSchema").equals(schemaTypeName.getNamespaceURI())) { + } else { + XmlSchema schemaOfType; + // see whether Schema type is in the same schema + XmlSchemaType childSchemaType = schema.getTypeByName(schemaTypeName.getLocalPart()); + if (childSchemaType == null) { + schemaOfType = getXmlSchema(schemaTypeName); + childSchemaType = schemaOfType.getTypeByName(schemaTypeName.getLocalPart()); + }else{ + schemaOfType = schema; + } + processSchemaType(childSchemaType, xmlNode, schemaOfType); + } + }else if (schemaType != null) { + xmlNode = new XmlNode(element.getName(), targetNamespace, false, (element.getMaxOccurs() == 1 ? false : true), schemaType.getQName().getLocalPart()); + parentNode.addChildtoList(xmlNode); + processSchemaType(schemaType, xmlNode, schema); + } + } + + + private void processSchemaType(XmlSchemaType xmlSchemaType , XmlNode parentNode , XmlSchema schema) { + if (xmlSchemaType instanceof XmlSchemaComplexType) { + XmlSchemaComplexType complexType = (XmlSchemaComplexType)xmlSchemaType; + XmlSchemaParticle particle = complexType.getParticle(); + if (particle instanceof XmlSchemaSequence) { + XmlSchemaSequence sequence = (XmlSchemaSequence)particle; + XmlSchemaObjectCollection objectCollection = sequence.getItems(); + Iterator objectIterator = objectCollection.getIterator(); + while (objectIterator.hasNext()) { + Object obj = objectIterator.next(); + if (obj instanceof XmlSchemaElement) { + processElement((XmlSchemaElement)obj , parentNode , schema); + } + } + } + }else if (xmlSchemaType instanceof XmlSchemaSimpleType) { + // nothing to do with simpleType + } + } + + + private XmlSchema getXmlSchema(QName qName) { + for (XmlSchema xmlSchema : xmlSchemaList) { + if (xmlSchema.getTargetNamespace().equals(qName.getNamespaceURI())) { + return xmlSchema; + } + } + return null; + } + + private void generateQueue(XmlNode node) { + if (node.isArray()) { + if (node.getChildrenList().size() > 0) { + queue.add(new JsonObject(node.getName(), JSONType.NESTED_ARRAY, node.getValueType() , node.getNamespaceUri())); + processXmlNodeChildren(node.getChildrenList()); + } else { + queue.add(new JsonObject(node.getName(), JSONType.ARRAY , node.getValueType() , node.getNamespaceUri())); + } + } else { + if (node.getChildrenList().size() > 0) { + queue.add(new JsonObject(node.getName(), JSONType.NESTED_OBJECT, node.getValueType() , node.getNamespaceUri())); + processXmlNodeChildren(node.getChildrenList()); + } else { + queue.add(new JsonObject(node.getName(), JSONType.OBJECT , node.getValueType() , node.getNamespaceUri())); + } + } + } + + private void processXmlNodeChildren(List<XmlNode> childrenNodes) { + for (int i = 0; i < childrenNodes.size(); i++) { + generateQueue(childrenNodes.get(i)); + } + } + + + public XmlNode getMainXmlNode() { + if (mainXmlNode == null) { + processSchemaList(); + } + return mainXmlNode; + } + + public Queue<JsonObject> getQueue(XmlNode node) { + generateQueue(node); + return queue; + } + +}
Added: axis/axis2/java/core/trunk/modules/json/src/org/apache/axis2/json/gson/rpc/JsonInOnlyRPCMessageReceiver.java URL: http://svn.apache.org/viewvc/axis/axis2/java/core/trunk/modules/json/src/org/apache/axis2/json/gson/rpc/JsonInOnlyRPCMessageReceiver.java?rev=1381640&view=auto ============================================================================== --- axis/axis2/java/core/trunk/modules/json/src/org/apache/axis2/json/gson/rpc/JsonInOnlyRPCMessageReceiver.java (added) +++ axis/axis2/java/core/trunk/modules/json/src/org/apache/axis2/json/gson/rpc/JsonInOnlyRPCMessageReceiver.java Thu Sep 6 15:24:44 2012 @@ -0,0 +1,134 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.axis2.json.gson.rpc; + +import com.google.gson.stream.JsonReader; +import org.apache.axis2.AxisFault; +import org.apache.axis2.context.MessageContext; +import org.apache.axis2.description.AxisOperation; +import org.apache.axis2.json.gson.GsonXMLStreamReader; +import org.apache.axis2.json.gson.factory.JsonConstant; +import org.apache.axis2.json.gson.factory.JsonUtils; +import org.apache.axis2.rpc.receivers.RPCInOnlyMessageReceiver; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + +import java.io.IOException; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; + +public class JsonInOnlyRPCMessageReceiver extends RPCInOnlyMessageReceiver { + + private static Log log = LogFactory.getLog(JsonInOnlyRPCMessageReceiver.class); + @Override + public void invokeBusinessLogic(MessageContext inMessage) throws AxisFault { + + Object tempObj = inMessage.getProperty(JsonConstant.IS_JSON_STREAM); + boolean isJsonStream; + + if (tempObj != null) { + isJsonStream = Boolean.valueOf(tempObj.toString()); + } else { + // if IS_JSON_STREAM property is not set then it is not a JSON request + isJsonStream = false; + } + + if (isJsonStream) { + Object o = inMessage.getProperty(JsonConstant.GSON_XML_STREAM_READER); + if (o != null) { + GsonXMLStreamReader gsonXMLStreamReader = (GsonXMLStreamReader)o; + JsonReader jsonReader = gsonXMLStreamReader.getJsonReader(); + if (jsonReader == null) { + throw new AxisFault("JsonReader should not be null"); + } + Method method = null; + String msg; + Object serviceObj = getTheImplementationObject(inMessage); + Class implClass = serviceObj.getClass(); + Method[] allMethods = implClass.getDeclaredMethods(); + AxisOperation op = inMessage.getOperationContext().getAxisOperation(); + String operation = op.getName().getLocalPart(); + method = JsonUtils.getOpMethod(operation, allMethods); + Class[] paramClasses = method.getParameterTypes(); + try { + int paramCount = paramClasses.length; + JsonUtils.invokeServiceClass(jsonReader, serviceObj, method, paramClasses, paramCount); + } catch (IllegalAccessException e) { + msg = "Does not have access to " + + "the definition of the specified class, field, method or constructor"; + log.error(msg, e); + throw AxisFault.makeFault(e); + + } catch (InvocationTargetException e) { + msg = "Exception occurred while trying to invoke service method " + + (method != null ? method.getName() : "null"); + log.error(msg, e); + throw AxisFault.makeFault(e); + } catch (IOException e) { + msg = "Exception occur while encording or " + + "access to the input string at the JsonRpcMessageReceiver"; + log.error(msg, e); + throw AxisFault.makeFault(e); + } + } else { + throw new AxisFault("GsonXMLStreamReader should have put as a property of messageContext " + + "to evaluate JSON message"); + } +/* InputStream inputStream = (InputStream)inMessage.getProperty(JsonConstant.INPUT_STREAM); + if (inputStream != null) { + Method method = null; + String msg; + + Object serviceObj = getTheImplementationObject(inMessage); + Class implClass = serviceObj.getClass(); + Method[] allmethods = implClass.getDeclaredMethods(); + AxisOperation op = inMessage.getOperationContext().getAxisOperation(); + String operation = op.getName().getLocalPart(); + method = JsonUtils.getOpMethod(operation, allmethods); + Class [] paramClasses = method.getParameterTypes(); + String charSetEncoding = (String) inMessage.getProperty(Constants.Configuration.CHARACTER_SET_ENCODING); + try { + int paramCount=paramClasses.length; + + JsonUtils.invokeServiceClass(inputStream, + serviceObj, method, paramClasses, paramCount, charSetEncoding); + + } catch (IllegalAccessException e) { + msg = "Does not have access to " + + "the definition of the specified class, field, method or constructor"; + log.error(msg, e); + throw AxisFault.makeFault(e); + + } catch (InvocationTargetException e) { + msg = "Exception occurred while trying to invoke service method " + + (method != null ? method.getName() : "null"); + log.error(msg, e); + throw AxisFault.makeFault(e); + } catch (IOException e) { + msg = "Exception occur while encording or " + + "access to the input string at the JsonRpcMessageReceiver"; + log.error(msg, e); + throw AxisFault.makeFault(e); + }*/ + } else{ + super.invokeBusinessLogic(inMessage); // call RPCMessageReceiver if inputstream is null + } + } +} Added: axis/axis2/java/core/trunk/modules/json/src/org/apache/axis2/json/gson/rpc/JsonRpcMessageReceiver.java URL: http://svn.apache.org/viewvc/axis/axis2/java/core/trunk/modules/json/src/org/apache/axis2/json/gson/rpc/JsonRpcMessageReceiver.java?rev=1381640&view=auto ============================================================================== --- axis/axis2/java/core/trunk/modules/json/src/org/apache/axis2/json/gson/rpc/JsonRpcMessageReceiver.java (added) +++ axis/axis2/java/core/trunk/modules/json/src/org/apache/axis2/json/gson/rpc/JsonRpcMessageReceiver.java Thu Sep 6 15:24:44 2012 @@ -0,0 +1,102 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.axis2.json.gson.rpc; + +import com.google.gson.stream.JsonReader; +import org.apache.axis2.AxisFault; +import org.apache.axis2.context.MessageContext; +import org.apache.axis2.description.AxisOperation; +import org.apache.axis2.json.gson.GsonXMLStreamReader; +import org.apache.axis2.json.gson.factory.JsonConstant; +import org.apache.axis2.json.gson.factory.JsonUtils; +import org.apache.axis2.rpc.receivers.RPCMessageReceiver; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + +import java.io.IOException; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; + + +public class JsonRpcMessageReceiver extends RPCMessageReceiver { + + private static Log log = LogFactory.getLog(RPCMessageReceiver.class); + @Override + public void invokeBusinessLogic(MessageContext inMessage, MessageContext outMessage) throws AxisFault { + Object tempObj = inMessage.getProperty(JsonConstant.IS_JSON_STREAM); + boolean isJsonStream; + + if (tempObj != null) { + isJsonStream = Boolean.valueOf(tempObj.toString()); + } else { + // if IS_JSON_STREAM property is not set then it is not a JSON request + isJsonStream = false; + } + + if (isJsonStream) { + Object o = inMessage.getProperty(JsonConstant.GSON_XML_STREAM_READER); + if (o != null) { + GsonXMLStreamReader gsonXMLStreamReader = (GsonXMLStreamReader)o; + JsonReader jsonReader = gsonXMLStreamReader.getJsonReader(); + if (jsonReader == null) { + throw new AxisFault("JsonReader should not be null"); + } + Method method = null; + String msg; + Object serviceObj = getTheImplementationObject(inMessage); + Class implClass = serviceObj.getClass(); + Method[] allMethods = implClass.getDeclaredMethods(); + AxisOperation op = inMessage.getOperationContext().getAxisOperation(); + String operation = op.getName().getLocalPart(); + method = JsonUtils.getOpMethod(operation, allMethods); + Class[] paramClasses = method.getParameterTypes(); + try { + int paramCount = paramClasses.length; + Object retObj = JsonUtils.invokeServiceClass(jsonReader, serviceObj, method, paramClasses, paramCount); + + // handle response + outMessage.setProperty(JsonConstant.RETURN_OBJECT, retObj); + outMessage.setProperty(JsonConstant.RETURN_TYPE, method.getReturnType()); + + } catch (IllegalAccessException e) { + msg = "Does not have access to " + + "the definition of the specified class, field, method or constructor"; + log.error(msg, e); + throw AxisFault.makeFault(e); + + } catch (InvocationTargetException e) { + msg = "Exception occurred while trying to invoke service method " + + (method != null ? method.getName() : "null"); + log.error(msg, e); + throw AxisFault.makeFault(e); + } catch (IOException e) { + msg = "Exception occur while encording or " + + "access to the input string at the JsonRpcMessageReceiver"; + log.error(msg, e); + throw AxisFault.makeFault(e); + } + } else { + throw new AxisFault("GsonXMLStreamReader should have put as a property of messageContext " + + "to evaluate JSON message"); + } + } else { + super.invokeBusinessLogic(inMessage, outMessage); // call RPCMessageReceiver if inputstream is null + } + } +} Added: axis/axis2/java/core/trunk/modules/json/test/org/apache/axis2/json/gson/GsonXMLStreamReaderTest.java URL: http://svn.apache.org/viewvc/axis/axis2/java/core/trunk/modules/json/test/org/apache/axis2/json/gson/GsonXMLStreamReaderTest.java?rev=1381640&view=auto ============================================================================== --- axis/axis2/java/core/trunk/modules/json/test/org/apache/axis2/json/gson/GsonXMLStreamReaderTest.java (added) +++ axis/axis2/java/core/trunk/modules/json/test/org/apache/axis2/json/gson/GsonXMLStreamReaderTest.java Thu Sep 6 15:24:44 2012 @@ -0,0 +1,68 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.axis2.json.gson; + +import com.google.gson.stream.JsonReader; +import org.apache.axiom.om.OMElement; +import org.apache.axiom.om.impl.builder.StAXOMBuilder; +import org.apache.axis2.context.ConfigurationContext; +import org.apache.axis2.engine.AxisConfiguration; +import org.apache.ws.commons.schema.XmlSchema; +import org.apache.ws.commons.schema.XmlSchemaCollection; +import org.junit.Assert; +import org.junit.Test; + +import javax.xml.namespace.QName; +import javax.xml.transform.stream.StreamSource; +import java.io.ByteArrayInputStream; +import java.io.FileInputStream; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.util.ArrayList; +import java.util.List; + +public class GsonXMLStreamReaderTest { + + + @Test + public void testGsonXMLStreamReader() throws Exception { + String jsonString = "{\"response\":{\"return\":{\"name\":\"kate\",\"age\":\"35\",\"gender\":\"female\"}}}"; + String xmlString = "<response xmlns=\"http://www.w3schools.com\"><return><name>kate</name><age>35</age><gender>female</gender></return></response>"; + InputStream inputStream = new ByteArrayInputStream(jsonString.getBytes()); + JsonReader jsonReader = new JsonReader(new InputStreamReader(inputStream , "UTF-8")); + String fileName = "test-resources/custom_schema/testSchema_1.xsd"; + InputStream is = new FileInputStream(fileName); + XmlSchemaCollection schemaCol = new XmlSchemaCollection(); + XmlSchema schema = schemaCol.read(new StreamSource(is), null); + List<XmlSchema> schemaList = new ArrayList<XmlSchema>(); + schemaList.add(schema); + QName elementQName = new QName("http://www.w3schools.com", "response"); + ConfigurationContext configCtxt = new ConfigurationContext(new AxisConfiguration()); + GsonXMLStreamReader gsonXMLStreamReader = new GsonXMLStreamReader(jsonReader); + gsonXMLStreamReader.initXmlStreamReader(elementQName , schemaList , configCtxt); + StAXOMBuilder stAXOMBuilder = new StAXOMBuilder(gsonXMLStreamReader); + OMElement omElement = stAXOMBuilder.getDocumentElement(); + String actual = omElement.toString(); + inputStream.close(); + is.close(); + Assert.assertEquals(xmlString , actual); + + } +} Added: axis/axis2/java/core/trunk/modules/json/test/org/apache/axis2/json/gson/GsonXMLStreamWriterTest.java URL: http://svn.apache.org/viewvc/axis/axis2/java/core/trunk/modules/json/test/org/apache/axis2/json/gson/GsonXMLStreamWriterTest.java?rev=1381640&view=auto ============================================================================== --- axis/axis2/java/core/trunk/modules/json/test/org/apache/axis2/json/gson/GsonXMLStreamWriterTest.java (added) +++ axis/axis2/java/core/trunk/modules/json/test/org/apache/axis2/json/gson/GsonXMLStreamWriterTest.java Thu Sep 6 15:24:44 2012 @@ -0,0 +1,92 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.axis2.json.gson; + +import com.google.gson.stream.JsonWriter; +import org.apache.axiom.om.OMAbstractFactory; +import org.apache.axiom.om.OMElement; +import org.apache.axiom.om.OMFactory; +import org.apache.axiom.om.OMNamespace; +import org.apache.axis2.context.ConfigurationContext; +import org.apache.axis2.engine.AxisConfiguration; +import org.apache.ws.commons.schema.XmlSchema; +import org.apache.ws.commons.schema.XmlSchemaCollection; +import org.junit.Assert; +import org.junit.Test; + +import javax.xml.namespace.QName; +import javax.xml.transform.stream.StreamSource; +import java.io.ByteArrayOutputStream; +import java.io.FileInputStream; +import java.io.InputStream; +import java.io.OutputStreamWriter; +import java.util.ArrayList; +import java.util.List; + + +public class GsonXMLStreamWriterTest { + private String jsonString; + + @Test + public void testGsonXMLStreamWriter() throws Exception { + jsonString = "{\"response\":{\"return\":{\"name\":\"kate\",\"age\":\"35\",\"gender\":\"female\"}}}"; + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + OutputStreamWriter outputStreamWriter = new OutputStreamWriter(baos, "UTF-8"); + JsonWriter jsonWriter = new JsonWriter(outputStreamWriter); + String fileName = "test-resources/custom_schema/testSchema_1.xsd"; + InputStream is = new FileInputStream(fileName); + XmlSchemaCollection schemaCol = new XmlSchemaCollection(); + XmlSchema schema = schemaCol.read(new StreamSource(is), null); + List<XmlSchema> schemaList = new ArrayList<XmlSchema>(); + schemaList.add(schema); + QName elementQName = new QName("http://www.w3schools.com", "response"); + ConfigurationContext configCtxt = new ConfigurationContext(new AxisConfiguration()); + + GsonXMLStreamWriter gsonXMLStreamWriter = new GsonXMLStreamWriter(jsonWriter, elementQName, schemaList, configCtxt); + OMElement omElement = getResponseOMElement(); + gsonXMLStreamWriter.writeStartDocument(); + omElement.serialize(gsonXMLStreamWriter); + gsonXMLStreamWriter.writeEndDocument(); + + String actualString = baos.toString(); + outputStreamWriter.close(); + Assert.assertEquals(jsonString, actualString); + } + + + private OMElement getResponseOMElement() { + OMFactory omFactory = OMAbstractFactory.getOMFactory(); + OMNamespace ns = omFactory.createOMNamespace("", ""); + + OMElement response = omFactory.createOMElement("response", ns); + OMElement ret = omFactory.createOMElement("return", ns); + OMElement name = omFactory.createOMElement("name", ns); + name.setText("kate"); + OMElement age = omFactory.createOMElement("age", ns); + age.setText("35"); + OMElement gender = omFactory.createOMElement("gender", ns); + gender.setText("female"); + ret.addChild(name); + ret.addChild(age); + ret.addChild(gender); + response.addChild(ret); + return response; + } +} Added: axis/axis2/java/core/trunk/modules/json/test/org/apache/axis2/json/gson/JsonBuilderTest.java URL: http://svn.apache.org/viewvc/axis/axis2/java/core/trunk/modules/json/test/org/apache/axis2/json/gson/JsonBuilderTest.java?rev=1381640&view=auto ============================================================================== --- axis/axis2/java/core/trunk/modules/json/test/org/apache/axis2/json/gson/JsonBuilderTest.java (added) +++ axis/axis2/java/core/trunk/modules/json/test/org/apache/axis2/json/gson/JsonBuilderTest.java Thu Sep 6 15:24:44 2012 @@ -0,0 +1,75 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.axis2.json.gson; + +import com.google.gson.stream.JsonReader; +import org.apache.axis2.Constants; +import org.apache.axis2.context.MessageContext; +import org.apache.axis2.json.gson.factory.JsonConstant; +import org.junit.Assert; +import org.junit.Test; + +import java.io.ByteArrayInputStream; +import java.io.IOException; + +public class JsonBuilderTest { + + @Test + public void testProcessDocument() throws Exception { + MessageContext messageContext = new MessageContext(); + String contentType = "application/json-impl"; + String jsonString = "{\"methodName\":{\"param\":\"value\"}}"; + ByteArrayInputStream inputStream = new ByteArrayInputStream(jsonString.getBytes("UTF-8")); + messageContext.setProperty(Constants.Configuration.CHARACTER_SET_ENCODING, "UTF-8"); + + JsonBuilder jsonBuilder = new JsonBuilder(); + jsonBuilder.processDocument(inputStream, contentType, messageContext); + + Object isJson = messageContext.getProperty(JsonConstant.IS_JSON_STREAM); + Assert.assertNotNull(isJson); + isJson = Boolean.valueOf(isJson.toString()); + Assert.assertEquals(true, isJson); + Object streamReader = messageContext.getProperty(JsonConstant.GSON_XML_STREAM_READER); + Assert.assertNotNull(streamReader); + GsonXMLStreamReader gsonXMLStreamReader = (GsonXMLStreamReader) streamReader; + JsonReader jsonReader = gsonXMLStreamReader.getJsonReader(); + Assert.assertNotNull(jsonReader); + try { + String actualString = readJsonReader(jsonReader); + Assert.assertEquals("value", actualString); + } catch (IOException e) { + Assert.assertFalse(true); + } + + inputStream.close(); + + } + + private String readJsonReader(JsonReader jsonReader) throws IOException { + jsonReader.beginObject(); + jsonReader.nextName(); + jsonReader.beginObject(); + jsonReader.nextName(); + String name = jsonReader.nextString(); + jsonReader.endObject(); + jsonReader.endObject(); + return name; + } +} Added: axis/axis2/java/core/trunk/modules/json/test/org/apache/axis2/json/gson/JsonFormatterTest.java URL: http://svn.apache.org/viewvc/axis/axis2/java/core/trunk/modules/json/test/org/apache/axis2/json/gson/JsonFormatterTest.java?rev=1381640&view=auto ============================================================================== --- axis/axis2/java/core/trunk/modules/json/test/org/apache/axis2/json/gson/JsonFormatterTest.java (added) +++ axis/axis2/java/core/trunk/modules/json/test/org/apache/axis2/json/gson/JsonFormatterTest.java Thu Sep 6 15:24:44 2012 @@ -0,0 +1,211 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.axis2.json.gson; + +import com.google.gson.Gson; +import org.apache.axiom.om.OMAbstractFactory; +import org.apache.axiom.om.OMElement; +import org.apache.axiom.om.OMFactory; +import org.apache.axiom.om.OMNamespace; +import org.apache.axiom.om.OMOutputFormat; +import org.apache.axiom.soap.SOAPEnvelope; +import org.apache.axiom.soap.SOAPFactory; +import org.apache.axis2.Constants; +import org.apache.axis2.context.ConfigurationContext; +import org.apache.axis2.context.MessageContext; +import org.apache.axis2.description.AxisMessage; +import org.apache.axis2.description.AxisOperation; +import org.apache.axis2.description.AxisOperationFactory; +import org.apache.axis2.description.AxisService; +import org.apache.axis2.engine.AxisConfiguration; +import org.apache.axis2.json.gson.factory.JsonConstant; +import org.apache.axis2.wsdl.WSDLConstants; +import org.apache.ws.commons.schema.XmlSchema; +import org.apache.ws.commons.schema.XmlSchemaCollection; +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import javax.xml.namespace.QName; +import javax.xml.transform.stream.StreamSource; +import java.io.ByteArrayOutputStream; +import java.io.FileInputStream; +import java.io.InputStream; +import java.io.OutputStream; + +public class JsonFormatterTest { + MessageContext outMsgContext; + String contentType; + String jsonString; + SOAPEnvelope soapEnvelope; + OMOutputFormat outputFormat; + OutputStream outputStream; + @Before + public void setUp() throws Exception { + contentType = "application/json-impl"; + SOAPFactory soapFactory = OMAbstractFactory.getSOAP11Factory(); + soapEnvelope = soapFactory.getDefaultEnvelope(); + outputFormat = new OMOutputFormat(); + outputStream = new ByteArrayOutputStream(); + + outMsgContext = new MessageContext(); + outMsgContext.setProperty(Constants.Configuration.CHARACTER_SET_ENCODING, "UTF-8"); + } + + @After + public void tearDown() throws Exception { + outputStream.close(); + } + + @Test + public void testWriteToFaultMessage() throws Exception { + jsonString = "{\"Fault\":{\"faultcode\":\"soapenv:Server\",\"faultstring\":\"javax.xml.stream.XMLStreamException\",\"detail\":\"testFaultMsg\"}}"; + outMsgContext.setProcessingFault(true); + soapEnvelope.getBody().addChild(createFaultOMElement()); + outMsgContext.setEnvelope(soapEnvelope); + JsonFormatter jsonFormatter = new JsonFormatter(); + jsonFormatter.writeTo(outMsgContext, outputFormat, outputStream, false); + String faultMsg = outputStream.toString(); + Assert.assertEquals(jsonString , faultMsg); + } + + + @Test + public void testWriteToXMLtoJSON() throws Exception { + jsonString = "{\"response\":{\"return\":{\"name\":\"kate\",\"age\":\"35\",\"gender\":\"female\"}}}"; + String fileName = "test-resources/custom_schema/testSchema_1.xsd"; + InputStream is = new FileInputStream(fileName); + XmlSchemaCollection schemaCol = new XmlSchemaCollection(); + XmlSchema schema = schemaCol.read(new StreamSource(is), null); + QName elementQName = new QName("http://www.w3schools.com", "response"); + ConfigurationContext configCtxt = new ConfigurationContext(new AxisConfiguration()); + outMsgContext.setConfigurationContext(configCtxt); + AxisOperation axisOperation = AxisOperationFactory.getAxisOperation(AxisOperation.MEP_CONSTANT_IN_OUT); + AxisMessage message = new AxisMessage(); + message.setElementQName(elementQName); + axisOperation.addMessage(message , WSDLConstants.MESSAGE_LABEL_OUT_VALUE); + outMsgContext.setAxisOperation(axisOperation); + AxisService axisService = new AxisService("testService"); + axisService.addSchema(schema); + outMsgContext.setAxisService(axisService); + soapEnvelope.getBody().addChild(getResponseOMElement()); + outMsgContext.setEnvelope(soapEnvelope); + JsonFormatter jsonFormatter = new JsonFormatter(); + jsonFormatter.writeTo(outMsgContext, outputFormat , outputStream , false); + String response = outputStream.toString(); + Assert.assertEquals(jsonString, response); + } + + + @Test + public void testWriteToJSON() throws Exception { + Person person = new Person(); + person.setName("Leo"); + person.setAge(27); + person.setGender("Male"); + person.setSingle(true); + outMsgContext.setProperty(JsonConstant.RETURN_OBJECT, person); + outMsgContext.setProperty(JsonConstant.RETURN_TYPE, Person.class); + jsonString = "{\""+ JsonConstant.RESPONSE +"\":" + new Gson().toJson(person, Person.class) + "}"; + + JsonFormatter jsonFormatter = new JsonFormatter(); + jsonFormatter.writeTo(outMsgContext, outputFormat, outputStream, false); + String personString = outputStream.toString(); + Assert.assertEquals(jsonString, personString); + + } + + + private OMElement createFaultOMElement() { + OMFactory omFactory = OMAbstractFactory.getOMFactory(); + OMNamespace ns = omFactory.createOMNamespace("", ""); + OMElement faultCode = omFactory.createOMElement("faultcode", ns); + faultCode.setText("soapenv:Server"); + OMElement faultString = omFactory.createOMElement("faultstring", ns); + faultString.setText("javax.xml.stream.XMLStreamException"); + OMElement detail = omFactory.createOMElement("detail", ns); + detail.setText("testFaultMsg"); + OMElement fault = omFactory.createOMElement("Fault", ns); + fault.addChild(faultCode); + fault.addChild(faultString); + fault.addChild(detail); + return fault; + } + + private OMElement getResponseOMElement() { + OMFactory omFactory = OMAbstractFactory.getOMFactory(); + OMNamespace ns = omFactory.createOMNamespace("", ""); + + OMElement response = omFactory.createOMElement("response", ns); + OMElement ret = omFactory.createOMElement("return", ns); + OMElement name = omFactory.createOMElement("name", ns); + name.setText("kate"); + OMElement age = omFactory.createOMElement("age", ns); + age.setText("35"); + OMElement gender = omFactory.createOMElement("gender", ns); + gender.setText("female"); + ret.addChild(name); + ret.addChild(age); + ret.addChild(gender); + response.addChild(ret); + return response; + } + + public static class Person { + + private String name; + private int age; + private String gender; + private boolean single; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public int getAge() { + return age; + } + + public void setAge(int age) { + this.age = age; + } + + public String getGender() { + return gender; + } + + public void setGender(String gender) { + this.gender = gender; + } + + public boolean isSingle() { + return single; + } + + public void setSingle(boolean single) { + this.single = single; + } + } +} Modified: axis/axis2/java/core/trunk/modules/samples/json/README.txt URL: http://svn.apache.org/viewvc/axis/axis2/java/core/trunk/modules/samples/json/README.txt?rev=1381640&r1=1381639&r2=1381640&view=diff ============================================================================== --- axis/axis2/java/core/trunk/modules/samples/json/README.txt (original) +++ axis/axis2/java/core/trunk/modules/samples/json/README.txt Thu Sep 6 15:24:44 2012 @@ -30,10 +30,10 @@ First of all add following message build configuration file in AXIS2_HOME/conf/ directory. <messageBuilder contentType="application/json-impl" - class="org.apache.axis2.json.impl.JsonBuilder" /> + class="org.apache.axis2.json.gson.JsonBuilder" /> <messageFormatter contentType="application/json-impl" - class="org.apache.axis2.json.impl.JsonFormatter" /> + class="org.apache.axis2.json.gson.JsonFormatter" /> Goto AXIS2_HOME/sample/json/ directory and Modified: axis/axis2/java/core/trunk/modules/samples/json/resources/axis2.xml URL: http://svn.apache.org/viewvc/axis/axis2/java/core/trunk/modules/samples/json/resources/axis2.xml?rev=1381640&r1=1381639&r2=1381640&view=diff ============================================================================== --- axis/axis2/java/core/trunk/modules/samples/json/resources/axis2.xml (original) +++ axis/axis2/java/core/trunk/modules/samples/json/resources/axis2.xml Thu Sep 6 15:24:44 2012 @@ -176,7 +176,7 @@ class="org.apache.axis2.transport.http.SOAPMessageFormatter"/> <messageFormatter contentType="application/json-impl" - class="org.apache.axis2.json.impl.JsonFormatter" /> + class="org.apache.axis2.json.gson.JsonFormatter" /> </messageFormatters> <!-- ================================================= --> @@ -193,7 +193,7 @@ class="org.apache.axis2.builder.MultipartFormDataBuilder"/> <messageBuilder contentType="application/json-impl" - class="org.apache.axis2.json.impl.JsonBuilder" /> + class="org.apache.axis2.json.gson.JsonBuilder" /> </messageBuilders> <!-- ================================================= --> Modified: axis/axis2/java/core/trunk/modules/samples/json/src/META-INF/services.xml URL: http://svn.apache.org/viewvc/axis/axis2/java/core/trunk/modules/samples/json/src/META-INF/services.xml?rev=1381640&r1=1381639&r2=1381640&view=diff ============================================================================== --- axis/axis2/java/core/trunk/modules/samples/json/src/META-INF/services.xml (original) +++ axis/axis2/java/core/trunk/modules/samples/json/src/META-INF/services.xml Thu Sep 6 15:24:44 2012 @@ -4,9 +4,9 @@ </Description> <messageReceivers> <messageReceiver mep="http://www.w3.org/ns/wsdl/in-out" - class="org.apache.axis2.json.impl.rpc.JsonRpcMessageReceiver" /> + class="org.apache.axis2.json.gson.rpc.JsonRpcMessageReceiver" /> <messageReceiver mep="http://www.w3.org/ns/wsdl/in-only" - class="org.apache.axis2.json.impl.rpc.JsonInOnlyRPCMessageReceiver"/> + class="org.apache.axis2.json.gson.rpc.JsonInOnlyRPCMessageReceiver"/> </messageReceivers> <parameter name="ServiceClass" locked="false">sample.json.service.JsonService</parameter> </service> \ No newline at end of file