Search in sources :

Example 6 with AbstractHttpTransaction

use of com.twinsoft.convertigo.beans.transactions.AbstractHttpTransaction in project convertigo by convertigo.

the class TransactionTreeObject method treeObjectRemoved.

@Override
public void treeObjectRemoved(TreeObjectEvent treeObjectEvent) {
    super.treeObjectRemoved(treeObjectEvent);
    TreeObject treeObject = (TreeObject) treeObjectEvent.getSource();
    if (!(treeObject.equals(this)) && (treeObject.getParents().contains(this))) {
        if (treeObject instanceof DatabaseObjectTreeObject) {
            DatabaseObject databaseObject = (DatabaseObject) treeObject.getObject();
            // A variable has been removed
            if (databaseObject instanceof Variable) {
                String variableName = databaseObject.getName();
                // This is an AbstractHttpTransaction
                if (getObject() instanceof AbstractHttpTransaction) {
                    AbstractHttpTransaction httpTransaction = (AbstractHttpTransaction) getObject();
                    String transactionSubDir = httpTransaction.getSubDir();
                    List<String> pathVariableList = AbstractHttpTransaction.getPathVariableList(httpTransaction.getSubDir());
                    // Update transaction SubDir property
                    if (pathVariableList.contains(variableName)) {
                        transactionSubDir = transactionSubDir.replaceAll("\\{" + variableName + "\\}", "");
                        httpTransaction.setSubDir(transactionSubDir);
                        httpTransaction.hasChanged = true;
                        try {
                            getProjectExplorerView().updateTreeObject(this);
                        } catch (Exception e) {
                            ConvertigoPlugin.logWarning(e, "Could not update in tree Transaction \"" + databaseObject.getName() + "\" !");
                        }
                    }
                }
            }
        }
    }
}
Also used : RequestableHttpVariable(com.twinsoft.convertigo.beans.variables.RequestableHttpVariable) Variable(com.twinsoft.convertigo.beans.core.Variable) RequestableVariable(com.twinsoft.convertigo.beans.variables.RequestableVariable) DatabaseObject(com.twinsoft.convertigo.beans.core.DatabaseObject) PartInitException(org.eclipse.ui.PartInitException) EngineException(com.twinsoft.convertigo.engine.EngineException) AbstractHttpTransaction(com.twinsoft.convertigo.beans.transactions.AbstractHttpTransaction)

Example 7 with AbstractHttpTransaction

use of com.twinsoft.convertigo.beans.transactions.AbstractHttpTransaction in project convertigo by convertigo.

the class TransactionTreeObject method treeObjectPropertyChanged.

@Override
public void treeObjectPropertyChanged(TreeObjectEvent treeObjectEvent) {
    super.treeObjectPropertyChanged(treeObjectEvent);
    String propertyName = (String) treeObjectEvent.propertyName;
    propertyName = ((propertyName == null) ? "" : propertyName);
    TreeObject treeObject = (TreeObject) treeObjectEvent.getSource();
    if (treeObject instanceof DatabaseObjectTreeObject) {
        DatabaseObject databaseObject = (DatabaseObject) treeObject.getObject();
        // If a bean name has changed
        if ("name".equals(propertyName)) {
            handlesBeanNameChanged(treeObjectEvent);
        } else if ("sqlQuery".equals(propertyName)) {
            if (treeObject.equals(this)) {
                try {
                    SqlTransaction sqlTransaction = (SqlTransaction) databaseObject;
                    sqlTransaction.initializeQueries(true);
                    String oldValue = (String) treeObjectEvent.oldValue;
                    detectVariables(sqlTransaction.getSqlQuery(), oldValue, sqlTransaction.getVariablesList());
                    getProjectExplorerView().reloadTreeObject(this);
                } catch (Exception e) {
                    ConvertigoPlugin.logWarning(e, "Could not reload in tree Transaction \"" + databaseObject.getName() + "\" !");
                }
            }
        } else if ("subDir".equals(propertyName)) {
            if (treeObject.equals(this)) {
                try {
                    Object oldValue = treeObjectEvent.oldValue;
                    Object newValue = treeObjectEvent.newValue;
                    AbstractHttpTransaction httpTransaction = (AbstractHttpTransaction) databaseObject;
                    List<String> oldPathVariableList = AbstractHttpTransaction.getPathVariableList(oldValue.toString());
                    List<String> newPathVariableList = AbstractHttpTransaction.getPathVariableList(newValue.toString());
                    // Check for variables to be renamed
                    if (oldValue.toString().replaceAll("\\{([a-zA-Z0-9_]+)\\}", "{}").equals(newValue.toString().replaceAll("\\{([a-zA-Z0-9_]+)\\}", "{}"))) {
                        for (int i = 0; i < oldPathVariableList.size(); i++) {
                            String oldVariableName = oldPathVariableList.get(i);
                            String newVariableName = newPathVariableList.get(i);
                            if (!oldVariableName.equals(newVariableName)) {
                                RequestableHttpVariable httpVariable = (RequestableHttpVariable) httpTransaction.getVariable(oldVariableName);
                                if (httpVariable != null) {
                                    try {
                                        VariableTreeObject2 vto = (VariableTreeObject2) findTreeObjectByUserObject(httpVariable);
                                        int update = TreeObjectEvent.UPDATE_NONE;
                                        CustomDialog customDialog = new CustomDialog(viewer.getControl().getShell(), "Update object references", "Do you want to update " + "variable" + " references ?\n You can replace '" + oldVariableName + "' by '" + newVariableName + "' in all loaded projects \n or replace '" + oldVariableName + "' by '" + newVariableName + "' in current project only.", 670, 200, new ButtonSpec("Replace in all loaded projects", true), new ButtonSpec("Replace in current project", false), new ButtonSpec("Do not replace anywhere", false));
                                        int response = customDialog.open();
                                        if (response == 0) {
                                            update = TreeObjectEvent.UPDATE_ALL;
                                        }
                                        if (response == 1) {
                                            update = TreeObjectEvent.UPDATE_LOCAL;
                                        }
                                        if (update != 0) {
                                            httpVariable.setName(newVariableName);
                                            httpVariable.hasChanged = true;
                                            TreeObjectEvent toEvent = new TreeObjectEvent(vto, "name", oldVariableName, newVariableName, update);
                                            getProjectExplorerView().fireTreeObjectPropertyChanged(toEvent);
                                        }
                                    } catch (Exception e) {
                                        ConvertigoPlugin.logWarning(e, "Could not rename variable for Transaction \"" + databaseObject.getName() + "\" !");
                                    }
                                }
                            }
                        }
                    } else {
                        // Check for variables to be added
                        for (String variableName : newPathVariableList) {
                            if (httpTransaction.getVariable(variableName) == null) {
                                RequestableHttpVariable httpVariable = new RequestableHttpVariable();
                                httpVariable.setName(variableName);
                                httpVariable.setHttpMethod("GET");
                                httpVariable.setHttpName("");
                                httpVariable.bNew = true;
                                httpVariable.hasChanged = true;
                                httpTransaction.addVariable(httpVariable);
                                httpTransaction.hasChanged = true;
                            }
                        }
                        // Check for variables to be deleted
                        for (String variableName : oldPathVariableList) {
                            RequestableHttpVariable httpVariable = (RequestableHttpVariable) httpTransaction.getVariable(variableName);
                            if (httpVariable != null) {
                                if (!newPathVariableList.contains(variableName)) {
                                    try {
                                        MessageBox messageBox = new MessageBox(viewer.getControl().getShell(), SWT.ICON_QUESTION | SWT.YES | SWT.NO);
                                        messageBox.setMessage("Do you want to delete the variable \"" + variableName + "\"?");
                                        messageBox.setText("Delete \"" + variableName + "\"?");
                                        if (messageBox.open() == SWT.YES) {
                                            httpVariable.delete();
                                            httpTransaction.hasChanged = true;
                                        }
                                    } catch (EngineException e) {
                                        ConvertigoPlugin.logException(e, "Error when deleting the variable \"" + variableName + "\"");
                                    }
                                }
                            }
                        }
                    }
                    if (httpTransaction.hasChanged) {
                        getProjectExplorerView().reloadTreeObject(this);
                    }
                } catch (Exception e) {
                    ConvertigoPlugin.logWarning(e, "Could not reload in tree Transaction \"" + databaseObject.getName() + "\" !");
                }
            }
        }
    }
}
Also used : RequestableHttpVariable(com.twinsoft.convertigo.beans.variables.RequestableHttpVariable) EngineException(com.twinsoft.convertigo.engine.EngineException) SqlTransaction(com.twinsoft.convertigo.beans.transactions.SqlTransaction) PartInitException(org.eclipse.ui.PartInitException) EngineException(com.twinsoft.convertigo.engine.EngineException) AbstractHttpTransaction(com.twinsoft.convertigo.beans.transactions.AbstractHttpTransaction) MessageBox(org.eclipse.swt.widgets.MessageBox) CustomDialog(com.twinsoft.convertigo.eclipse.dialogs.CustomDialog) DatabaseObject(com.twinsoft.convertigo.beans.core.DatabaseObject) DatabaseObject(com.twinsoft.convertigo.beans.core.DatabaseObject) TreeObjectEvent(com.twinsoft.convertigo.eclipse.views.projectexplorer.TreeObjectEvent) ButtonSpec(com.twinsoft.convertigo.eclipse.dialogs.ButtonSpec)

Example 8 with AbstractHttpTransaction

use of com.twinsoft.convertigo.beans.transactions.AbstractHttpTransaction in project convertigo by convertigo.

the class Context method getCookieStrings.

public Vector<String> getCookieStrings() {
    // to the client.
    if (requestedObject instanceof AbstractHttpTransaction) {
        if (!((AbstractHttpTransaction) requestedObject).isHandleCookie())
            return new Vector<String>();
    }
    Vector<String> cookies = new Vector<String>();
    if (httpState != null) {
        Cookie[] httpCookies = httpState.getCookies();
        int len = httpCookies.length;
        Cookie cookie = null;
        String sCookie;
        DateFormat df = new SimpleDateFormat("EEE, dd-MMM-yyyy HH:mm:ss z", Locale.US);
        df.setTimeZone(TimeZone.getTimeZone("GMT"));
        for (int i = 0; i < len; i++) {
            cookie = httpCookies[i];
            sCookie = cookie.getName() + "=" + cookie.getValue() + ";";
            sCookie += (cookie.getExpiryDate() != null) ? "expires=" + df.format(cookie.getExpiryDate()) + ";" : "";
            sCookie += "path=" + cookie.getPath() + ";";
            sCookie += "domain=" + cookie.getDomain() + ";";
            sCookie += cookie.getSecure() ? "secure" : "";
            cookies.add(sCookie);
        }
    }
    return cookies;
}
Also used : Cookie(org.apache.commons.httpclient.Cookie) DateFormat(java.text.DateFormat) SimpleDateFormat(java.text.SimpleDateFormat) Vector(java.util.Vector) SimpleDateFormat(java.text.SimpleDateFormat) AbstractHttpTransaction(com.twinsoft.convertigo.beans.transactions.AbstractHttpTransaction)

Example 9 with AbstractHttpTransaction

use of com.twinsoft.convertigo.beans.transactions.AbstractHttpTransaction in project convertigo by convertigo.

the class SwaggerUtils method createRestConnector.

@SuppressWarnings("unused")
private static HttpConnector createRestConnector(JSONObject json) throws Exception {
    try {
        HttpConnector httpConnector = new HttpConnector();
        httpConnector.bNew = true;
        JSONObject info = json.getJSONObject("info");
        httpConnector.setName(StringUtils.normalize(info.getString("title")));
        String host = json.getString("host");
        int index = host.indexOf(":");
        String server = index == -1 ? host : host.substring(0, index);
        int port = index == -1 ? 0 : Integer.parseInt(host.substring(index + 1, 10));
        httpConnector.setServer(server);
        httpConnector.setPort(port <= 0 ? 80 : port);
        String basePath = json.getString("basePath");
        httpConnector.setBaseDir(basePath);
        JSONArray _consumes = new JSONArray();
        if (json.has("consumes")) {
            _consumes = json.getJSONArray("consumes");
        }
        JSONArray _produces = new JSONArray();
        if (json.has("produces")) {
            _produces = json.getJSONArray("produces");
        }
        Map<String, JSONObject> models = new HashMap<String, JSONObject>();
        JSONObject definitions = new JSONObject();
        if (json.has("definitions")) {
            definitions = json.getJSONObject("definitions");
            for (Iterator<String> i = GenericUtils.cast(definitions.keys()); i.hasNext(); ) {
                String key = i.next();
                JSONObject model = definitions.getJSONObject(key);
                models.put(key, model);
            }
        }
        JSONObject paths = json.getJSONObject("paths");
        for (Iterator<String> i1 = GenericUtils.cast(paths.keys()); i1.hasNext(); ) {
            String subDir = i1.next();
            JSONObject path = paths.getJSONObject(subDir);
            for (Iterator<String> i2 = GenericUtils.cast(path.keys()); i2.hasNext(); ) {
                String httpVerb = i2.next();
                JSONObject verb = path.getJSONObject(httpVerb);
                XMLVector<XMLVector<String>> httpParameters = new XMLVector<XMLVector<String>>();
                AbstractHttpTransaction transaction = new HttpTransaction();
                JSONArray consumes = verb.has("consumes") ? verb.getJSONArray("consumes") : _consumes;
                List<String> consumeList = new ArrayList<String>();
                for (int i = 0; i < consumes.length(); i++) {
                    consumeList.add(consumes.getString(i));
                }
                String h_ContentType = null;
                if (consumeList.contains(MimeType.Xml.value())) {
                    h_ContentType = MimeType.Xml.value();
                } else if (consumeList.contains(MimeType.Json.value())) {
                    h_ContentType = MimeType.Json.value();
                } else {
                    h_ContentType = consumeList.size() > 0 ? consumeList.get(0) : MimeType.WwwForm.value();
                }
                JSONArray produces = verb.has("produces") ? verb.getJSONArray("produces") : _produces;
                List<String> produceList = new ArrayList<String>();
                for (int i = 0; i < produces.length(); i++) {
                    produceList.add(produces.getString(i));
                }
                String h_Accept = null;
                if (produceList.contains(h_ContentType)) {
                    h_Accept = h_ContentType;
                } else {
                    if (produceList.contains(MimeType.Xml.value())) {
                        h_Accept = MimeType.Xml.value();
                    } else if (produceList.contains(MimeType.Json.value())) {
                        h_Accept = MimeType.Json.value();
                    }
                }
                if (h_Accept != null) {
                    XMLVector<String> xmlv = new XMLVector<String>();
                    xmlv.add("Accept");
                    xmlv.add(h_Accept);
                    httpParameters.add(xmlv);
                    if (h_Accept.equals(MimeType.Xml.value())) {
                        transaction = new XmlHttpTransaction();
                        ((XmlHttpTransaction) transaction).setXmlEncoding("UTF-8");
                    } else if (h_Accept.equals(MimeType.Json.value())) {
                        transaction = new JsonHttpTransaction();
                        ((JsonHttpTransaction) transaction).setIncludeDataType(false);
                    }
                }
                if (h_ContentType != null) {
                    XMLVector<String> xmlv = new XMLVector<String>();
                    xmlv.add(HeaderName.ContentType.value());
                    xmlv.add(h_ContentType);
                    httpParameters.add(xmlv);
                }
                String operationId = "";
                if (verb.has("operationId")) {
                    operationId = verb.getString("operationId");
                }
                String summary = "";
                if (verb.has("summary")) {
                    summary = verb.getString("summary");
                }
                String description = "";
                if (verb.has("description")) {
                    description = verb.getString("description");
                }
                String name = StringUtils.normalize(operationId);
                if (name.isEmpty()) {
                    name = StringUtils.normalize(summary);
                    if (name.isEmpty()) {
                        name = "operation";
                    }
                }
                String comment = summary;
                if (comment.isEmpty()) {
                    comment = description;
                }
                JSONArray parameters = new JSONArray();
                if (verb.has("parameters")) {
                    parameters = verb.getJSONArray("parameters");
                    for (int i = 0; i < parameters.length(); i++) {
                        JSONObject parameter = (JSONObject) parameters.get(i);
                        String type = "string";
                        if (parameter.has("collectionFormat")) {
                            type = parameter.getString("type");
                        }
                        String collectionFormat = "csv";
                        if (parameter.has("collectionFormat")) {
                            collectionFormat = parameter.getString("collectionFormat");
                        }
                        boolean isMultiValued = type.equalsIgnoreCase("array") && collectionFormat.equals("multi");
                        RequestableHttpVariable httpVariable = isMultiValued ? new RequestableHttpMultiValuedVariable() : new RequestableHttpVariable();
                        httpVariable.bNew = true;
                        httpVariable.setName(parameter.getString("name"));
                        httpVariable.setHttpName(parameter.getString("name"));
                        String in = parameter.getString("in");
                        if (in.equals("query") || in.equals("path") || in.equals("header")) {
                            httpVariable.setHttpMethod(HttpMethodType.GET.name());
                            if (in.equals("header")) {
                                // overrides variable's name : will be treated as dynamic header
                                httpVariable.setName(com.twinsoft.convertigo.engine.enums.Parameter.HttpHeader.getName() + parameter.getString("name"));
                                // do not post on target server
                                httpVariable.setHttpName("");
                            }
                        } else if (in.equals("formData") || in.equals("body")) {
                            httpVariable.setHttpMethod(HttpMethodType.POST.name());
                            if (in.equals("body")) {
                                // overrides variable's name for internal use
                                httpVariable.setName(com.twinsoft.convertigo.engine.enums.Parameter.HttpBody.getName());
                                // add internal __contentType variable
                                RequestableHttpVariable ct = new RequestableHttpVariable();
                                ct.setName(com.twinsoft.convertigo.engine.enums.Parameter.HttpContentType.getName());
                                // do not post on target server
                                ct.setHttpName("");
                                ct.setHttpMethod(HttpMethodType.POST.name());
                                ct.setValueOrNull(null);
                                ct.bNew = true;
                                transaction.addVariable(ct);
                                // 
                                if (parameter.has("schema")) {
                                // String schema = parameter.getString("schema");
                                }
                            }
                        } else {
                            httpVariable.setHttpMethod("");
                        }
                        Object defaultValue = null;
                        if (parameter.has("default")) {
                            defaultValue = parameter.get("default");
                        }
                        if (defaultValue == null && type.equalsIgnoreCase("array")) {
                            JSONObject items = parameter.getJSONObject("items");
                            if (items.has("default")) {
                                defaultValue = items.get("default");
                            }
                        }
                        httpVariable.setValueOrNull(defaultValue);
                        if (parameter.has("description")) {
                            httpVariable.setDescription(parameter.getString("description"));
                        }
                        transaction.addVariable(httpVariable);
                    }
                }
                transaction.bNew = true;
                transaction.setName(name);
                transaction.setComment(comment);
                transaction.setSubDir(subDir);
                transaction.setHttpVerb(HttpMethodType.valueOf(httpVerb.toUpperCase()));
                transaction.setHttpParameters(httpParameters);
                transaction.setHttpInfo(true);
                httpConnector.add(transaction);
            }
        }
        return httpConnector;
    } catch (Throwable t) {
        System.out.println(t);
        throw new Exception("Invalid Swagger format", t);
    }
}
Also used : XmlHttpTransaction(com.twinsoft.convertigo.beans.transactions.XmlHttpTransaction) HttpTransaction(com.twinsoft.convertigo.beans.transactions.HttpTransaction) AbstractHttpTransaction(com.twinsoft.convertigo.beans.transactions.AbstractHttpTransaction) JsonHttpTransaction(com.twinsoft.convertigo.beans.transactions.JsonHttpTransaction) HttpConnector(com.twinsoft.convertigo.beans.connectors.HttpConnector) RequestableHttpVariable(com.twinsoft.convertigo.beans.variables.RequestableHttpVariable) XMLVector(com.twinsoft.convertigo.beans.common.XMLVector) HashMap(java.util.HashMap) XmlHttpTransaction(com.twinsoft.convertigo.beans.transactions.XmlHttpTransaction) JSONArray(org.codehaus.jettison.json.JSONArray) ArrayList(java.util.ArrayList) IOException(java.io.IOException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) AbstractHttpTransaction(com.twinsoft.convertigo.beans.transactions.AbstractHttpTransaction) JSONObject(org.codehaus.jettison.json.JSONObject) RequestableHttpMultiValuedVariable(com.twinsoft.convertigo.beans.variables.RequestableHttpMultiValuedVariable) JsonHttpTransaction(com.twinsoft.convertigo.beans.transactions.JsonHttpTransaction) JSONObject(org.codehaus.jettison.json.JSONObject)

Example 10 with AbstractHttpTransaction

use of com.twinsoft.convertigo.beans.transactions.AbstractHttpTransaction in project convertigo by convertigo.

the class ObjectInfoWizardPage method doApply.

public void doApply() {
    ObjectExplorerWizardPage objectExplorerWizardPage = (ObjectExplorerWizardPage) this.getPreviousPage();
    objectExplorerWizardPage.getControl().getDisplay().syncExec(() -> {
        Object o = objectExplorerWizardPage.getCreatedBean();
        if (o instanceof AbstractCouchDbTransaction) {
            AbstractCouchDbTransaction dbo = (AbstractCouchDbTransaction) objectExplorerWizardPage.getCreatedBean();
            if (dbo != null && couchVariablesComposite != null) {
                dbo.createVariables(couchVariablesComposite.getSelectedParameters());
            }
        } else if (o instanceof AbstractHttpTransaction) {
            AbstractHttpTransaction dbo = (AbstractHttpTransaction) objectExplorerWizardPage.getCreatedBean();
            if (dbo != null && httpTransactionVariablesComposite != null) {
                httpTransactionVariablesComposite.applyProceed();
            }
        }
    });
}
Also used : AbstractCouchDbTransaction(com.twinsoft.convertigo.beans.transactions.couchdb.AbstractCouchDbTransaction) DatabaseObject(com.twinsoft.convertigo.beans.core.DatabaseObject) AbstractHttpTransaction(com.twinsoft.convertigo.beans.transactions.AbstractHttpTransaction)

Aggregations

AbstractHttpTransaction (com.twinsoft.convertigo.beans.transactions.AbstractHttpTransaction)16 RequestableHttpVariable (com.twinsoft.convertigo.beans.variables.RequestableHttpVariable)9 EngineException (com.twinsoft.convertigo.engine.EngineException)7 IOException (java.io.IOException)7 DatabaseObject (com.twinsoft.convertigo.beans.core.DatabaseObject)5 RequestableVariable (com.twinsoft.convertigo.beans.variables.RequestableVariable)4 SocketTimeoutException (java.net.SocketTimeoutException)4 OAuthException (oauth.signpost.exception.OAuthException)4 JSONException (org.codehaus.jettison.json.JSONException)4 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)3 XMLVector (com.twinsoft.convertigo.beans.common.XMLVector)3 HttpConnector (com.twinsoft.convertigo.beans.connectors.HttpConnector)3 SqlTransaction (com.twinsoft.convertigo.beans.transactions.SqlTransaction)3 HttpMethodType (com.twinsoft.convertigo.engine.enums.HttpMethodType)3 File (java.io.File)3 FileInputStream (java.io.FileInputStream)3 ArrayList (java.util.ArrayList)3 PartInitException (org.eclipse.ui.PartInitException)3 Element (org.w3c.dom.Element)3 ScreenClass (com.twinsoft.convertigo.beans.core.ScreenClass)2