use of com.twinsoft.convertigo.beans.variables.RequestableVariable in project convertigo by convertigo.
the class MobileComponentImportVariablesAction method run.
public void run() {
Display display = Display.getDefault();
Cursor waitCursor = new Cursor(display, SWT.CURSOR_WAIT);
Shell shell = getParentShell();
shell.setCursor(waitCursor);
try {
ProjectExplorerView explorerView = getProjectExplorerView();
if (explorerView != null) {
TreeObject treeObject = explorerView.getFirstSelectedTreeObject();
Object databaseObject = treeObject.getObject();
if (databaseObject != null) {
if (databaseObject instanceof UIDynamicAction) {
UIDynamicAction dynAction = (UIDynamicAction) databaseObject;
IonBean ionBean = ((UIDynamicAction) dynAction).getIonBean();
if (ionBean != null) {
// Case of CallSequenceAction
if (ionBean.getName().equals("CallSequenceAction")) {
Object value = ionBean.getProperty("requestable").getValue();
if (!value.equals(false)) {
String target = value.toString();
if (!target.isEmpty()) {
try {
String projectName = target.substring(0, target.indexOf('.'));
String sequenceName = target.substring(target.indexOf('.') + 1);
Project p = Engine.theApp.databaseObjectsManager.getProjectByName(projectName);
Sequence sequence = p.getSequenceByName(sequenceName);
int size = sequence.numberOfVariables();
for (int i = 0; i < size; i++) {
RequestableVariable variable = (RequestableVariable) sequence.getVariable(i);
if (variable != null) {
String variableName = variable.getName();
if (dynAction.getVariable(variableName) == null) {
if (!StringUtils.isNormalized(variableName))
throw new EngineException("Variable name is not normalized : \"" + variableName + "\".");
UIControlVariable uiVariable = new UIControlVariable();
uiVariable.setName(variableName);
uiVariable.setComment(variable.getDescription());
uiVariable.setVarSmartType(new MobileSmartSourceType(variable.getDefaultValue().toString()));
dynAction.addUIComponent(uiVariable);
uiVariable.bNew = true;
uiVariable.hasChanged = true;
dynAction.hasChanged = true;
}
}
}
} catch (Exception e) {
}
}
}
} else // Case of InvokeAction
if (ionBean.getName().equals("InvokeAction")) {
UIDynamicInvoke dynInvoke = (UIDynamicInvoke) databaseObject;
UIActionStack stack = dynInvoke.getTargetSharedAction();
if (stack != null) {
for (UIStackVariable variable : stack.getVariables()) {
String variableName = variable.getName();
if (dynAction.getVariable(variableName) == null) {
if (!StringUtils.isNormalized(variableName))
throw new EngineException("Variable name is not normalized : \"" + variableName + "\".");
UIControlVariable uiVariable = new UIControlVariable();
uiVariable.setName(variableName);
uiVariable.setComment(variable.getComment());
MobileSmartSourceType msst = new MobileSmartSourceType();
msst.setMode(MobileSmartSourceType.Mode.SCRIPT);
msst.setSmartValue(variable.getVariableValue());
uiVariable.setVarSmartType(msst);
dynAction.addUIComponent(uiVariable);
uiVariable.bNew = true;
uiVariable.hasChanged = true;
dynAction.hasChanged = true;
}
}
}
}
if (dynAction.hasChanged) {
IScriptComponent main = dynAction.getMainScriptComponent();
if (main != null) {
if (main instanceof ApplicationComponent) {
((ApplicationComponent) main).markApplicationAsDirty();
}
if (main instanceof PageComponent) {
((PageComponent) main).markPageAsDirty();
}
}
explorerView.reloadTreeObject(treeObject);
StructuredSelection structuredSelection = new StructuredSelection(treeObject);
ConvertigoPlugin.getDefault().getPropertiesView().selectionChanged((IWorkbenchPart) explorerView, structuredSelection);
}
}
} else if (databaseObject instanceof UIUseShared) {
UIUseShared useShared = (UIUseShared) databaseObject;
UISharedComponent sharedComp = useShared.getTargetSharedComponent();
if (sharedComp != null) {
for (UICompVariable variable : sharedComp.getVariables()) {
String variableName = variable.getName();
if (useShared.getVariable(variableName) == null) {
if (!StringUtils.isNormalized(variableName))
throw new EngineException("Variable name is not normalized : \"" + variableName + "\".");
UIControlVariable uiVariable = new UIControlVariable();
uiVariable.setName(variableName);
uiVariable.setComment(variable.getComment());
MobileSmartSourceType msst = new MobileSmartSourceType();
msst.setMode(MobileSmartSourceType.Mode.SCRIPT);
msst.setSmartValue(variable.getVariableValue());
uiVariable.setVarSmartType(msst);
useShared.addUIComponent(uiVariable);
uiVariable.bNew = true;
uiVariable.hasChanged = true;
useShared.hasChanged = true;
}
}
if (useShared.hasChanged) {
IScriptComponent main = useShared.getMainScriptComponent();
if (main != null) {
if (main instanceof ApplicationComponent) {
((ApplicationComponent) main).markApplicationAsDirty();
}
if (main instanceof PageComponent) {
((PageComponent) main).markPageAsDirty();
}
}
explorerView.reloadTreeObject(treeObject);
StructuredSelection structuredSelection = new StructuredSelection(treeObject);
ConvertigoPlugin.getDefault().getPropertiesView().selectionChanged((IWorkbenchPart) explorerView, structuredSelection);
}
}
}
}
}
} catch (Throwable e) {
ConvertigoPlugin.logException(e, "Unable to add variables to action !");
} finally {
shell.setCursor(null);
waitCursor.dispose();
}
}
use of com.twinsoft.convertigo.beans.variables.RequestableVariable in project convertigo by convertigo.
the class OperationImportParametersFromVariablesAction method run.
public void run() {
Display display = Display.getDefault();
Cursor waitCursor = new Cursor(display, SWT.CURSOR_WAIT);
Shell shell = getParentShell();
shell.setCursor(waitCursor);
try {
ProjectExplorerView explorerView = getProjectExplorerView();
if (explorerView != null) {
TreeObject treeObject = explorerView.getFirstSelectedTreeObject();
Object databaseObject = treeObject.getObject();
if ((databaseObject != null) && (databaseObject instanceof UrlMappingOperation)) {
UrlMappingOperation operation = (UrlMappingOperation) databaseObject;
String targetRequestable = operation.getTargetRequestable();
if (!targetRequestable.isEmpty()) {
StringTokenizer st = new StringTokenizer(targetRequestable, ".");
int count = st.countTokens();
String projectName = st.nextToken();
String sequenceName = count == 2 ? st.nextToken() : "";
String connectorName = count == 3 ? st.nextToken() : "";
String transactionName = count == 3 ? st.nextToken() : "";
Project project = Engine.theApp.databaseObjectsManager.getProjectByName(projectName);
RequestableObject requestableObject = null;
if (sequenceName.isEmpty()) {
requestableObject = project.getConnectorByName(connectorName).getTransactionByName(transactionName);
} else {
requestableObject = project.getSequenceByName(sequenceName);
}
if (requestableObject != null && requestableObject instanceof IVariableContainer) {
IVariableContainer variableContainer = (IVariableContainer) requestableObject;
for (Variable variable : variableContainer.getVariables()) {
String variableName = variable.getName();
Object variableValue = variable.getValueOrNull();
UrlMappingParameter parameter = null;
try {
parameter = operation.getParameterByName(variableName);
} catch (Exception e) {
}
if (parameter == null) {
if (operation instanceof PostOperation || operation instanceof PutOperation)
parameter = new FormParameter();
else
parameter = new QueryParameter();
parameter.setName(variableName);
parameter.setComment(variable.getComment());
parameter.setArray(false);
parameter.setExposed(((RequestableVariable) variable).isWsdl());
parameter.setMultiValued(variable.isMultiValued());
parameter.setRequired(variable.isRequired());
parameter.setValueOrNull(!variable.isMultiValued() ? variableValue : null);
parameter.setMappedVariableName(variableName);
parameter.bNew = true;
parameter.hasChanged = true;
operation.add(parameter);
operation.hasChanged = true;
}
}
if (operation.hasChanged) {
explorerView.reloadTreeObject(treeObject);
StructuredSelection structuredSelection = new StructuredSelection(treeObject);
ConvertigoPlugin.getDefault().getPropertiesView().selectionChanged((IWorkbenchPart) explorerView, structuredSelection);
}
}
} else {
throw new ConvertigoException("Operation has no target requestable : please select one first.");
}
}
}
} catch (Throwable e) {
ConvertigoPlugin.logException(e, "Unable to import variables as new parameters in operation!");
} finally {
shell.setCursor(null);
waitCursor.dispose();
}
}
use of com.twinsoft.convertigo.beans.variables.RequestableVariable in project convertigo by convertigo.
the class NgxComponentImportVariablesAction method run.
public void run() {
Display display = Display.getDefault();
Cursor waitCursor = new Cursor(display, SWT.CURSOR_WAIT);
Shell shell = getParentShell();
shell.setCursor(waitCursor);
try {
ProjectExplorerView explorerView = getProjectExplorerView();
if (explorerView != null) {
TreeObject treeObject = explorerView.getFirstSelectedTreeObject();
Object databaseObject = treeObject.getObject();
if (databaseObject != null) {
if (databaseObject instanceof UIDynamicAction) {
UIDynamicAction dynAction = (UIDynamicAction) databaseObject;
IonBean ionBean = ((UIDynamicAction) dynAction).getIonBean();
if (ionBean != null) {
// Case of CallSequenceAction
if (ionBean.getName().equals("CallSequenceAction")) {
Object value = ionBean.getProperty("requestable").getValue();
if (!value.equals(false)) {
String target = value.toString();
if (!target.isEmpty()) {
try {
String projectName = target.substring(0, target.indexOf('.'));
String sequenceName = target.substring(target.indexOf('.') + 1);
Project p = Engine.theApp.databaseObjectsManager.getProjectByName(projectName);
Sequence sequence = p.getSequenceByName(sequenceName);
int size = sequence.numberOfVariables();
for (int i = 0; i < size; i++) {
RequestableVariable variable = (RequestableVariable) sequence.getVariable(i);
if (variable != null) {
String variableName = variable.getName();
if (dynAction.getVariable(variableName) == null) {
if (!StringUtils.isNormalized(variableName))
throw new EngineException("Variable name is not normalized : \"" + variableName + "\".");
UIControlVariable uiVariable = new UIControlVariable();
uiVariable.setName(variableName);
uiVariable.setComment(variable.getDescription());
uiVariable.setVarSmartType(new MobileSmartSourceType(variable.getDefaultValue().toString()));
dynAction.addUIComponent(uiVariable);
uiVariable.bNew = true;
uiVariable.hasChanged = true;
dynAction.hasChanged = true;
}
}
}
} catch (Exception e) {
}
}
}
} else // Case of InvokeAction
if (ionBean.getName().equals("InvokeAction")) {
UIDynamicInvoke dynInvoke = (UIDynamicInvoke) databaseObject;
UIActionStack stack = dynInvoke.getTargetSharedAction();
if (stack != null) {
for (UIStackVariable variable : stack.getVariables()) {
String variableName = variable.getName();
if (dynAction.getVariable(variableName) == null) {
if (!StringUtils.isNormalized(variableName))
throw new EngineException("Variable name is not normalized : \"" + variableName + "\".");
UIControlVariable uiVariable = new UIControlVariable();
uiVariable.setName(variableName);
uiVariable.setComment(variable.getComment());
MobileSmartSourceType msst = new MobileSmartSourceType();
msst.setMode(MobileSmartSourceType.Mode.SCRIPT);
msst.setSmartValue(variable.getVariableValue());
uiVariable.setVarSmartType(msst);
dynAction.addUIComponent(uiVariable);
uiVariable.bNew = true;
uiVariable.hasChanged = true;
dynAction.hasChanged = true;
}
}
}
}
if (dynAction.hasChanged) {
IScriptComponent main = dynAction.getMainScriptComponent();
if (main != null) {
if (main instanceof ApplicationComponent) {
((ApplicationComponent) main).markApplicationAsDirty();
}
if (main instanceof PageComponent) {
((PageComponent) main).markPageAsDirty();
}
}
explorerView.reloadTreeObject(treeObject);
StructuredSelection structuredSelection = new StructuredSelection(treeObject);
ConvertigoPlugin.getDefault().getPropertiesView().selectionChanged((IWorkbenchPart) explorerView, structuredSelection);
}
}
} else if (databaseObject instanceof UIUseShared) {
UIUseShared useShared = (UIUseShared) databaseObject;
UISharedComponent sharedComp = useShared.getTargetSharedComponent();
if (sharedComp != null) {
for (UICompVariable variable : sharedComp.getVariables()) {
String variableName = variable.getName();
if (useShared.getVariable(variableName) == null) {
if (!StringUtils.isNormalized(variableName))
throw new EngineException("Variable name is not normalized : \"" + variableName + "\".");
UIControlVariable uiVariable = new UIControlVariable();
uiVariable.setName(variableName);
uiVariable.setComment(variable.getComment());
MobileSmartSourceType msst = new MobileSmartSourceType();
msst.setMode(MobileSmartSourceType.Mode.SCRIPT);
msst.setSmartValue(variable.getVariableValue());
uiVariable.setVarSmartType(msst);
useShared.addUIComponent(uiVariable);
uiVariable.bNew = true;
uiVariable.hasChanged = true;
useShared.hasChanged = true;
}
}
if (useShared.hasChanged) {
IScriptComponent main = useShared.getMainScriptComponent();
if (main != null) {
if (main instanceof ApplicationComponent) {
((ApplicationComponent) main).markApplicationAsDirty();
}
if (main instanceof PageComponent) {
((PageComponent) main).markPageAsDirty();
}
}
explorerView.reloadTreeObject(treeObject);
StructuredSelection structuredSelection = new StructuredSelection(treeObject);
ConvertigoPlugin.getDefault().getPropertiesView().selectionChanged((IWorkbenchPart) explorerView, structuredSelection);
}
}
}
}
}
} catch (Throwable e) {
ConvertigoPlugin.logException(e, "Unable to add variables to action !");
} finally {
shell.setCursor(null);
waitCursor.dispose();
}
}
use of com.twinsoft.convertigo.beans.variables.RequestableVariable in project convertigo by convertigo.
the class VariableGenerateFromXmlAndLinkAction method run.
public void run() {
Display display = Display.getDefault();
Cursor waitCursor = new Cursor(display, SWT.CURSOR_WAIT);
Shell shell = getParentShell();
shell.setCursor(waitCursor);
try {
ProjectExplorerView explorerView = getProjectExplorerView();
if (explorerView != null) {
TreeObject treeObject = explorerView.getFirstSelectedTreeObject();
Object databaseObject = treeObject.getObject();
if ((databaseObject != null) && ((databaseObject instanceof StepVariable))) {
StepVariable stepVariable = (StepVariable) databaseObject;
RequestableStep requestableStep = (RequestableStep) stepVariable.getParent();
if (requestableStep != null) {
DatabaseObjectTreeObject parentDboTreeObject = ((DatabaseObjectTreeObject) treeObject).getParentDatabaseObjectTreeObject().getParentDatabaseObjectTreeObject();
RequestableObject requestableObject = null;
if (requestableStep instanceof SequenceStep) {
requestableObject = ((SequenceStep) requestableStep).getTargetSequence();
} else if (requestableStep instanceof TransactionStep) {
requestableObject = ((TransactionStep) requestableStep).getTargetTransaction();
}
if ((requestableObject != null) && (requestableObject instanceof IVariableContainer)) {
String variableName = stepVariable.getName();
IVariableContainer container = (IVariableContainer) requestableObject;
RequestableVariable variable = (RequestableVariable) container.getVariable(variableName);
// generate dom model
Document document = null;
try {
String description = variable.getDescription();
document = XMLUtils.parseDOMFromString(description);
} catch (Exception e) {
}
if (document != null) {
Element root = document.getDocumentElement();
if (root != null) {
// create step's structure from dom
DatabaseObject parentObject = requestableStep.getParent();
Step step = StepUtils.createStepFromXmlDomModel(parentObject, root);
// add step's structure to parent of requestableStep
if (parentObject instanceof Sequence) {
Sequence parentSequence = (Sequence) parentObject;
parentSequence.addStep(step);
parentSequence.insertAtOrder(step, requestableStep.priority);
} else {
StepWithExpressions parentSwe = (StepWithExpressions) parentObject;
parentSwe.addStep(step);
parentSwe.insertAtOrder(step, requestableStep.priority);
}
// set source definition of variable
XMLVector<String> sourceDefinition = new XMLVector<String>();
sourceDefinition.add(String.valueOf(step.priority));
sourceDefinition.add(".");
stepVariable.setSourceDefinition(sourceDefinition);
stepVariable.hasChanged = true;
// Reload parent dbo in tree
explorerView.reloadTreeObject(parentDboTreeObject);
// Select variable dbo in tree
explorerView.objectSelected(new CompositeEvent(databaseObject));
}
}
}
}
}
}
} catch (Throwable e) {
ConvertigoPlugin.logException(e, "Unable to generate and link variable!");
} finally {
shell.setCursor(null);
waitCursor.dispose();
}
}
use of com.twinsoft.convertigo.beans.variables.RequestableVariable in project convertigo by convertigo.
the class HttpConnector method prepareForTransaction.
@Override
public void prepareForTransaction(Context context) throws EngineException {
Engine.logBeans.debug("(HttpConnector) Preparing for transaction");
if (Boolean.parseBoolean(EnginePropertiesManager.getProperty(PropertyName.SSL_DEBUG))) {
System.setProperty("javax.net.debug", "all");
Engine.logBeans.trace("(HttpConnector) Enabling SSL debug mode");
} else {
System.setProperty("javax.net.debug", "");
Engine.logBeans.debug("(HttpConnector) Disabling SSL debug mode");
}
Engine.logBeans.debug("(HttpConnector) Initializing...");
if (context.isRequestFromVic) {
// instance, from a web service call).
if (!context.isTrustedRequest) {
try {
VicApi vicApi = new VicApi();
if (!vicApi.isServiceAuthorized(context.tasUserName, context.tasVirtualServerName, context.tasServiceCode)) {
throw new EngineException("The service '" + context.tasServiceCode + "' is not authorized for the user '" + context.tasUserName + "'");
}
} catch (IOException e) {
throw new EngineException("Unable to retrieve authorization from the VIC database.", e);
}
}
}
AbstractHttpTransaction httpTransaction = null;
try {
httpTransaction = (AbstractHttpTransaction) context.requestedObject;
} catch (ClassCastException e) {
throw new EngineException("Requested object is not a transaction", e);
}
handleCookie = httpTransaction.isHandleCookie();
if (!handleCookie && httpState != null) {
// remove cookies from previous transaction
httpState.clearCookies();
}
httpParameters = httpTransaction.getCurrentHttpParameters();
contentType = MimeType.WwwForm.value();
for (List<String> httpParameter : httpParameters) {
String headerName = httpParameter.get(0);
String value = httpParameter.get(1);
// Content-Type
if (HeaderName.ContentType.is(headerName)) {
contentType = value;
}
// oAuth Parameters are passed as standard Headers
if (HeaderName.OAuthKey.is(headerName)) {
oAuthKey = value;
}
if (HeaderName.OAuthSecret.is(headerName)) {
oAuthSecret = value;
}
if (HeaderName.OAuthToken.is(headerName)) {
oAuthToken = value;
}
if (HeaderName.OAuthTokenSecret.is(headerName)) {
oAuthTokenSecret = value;
}
}
{
String overrideContentType = ParameterUtils.toString(httpTransaction.getParameterValue(Parameter.HttpContentType.getName()));
if (overrideContentType != null) {
contentType = overrideContentType;
}
}
int len = httpTransaction.numberOfVariables();
boolean isFormUrlEncoded = MimeType.WwwForm.is(contentType);
doMultipartFormData = false;
for (int i = 0; i < len; i++) {
RequestableHttpVariable trVariable = (RequestableHttpVariable) httpTransaction.getVariable(i);
if (trVariable.getDoFileUploadMode() == DoFileUploadMode.multipartFormData) {
doMultipartFormData = true;
isFormUrlEncoded = true;
}
}
// Retrieve request template file if necessary
File requestTemplateFile = null;
if (!isFormUrlEncoded) {
String requestTemplateUrl = httpTransaction.getRequestTemplate();
if (!requestTemplateUrl.equals("")) {
String projectDirectoryName = context.project.getName();
String absoluteRequestTemplateUrl = Engine.projectDir(projectDirectoryName) + "/" + (context.subPath.length() > 0 ? context.subPath + "/" : "") + requestTemplateUrl;
Engine.logBeans.debug("(HttpConnector) Request template Url: " + absoluteRequestTemplateUrl);
requestTemplateFile = new File(absoluteRequestTemplateUrl);
if (!requestTemplateFile.exists()) {
Engine.logBeans.debug("(HttpConnector) The local request template file (\"" + absoluteRequestTemplateUrl + "\") does not exist. Trying search in Convertigo TEMPLATES directory...");
absoluteRequestTemplateUrl = Engine.TEMPLATES_PATH + "/" + requestTemplateUrl;
Engine.logBeans.debug("(HttpConnector) Request template Url: " + absoluteRequestTemplateUrl);
requestTemplateFile = new File(absoluteRequestTemplateUrl);
if (!requestTemplateFile.exists()) {
Engine.logBeans.debug("(HttpConnector) The common request template file (\"" + absoluteRequestTemplateUrl + "\") does not exist. Trying absolute search...");
absoluteRequestTemplateUrl = requestTemplateUrl;
Engine.logBeans.debug("(HttpConnector) Request template Url: " + absoluteRequestTemplateUrl);
requestTemplateFile = new File(absoluteRequestTemplateUrl);
if (!requestTemplateFile.exists()) {
throw new EngineException("Could not find any request template file \"" + requestTemplateUrl + "\" for transaction \"" + httpTransaction.getName() + "\".");
}
}
}
}
}
// Sets or overwrites server url
String httpUrl = httpTransaction.getParameterStringValue(Parameter.ConnectorConnectionString.getName());
if (org.apache.commons.lang3.StringUtils.isNotBlank(httpUrl)) {
setBaseUrl(httpUrl);
} else {
setBaseUrl();
String transactionBaseDir = httpTransaction.getCurrentSubDir();
if (transactionBaseDir.startsWith("http")) {
sUrl = transactionBaseDir;
/*
* if (transactionBaseDir.startsWith("https")) setHttps(true);
*/
} else {
sUrl += transactionBaseDir;
}
}
// Setup the SSL properties if needed
if (https) {
Engine.logBeans.debug("(HttpConnector) Setting up SSL properties");
certificateManager.collectStoreInformation(context);
}
String variable, method, httpVariable, queryString = "";
Object httpObjectVariableValue;
boolean isMultiValued = false;
boolean bIgnoreVariable = false;
String urlEncodingCharset = httpTransaction.getComputedUrlEncodingCharset();
// Replace variables in URL
List<String> urlPathVariableList = AbstractHttpTransaction.getPathVariableList(sUrl);
if (!urlPathVariableList.isEmpty()) {
Engine.logBeans.debug("(HttpConnector) Defined URL: " + sUrl);
for (String varName : urlPathVariableList) {
RequestableHttpVariable rVariable = (RequestableHttpVariable) httpTransaction.getVariable(varName);
httpObjectVariableValue = rVariable == null ? "" : httpTransaction.getParameterValue(varName);
httpVariable = rVariable == null ? "null" : varName;
method = rVariable == null ? "NULL" : rVariable.getHttpMethod();
Engine.logBeans.trace("(HttpConnector) Path variable: " + varName + " => (" + method + ") " + httpVariable);
sUrl = sUrl.replaceAll("\\{" + varName + "\\}", Matcher.quoteReplacement(ParameterUtils.toString(httpObjectVariableValue)));
}
}
// Build query string
for (int i = 0; i < len; i++) {
RequestableHttpVariable rVariable = (RequestableHttpVariable) httpTransaction.getVariable(i);
variable = rVariable.getName();
isMultiValued = rVariable.isMultiValued();
method = rVariable.getHttpMethod();
httpVariable = rVariable.getHttpName();
httpObjectVariableValue = httpTransaction.getParameterValue(variable);
bIgnoreVariable = urlPathVariableList.contains(variable) || httpObjectVariableValue == null || httpVariable.isEmpty() || !method.equals("GET");
if (!bIgnoreVariable) {
Engine.logBeans.trace("(HttpConnector) Query variable: " + variable + " => (" + method + ") " + httpVariable);
queryString = appendToQuery(queryString, isMultiValued, httpVariable, httpObjectVariableValue);
}
}
// Encodes URL if it contains special characters
sUrl = URLUtils.encodeAbsoluteURL(sUrl, urlEncodingCharset);
if (queryString.length() != 0) {
if (sUrl.indexOf('?') == -1) {
sUrl += "?" + queryString;
} else {
sUrl += "&" + queryString;
}
}
Engine.logBeans.debug("(HttpConnector) URL: " + sUrl);
if (Engine.logBeans.isDebugEnabled()) {
Engine.logBeans.debug("(HttpConnector) GET query: " + Visibility.Logs.replaceVariables(httpTransaction.getVariablesList(), queryString));
}
if (doMultipartFormData) {
Engine.logBeans.debug("(HttpConnector) Skip postQuery computing and do a multipart/formData content");
return;
}
// Build body for POST/PUT
postQuery = "";
// Load request template in postQuery if necessary
if (!isFormUrlEncoded) {
// the XSL in order to produce a real XML request template.
if (requestTemplateFile != null) {
try {
FileInputStream fis = new FileInputStream(requestTemplateFile);
Document requestTemplate = XMLUtils.parseDOM(fis);
Element documentElement = requestTemplate.getDocumentElement();
// XSL document
if (documentElement.getNodeName().equalsIgnoreCase("xsl:stylesheet")) {
// Build the variables XML document
Document variablesDocument = XMLUtils.createDom("java");
Element variablesElement = variablesDocument.createElement("variables");
variablesDocument.appendChild(variablesElement);
for (RequestableVariable requestableVariable : httpTransaction.getVariablesList()) {
RequestableHttpVariable trVariable = (RequestableHttpVariable) requestableVariable;
variable = trVariable.getName();
isMultiValued = trVariable.isMultiValued();
httpVariable = trVariable.getHttpName();
Element variableElement = variablesDocument.createElement("variable");
variablesElement.appendChild(variableElement);
variableElement.setAttribute("name", variable);
httpObjectVariableValue = httpTransaction.getParameterValue(variable);
if (httpObjectVariableValue != null) {
if (isMultiValued) {
variableElement.setAttribute("multi", "true");
if (httpObjectVariableValue instanceof Collection<?>) {
for (Object httpVariableValue : (Collection<?>) httpObjectVariableValue) {
Element valueElement = variablesDocument.createElement("value");
variableElement.appendChild(valueElement);
Text valueText = variablesDocument.createTextNode(getStringValue(trVariable, httpVariableValue));
valueElement.appendChild(valueText);
}
}
} else {
Element valueElement = variablesDocument.createElement("value");
variableElement.appendChild(valueElement);
Text valueText = variablesDocument.createTextNode(getStringValue(trVariable, httpObjectVariableValue));
valueElement.appendChild(valueText);
}
}
}
if (Engine.logBeans.isDebugEnabled()) {
String sVariablesDocument = XMLUtils.prettyPrintDOM((Document) Visibility.Logs.replaceVariables(httpTransaction.getVariablesList(), variablesDocument));
Engine.logBeans.debug("Build variables XML document:\n" + sVariablesDocument);
}
// Apply XSL
TransformerFactory tFactory = TransformerFactory.newInstance();
StreamSource streamSource = new StreamSource(new FileInputStream(requestTemplateFile));
Transformer transformer = tFactory.newTransformer(streamSource);
StringWriter sw = new StringWriter();
transformer.transform(new DOMSource(variablesElement), new StreamResult(sw));
postQuery = sw.getBuffer().toString();
} else // XML document
{
// Template has been parsed from file, retrieve its declared encoding char set
// If not found use "UTF-8" according to HTTP POST for text/xml (see getData)
String xmlEncoding = requestTemplate.getXmlEncoding();
xmlEncoding = (xmlEncoding == null) ? "UTF-8" : xmlEncoding;
postQuery = XMLUtils.prettyPrintDOMWithEncoding(requestTemplate, xmlEncoding);
}
} catch (Exception e) {
Engine.logBeans.warn("Unable to parse the request template file as a valid XML/XSL document");
throw new EngineException("An unexpected error occured while retrieving the request template file for transaction \"" + httpTransaction.getName() + "\".", e);
}
}
}
RequestableHttpVariable body = (RequestableHttpVariable) httpTransaction.getVariable(Parameter.HttpBody.getName());
if (body != null) {
method = body.getHttpMethod();
httpObjectVariableValue = httpTransaction.getParameterValue(Parameter.HttpBody.getName());
if (method.equals("POST") && httpObjectVariableValue != null) {
if ("application/json".equals(contentType) && httpObjectVariableValue instanceof Element) {
try {
postQuery = XMLUtils.XmlToJson(((Element) httpObjectVariableValue), true, true, JsonRoot.docChildNodes);
} catch (JSONException e) {
Engine.logBeans.warn("Failed to transform the XML input to JSON string: [" + e.getClass().getCanonicalName() + "] " + e.getMessage());
postQuery = ParameterUtils.toString(httpObjectVariableValue);
}
} else {
postQuery = ParameterUtils.toString(httpObjectVariableValue);
}
isFormUrlEncoded = false;
}
}
// Add all input variables marked as POST
boolean isLogHidden = false;
List<String> logHiddenValues = new ArrayList<String>();
for (int i = 0; i < len; i++) {
bIgnoreVariable = false;
RequestableHttpVariable trVariable = (RequestableHttpVariable) httpTransaction.getVariable(i);
variable = trVariable.getName();
isMultiValued = trVariable.isMultiValued();
method = trVariable.getHttpMethod();
httpVariable = trVariable.getHttpName();
isLogHidden = Visibility.Logs.isMasked(trVariable.getVisibility());
// do not add variable to query if empty name
if (httpVariable.equals(""))
bIgnoreVariable = true;
// Retrieves variable value
httpObjectVariableValue = httpTransaction.getParameterValue(variable);
if (method.equals("POST")) {
// variable must be sent as an HTTP parameter
if (!bIgnoreVariable) {
Engine.logBeans.trace("(HttpConnector) Parameter variable: " + variable + " => (" + method + ") " + httpVariable);
// Content-Type is 'application/x-www-form-urlencoded'
if (isFormUrlEncoded) {
// Replace variable value in postQuery
if (httpObjectVariableValue != null) {
// handle multivalued variable
postQuery = appendToQuery(postQuery, isMultiValued, httpVariable, httpObjectVariableValue);
}
} else // Content-Type is 'text/xml'
{
// Replace variable value in postQuery
if (httpObjectVariableValue != null) {
// Handle multivalued variable
if (isMultiValued) {
String varPattern = "$(" + httpVariable + ")";
int varPatternIndex, indexAfterPattern, beginTagIndex, endTagIndex;
if (httpObjectVariableValue instanceof Collection<?>) {
// pattern
while (postQuery.indexOf(varPattern) != -1) {
varPatternIndex = postQuery.indexOf(varPattern);
indexAfterPattern = varPatternIndex + varPattern.length();
if (postQuery.substring(indexAfterPattern).startsWith("concat")) {
// concat every value from the
// vector
// to replace the occurrence in the
// template
// by the concatenation of the
// multiple values
String httpVariableValue = "";
for (Object var : (Collection<?>) httpObjectVariableValue) httpVariableValue += getStringValue(trVariable, var);
if (isLogHidden)
logHiddenValues.add(httpVariableValue);
postQuery = postQuery.substring(0, varPatternIndex) + httpVariableValue + postQuery.substring(indexAfterPattern + "concat".length());
} else {
// duplicate the tag surrounding the
// occurrence in the template
// for each value from the vector
beginTagIndex = postQuery.substring(0, varPatternIndex).lastIndexOf('<');
endTagIndex = indexAfterPattern + postQuery.substring(indexAfterPattern).indexOf('>');
String tmpPostQuery = postQuery.substring(0, beginTagIndex);
for (Object httpVariableValue : (Collection<?>) httpObjectVariableValue) {
String stringValue = getStringValue(trVariable, httpVariableValue);
if (isLogHidden) {
logHiddenValues.add(stringValue);
}
tmpPostQuery += (postQuery.substring(beginTagIndex, varPatternIndex) + stringValue + postQuery.substring(indexAfterPattern, endTagIndex + 1));
}
tmpPostQuery += postQuery.substring(endTagIndex + 1);
postQuery = tmpPostQuery;
}
}
} else {
String stringValue = getStringValue(trVariable, httpObjectVariableValue);
if (isLogHidden) {
logHiddenValues.add(stringValue);
}
StringEx sx = new StringEx(postQuery);
sx.replaceAll("$(" + httpVariable + ")concat", stringValue);
postQuery = sx.toString();
}
} else // Handle single valued variable
{
String stringValue = getStringValue(trVariable, httpObjectVariableValue);
if (isLogHidden) {
logHiddenValues.add(stringValue);
}
StringEx sx = new StringEx(postQuery);
sx.replaceAll("$(" + httpVariable + ")noE", stringValue);
sx.replaceAll("$(" + httpVariable + ")", stringValue);
postQuery = sx.toString();
}
} else // Remove variable from postQuery
{
String varPattern = "$(" + httpVariable + ")";
int varPatternIndex, beginTagIndex, endTagIndex;
// while postQuery contains the variable pattern
while (postQuery.indexOf(varPattern) != -1) {
varPatternIndex = postQuery.indexOf(varPattern);
beginTagIndex = postQuery.substring(0, varPatternIndex).lastIndexOf('<');
endTagIndex = postQuery.indexOf('>', varPatternIndex);
postQuery = postQuery.substring(0, beginTagIndex) + postQuery.substring(endTagIndex + 1);
}
}
}
}
} else if (method.equals("")) {
// Replace variable value in postQuery
if (httpObjectVariableValue != null) {
if (!isFormUrlEncoded && (!(httpVariable.equals("")))) {
// used
// to
// replace
// empty
// element
String stringValue = getStringValue(trVariable, httpObjectVariableValue);
if (isLogHidden) {
logHiddenValues.add(stringValue);
}
StringEx sx = new StringEx(postQuery);
sx.replaceAll(httpVariable, stringValue);
postQuery = sx.toString();
}
}
}
}
if (Engine.logBeans.isDebugEnabled()) {
Engine.logBeans.debug("(HttpConnector) POST query: " + (isFormUrlEncoded ? "" : "\n") + (isFormUrlEncoded ? Visibility.Logs.replaceVariables(httpTransaction.getVariablesList(), postQuery) : Visibility.Logs.replaceValues(logHiddenValues, postQuery)));
}
Engine.logBeans.debug("(HttpConnector) Connector successfully prepared for transaction");
}
Aggregations