Search in sources :

Example 46 with RequestableVariable

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

the class TransactionTreeObject method detectVariables.

/**
 * SQL TRANSACTION *
 */
private void detectVariables(String queries, String oldQueries, List<RequestableVariable> listVariables) {
    if (queries != null && !queries.equals("")) {
        // We create an another List which permit to compare and update variables
        Set<String> newSQLQueriesVariablesNames = getSetVariableNames(queries);
        Set<String> oldSQLQueriesVariablesNames = getSetVariableNames(oldQueries);
        // Modify variables definition if needed
        if (listVariables != null && !oldSQLQueriesVariablesNames.equals(newSQLQueriesVariablesNames)) {
            for (RequestableVariable variable : listVariables) {
                String variableName = variable.getName();
                if (oldSQLQueriesVariablesNames.contains(variableName) && !newSQLQueriesVariablesNames.contains(variableName)) {
                    try {
                        MessageBox messageBox = new MessageBox(viewer.getControl().getShell(), SWT.ICON_QUESTION | SWT.YES | SWT.NO);
                        messageBox.setMessage("Do you really want to delete the variable \"" + variableName + "\"?");
                        messageBox.setText("Delete \"" + variableName + "\"?");
                        if (messageBox.open() == SWT.YES) {
                            variable.delete();
                        }
                    } catch (EngineException e) {
                        ConvertigoPlugin.logException(e, "Error when deleting the variable \"" + variableName + "\"");
                    }
                }
            }
        }
    }
}
Also used : EngineException(com.twinsoft.convertigo.engine.EngineException) RequestableVariable(com.twinsoft.convertigo.beans.variables.RequestableVariable) MessageBox(org.eclipse.swt.widgets.MessageBox)

Example 47 with RequestableVariable

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

the class HttpConnector method getData.

public byte[] getData(Context context, String sUrl) throws IOException, EngineException {
    HttpMethod method = null;
    try {
        // Fire event for plugins
        long t0 = System.currentTimeMillis();
        Engine.theApp.pluginsManager.fireHttpConnectorGetDataStart(context);
        Engine.logBeans.trace("(HttpConnector) Retrieving data as a bytes array...");
        Engine.logBeans.debug("(HttpConnector) Connecting to: " + sUrl);
        // Setting the referer
        referer = sUrl;
        Engine.logBeans.debug("(HttpConnector) Https: " + https);
        URL url = new URL(sUrl);
        String host = url.getHost();
        int port = url.getPort();
        if (sUrl.toLowerCase().startsWith("https:")) {
            if (!https) {
                Engine.logBeans.debug("(HttpConnector) Setting up SSL properties");
                certificateManager.collectStoreInformation(context);
            }
            if (port == -1)
                port = 443;
            Engine.logBeans.debug("(HttpConnector) Host: " + host + ":" + port);
            Engine.logBeans.debug("(HttpConnector) CertificateManager has changed: " + certificateManager.hasChanged);
            if (certificateManager.hasChanged || (!host.equalsIgnoreCase(hostConfiguration.getHost())) || (hostConfiguration.getPort() != port)) {
                Engine.logBeans.debug("(HttpConnector) Using MySSLSocketFactory for creating the SSL socket");
                Protocol myhttps = new Protocol("https", MySSLSocketFactory.getSSLSocketFactory(certificateManager.keyStore, certificateManager.keyStorePassword, certificateManager.trustStore, certificateManager.trustStorePassword, this.trustAllServerCertificates), port);
                hostConfiguration.setHost(host, port, myhttps);
            }
            sUrl = url.getFile();
            Engine.logBeans.debug("(HttpConnector) Updated URL for SSL purposes: " + sUrl);
        } else {
            Engine.logBeans.debug("(HttpConnector) Host: " + host + ":" + port);
            hostConfiguration.setHost(host, port);
        }
        // Retrieving httpState
        getHttpState(context);
        // Proxy configuration
        Engine.theApp.proxyManager.setProxy(hostConfiguration, httpState, url);
        AbstractHttpTransaction httpTransaction = (AbstractHttpTransaction) context.transaction;
        // Retrieve HTTP method
        HttpMethodType httpVerb = httpTransaction.getHttpVerb();
        String sHttpVerb = httpVerb.name();
        final String sCustomHttpVerb = httpTransaction.getCustomHttpVerb();
        if (sCustomHttpVerb.length() > 0) {
            Engine.logBeans.debug("(HttpConnector) HTTP verb: " + sHttpVerb + " overridden to '" + sCustomHttpVerb + "'");
            switch(httpVerb) {
                case GET:
                    method = new GetMethod(sUrl) {

                        @Override
                        public String getName() {
                            return sCustomHttpVerb;
                        }
                    };
                    break;
                case POST:
                    method = new PostMethod(sUrl) {

                        @Override
                        public String getName() {
                            return sCustomHttpVerb;
                        }
                    };
                    break;
                case PUT:
                    method = new PutMethod(sUrl) {

                        @Override
                        public String getName() {
                            return sCustomHttpVerb;
                        }
                    };
                    break;
                case DELETE:
                    method = new DeleteMethod(sUrl) {

                        @Override
                        public String getName() {
                            return sCustomHttpVerb;
                        }
                    };
                    break;
                case HEAD:
                    method = new HeadMethod(sUrl) {

                        @Override
                        public String getName() {
                            return sCustomHttpVerb;
                        }
                    };
                    break;
                case OPTIONS:
                    method = new OptionsMethod(sUrl) {

                        @Override
                        public String getName() {
                            return sCustomHttpVerb;
                        }
                    };
                    break;
                case TRACE:
                    method = new TraceMethod(sUrl) {

                        @Override
                        public String getName() {
                            return sCustomHttpVerb;
                        }
                    };
                    break;
            }
        } else {
            Engine.logBeans.debug("(HttpConnector) HTTP verb: " + sHttpVerb);
            switch(httpVerb) {
                case GET:
                    method = new GetMethod(sUrl);
                    break;
                case POST:
                    method = new PostMethod(sUrl);
                    break;
                case PUT:
                    method = new PutMethod(sUrl);
                    break;
                case DELETE:
                    method = new DeleteMethod(sUrl);
                    break;
                case HEAD:
                    method = new HeadMethod(sUrl);
                    break;
                case OPTIONS:
                    method = new OptionsMethod(sUrl);
                    break;
                case TRACE:
                    method = new TraceMethod(sUrl);
                    break;
            }
        }
        // Setting HTTP parameters
        boolean hasUserAgent = false;
        for (List<String> httpParameter : httpParameters) {
            String key = httpParameter.get(0);
            String value = httpParameter.get(1);
            if (key.equalsIgnoreCase("host") && !value.equals(host)) {
                value = host;
            }
            if (!key.startsWith(DYNAMIC_HEADER_PREFIX)) {
                method.setRequestHeader(key, value);
            }
            if (HeaderName.UserAgent.is(key)) {
                hasUserAgent = true;
            }
        }
        // set user-agent header if not found
        if (!hasUserAgent) {
            HeaderName.UserAgent.setRequestHeader(method, getUserAgent(context));
        }
        // Setting POST or PUT parameters if any
        Engine.logBeans.debug("(HttpConnector) Setting " + httpVerb + " data");
        if (method instanceof EntityEnclosingMethod) {
            EntityEnclosingMethod entityEnclosingMethod = (EntityEnclosingMethod) method;
            AbstractHttpTransaction transaction = (AbstractHttpTransaction) context.requestedObject;
            if (doMultipartFormData) {
                RequestableHttpVariable body = (RequestableHttpVariable) httpTransaction.getVariable(Parameter.HttpBody.getName());
                if (body != null && body.getDoFileUploadMode() == DoFileUploadMode.multipartFormData) {
                    String stringValue = httpTransaction.getParameterStringValue(Parameter.HttpBody.getName());
                    String filepath = Engine.theApp.filePropertyManager.getFilepathFromProperty(stringValue, getProject().getName());
                    File file = new File(filepath);
                    if (file.exists()) {
                        HeaderName.ContentType.setRequestHeader(method, contentType);
                        entityEnclosingMethod.setRequestEntity(new FileRequestEntity(file, contentType));
                    } else {
                        throw new FileNotFoundException(file.getAbsolutePath());
                    }
                } else {
                    List<Part> parts = new LinkedList<Part>();
                    for (RequestableVariable variable : transaction.getVariablesList()) {
                        if (variable instanceof RequestableHttpVariable) {
                            RequestableHttpVariable httpVariable = (RequestableHttpVariable) variable;
                            if ("POST".equals(httpVariable.getHttpMethod())) {
                                Object httpObjectVariableValue = transaction.getVariableValue(httpVariable.getName());
                                if (httpVariable.isMultiValued()) {
                                    if (httpObjectVariableValue instanceof Collection<?>) {
                                        for (Object httpVariableValue : (Collection<?>) httpObjectVariableValue) {
                                            addFormDataPart(parts, httpVariable, httpVariableValue);
                                        }
                                    }
                                } else {
                                    addFormDataPart(parts, httpVariable, httpObjectVariableValue);
                                }
                            }
                        }
                    }
                    MultipartRequestEntity mre = new MultipartRequestEntity(parts.toArray(new Part[parts.size()]), entityEnclosingMethod.getParams());
                    HeaderName.ContentType.setRequestHeader(method, mre.getContentType());
                    entityEnclosingMethod.setRequestEntity(mre);
                }
            } else if (MimeType.TextXml.is(contentType)) {
                final MimeMultipart[] mp = { null };
                for (RequestableVariable variable : transaction.getVariablesList()) {
                    if (variable instanceof RequestableHttpVariable) {
                        RequestableHttpVariable httpVariable = (RequestableHttpVariable) variable;
                        if (httpVariable.getDoFileUploadMode() == DoFileUploadMode.MTOM) {
                            Engine.logBeans.trace("(HttpConnector) Variable " + httpVariable.getName() + " detected as MTOM");
                            MimeMultipart mimeMultipart = mp[0];
                            try {
                                if (mimeMultipart == null) {
                                    Engine.logBeans.debug("(HttpConnector) Preparing the MTOM request");
                                    mimeMultipart = new MimeMultipart("related; type=\"application/xop+xml\"");
                                    MimeBodyPart bp = new MimeBodyPart();
                                    bp.setText(postQuery, "UTF-8");
                                    bp.setHeader(HeaderName.ContentType.value(), contentType);
                                    mimeMultipart.addBodyPart(bp);
                                }
                                Object httpObjectVariableValue = transaction.getVariableValue(httpVariable.getName());
                                if (httpVariable.isMultiValued()) {
                                    if (httpObjectVariableValue instanceof Collection<?>) {
                                        for (Object httpVariableValue : (Collection<?>) httpObjectVariableValue) {
                                            addMtomPart(mimeMultipart, httpVariable, httpVariableValue);
                                        }
                                    }
                                } else {
                                    addMtomPart(mimeMultipart, httpVariable, httpObjectVariableValue);
                                }
                                mp[0] = mimeMultipart;
                            } catch (Exception e) {
                                Engine.logBeans.warn("(HttpConnector) Failed to add MTOM part for " + httpVariable.getName(), e);
                            }
                        }
                    }
                }
                if (mp[0] == null) {
                    entityEnclosingMethod.setRequestEntity(new StringRequestEntity(postQuery, MimeType.TextXml.value(), "UTF-8"));
                } else {
                    Engine.logBeans.debug("(HttpConnector) Commit the MTOM request with the ContentType: " + mp[0].getContentType());
                    HeaderName.ContentType.setRequestHeader(method, mp[0].getContentType());
                    entityEnclosingMethod.setRequestEntity(new RequestEntity() {

                        @Override
                        public void writeRequest(OutputStream outputStream) throws IOException {
                            try {
                                mp[0].writeTo(outputStream);
                            } catch (MessagingException e) {
                                new IOException(e);
                            }
                        }

                        @Override
                        public boolean isRepeatable() {
                            return true;
                        }

                        @Override
                        public String getContentType() {
                            return mp[0].getContentType();
                        }

                        @Override
                        public long getContentLength() {
                            return -1;
                        }
                    });
                }
            } else {
                String charset = httpTransaction.getComputedUrlEncodingCharset();
                HeaderName.ContentType.setRequestHeader(method, contentType);
                entityEnclosingMethod.setRequestEntity(new StringRequestEntity(postQuery, contentType, charset));
            }
        }
        // Getting the result
        Engine.logBeans.debug("(HttpConnector) HttpClient: getting response body");
        byte[] result = executeMethod(method, context);
        long length = result != null ? result.length : 0;
        if (context.transaction instanceof DownloadHttpTransaction) {
            try {
                length = (long) context.get("__downloadedFileLength");
            } catch (Exception e) {
            }
        }
        Engine.logBeans.debug("(HttpConnector) Total read bytes: " + length);
        // Fire event for plugins
        long t1 = System.currentTimeMillis();
        Engine.theApp.pluginsManager.fireHttpConnectorGetDataEnd(context, t0, t1);
        StringBuilder sb = new StringBuilder();
        sb.append("HTTP result {ContentType: " + context.contentType + ", Length: " + length + "}\n\n");
        if (result != null && context.contentType != null && (context.contentType.startsWith("text/") || context.contentType.startsWith("application/xml") || context.contentType.startsWith("application/json"))) {
            sb.append(new String(result, "UTF-8"));
        }
        fireDataChanged(new ConnectorEvent(this, sb.toString()));
        return result;
    } finally {
        if (method != null)
            method.releaseConnection();
    }
}
Also used : RequestableHttpVariable(com.twinsoft.convertigo.beans.variables.RequestableHttpVariable) StringRequestEntity(org.apache.commons.httpclient.methods.StringRequestEntity) HttpMethodType(com.twinsoft.convertigo.engine.enums.HttpMethodType) PostMethod(org.apache.commons.httpclient.methods.PostMethod) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) FileNotFoundException(java.io.FileNotFoundException) URL(java.net.URL) AbstractHttpTransaction(com.twinsoft.convertigo.beans.transactions.AbstractHttpTransaction) HeadMethod(org.apache.commons.httpclient.methods.HeadMethod) OptionsMethod(org.apache.commons.httpclient.methods.OptionsMethod) MimeMultipart(javax.mail.internet.MimeMultipart) BigMimeMultipart(com.twinsoft.convertigo.engine.util.BigMimeMultipart) Protocol(org.apache.commons.httpclient.protocol.Protocol) DeleteMethod(org.apache.commons.httpclient.methods.DeleteMethod) ConnectorEvent(com.twinsoft.convertigo.beans.core.ConnectorEvent) MessagingException(javax.mail.MessagingException) TraceMethod(org.apache.commons.httpclient.methods.TraceMethod) EntityEnclosingMethod(org.apache.commons.httpclient.methods.EntityEnclosingMethod) RequestableVariable(com.twinsoft.convertigo.beans.variables.RequestableVariable) IOException(java.io.IOException) MultipartRequestEntity(org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity) LinkedList(java.util.LinkedList) URIException(org.apache.commons.httpclient.URIException) SocketTimeoutException(java.net.SocketTimeoutException) OAuthException(oauth.signpost.exception.OAuthException) IOException(java.io.IOException) JSONException(org.codehaus.jettison.json.JSONException) MessagingException(javax.mail.MessagingException) FileNotFoundException(java.io.FileNotFoundException) EngineException(com.twinsoft.convertigo.engine.EngineException) MalformedURLException(java.net.MalformedURLException) FileRequestEntity(org.apache.commons.httpclient.methods.FileRequestEntity) DownloadHttpTransaction(com.twinsoft.convertigo.beans.transactions.DownloadHttpTransaction) StringPart(org.apache.commons.httpclient.methods.multipart.StringPart) FilePart(org.apache.commons.httpclient.methods.multipart.FilePart) MimePart(javax.mail.internet.MimePart) Part(org.apache.commons.httpclient.methods.multipart.Part) MimeBodyPart(javax.mail.internet.MimeBodyPart) GetMethod(org.apache.commons.httpclient.methods.GetMethod) PutMethod(org.apache.commons.httpclient.methods.PutMethod) Collection(java.util.Collection) MimeBodyPart(javax.mail.internet.MimeBodyPart) RequestEntity(org.apache.commons.httpclient.methods.RequestEntity) FileRequestEntity(org.apache.commons.httpclient.methods.FileRequestEntity) MultipartRequestEntity(org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity) StringRequestEntity(org.apache.commons.httpclient.methods.StringRequestEntity) File(java.io.File) HttpMethod(org.apache.commons.httpclient.HttpMethod)

Example 48 with RequestableVariable

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

the class SapJcoConnector method executeJCoSearch.

public static Document executeJCoSearch(SapJcoConnector connector, String pattern) throws EngineException {
    SapJcoTransaction transaction = (SapJcoTransaction) connector.newTransaction();
    transaction.setBapiName("RFC_FUNCTION_SEARCH");
    transaction.context = connector.context;
    transaction.setParent(connector);
    RequestableVariable variable = new RequestableVariable();
    variable.setName("FUNCNAME");
    variable.setValueOrNull(pattern);
    transaction.addVariable(variable);
    return connector.getSapJCoProvider().executeJCoFunction(transaction, true);
}
Also used : SapJcoTransaction(com.twinsoft.convertigo.beans.transactions.SapJcoTransaction) RequestableVariable(com.twinsoft.convertigo.beans.variables.RequestableVariable)

Example 49 with RequestableVariable

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

the class RequestableStep method exportVariableDefinition.

public void exportVariableDefinition() throws EngineException {
    for (StepVariable stepVariable : getVariables()) {
        String variableName = stepVariable.getName();
        if (sequence.getVariable(variableName) == null) {
            if (!StringUtils.isNormalized(variableName))
                throw new EngineException("Variable name is not normalized : \"" + variableName + "\".");
            RequestableVariable requestableVariable = stepVariable.isMultiValued() ? new RequestableMultiValuedVariable() : new RequestableVariable();
            requestableVariable.setName(variableName);
            requestableVariable.setComment(stepVariable.getComment());
            requestableVariable.setDescription(stepVariable.getDescription());
            requestableVariable.setComment(stepVariable.getComment());
            requestableVariable.setRequired(stepVariable.isRequired());
            requestableVariable.setValueOrNull(stepVariable.getValueOrNull());
            requestableVariable.setVisibility(stepVariable.getVisibility());
            sequence.addVariable(requestableVariable);
            requestableVariable.bNew = true;
            requestableVariable.hasChanged = true;
            sequence.hasChanged = true;
        }
    }
}
Also used : EngineException(com.twinsoft.convertigo.engine.EngineException) RequestableMultiValuedVariable(com.twinsoft.convertigo.beans.variables.RequestableMultiValuedVariable) RequestableVariable(com.twinsoft.convertigo.beans.variables.RequestableVariable) StepVariable(com.twinsoft.convertigo.beans.variables.StepVariable)

Example 50 with RequestableVariable

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

the class RequestableStep method importVariableDefinition.

public void importVariableDefinition(RequestableObject requestable) throws EngineException {
    if (!(requestable instanceof IVariableContainer))
        return;
    IVariableContainer container = (IVariableContainer) requestable;
    int size = container.numberOfVariables();
    for (int i = 0; i < size; i++) {
        RequestableVariable variable = (RequestableVariable) container.getVariable(i);
        if (variable != null) {
            String variableName = variable.getName();
            if (getVariable(variableName) == null) {
                if (!StringUtils.isNormalized(variableName))
                    throw new EngineException("Variable name is not normalized : \"" + variableName + "\".");
                StepVariable stepVariable = variable.isMultiValued() ? new StepMultiValuedVariable() : new StepVariable();
                stepVariable.setName(variableName);
                stepVariable.setComment(variable.getComment());
                stepVariable.setDescription(variable.getDescription());
                stepVariable.setSourceDefinition(new XMLVector<String>());
                stepVariable.setRequired(variable.isRequired());
                stepVariable.setValueOrNull(variable.getValueOrNull());
                stepVariable.setVisibility(variable.getVisibility());
                addVariable(stepVariable);
                stepVariable.bNew = true;
                stepVariable.hasChanged = true;
                hasChanged = true;
            }
        }
    }
}
Also used : EngineException(com.twinsoft.convertigo.engine.EngineException) RequestableVariable(com.twinsoft.convertigo.beans.variables.RequestableVariable) StepVariable(com.twinsoft.convertigo.beans.variables.StepVariable) StepMultiValuedVariable(com.twinsoft.convertigo.beans.variables.StepMultiValuedVariable)

Aggregations

RequestableVariable (com.twinsoft.convertigo.beans.variables.RequestableVariable)57 EngineException (com.twinsoft.convertigo.engine.EngineException)24 DatabaseObject (com.twinsoft.convertigo.beans.core.DatabaseObject)14 Sequence (com.twinsoft.convertigo.beans.core.Sequence)11 Element (org.w3c.dom.Element)11 RequestableHttpVariable (com.twinsoft.convertigo.beans.variables.RequestableHttpVariable)10 DatabaseObjectTreeObject (com.twinsoft.convertigo.eclipse.views.projectexplorer.model.DatabaseObjectTreeObject)8 TreeObject (com.twinsoft.convertigo.eclipse.views.projectexplorer.model.TreeObject)8 StepVariable (com.twinsoft.convertigo.beans.variables.StepVariable)7 TestCaseVariable (com.twinsoft.convertigo.beans.variables.TestCaseVariable)7 Variable (com.twinsoft.convertigo.beans.core.Variable)6 Shell (org.eclipse.swt.widgets.Shell)6 TransactionWithVariables (com.twinsoft.convertigo.beans.core.TransactionWithVariables)5 RequestableMultiValuedVariable (com.twinsoft.convertigo.beans.variables.RequestableMultiValuedVariable)5 ProjectExplorerView (com.twinsoft.convertigo.eclipse.views.projectexplorer.ProjectExplorerView)5 Cursor (org.eclipse.swt.graphics.Cursor)5 Display (org.eclipse.swt.widgets.Display)5 XMLVector (com.twinsoft.convertigo.beans.common.XMLVector)4 RequestableObject (com.twinsoft.convertigo.beans.core.RequestableObject)4 XmlSchemaElement (org.apache.ws.commons.schema.XmlSchemaElement)4