Search in sources :

Example 1 with UniqueTag

use of org.mozilla.javascript.UniqueTag in project convertigo by convertigo.

the class RequestableStep method getPostQuery.

protected String getPostQuery(Scriptable scope) throws EngineException {
    StepVariable stepVariable;
    String postQuery = "";
    int len = numberOfVariables();
    String variableName;
    int variableVisibility;
    for (int i = 0; i < len; i++) {
        stepVariable = (StepVariable) getVariable(i);
        variableName = stepVariable.getName();
        variableVisibility = stepVariable.getVisibility();
        try {
            // Source value
            Object variableValue = stepVariable.getSourceValue();
            if (variableValue instanceof NodeList && ((NodeList) variableValue).getLength() == 0) {
                if (getProject().isStrictMode() && !stepVariable.isMultiValued()) {
                    // override with null (fix #24)
                    variableValue = null;
                }
            }
            if (variableValue != null) {
                Engine.logBeans.trace("(RequestableStep) found value from source: " + Visibility.Logs.printValue(variableVisibility, variableValue));
            }
            // Otherwise Scope parameter
            if (variableValue == null) {
                Scriptable searchScope = scope;
                while ((variableValue == null) && (searchScope != null)) {
                    variableValue = searchScope.get(variableName, searchScope);
                    Engine.logBeans.trace("(RequestableStep) found value from scope: " + Visibility.Logs.printValue(variableVisibility, variableValue));
                    if (variableValue instanceof Undefined) {
                        variableValue = null;
                    } else if (variableValue instanceof UniqueTag && ((UniqueTag) variableValue).equals(UniqueTag.NOT_FOUND)) {
                        variableValue = null;
                    } else if (variableValue instanceof NativeJavaObject) {
                        variableValue = ((NativeJavaObject) variableValue).unwrap();
                    }
                    if (variableValue == null) {
                        // looks up in parent's scope
                        searchScope = searchScope.getParentScope();
                    }
                }
            }
            // Otherwise context parameter
            if (variableValue == null) {
                variableValue = (sequence.context.get(variableName) == null ? null : sequence.context.get(variableName));
                if (variableValue != null)
                    Engine.logBeans.trace("(RequestableStep) found value from context: " + Visibility.Logs.printValue(variableVisibility, variableValue));
            }
            // Otherwise sequence step default value
            if (variableValue == null) {
                variableValue = getVariableValue(variableName);
                if (variableValue != null)
                    Engine.logBeans.trace("(RequestableStep) found default value from step: " + Visibility.Logs.printValue(variableVisibility, variableValue));
            }
            // otherwise value not found
            if (variableValue == null) {
                Engine.logBeans.trace("(RequestableStep) Did not find any value for \"" + variableName + "\", ignore it");
            } else {
                if (bInternalInvoke) {
                    if (stepVariable.isMultiValued() && getProject().isStrictMode() && variableValue instanceof NodeList) {
                        String subXPath = ((StepMultiValuedVariable) stepVariable).getSubXPath();
                        if (subXPath.isEmpty()) {
                            variableValue = XMLUtils.toNodeArray((NodeList) variableValue);
                        } else {
                            TwsCachedXPathAPI xpathAPI = new TwsCachedXPathAPI(this.getProject());
                            NodeList nodeList = (NodeList) variableValue;
                            NodeList[] nodeLists = new NodeList[nodeList.getLength()];
                            for (int j = 0; j < nodeLists.length; j++) {
                                try {
                                    nodeLists[j] = xpathAPI.selectNodeList(nodeList.item(j), subXPath);
                                } catch (TransformerException e) {
                                    Engine.logBeans.debug("(RequestableStep) Failed to select subXpath", e);
                                }
                            }
                            variableValue = nodeLists;
                        }
                    }
                    request.put(variableName, variableValue);
                } else {
                    String parameterValue;
                    if (variableValue instanceof NodeList) {
                        NodeList list = (NodeList) variableValue;
                        if (list != null) {
                            if (list.getLength() == 0) {
                                // Specifies here empty multivalued variable (HTTP invoque only)
                                postQuery = addParamToPostQuery(variableName, "_empty_array_", postQuery);
                            } else {
                                for (int j = 0; j < list.getLength(); j++) {
                                    parameterValue = getNodeValue(list.item(j));
                                    postQuery = addParamToPostQuery(variableName, parameterValue, postQuery);
                                }
                            }
                        }
                    } else if (variableValue instanceof NativeJavaArray) {
                        Object object = ((NativeJavaArray) variableValue).unwrap();
                        List<String> list = GenericUtils.toString(Arrays.asList((Object[]) object));
                        if (list.size() == 0) {
                            // Specifies here empty multivalued variable (HTTP invoque only)
                            postQuery = addParamToPostQuery(variableName, "_empty_array_", postQuery);
                        } else {
                            for (String value : list) {
                                postQuery = addParamToPostQuery(variableName, value, postQuery);
                            }
                        }
                    } else if (variableValue instanceof Collection<?>) {
                        List<String> list = GenericUtils.toString((Collection<?>) variableValue);
                        if (list.size() == 0) {
                            // Specifies here empty multivalued variable (HTTP invoque only)
                            postQuery = addParamToPostQuery(variableName, "_empty_array_", postQuery);
                        } else {
                            for (String value : list) {
                                postQuery = addParamToPostQuery(variableName, value, postQuery);
                            }
                        }
                    } else if (variableValue instanceof String) {
                        parameterValue = variableValue.toString();
                        postQuery = addParamToPostQuery(variableName, parameterValue, postQuery);
                    } else {
                        parameterValue = variableValue.toString();
                        postQuery = addParamToPostQuery(variableName, parameterValue, postQuery);
                    }
                }
            }
        } catch (ClassCastException e) {
            Engine.logBeans.warn("(RequestableStep) Ignoring parameter '" + variableName + "' because its value is not a string");
        }
    }
    if (bInternalInvoke) {
        return null;
    } else {
        if (Engine.logBeans.isTraceEnabled()) {
            Engine.logBeans.trace("(RequestableStep) postQuery :" + Visibility.Logs.replaceVariables(getVariables(), postQuery));
        }
        return postQuery;
    }
}
Also used : Undefined(org.mozilla.javascript.Undefined) NodeList(org.w3c.dom.NodeList) StepVariable(com.twinsoft.convertigo.beans.variables.StepVariable) Scriptable(org.mozilla.javascript.Scriptable) NativeJavaArray(org.mozilla.javascript.NativeJavaArray) StepMultiValuedVariable(com.twinsoft.convertigo.beans.variables.StepMultiValuedVariable) UniqueTag(org.mozilla.javascript.UniqueTag) XmlSchemaCollection(org.apache.ws.commons.schema.XmlSchemaCollection) Collection(java.util.Collection) NativeJavaObject(org.mozilla.javascript.NativeJavaObject) List(java.util.List) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) NodeList(org.w3c.dom.NodeList) NativeJavaObject(org.mozilla.javascript.NativeJavaObject) TransformerException(javax.xml.transform.TransformerException) TwsCachedXPathAPI(com.twinsoft.convertigo.engine.util.TwsCachedXPathAPI)

Aggregations

StepMultiValuedVariable (com.twinsoft.convertigo.beans.variables.StepMultiValuedVariable)1 StepVariable (com.twinsoft.convertigo.beans.variables.StepVariable)1 TwsCachedXPathAPI (com.twinsoft.convertigo.engine.util.TwsCachedXPathAPI)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 TransformerException (javax.xml.transform.TransformerException)1 XmlSchemaCollection (org.apache.ws.commons.schema.XmlSchemaCollection)1 NativeJavaArray (org.mozilla.javascript.NativeJavaArray)1 NativeJavaObject (org.mozilla.javascript.NativeJavaObject)1 Scriptable (org.mozilla.javascript.Scriptable)1 Undefined (org.mozilla.javascript.Undefined)1 UniqueTag (org.mozilla.javascript.UniqueTag)1 NodeList (org.w3c.dom.NodeList)1