Search in sources :

Example 1 with ParticleInfo

use of org.apache.cxf.javascript.ParticleInfo in project cxf by apache.

the class ServiceJavascriptBuilder method createResponseDeserializer.

// This ignores 'wrapped', because it assumes one part that we can use one way or
// the other. For simple cases, this is certainly OK.
private void createResponseDeserializer(MessageInfo outputMessage) {
    List<MessagePartInfo> parts = outputMessage.getMessageParts();
    if (parts.size() != 1) {
        unsupportedConstruct("MULTIPLE_OUTPUTS", outputMessage.getName().toString());
    }
    List<ParticleInfo> elements = new ArrayList<>();
    String functionName = outputDeserializerFunctionName(outputMessage);
    code.append("function " + functionName + "(cxfjsutils, partElement) {\n");
    getElementsForParts(outputMessage, elements);
    ParticleInfo element = elements.get(0);
    XmlSchemaType type = null;
    if (isRPC) {
        utils.appendLine("cxfjsutils.trace('rpc element: ' + cxfjsutils.traceElementName(partElement));");
        utils.appendLine("partElement = cxfjsutils.getFirstElementChild(partElement);");
        utils.appendLine("cxfjsutils.trace('rpc element: ' + cxfjsutils.traceElementName(partElement));");
    }
    type = element.getType();
    if (!element.isEmpty()) {
        if (type instanceof XmlSchemaComplexType) {
            String typeObjectName = nameManager.getJavascriptName(element.getControllingName());
            utils.appendLine("var returnObject = " + typeObjectName + "_deserialize (cxfjsutils, partElement);\n");
            utils.appendLine("return returnObject;");
        } else if (type instanceof XmlSchemaSimpleType) {
            XmlSchemaSimpleType simpleType = (XmlSchemaSimpleType) type;
            utils.appendLine("var returnText = cxfjsutils.getNodeText(partElement);");
            utils.appendLine("var returnObject = " + utils.javascriptParseExpression(simpleType, "returnText") + ";");
            utils.appendLine("return returnObject;");
        } else if (type != null) {
            utils.appendLine("// Unsupported construct " + type.getClass());
        } else {
            utils.appendLine("// No type for element " + element.getXmlName());
        }
    }
    code.append("}\n");
}
Also used : XmlSchemaSimpleType(org.apache.ws.commons.schema.XmlSchemaSimpleType) ParticleInfo(org.apache.cxf.javascript.ParticleInfo) ArrayList(java.util.ArrayList) XmlSchemaComplexType(org.apache.ws.commons.schema.XmlSchemaComplexType) XmlSchemaType(org.apache.ws.commons.schema.XmlSchemaType) MessagePartInfo(org.apache.cxf.service.model.MessagePartInfo)

Example 2 with ParticleInfo

use of org.apache.cxf.javascript.ParticleInfo in project cxf by apache.

the class ServiceJavascriptBuilder method createInputSerializer.

private void createInputSerializer() {
    // If are working on a wrapped method, then we use the wrapper element.
    // If we are working on an unwrapped method, we will have to work from the unwrapped parts.
    MessageInfo message = currentOperation.getInput();
    String serializerFunctionGlobalName = getFunctionGlobalName(message.getName(), "serializeInput");
    String serializerFunctionPropertyName = getFunctionPropertyName(inputMessagesWithNameConflicts, message, message.getName()) + "_serializeInput";
    code.append("function " + serializerFunctionGlobalName + "(cxfjsutils, args) {\n");
    String wrapperXmlElementName = null;
    // we could use the wrapped part, or we could use a conventional name.
    if (isWrapped) {
        wrapperXmlElementName = prefixAccumulator.xmlElementString(inputWrapperPartInfo.getConcreteName());
        utils.appendLine("var wrapperObj = new " + inputWrapperClassName + "();");
        int px = 0;
        for (String param : inputParameterNames) {
            utils.appendLine("wrapperObj.set" + StringUtils.capitalize(param) + "(args[" + px + "]);");
            px++;
        }
    } else if (isRPC) {
        // make sure the rpc element has a valid prefix
        prefixAccumulator.xmlElementString(currentOperation.getName());
    }
    if (soapBindingInfo != null) {
        SoapVersion soapVersion = soapBindingInfo.getSoapVersion();
        assert soapVersion.getVersion() == 1.1;
        utils.appendLine("var xml;");
        utils.appendLine("xml = cxfjsutils.beginSoap11Message(\"" + prefixAccumulator.getAttributes() + "\");");
    } else {
        // other alternative is XML, which isn't really all here yet.
        unsupportedConstruct("XML_BINDING", currentInterfaceClassName, xmlBindingInfo.getName());
    }
    utils.setXmlStringAccumulator("xml");
    if (isWrapped) {
        ParticleInfo elementInfo = ParticleInfo.forPartElement(inputWrapperElement, xmlSchemaCollection, "wrapperObj", wrapperXmlElementName);
        elementInfo.setContainingType(null);
        utils.generateCodeToSerializeElement(elementInfo, "", xmlSchemaCollection);
    } else {
        String operationXmlElement = null;
        if (isRPC) {
            operationXmlElement = prefixAccumulator.xmlElementString(currentOperation.getName());
            // RPC has a level of element for the entire operation.
            // we might have some schema to model this, but the following seems
            // sufficient.
            utils.appendString("<" + operationXmlElement + ">");
        }
        int px = 0;
        // the JavaScript programmer is stuck with the situation).
        for (ParticleInfo ean : unwrappedElementsAndNames) {
            String savedjsName = ean.getJavascriptName();
            try {
                ean.setJavascriptName("args[" + px + "]");
                utils.generateCodeToSerializeElement(ean, "", xmlSchemaCollection);
                px++;
            } finally {
                ean.setJavascriptName(savedjsName);
            }
        }
        if (isRPC) {
            utils.appendString("</" + operationXmlElement + ">");
        }
    }
    utils.appendLine("xml = xml + cxfjsutils.endSoap11Message();");
    utils.appendLine("return xml;");
    code.append("}\n\n");
    code.append(currentInterfaceClassName + ".prototype." + serializerFunctionPropertyName + " = " + serializerFunctionGlobalName + ";\n\n");
}
Also used : SoapVersion(org.apache.cxf.binding.soap.SoapVersion) ParticleInfo(org.apache.cxf.javascript.ParticleInfo) MessageInfo(org.apache.cxf.service.model.MessageInfo)

Example 3 with ParticleInfo

use of org.apache.cxf.javascript.ParticleInfo in project cxf by apache.

the class SchemaJavascriptBuilder method deserializeElement.

private void deserializeElement(XmlSchemaComplexType type, ParticleInfo itemInfo) {
    if (itemInfo.isGroup()) {
        for (ParticleInfo childElement : itemInfo.getChildren()) {
            deserializeElement(type, childElement);
        }
        return;
    }
    XmlSchemaType itemType = itemInfo.getType();
    boolean simple = itemType instanceof XmlSchemaSimpleType || JavascriptUtils.notVeryComplexType(itemType);
    boolean mtomCandidate = JavascriptUtils.mtomCandidateType(itemType);
    String accessorName = "set" + StringUtils.capitalize(itemInfo.getJavascriptName());
    utils.appendLine("cxfjsutils.trace('processing " + itemInfo.getJavascriptName() + "');");
    XmlSchemaElement element = (XmlSchemaElement) itemInfo.getParticle();
    QName elementQName = XmlSchemaUtils.getElementQualifiedName(element, xmlSchema);
    String elementNamespaceURI = elementQName.getNamespaceURI();
    boolean elementNoNamespace = "".equals(elementNamespaceURI);
    XmlSchema elementSchema = null;
    if (!elementNoNamespace) {
        elementSchema = xmlSchemaCollection.getSchemaByTargetNamespace(elementNamespaceURI);
    }
    boolean qualified = !elementNoNamespace && XmlSchemaUtils.isElementQualified(element, itemInfo.isGlobal(), xmlSchema, elementSchema);
    if (!qualified) {
        elementNamespaceURI = "";
    }
    String localName = elementQName.getLocalPart();
    String valueTarget = "item";
    if (itemInfo.isOptional() || itemInfo.isArray()) {
        utils.startIf("curElement != null && cxfjsutils.isNodeNamedNS(curElement, '" + elementNamespaceURI + "', '" + localName + "')");
        if (itemInfo.isArray()) {
            utils.appendLine("item = [];");
            utils.startDo();
            valueTarget = "arrayItem";
            utils.appendLine("var arrayItem = null;");
        }
    }
    utils.appendLine("var value = null;");
    utils.startIf("!cxfjsutils.isElementNil(curElement)");
    if (itemInfo.isAnyType()) {
        // use our utility
        utils.appendLine(valueTarget + " = org_apache_cxf_deserialize_anyType(cxfjsutils, curElement);");
    } else if (simple) {
        if (mtomCandidate) {
            utils.appendLine(valueTarget + " = cxfjsutils.deserializeBase64orMom(curElement);");
        } else {
            utils.appendLine("value = cxfjsutils.getNodeText(curElement);");
            utils.appendLine(valueTarget + " = " + utils.javascriptParseExpression(itemType, "value") + ";");
        }
    } else {
        XmlSchemaComplexType complexType = (XmlSchemaComplexType) itemType;
        QName baseQName = complexType.getQName();
        if (baseQName == null) {
            baseQName = element.getQName();
        }
        String elTypeJsName = nameManager.getJavascriptName(baseQName);
        utils.appendLine(valueTarget + " = " + elTypeJsName + "_deserialize(cxfjsutils, curElement);");
    }
    // the if for the nil.
    utils.endBlock();
    if (itemInfo.isArray()) {
        utils.appendLine("item.push(arrayItem);");
        utils.appendLine("curElement = cxfjsutils.getNextElementSibling(curElement);");
        utils.endBlock();
        utils.appendLine("  while(curElement != null && cxfjsutils.isNodeNamedNS(curElement, '" + elementNamespaceURI + "', '" + localName + "'));");
    }
    utils.appendLine("newobject." + accessorName + "(item);");
    utils.appendLine("var item = null;");
    if (!itemInfo.isArray()) {
        utils.startIf("curElement != null");
        utils.appendLine("curElement = cxfjsutils.getNextElementSibling(curElement);");
        utils.endBlock();
    }
    if (itemInfo.isOptional() || itemInfo.isArray()) {
        utils.endBlock();
    }
}
Also used : XmlSchema(org.apache.ws.commons.schema.XmlSchema) XmlSchemaSimpleType(org.apache.ws.commons.schema.XmlSchemaSimpleType) XmlSchemaElement(org.apache.ws.commons.schema.XmlSchemaElement) QName(javax.xml.namespace.QName) ParticleInfo(org.apache.cxf.javascript.ParticleInfo) XmlSchemaComplexType(org.apache.ws.commons.schema.XmlSchemaComplexType) XmlSchemaType(org.apache.ws.commons.schema.XmlSchemaType)

Example 4 with ParticleInfo

use of org.apache.cxf.javascript.ParticleInfo in project cxf by apache.

the class ServiceJavascriptBuilder method getElementsForParts.

/**
 * Collect information about the parts of an unwrapped message.
 * @param parts
 * @param elements
 */
private void getElementsForParts(MessageInfo message, List<ParticleInfo> elements) {
    for (MessagePartInfo mpi : message.getMessageParts()) {
        XmlSchemaElement element = null;
        XmlSchemaType type = null;
        QName diagnosticName = mpi.getName();
        if (mpi.isElement()) {
            element = (XmlSchemaElement) mpi.getXmlSchema();
            if (element == null) {
                element = XmlSchemaUtils.findElementByRefName(xmlSchemaCollection, mpi.getElementQName(), serviceInfo.getTargetNamespace());
            }
            diagnosticName = element.getQName();
            type = element.getSchemaType();
            if (type == null) {
                type = getElementType(xmlSchemaCollection, null, element, null);
            }
        } else {
            // RPC (!isElement)
            type = (XmlSchemaType) mpi.getXmlSchema();
            if (type == null) {
                type = xmlSchemaCollection.getTypeByQName(mpi.getTypeQName());
                diagnosticName = type.getQName();
            }
        }
        boolean empty = isEmptyType(type, diagnosticName);
        String partJavascriptVar = JavascriptUtils.javaScriptNameToken(mpi.getConcreteName().getLocalPart());
        String elementXmlRef = prefixAccumulator.xmlElementString(mpi.getConcreteName());
        ParticleInfo elementInfo = ParticleInfo.forPartElement(element, xmlSchemaCollection, partJavascriptVar, elementXmlRef);
        // the type may have been recalculated above.
        elementInfo.setType(type);
        elementInfo.setEmpty(empty);
        elements.add(elementInfo);
    }
}
Also used : XmlSchemaElement(org.apache.ws.commons.schema.XmlSchemaElement) QName(javax.xml.namespace.QName) ParticleInfo(org.apache.cxf.javascript.ParticleInfo) XmlSchemaType(org.apache.ws.commons.schema.XmlSchemaType) MessagePartInfo(org.apache.cxf.service.model.MessagePartInfo)

Example 5 with ParticleInfo

use of org.apache.cxf.javascript.ParticleInfo in project cxf by apache.

the class ServiceJavascriptBuilder method buildOperationFunction.

private void buildOperationFunction(StringBuilder parameterList) {
    String responseCallbackParams = "";
    if (!currentOperation.isOneWay()) {
        responseCallbackParams = "successCallback, errorCallback";
    }
    MessageInfo inputMessage = currentOperation.getInput();
    code.append("//\n");
    code.append("// Operation " + currentOperation.getName() + "\n");
    if (!isWrapped) {
        code.append("// - bare operation. Parameters:\n");
        for (ParticleInfo ei : unwrappedElementsAndNames) {
            code.append("// - " + getElementObjectName(ei) + "\n");
        }
    } else {
        code.append("// Wrapped operation.\n");
        QName contextQName = inputWrapperComplexType.getQName();
        if (contextQName == null) {
            contextQName = inputWrapperElement.getQName();
        }
        XmlSchemaSequence sequence = JavascriptUtils.getSequence(inputWrapperComplexType);
        XmlSchema wrapperSchema = xmlSchemaCollection.getSchemaByTargetNamespace(contextQName.getNamespaceURI());
        for (int i = 0; i < sequence.getItems().size(); i++) {
            code.append("// parameter " + inputParameterNames.get(i) + "\n");
            XmlSchemaSequenceMember sequenceItem = sequence.getItems().get(i);
            ParticleInfo itemInfo = ParticleInfo.forLocalItem((XmlSchemaObject) sequenceItem, wrapperSchema, xmlSchemaCollection, prefixAccumulator, contextQName);
            if (itemInfo.isArray()) {
                code.append("// - array\n");
            }
            // null for an any.
            XmlSchemaType type = itemInfo.getType();
            if (type instanceof XmlSchemaComplexType) {
                QName baseName;
                if (type.getQName() != null) {
                    baseName = type.getQName();
                } else {
                    baseName = ((XmlSchemaElement) sequenceItem).getQName();
                }
                code.append("// - Object constructor is " + nameManager.getJavascriptName(baseName) + "\n");
            } else if (type != null) {
                code.append("// - simple type " + type.getQName());
            }
        }
    }
    code.append("//\n");
    code.append("function " + opFunctionGlobalName + "(" + responseCallbackParams + ((parameterList.length() > 0 && !currentOperation.isOneWay()) ? ", " : "") + parameterList + ") {\n");
    utils.appendLine("this.client = new CxfApacheOrgClient(this.jsutils);");
    utils.appendLine("var xml = null;");
    if (inputMessage != null) {
        utils.appendLine("var args = new Array(" + inputParameterNames.size() + ");");
        int px = 0;
        for (String param : inputParameterNames) {
            utils.appendLine("args[" + px + "] = " + param + ";");
            px++;
        }
        utils.appendLine("xml = this." + getFunctionPropertyName(inputMessagesWithNameConflicts, inputMessage, inputMessage.getName()) + "_serializeInput" + "(this.jsutils, args);");
    }
    // functions.
    if (!currentOperation.isOneWay()) {
        utils.appendLine("this.client.user_onsuccess = successCallback;");
        utils.appendLine("this.client.user_onerror = errorCallback;");
        utils.appendLine("var closureThis = this;");
        // client will pass itself and the response XML.
        utils.appendLine("this.client.onsuccess = function(client, responseXml) { closureThis." + opFunctionPropertyName + "_onsuccess(client, responseXml); };");
        // client will pass itself.
        utils.appendLine("this.client.onerror = function(client) { closureThis." + opFunctionPropertyName + "_onerror(client); };");
    }
    utils.appendLine("var requestHeaders = [];");
    if (soapBindingInfo != null) {
        String action = soapBindingInfo.getSoapAction(currentOperation);
        utils.appendLine("requestHeaders['SOAPAction'] = '" + action + "';");
    }
    // default 'method' by passing null. Is there some place this lives in the
    // service model?
    String syncAsyncFlag;
    if (currentOperation.isOneWay()) {
        utils.appendLine("this.jsutils.trace('oneway operation');");
        syncAsyncFlag = "false";
    } else {
        utils.appendLine("this.jsutils.trace('synchronous = ' + this.synchronous);");
        syncAsyncFlag = "this.synchronous";
    }
    utils.appendLine("this.client.request(this.url, xml, null, " + syncAsyncFlag + ", requestHeaders);");
    code.append("}\n\n");
    code.append(currentInterfaceClassName + ".prototype." + opFunctionPropertyName + " = " + opFunctionGlobalName + ";\n\n");
}
Also used : XmlSchemaSequence(org.apache.ws.commons.schema.XmlSchemaSequence) XmlSchema(org.apache.ws.commons.schema.XmlSchema) QName(javax.xml.namespace.QName) ParticleInfo(org.apache.cxf.javascript.ParticleInfo) XmlSchemaSequenceMember(org.apache.ws.commons.schema.XmlSchemaSequenceMember) XmlSchemaComplexType(org.apache.ws.commons.schema.XmlSchemaComplexType) XmlSchemaType(org.apache.ws.commons.schema.XmlSchemaType) MessageInfo(org.apache.cxf.service.model.MessageInfo)

Aggregations

ParticleInfo (org.apache.cxf.javascript.ParticleInfo)7 XmlSchemaType (org.apache.ws.commons.schema.XmlSchemaType)4 QName (javax.xml.namespace.QName)3 XmlSchemaComplexType (org.apache.ws.commons.schema.XmlSchemaComplexType)3 JavascriptUtils (org.apache.cxf.javascript.JavascriptUtils)2 MessageInfo (org.apache.cxf.service.model.MessageInfo)2 MessagePartInfo (org.apache.cxf.service.model.MessagePartInfo)2 XmlSchema (org.apache.ws.commons.schema.XmlSchema)2 XmlSchemaElement (org.apache.ws.commons.schema.XmlSchemaElement)2 XmlSchemaObject (org.apache.ws.commons.schema.XmlSchemaObject)2 XmlSchemaSimpleType (org.apache.ws.commons.schema.XmlSchemaSimpleType)2 ArrayList (java.util.ArrayList)1 SoapVersion (org.apache.cxf.binding.soap.SoapVersion)1 AttributeInfo (org.apache.cxf.javascript.AttributeInfo)1 XmlSchemaAnnotated (org.apache.ws.commons.schema.XmlSchemaAnnotated)1 XmlSchemaSequence (org.apache.ws.commons.schema.XmlSchemaSequence)1 XmlSchemaSequenceMember (org.apache.ws.commons.schema.XmlSchemaSequenceMember)1