Search in sources :

Example 1 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 2 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 {
                            ConvertigoPlugin.getDefault().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 3 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());
                    ConvertigoPlugin.getDefault().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);
                                            ConvertigoPlugin.getDefault().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) {
                        ConvertigoPlugin.getDefault().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 4 with AbstractHttpTransaction

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

the class TransactionTreeObject method handlesBeanNameChanged.

protected void handlesBeanNameChanged(TreeObjectEvent treeObjectEvent) {
    DatabaseObjectTreeObject treeObject = (DatabaseObjectTreeObject) treeObjectEvent.getSource();
    DatabaseObject databaseObject = (DatabaseObject) treeObject.getObject();
    Object oldValue = treeObjectEvent.oldValue;
    Object newValue = treeObjectEvent.newValue;
    if (databaseObject instanceof ScreenClass) {
        String oldName = StringUtils.normalize((String) oldValue);
        String newName = StringUtils.normalize((String) newValue);
        Transaction transaction = getObject();
        // Modify Screenclass name in Transaction handlers
        if (!(transaction instanceof HtmlTransaction)) {
            // ScreenClass and Transaction must have the same connector!
            if (transaction.getConnector().equals(databaseObject.getConnector())) {
                String oldHandlerPrefix = "on" + StringUtils.normalize(oldName);
                String newHandlerPrefix = "on" + StringUtils.normalize(newName);
                if (transaction.handlers.indexOf(oldHandlerPrefix) != -1) {
                    StringEx sx = new StringEx(transaction.handlers);
                    // Updating comments
                    sx.replaceAll("handler for screen class \"" + oldName + "\"", "handler for screen class \"" + newName + "\"");
                    // Updating functions def & calls
                    sx.replaceAll(oldHandlerPrefix + "Entry", newHandlerPrefix + "Entry");
                    sx.replaceAll(oldHandlerPrefix + "Exit", newHandlerPrefix + "Exit");
                    String newHandlers = sx.toString();
                    if (!newHandlers.equals(transaction.handlers)) {
                        transaction.handlers = newHandlers;
                        hasBeenModified(true);
                    }
                    // Update the opened handlers editor if any
                    JScriptEditorInput jsinput = ConvertigoPlugin.getDefault().getJScriptEditorInput(transaction);
                    if (jsinput != null) {
                        jsinput.reload();
                    }
                    try {
                        ConvertigoPlugin.getDefault().getProjectExplorerView().reloadTreeObject(this);
                    } catch (Exception e) {
                        ConvertigoPlugin.logWarning(e, "Could not reload in tree Transaction \"" + databaseObject.getName() + "\" !");
                    }
                }
            }
        }
    }
    if (databaseObject instanceof Variable) {
        String oldVariableName = oldValue.toString();
        String newVariableName = newValue.toString();
        // A variable of this transaction has been renamed
        if (getObject().equals(databaseObject.getParent())) {
            if (getObject() instanceof AbstractHttpTransaction) {
                AbstractHttpTransaction httpTransaction = (AbstractHttpTransaction) getObject();
                try {
                    // Check for variables to be renamed in SubDir property
                    String transactionSubDir = httpTransaction.getSubDir();
                    List<String> pathVariableList = AbstractHttpTransaction.getPathVariableList(transactionSubDir);
                    if (pathVariableList.contains(oldVariableName)) {
                        transactionSubDir = transactionSubDir.replaceAll("\\{" + oldVariableName + "\\}", "{" + newVariableName + "}");
                        httpTransaction.setSubDir(transactionSubDir);
                        httpTransaction.hasChanged = true;
                    }
                    ConvertigoPlugin.getDefault().getProjectExplorerView().refreshTreeObject(this);
                } catch (Exception e) {
                    ConvertigoPlugin.logWarning(e, "Could not reload in tree Transaction \"" + databaseObject.getName() + "\" !");
                }
            }
        }
    }
    // Case of this transaction rename : update transaction's schema
    if (treeObject.equals(this)) {
        String path = Project.XSD_FOLDER_NAME + "/" + Project.XSD_INTERNAL_FOLDER_NAME + "/" + getConnectorTreeObject().getName();
        String oldPath = path + "/" + (String) oldValue + ".xsd";
        String newPath = path + "/" + (String) newValue + ".xsd";
        IFile file = getProjectTreeObject().getFile(oldPath);
        try {
            file.getParent().refreshLocal(IResource.DEPTH_ONE, null);
            if (file.exists()) {
                // rename file (xsd/internal/connector/transaction.xsd)
                file.move(new Path((String) newValue + ".xsd"), true, null);
                // make replacements in schema files
                List<Replacement> replacements = new ArrayList<Replacement>();
                replacements.add(new Replacement("__" + (String) oldValue, "__" + (String) newValue));
                IFile newFile = file.getParent().getFile(new Path((String) newValue + ".xsd"));
                String newFilePath = newFile.getLocation().makeAbsolute().toString();
                try {
                    ProjectUtils.makeReplacementsInFile(replacements, newFilePath);
                } catch (Exception e) {
                    ConvertigoPlugin.logWarning(e, "Could not rename \"" + oldValue + "\" to \"" + newValue + "\" in schema file \"" + newPath + "\" !");
                }
                // refresh file
                file.refreshLocal(IResource.DEPTH_ZERO, null);
                Engine.theApp.schemaManager.clearCache(getProjectTreeObject().getName());
            }
        } catch (Exception e) {
            ConvertigoPlugin.logWarning(e, "Could not rename schema file from \"" + oldPath + "\" to \"" + newPath + "\" !");
        }
    }
}
Also used : JScriptEditorInput(com.twinsoft.convertigo.eclipse.editors.jscript.JScriptEditorInput) Path(org.eclipse.core.runtime.Path) RequestableHttpVariable(com.twinsoft.convertigo.beans.variables.RequestableHttpVariable) Variable(com.twinsoft.convertigo.beans.core.Variable) RequestableVariable(com.twinsoft.convertigo.beans.variables.RequestableVariable) IFile(org.eclipse.core.resources.IFile) ScreenClass(com.twinsoft.convertigo.beans.core.ScreenClass) HtmlTransaction(com.twinsoft.convertigo.beans.transactions.HtmlTransaction) ArrayList(java.util.ArrayList) Replacement(com.twinsoft.convertigo.engine.util.Replacement) PartInitException(org.eclipse.ui.PartInitException) EngineException(com.twinsoft.convertigo.engine.EngineException) AbstractHttpTransaction(com.twinsoft.convertigo.beans.transactions.AbstractHttpTransaction) AbstractHttpTransaction(com.twinsoft.convertigo.beans.transactions.AbstractHttpTransaction) HtmlTransaction(com.twinsoft.convertigo.beans.transactions.HtmlTransaction) SqlTransaction(com.twinsoft.convertigo.beans.transactions.SqlTransaction) Transaction(com.twinsoft.convertigo.beans.core.Transaction) DatabaseObject(com.twinsoft.convertigo.beans.core.DatabaseObject) DatabaseObject(com.twinsoft.convertigo.beans.core.DatabaseObject) StringEx(com.twinsoft.util.StringEx)

Example 5 with AbstractHttpTransaction

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

the class WebServiceTranslator method addSoapElement.

private SOAPElement addSoapElement(Context context, SOAPEnvelope se, SOAPElement soapParent, Node node) throws Exception {
    String prefix = node.getPrefix();
    String namespace = node.getNamespaceURI();
    String nodeName = node.getNodeName();
    String localName = node.getLocalName();
    String value = node.getNodeValue();
    boolean includeResponseElement = true;
    if (context.sequenceName != null) {
        includeResponseElement = ((Sequence) context.requestedObject).isIncludeResponseElement();
    }
    SOAPElement soapElement = null;
    if (node.getNodeType() == Node.ELEMENT_NODE) {
        Element element = (Element) node;
        boolean toAdd = true;
        if (!includeResponseElement && "response".equalsIgnoreCase(localName)) {
            toAdd = false;
        }
        if ("http://schemas.xmlsoap.org/soap/envelope/".equals(element.getParentNode().getNamespaceURI()) || "http://schemas.xmlsoap.org/soap/envelope/".equals(namespace) || nodeName.toLowerCase().endsWith(":envelope") || nodeName.toLowerCase().endsWith(":body")) // element.getParentNode().getNodeName().toUpperCase().indexOf("NS0:") != -1 ||
        // nodeName.toUpperCase().indexOf("NS0:") != -1
        {
            toAdd = false;
        }
        if (toAdd) {
            if (prefix == null || prefix.equals("")) {
                soapElement = soapParent.addChildElement(nodeName);
            } else {
                soapElement = soapParent.addChildElement(se.createName(localName, prefix, namespace));
            }
        } else {
            soapElement = soapParent;
        }
        if (soapElement != null) {
            if (soapParent.equals(se.getBody()) && !soapParent.equals(soapElement)) {
                if (XsdForm.qualified == context.project.getSchemaElementForm()) {
                    if (soapElement.getAttribute("xmlns") == null) {
                        soapElement.addAttribute(se.createName("xmlns"), context.project.getTargetNamespace());
                    }
                }
            }
            if (element.hasAttributes()) {
                String attrType = element.getAttribute("type");
                if (("attachment").equals(attrType)) {
                    if (context.requestedObject instanceof AbstractHttpTransaction) {
                        AttachmentDetails attachment = AttachmentManager.getAttachment(element);
                        if (attachment != null) {
                            byte[] raw = attachment.getData();
                            if (raw != null)
                                soapElement.addTextNode(Base64.encodeBase64String(raw));
                        }
                    /* DON'T WORK YET *\
	    					AttachmentPart ap = responseMessage.createAttachmentPart(new ByteArrayInputStream(raw), element.getAttribute("content-type"));
	    					ap.setContentId(key);
	    					ap.setContentLocation(element.getAttribute("url"));
	    					responseMessage.addAttachmentPart(ap);
	    					\* DON'T WORK YET */
                    }
                }
                if (!includeResponseElement && "response".equalsIgnoreCase(localName)) {
                // do not add attributes
                } else {
                    NamedNodeMap attributes = element.getAttributes();
                    int len = attributes.getLength();
                    for (int i = 0; i < len; i++) {
                        Node item = attributes.item(i);
                        addSoapElement(context, se, soapElement, item);
                    }
                }
            }
            if (element.hasChildNodes()) {
                NodeList childNodes = element.getChildNodes();
                int len = childNodes.getLength();
                for (int i = 0; i < len; i++) {
                    Node item = childNodes.item(i);
                    switch(item.getNodeType()) {
                        case Node.ELEMENT_NODE:
                            addSoapElement(context, se, soapElement, item);
                            break;
                        case Node.CDATA_SECTION_NODE:
                        case Node.TEXT_NODE:
                            String text = item.getNodeValue();
                            text = (text == null) ? "" : text;
                            soapElement.addTextNode(text);
                            break;
                        default:
                            break;
                    }
                }
            }
        }
    } else if (node.getNodeType() == Node.ATTRIBUTE_NODE) {
        if (prefix == null || prefix.equals("")) {
            soapElement = soapParent.addAttribute(se.createName(nodeName), value);
        } else {
            soapElement = soapParent.addAttribute(se.createName(localName, prefix, namespace), value);
        }
    }
    return soapElement;
}
Also used : NamedNodeMap(org.w3c.dom.NamedNodeMap) XmlSchemaElement(org.apache.ws.commons.schema.XmlSchemaElement) SOAPElement(javax.xml.soap.SOAPElement) Element(org.w3c.dom.Element) Node(org.w3c.dom.Node) NodeList(org.w3c.dom.NodeList) SOAPElement(javax.xml.soap.SOAPElement) AttachmentDetails(com.twinsoft.convertigo.engine.AttachmentManager.AttachmentDetails) AbstractHttpTransaction(com.twinsoft.convertigo.beans.transactions.AbstractHttpTransaction)

Aggregations

AbstractHttpTransaction (com.twinsoft.convertigo.beans.transactions.AbstractHttpTransaction)14 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)4 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