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;
}
Aggregations