Search in sources :

Example 1 with StringEx

use of com.twinsoft.util.StringEx 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 2 with StringEx

use of com.twinsoft.util.StringEx in project convertigo by convertigo.

the class SourcePickerHelperWrap method modifyXpathText.

public void modifyXpathText(String xpath) {
    xpathEvaluator.setXpathText(xpath, true);
    String anchor = xpathEvaluator.getAnchor();
    StringEx sx = new StringEx(xpathEvaluator.getXpath());
    sx.replace(anchor, ".");
    String text = sx.toString();
    if (!text.equals("")) {
        setSourceXPath(text);
    }
}
Also used : StringEx(com.twinsoft.util.StringEx)

Example 3 with StringEx

use of com.twinsoft.util.StringEx in project convertigo by convertigo.

the class ProxyServletRequester method coreProcessRequest.

protected Object coreProcessRequest() throws Exception {
    // The proxy converts HTML data on the fly
    ParameterShuttle infoShuttle = threadParameterShuttle.get();
    HttpClient httpClient = threadHttpClient.get();
    HttpServletRequest request = (HttpServletRequest) inputData;
    InputStream siteIn = null;
    try {
        try {
            getUserRequest(infoShuttle, context, request);
            // Loading project
            if (context.projectName == null)
                throw new EngineException("The project name has not been specified!");
            Project currentProject;
            if (Engine.isStudioMode()) {
                currentProject = Engine.objectsProvider.getProject(context.projectName);
                if (currentProject == null) {
                    throw new EngineException("No project has been opened in the Studio. A project should be opened in the Studio in order that the Convertigo engine can work.");
                } else if (!currentProject.getName().equalsIgnoreCase(context.projectName)) {
                    throw new EngineException("The requested project (\"" + context.projectName + "\") does not match with the opened project (\"" + currentProject.getName() + "\") in the Studio.\nYou cannot make a request on a different project than the one opened in the Studio.");
                }
                Engine.logEngine.debug("Using project from Studio");
                context.project = currentProject;
            } else {
                if ((context.project == null) || (context.isNewSession)) {
                    Engine.logEngine.debug("New project requested: '" + context.projectName + "'");
                    context.project = Engine.theApp.databaseObjectsManager.getProjectByName(context.projectName);
                    Engine.logEngine.debug("Project loaded: " + context.project.getName());
                }
            }
            // Loading sequence
            if (context.sequenceName != null) {
                context.requestedObject = context.project.getSequenceByName(context.sequenceName);
                Engine.logEngine.debug("Loaded sequence: " + context.requestedObject.getName());
            }
            // Loading connector
            context.loadConnector();
            if (context.requestedObject != null)
                context.requestedObject.context = context;
            if (context.getConnector() != null)
                context.getConnector().context = context;
            if (Boolean.parseBoolean(EnginePropertiesManager.getProperty(PropertyName.SSL_DEBUG))) {
                System.setProperty("javax.net.debug", "all");
                Engine.logEngine.trace("(ProxyServletRequester) Enabling SSL debug mode");
            } else {
                System.setProperty("javax.net.debug", "");
                Engine.logEngine.debug("(ProxyServletRequester) Disabling SSL debug mode");
            }
            if (context.getConnector().isTasAuthenticationRequired() && (context.tasSessionKey == null)) {
                throw new EngineException("A Carioca authentication is required in order to process the transaction.");
            }
            infoShuttle.userID = context.tasUserName;
            infoShuttle.userIP = context.remoteAddr;
            // gather user id, parameters, headers from request
            gatherRequestInfo(infoShuttle, request);
            String t = context.statistics.start(EngineStatistics.APPLY_USER_REQUEST);
            try {
                // get connected
                httpClient.connect(infoShuttle);
            } finally {
                context.statistics.stop(t);
            }
            if (infoShuttle.siteInputStream == null) {
                Engine.logEngine.debug("(ProxyServletRequester) No input stream!");
                return null;
            }
            siteIn = infoShuttle.siteInputStream;
            Engine.logEngine.debug("(ProxyServletRequester) Start of document retransmission");
            Object result;
            ProxyHttpConnector proxyHttpConnector = (ProxyHttpConnector) infoShuttle.context.getConnector();
            String host = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort();
            String newBaseUrl = host + request.getRequestURI() + "?" + Parameter.Connector.getName() + "=" + proxyHttpConnector.getName() + "&" + Parameter.ProxyGoto.getName() + "=";
            String newBaseUrlThen = newBaseUrl + URLEncoder.encode(infoShuttle.siteURL.getPath(), "UTF-8") + "&" + Parameter.ProxyThen.getName() + "=";
            if (isDynamicContent(infoShuttle.siteURL.getPath(), proxyHttpConnector.getDynamicContentFiles())) {
                Engine.logEngine.debug("(ProxyServletRequester) Dynamic content");
                String sResponse;
                StringBuffer sbResponse = new StringBuffer("");
                t = context.statistics.start(EngineStatistics.APPLY_USER_REQUEST);
                try {
                    int c = siteIn.read();
                    char cc;
                    while (c > -1) {
                        cc = (char) c;
                        sbResponse.append(cc);
                        c = siteIn.read();
                    }
                } finally {
                    context.statistics.stop(t, true);
                }
                sResponse = sbResponse.toString();
                result = sResponse;
                if (infoShuttle.siteContentType == null) {
                    Engine.logEngine.warn("(ProxyServletRequester) Aborting string replacements because of null mimetype! Resource: " + infoShuttle.siteURL);
                } else {
                    Engine.logEngine.debug("(ProxyServletRequester) String replacements");
                    // Text/html replacements
                    Engine.logEngine.debug("(ProxyServletRequester) Replacements for mime type '" + infoShuttle.siteContentType + "'");
                    Replacements replacements = proxyHttpConnector.getReplacementsForMimeType(infoShuttle.siteContentType);
                    if (!replacements.isEmpty()) {
                        StringEx sxResponse = new StringEx(sResponse);
                        StringEx sx;
                        String strSearched, strReplacing;
                        for (int i = 0; i < replacements.strReplacing.length; i++) {
                            strSearched = replacements.strSearched[i];
                            sx = new StringEx(strSearched);
                            sx.replaceAll("{tab}", "\t");
                            sx.replaceAll("{apos0x92}", "" + (char) 146);
                            sx.replaceAll("{newBaseUrl}", newBaseUrl);
                            sx.replaceAll("{newBaseUrlThen}", newBaseUrlThen);
                            strSearched = sx.toString();
                            replacements.strSearched[i] = strSearched;
                            Engine.logEngine.debug("(ProxyServletRequester) Replacing: " + strSearched);
                            strReplacing = replacements.strReplacing[i];
                            sx = new StringEx(strReplacing);
                            sx.replaceAll("{newBaseUrl}", newBaseUrl);
                            sx.replaceAll("{newBaseUrlThen}", newBaseUrlThen);
                            strReplacing = sx.toString();
                            replacements.strReplacing[i] = strReplacing;
                            Engine.logEngine.debug("(ProxyServletRequester) By: " + strReplacing);
                        }
                        Engine.logEngine.debug("(ProxyServletRequester) Replacements in progress");
                        sxResponse.replaceAll(replacements.strSearched, replacements.strReplacing);
                        Engine.logEngine.debug("(ProxyServletRequester) Replacements done!");
                        if (Engine.isStudioMode()) {
                            sxResponse.replaceAll("?" + Parameter.Connector.getName() + "=", "?" + Parameter.Context.getName() + "=" + infoShuttle.context.name + "&" + Parameter.Connector.getName() + "=");
                        }
                        result = sxResponse.toString();
                    }
                }
                infoShuttle.siteContentSize = ((String) result).length();
                Engine.logEngine.debug("(ProxyServletRequester) HTML data retrieved!");
                String billingClassName = context.getConnector().getBillingClassName();
                if (billingClassName != null) {
                    try {
                        Engine.logContext.debug("Billing class name required: " + billingClassName);
                        AbstractBiller biller = (AbstractBiller) Class.forName(billingClassName).getConstructor().newInstance();
                        Engine.logContext.debug("Executing the biller");
                        biller.insertBilling(context);
                    } catch (Throwable e) {
                        Engine.logContext.warn("Unable to execute the biller (the billing is thus ignored): [" + e.getClass().getName() + "] " + e.getMessage());
                    }
                }
            } else {
                Engine.logEngine.debug("(ProxyServletRequester) Static content: " + infoShuttle.siteContentType);
                ByteArrayOutputStream baos = new ByteArrayOutputStream(2048);
                t = context.statistics.start(EngineStatistics.APPLY_USER_REQUEST);
                try {
                    // Read either from the cache, either from the remote server
                    int c = siteIn.read();
                    while (c > -1) {
                        baos.write(c);
                        c = siteIn.read();
                    }
                } finally {
                    context.statistics.stop(t, true);
                }
                result = baos.toByteArray();
                Engine.logEngine.debug("(ProxyServletRequester) Static data retrieved!");
                // Determine if the resource has already been cached or not
                String resourceUrl = infoShuttle.siteURL.toString();
                CacheEntry cacheEntry = ProxyServletRequester.proxyCacheManager.getCacheEntry(resourceUrl);
                if (cacheEntry == null) {
                    // Managing text replacements
                    Engine.logEngine.debug("(ProxyServletRequester) Replacements for mime type '" + infoShuttle.siteContentType + "'");
                    Replacements replacements = proxyHttpConnector.getReplacementsForMimeType(infoShuttle.siteContentType);
                    if (!replacements.isEmpty()) {
                        String sResult = new String((byte[]) result);
                        StringEx sxResponse = new StringEx(sResult);
                        StringEx sx;
                        String strSearched, strReplacing;
                        for (int i = 0; i < replacements.strReplacing.length; i++) {
                            strSearched = replacements.strSearched[i];
                            sx = new StringEx(strSearched);
                            sx.replaceAll("{tab}", "\t");
                            sx.replaceAll("{newBaseUrl}", newBaseUrl);
                            sx.replaceAll("{newBaseUrlThen}", newBaseUrlThen);
                            strSearched = sx.toString();
                            replacements.strSearched[i] = strSearched;
                            Engine.logEngine.debug("(ProxyServletRequester) Replacing: " + strSearched);
                            strReplacing = replacements.strReplacing[i];
                            sx = new StringEx(strReplacing);
                            sx.replaceAll("{newBaseUrl}", newBaseUrl);
                            sx.replaceAll("{newBaseUrlThen}", newBaseUrlThen);
                            strReplacing = sx.toString();
                            replacements.strReplacing[i] = strReplacing;
                            Engine.logEngine.debug("(ProxyServletRequester) By: " + strReplacing);
                        }
                        Engine.logEngine.debug("(ProxyServletRequester) Replacements in progress");
                        sxResponse.replaceAll(replacements.strSearched, replacements.strReplacing);
                        Engine.logEngine.debug("(ProxyServletRequester) Replacements done!");
                        result = sxResponse.toString().getBytes();
                    }
                    if (infoShuttle.httpCode == 200) {
                        Engine.logEngine.debug("(ProxyServletRequester) Resource stored: " + resourceUrl);
                        cacheEntry = proxyCacheManager.storeResponse(resourceUrl, (byte[]) result);
                        cacheEntry.contentLength = ((byte[]) result).length;
                        infoShuttle.siteContentSize = cacheEntry.contentLength;
                        cacheEntry.contentType = infoShuttle.siteContentType;
                    }
                } else {
                    infoShuttle.httpCode = 200;
                    infoShuttle.siteContentSize = cacheEntry.contentLength;
                    infoShuttle.siteContentType = cacheEntry.contentType;
                }
                baos.close();
            }
            Engine.logEngine.debug("(ProxyServletRequester) End of document retransmission");
            return result;
        } finally {
            if (siteIn != null) {
                try {
                    siteIn.close();
                } catch (Exception e) {
                }
            }
        }
    } finally {
        context.contentType = infoShuttle.siteContentType;
        httpClient.disconnect();
    }
}
Also used : Replacements(com.twinsoft.convertigo.beans.connectors.ProxyHttpConnector.Replacements) InputStream(java.io.InputStream) ProxyHttpConnector(com.twinsoft.convertigo.beans.connectors.ProxyHttpConnector) EngineException(com.twinsoft.convertigo.engine.EngineException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) CacheEntry(com.twinsoft.convertigo.engine.proxy.cache.CacheEntry) IOException(java.io.IOException) EngineException(com.twinsoft.convertigo.engine.EngineException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) HttpServletRequest(javax.servlet.http.HttpServletRequest) Project(com.twinsoft.convertigo.beans.core.Project) StringEx(com.twinsoft.util.StringEx) AbstractBiller(com.twinsoft.convertigo.engine.AbstractBiller)

Example 4 with StringEx

use of com.twinsoft.util.StringEx in project convertigo by convertigo.

the class SourcePickerHelper method createXPathEvaluator.

public void createXPathEvaluator(StepXpathEvaluatorComposite xpathEvaluatorComposite) {
    xpathEvaluator = xpathEvaluatorComposite;
    GridData gd = new GridData();
    gd.horizontalAlignment = GridData.FILL;
    gd.verticalAlignment = GridData.FILL;
    gd.grabExcessVerticalSpace = true;
    gd.grabExcessHorizontalSpace = true;
    xpathEvaluator.setLayoutData(gd);
    xpathEvaluator.getXpath().addModifyListener(new ModifyListener() {

        public void modifyText(ModifyEvent e) {
            String anchor = xpathEvaluator.getAnchor();
            StringEx sx = new StringEx(xpathEvaluator.getXpath().getText());
            sx.replace(anchor, ".");
            String text = sx.toString();
            if (!text.equals("")) {
                setSourceXPath(text);
            }
        // TODO: disable/enable OK button
        }
    });
}
Also used : ModifyEvent(org.eclipse.swt.events.ModifyEvent) ModifyListener(org.eclipse.swt.events.ModifyListener) GridData(org.eclipse.swt.layout.GridData) StringEx(com.twinsoft.util.StringEx)

Example 5 with StringEx

use of com.twinsoft.util.StringEx in project convertigo by convertigo.

the class Migration5_0_0 method makeCdataReplacements.

// private static void makeScReplacements(Element element) {
// String elementClassName = element.getAttribute("classname");
// NodeList children = element.getChildNodes();
// Element propertyName = MigrationUtils.findChildElementByAttributeValue(children, "property", "name");
// Element nameElement = (Element) XMLUtils.findChildNode(propertyName, Node.ELEMENT_NODE);
// String name = null;
// try {
// name = (String) XMLUtils.readObjectFromXml(nameElement);
// StringEx sx = new StringEx(name);
// 
// boolean bChanged = false;
// Enumeration<String> keys = nameReplacements.keys();
// String key, value;
// while (keys.hasMoreElements()) {
// key = keys.nextElement();
// value = nameReplacements.get(key);
// if (name.indexOf(key) != -1) {
// sx.replaceAll(key, value);
// bChanged = true;
// }
// }
// if (bChanged) {
// String newName = sx.toString();
// nameElement.setAttribute("value", newName);
// Engine.logDatabaseObjectManager.info("[Migration 4.6.0] ScHandlerStatement \""+name+"\" has been replaced by \""+newName+"\"");
// }
// }
// catch (Exception e) {
// Engine.logDatabaseObjectManager.warn("[Migration 4.6.0] Could not change name \""+name+"\" ("+elementClassName+")! Will remain unchanged");
// }
// }
private static void makeCdataReplacements(Element element) throws Exception {
    try {
        Node cdata = XMLUtils.findChildNode(element, Node.CDATA_SECTION_NODE);
        if (cdata != null) {
            String s = cdata.getNodeValue();
            if (!s.equals("")) {
                StringEx sx = new StringEx(s);
                Enumeration<String> keys = nameReplacements.keys();
                String key, value;
                while (keys.hasMoreElements()) {
                    key = keys.nextElement();
                    value = nameReplacements.get(key);
                    sx.replaceAll(key, value);
                }
                s = sx.toString();
                cdata.setNodeValue(s);
            }
        }
    } catch (Exception e) {
        throw new Exception("Unable to make replacements", e);
    }
}
Also used : Node(org.w3c.dom.Node) StringEx(com.twinsoft.util.StringEx) EngineException(com.twinsoft.convertigo.engine.EngineException)

Aggregations

StringEx (com.twinsoft.util.StringEx)30 EngineException (com.twinsoft.convertigo.engine.EngineException)14 IOException (java.io.IOException)13 Statement (java.sql.Statement)10 SQLException (java.sql.SQLException)9 File (java.io.File)7 PreparedStatement (java.sql.PreparedStatement)7 Document (org.w3c.dom.Document)5 ResultSet (java.sql.ResultSet)4 HttpConnector (com.twinsoft.convertigo.beans.connectors.HttpConnector)3 Element (org.w3c.dom.Element)3 AbstractHttpTransaction (com.twinsoft.convertigo.beans.transactions.AbstractHttpTransaction)2 RequestableHttpVariable (com.twinsoft.convertigo.beans.variables.RequestableHttpVariable)2 RequestableVariable (com.twinsoft.convertigo.beans.variables.RequestableVariable)2 StringReader (java.io.StringReader)2 StringWriter (java.io.StringWriter)2 MalformedURLException (java.net.MalformedURLException)2 SimpleDateFormat (java.text.SimpleDateFormat)2 ArrayList (java.util.ArrayList)2 DocumentBuilder (javax.xml.parsers.DocumentBuilder)2