Search in sources :

Example 6 with HttpStatementVariable

use of com.twinsoft.convertigo.beans.variables.HttpStatementVariable in project convertigo by convertigo.

the class HTTPStatement method makeQuery.

private String makeQuery(com.twinsoft.convertigo.engine.Context context, String methodToAnalyse) throws EngineException {
    String variable, httpVariable, httpVariableValue, method, query = "";
    int len = numberOfVariables();
    String urlEncodingCharset = getUrlEncodingCharset();
    if (urlEncodingCharset == null || urlEncodingCharset.length() == 0) {
        urlEncodingCharset = getParentTransaction().getComputedUrlEncodingCharset();
    }
    try {
        for (int i = 0; i < len; i++) {
            HttpStatementVariable httpStatementVariable = (HttpStatementVariable) getVariable(i);
            if (httpStatementVariable != null) {
                variable = httpStatementVariable.getName();
                method = httpStatementVariable.getHttpMethod();
                httpVariable = httpStatementVariable.getHttpName();
                if (method.equals(methodToAnalyse)) {
                    if (query.length() != 0) {
                        query += "&";
                    }
                    try {
                        // evaluate method can throw EngineException
                        // try catch to get the default value in this case
                        evaluate(javascriptContext, scope, variable, httpVariable, false);
                        Engine.logBeans.debug("Javascript evaluation of httpVariable named '" + httpVariable + "' executed");
                        // if no Engine Exception has been thown until here, normal execution
                        if (evaluated != null) {
                            if (evaluated instanceof NativeJavaArray) {
                                Object object = ((NativeJavaArray) evaluated).unwrap();
                                List<Object> list = Arrays.asList((Object[]) object);
                                for (int j = 0; j < list.size(); j++) {
                                    Object item = list.get(j);
                                    httpVariableValue = item.toString();
                                    query = addVariableToQuery(methodToAnalyse, httpVariable, httpVariableValue, query, urlEncodingCharset, j != 0);
                                }
                            } else if (evaluated instanceof NativeJavaObject) {
                                NativeJavaObject nativeJavaObject = (NativeJavaObject) evaluated;
                                Object javaObject = nativeJavaObject.unwrap();
                                if (javaObject instanceof List) {
                                    Vector<String> v = GenericUtils.cast(javaObject);
                                    for (int j = 0; j < v.size(); j++) {
                                        httpVariableValue = v.get(j);
                                        query = addVariableToQuery(methodToAnalyse, httpVariable, httpVariableValue, query, urlEncodingCharset, j != 0);
                                    }
                                } else {
                                    httpVariableValue = (String) nativeJavaObject.getDefaultValue(String.class);
                                    query = addVariableToQuery(methodToAnalyse, httpVariable, httpVariableValue, query, urlEncodingCharset, false);
                                }
                            } else if (evaluated instanceof NativeArray) {
                                NativeArray array = (NativeArray) evaluated;
                                for (int j = 0; j < array.getLength(); j++) {
                                    Object item = array.get(j, array);
                                    httpVariableValue = item.toString();
                                    query = addVariableToQuery(methodToAnalyse, httpVariable, httpVariableValue, query, urlEncodingCharset, j != 0);
                                }
                            } else if (evaluated instanceof List) {
                                Vector<String> v = GenericUtils.cast(evaluated);
                                for (int j = 0; j < v.size(); j++) {
                                    httpVariableValue = v.get(j);
                                    query = addVariableToQuery(methodToAnalyse, httpVariable, httpVariableValue, query, urlEncodingCharset, j != 0);
                                }
                            } else if (evaluated instanceof Undefined) {
                                throw new EngineException("Undefined");
                            } else {
                                httpVariableValue = evaluated.toString();
                                query = addVariableToQuery(methodToAnalyse, httpVariable, httpVariableValue, query, urlEncodingCharset, false);
                            }
                        }
                    } catch (EngineException e) {
                        // Engine Exception has been thrown ==> get variable default value
                        Object value = getVariableValue(variable);
                        if (value != null) {
                            if (value instanceof Collection) {
                                List<String> list = GenericUtils.toString((Collection<?>) value);
                                for (String val : list) {
                                    httpVariableValue = val;
                                    query = addVariableToQuery(methodToAnalyse, httpVariable, httpVariableValue, query, urlEncodingCharset, false);
                                }
                            } else {
                                httpVariableValue = value.toString();
                                query = addVariableToQuery(methodToAnalyse, httpVariable, httpVariableValue, query, urlEncodingCharset, false);
                            }
                        }
                    }
                }
            }
        }
    } catch (UnsupportedEncodingException e) {
        throw new EngineException("UTF-8 encoding is not supported.", e);
    }
    return query;
}
Also used : NativeArray(org.mozilla.javascript.NativeArray) Undefined(org.mozilla.javascript.Undefined) EngineException(com.twinsoft.convertigo.engine.EngineException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) HttpStatementVariable(com.twinsoft.convertigo.beans.variables.HttpStatementVariable) NativeJavaArray(org.mozilla.javascript.NativeJavaArray) Collection(java.util.Collection) DatabaseObject(com.twinsoft.convertigo.beans.core.DatabaseObject) NativeJavaObject(org.mozilla.javascript.NativeJavaObject) ArrayList(java.util.ArrayList) NodeList(org.w3c.dom.NodeList) List(java.util.List) NativeJavaObject(org.mozilla.javascript.NativeJavaObject) XMLVector(com.twinsoft.convertigo.beans.common.XMLVector) Vector(java.util.Vector)

Aggregations

HttpStatementVariable (com.twinsoft.convertigo.beans.variables.HttpStatementVariable)6 HttpStatementMultiValuedVariable (com.twinsoft.convertigo.beans.variables.HttpStatementMultiValuedVariable)4 RequestableHttpMultiValuedVariable (com.twinsoft.convertigo.beans.variables.RequestableHttpMultiValuedVariable)4 RequestableHttpVariable (com.twinsoft.convertigo.beans.variables.RequestableHttpVariable)4 DatabaseObject (com.twinsoft.convertigo.beans.core.DatabaseObject)3 HTTPStatement (com.twinsoft.convertigo.beans.statements.HTTPStatement)3 RequestableMultiValuedVariable (com.twinsoft.convertigo.beans.variables.RequestableMultiValuedVariable)3 RequestableVariable (com.twinsoft.convertigo.beans.variables.RequestableVariable)3 StepVariable (com.twinsoft.convertigo.beans.variables.StepVariable)3 ProjectExplorerView (com.twinsoft.convertigo.eclipse.views.projectexplorer.ProjectExplorerView)3 TreeObject (com.twinsoft.convertigo.eclipse.views.projectexplorer.model.TreeObject)3 EngineException (com.twinsoft.convertigo.engine.EngineException)3 Cursor (org.eclipse.swt.graphics.Cursor)3 Display (org.eclipse.swt.widgets.Display)3 Shell (org.eclipse.swt.widgets.Shell)3 RequestableStep (com.twinsoft.convertigo.beans.core.RequestableStep)2 Sequence (com.twinsoft.convertigo.beans.core.Sequence)2 TestCase (com.twinsoft.convertigo.beans.core.TestCase)2 TransactionWithVariables (com.twinsoft.convertigo.beans.core.TransactionWithVariables)2 Variable (com.twinsoft.convertigo.beans.core.Variable)2