Search in sources :

Example 11 with StringEx

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

the class Biller method insertVicBilling.

public void insertVicBilling(Context context, Object data) {
    String sSqlRequest = null;
    try {
        Engine.logBillers.debug("[Biller] Trying to insert the billing into a VIC database ");
        sqlRequester.checkConnection();
        CertificateManager certificateManager = ((HttpConnector) context.getConnector()).certificateManager;
        if (!certificateManager.storeInformationCollected) {
            certificateManager.collectStoreInformation(context);
        }
        int cache = 0;
        double cost = getCost(context, data);
        if (cost == -1) {
            Engine.logBillers.debug("[Biller] Billing aborted because the returned cost is -1, i.e. do not need to bill.");
            return;
        } else if (cost == -2) {
            Engine.logBillers.debug("[Biller] Billing zero cost because the response was in cache.");
            cost = 0;
            cache = 1;
        }
        TransactionWithVariables transaction = null;
        try {
            transaction = (TransactionWithVariables) context.requestedObject;
        } catch (ClassCastException e) {
            throw new Exception("Requested object is not a transaction");
        }
        StringEx sqlRequest = new StringEx(sqlRequester.getProperty(Biller.PROPERTIES_SQL_REQUEST_INSERT_BILLING));
        sqlRequest.replace("{UserName}", context.tasUserName);
        sqlRequest.replace("{UserGroup}", context.tasUserGroup);
        sqlRequest.replace("{Service}", context.tasServiceCode);
        sqlRequest.replace("{CVX25}", "0");
        String cdbanque = transaction.getVariableValue("cdbanque").toString();
        if (cdbanque == null)
            cdbanque = context.get("cdbanque").toString();
        String cdguichet = transaction.getVariableValue("cdguichet").toString();
        if (cdguichet == null)
            cdguichet = context.get("cdguichet").toString();
        sqlRequest.replace("{CodeBanque}", cdbanque);
        sqlRequest.replace("{CodeGuichet}", cdguichet);
        String certificate = new File(certificateManager.keyStore).getName();
        int idx = certificate.indexOf('.');
        if (idx != -1) {
            certificate = certificate.substring(0, idx);
        }
        sqlRequest.replace("{Certificat}", certificate);
        sqlRequest.replace("{Cache}", Integer.toString(cache));
        Engine.logBillers.debug("[Biller] Replacement done");
        Calendar rightNow = Calendar.getInstance();
        SimpleDateFormat df = new SimpleDateFormat(sqlRequester.getProperty(Biller.PROPERTIES_SQL_DATE_FORMAT));
        String date = df.format(rightNow.getTime());
        sqlRequest.replace("{StartHour}", date);
        sqlRequest.replace("{EndHour}", date);
        Engine.logBillers.debug("[Biller] Start and End hour done");
        sqlRequest.replace("{NomSv}", context.tasVirtualServerName);
        Engine.logBillers.debug("[Biller] NomSv done");
        sqlRequest.replace("{Cost}", Double.toString(cost));
        Engine.logBillers.debug("[Biller] Cost Done");
        sSqlRequest = sqlRequest.toString();
        Engine.logBillers.debug("[Biller] SQL: " + sSqlRequest);
        Statement statement = null;
        long startBilling = System.currentTimeMillis();
        try {
            statement = sqlRequester.connection.createStatement();
            int nResult = statement.executeUpdate(sSqlRequest);
            Engine.logBillers.debug("[Biller] " + nResult + " row(s) inserted.");
        } finally {
            if (statement != null) {
                statement.close();
            }
            Engine.logBillers.info("[Biller] insertVicBilling, 1 request in " + (System.currentTimeMillis() - startBilling) + " ms");
        }
    } catch (SQLException e) {
        Engine.logBillers.warn("[Biller] Unable to insert the billing.\n" + e.getMessage() + " (error code: " + e.getErrorCode() + ")\nSQL: " + sSqlRequest);
    } catch (Exception e) {
        Engine.logBillers.error("[Biller] Unable to insert the billing", e);
    }
}
Also used : HttpConnector(com.twinsoft.convertigo.beans.connectors.HttpConnector) SQLException(java.sql.SQLException) Statement(java.sql.Statement) Calendar(java.util.Calendar) IOException(java.io.IOException) SQLException(java.sql.SQLException) TransactionWithVariables(com.twinsoft.convertigo.beans.core.TransactionWithVariables) StringEx(com.twinsoft.util.StringEx) File(java.io.File) SimpleDateFormat(java.text.SimpleDateFormat)

Example 12 with StringEx

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

the class ReplaceText method execute.

/**
 * Applies the extraction rule to the current iJavelin object.
 *
 * @param javelin the Javelin object.
 * @param block the current block to analyze.
 * @param blockFactory the block context of the current block.
 * @param dom the XML DOM.
 *
 * @return an ExtractionRuleResult object containing the result of
 * the query.
 */
public JavelinExtractionRuleResult execute(iJavelin javelin, Block block, BlockFactory blockFactory, org.w3c.dom.Document dom) {
    JavelinExtractionRuleResult xrs = new JavelinExtractionRuleResult();
    // save the original len of the block
    int len = block.length;
    if (!regExp) {
        StringEx strEx;
        strEx = new StringEx(block.getText());
        strEx.replaceAll(searchedText, replacedText);
        block.setText(strEx.toString());
    } else {
        block.setText(block.getText().replaceAll(searchedText, replacedText));
    }
    // restore the len
    block.length = len;
    xrs.hasMatched = true;
    xrs.newCurrentBlock = block;
    return xrs;
}
Also used : JavelinExtractionRuleResult(com.twinsoft.convertigo.beans.extractionrules.JavelinExtractionRuleResult) StringEx(com.twinsoft.util.StringEx)

Example 13 with StringEx

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

the class Migration3_0_0 method renameFileWithDollarSign.

private static void renameFileWithDollarSign(File file) {
    StringEx sx = new StringEx(file.getName());
    sx.replaceAll("$", "-");
    String newName = file.getParentFile().getAbsolutePath() + "/" + sx.toString();
    if (!file.renameTo(new File(newName))) {
        Engine.logDatabaseObjectManager.warn("Unable to rename '" + file.getAbsolutePath() + "' to '" + newName + "'");
    }
}
Also used : StringEx(com.twinsoft.util.StringEx) File(java.io.File)

Example 14 with StringEx

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

the class HttpBridge method convertIncomingRequest.

public static void convertIncomingRequest(ParameterShuttle infoShuttle) {
    HttpConnector connector = (HttpConnector) infoShuttle.context.getConnector();
    connector.setBaseUrl();
    // compose target siteURL by Http Bridge parameters
    String bridgeGoto = infoShuttle.userGoto;
    // BUGFIX: if the url contains blank spaces, replace them by "%20"
    if (bridgeGoto != null) {
        StringEx sx = new StringEx(bridgeGoto);
        sx.replaceAll(" ", "%20");
        bridgeGoto = sx.toString();
    }
    String bridgeThen = infoShuttle.userThen;
    // BUGFIX: if the url contains blank spaces, replace them by "%20"
    if (bridgeThen != null) {
        StringEx sx = new StringEx(bridgeThen);
        sx.replaceAll(" ", "%20");
        bridgeThen = sx.toString();
    }
    if (bridgeGoto == null) {
        bridgeGoto = connector.sUrl;
    } else if (!bridgeGoto.startsWith(connector.sUrl)) {
        bridgeGoto = connector.sUrl + bridgeGoto;
    }
    Engine.logEngine.debug("(Proxy) goto: " + bridgeGoto);
    Engine.logEngine.debug("(Proxy) then: " + bridgeThen);
    if (bridgeGoto == null)
        bridgeGoto = infoShuttle.prevSiteURL.toString();
    URI gotoURI = null, thenURI = null;
    if (bridgeThen != null) {
        try {
            thenURI = new URI(bridgeThen);
        } catch (URISyntaxException e) {
            throw new IllegalArgumentException("2nd target address in wrong syntax: " + bridgeThen);
        }
        if (thenURI.isAbsolute()) {
            gotoURI = thenURI;
            thenURI = null;
            bridgeGoto = bridgeThen;
            bridgeThen = null;
        }
    }
    if (gotoURI == null) {
        try {
            if (bridgeGoto.charAt(0) == '/' && infoShuttle.siteURL != null) {
                if (bridgeGoto.startsWith("//"))
                    gotoURI = new URI(infoShuttle.siteURL.getProtocol() + ":" + bridgeGoto);
                else
                    gotoURI = new URI(infoShuttle.siteURL.getProtocol() + "://" + infoShuttle.siteURL.getHost() + (infoShuttle.siteURL.getPort() == -1 ? "" : ":" + infoShuttle.siteURL.getPort()) + bridgeGoto);
            } else if (bridgeGoto.indexOf("://") < 0)
                gotoURI = new URI("http://" + bridgeGoto);
            else
                gotoURI = new URI(bridgeGoto);
        } catch (URISyntaxException e) {
            throw new IllegalArgumentException("main target address in wrong syntax: " + bridgeGoto);
        }
    }
    if (gotoURI.getScheme() == null || !gotoURI.getScheme().startsWith("http"))
        throw new IllegalArgumentException("missing or unsupported protocol in address: " + bridgeGoto);
    if (gotoURI.getHost() == null)
        throw new IllegalArgumentException("missing server in address: " + bridgeGoto);
    if (thenURI != null)
        gotoURI = gotoURI.resolve(thenURI);
    if (infoShuttle.postFromUser && !infoShuttle.postToSite) {
        StringBuffer sb = new StringBuffer(128 + infoShuttle.userContentLength);
        sb.append(gotoURI.getScheme()).append("://").append(gotoURI.getHost());
        if (gotoURI.getPort() != -1)
            sb.append(":").append(gotoURI.getPort());
        sb.append(gotoURI.getPath()).append("?");
        try {
            sb.append(infoShuttle.userPostData);
            infoShuttle.siteURL = new URL(sb.toString());
        } catch (MalformedURLException e) {
            throw new IllegalArgumentException("Cannot compose an URL (A): " + sb.toString());
        }
    } else {
        try {
            infoShuttle.siteURL = gotoURI.toURL();
        } catch (MalformedURLException e) {
            throw new IllegalArgumentException("Cannot compose an URL (B): " + gotoURI.toString());
        }
    }
    // filter http headers
    int index = 0;
    while (index < infoShuttle.userHeaderNames.size()) {
        String name = (String) infoShuttle.userHeaderNames.get(index);
        int pos = findString(name, ALL_HEADERS, AllowedToRequest, true);
        if (pos == -1 || (pos == HH_CONTENT_TYPE && !infoShuttle.postToSite)) {
            infoShuttle.userHeaderNames.remove(index);
            infoShuttle.userHeaderValues.remove(index);
            continue;
        }
        index++;
    }
    // always set Accept-Encoding to empty
    infoShuttle.userHeaderNames.add(ALL_HEADERS[HH_ACCEPT_ENCODING]);
    infoShuttle.userHeaderValues.add("");
    // compose cookie for target site
    String serverCookieStr = cookieMgr.getServerCookieString(infoShuttle.sessionID, infoShuttle.siteURL.getHost(), infoShuttle.siteURL.getPath());
    if (serverCookieStr != null) {
        infoShuttle.userHeaderNames.add("cookie");
        infoShuttle.userHeaderValues.add(serverCookieStr);
    }
}
Also used : HttpConnector(com.twinsoft.convertigo.beans.connectors.HttpConnector) MalformedURLException(java.net.MalformedURLException) URISyntaxException(java.net.URISyntaxException) StringEx(com.twinsoft.util.StringEx) URI(java.net.URI) URL(java.net.URL)

Example 15 with StringEx

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

the class DatabaseCacheManager method updateCacheEntry.

public void updateCacheEntry(CacheEntry cacheEntry) throws EngineException {
    DatabaseCacheEntry dbCacheEntry = (DatabaseCacheEntry) cacheEntry;
    try {
        Engine.logCacheManager.debug("Trying to update cache entry: " + cacheEntry);
        sqlRequester.checkConnection();
        StringEx sqlRequest = new StringEx(sqlRequester.getProperty(DatabaseCacheManager.PROPERTIES_SQL_REQUEST_UPDATE_CACHE_ENTRY));
        String cacheTableName = sqlRequester.getProperty(DatabaseCacheManager.PROPERTIES_SQL_CACHE_TABLE_NAME, "CacheTable");
        sqlRequest.replace("CacheTable", cacheTableName);
        sqlRequest.replace("{ExpiryDate}", Long.toString(dbCacheEntry.expiryDate));
        sqlRequest.replace("{SheetUrl}", dbCacheEntry.sheetUrl);
        sqlRequest.replace("{AbsoluteSheetUrl}", dbCacheEntry.absoluteSheetUrl);
        sqlRequest.replace("{ContentType}", dbCacheEntry.contentType);
        sqlRequest.replace("{Id}", Long.toString(dbCacheEntry.id));
        String sSqlRequest = sqlRequest.toString();
        Engine.logCacheManager.debug("SQL: " + sSqlRequest);
        Statement statement = null;
        try {
            statement = sqlRequester.connection.createStatement();
            // Retry request if needed
            int i = 1;
            do {
                try {
                    int nResult = statement.executeUpdate(sSqlRequest);
                    Engine.logCacheManager.debug(nResult + " row(s) updated.");
                    break;
                } catch (SQLException e) {
                    Engine.logCacheManager.warn("updateCacheEntry(): exception while processing SQL request: " + e.getMessage() + "; attempt " + i + " of 3");
                    Thread.sleep(500);
                    i++;
                }
            } while (i < 4);
        } finally {
            if (statement != null) {
                statement.close();
            }
        }
        Engine.logCacheManager.debug("The cache entry has been updated.");
    } catch (Exception e) {
        throw new EngineException("Unable to update the cache entry!", e);
    }
}
Also used : SQLException(java.sql.SQLException) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) EngineException(com.twinsoft.convertigo.engine.EngineException) StringEx(com.twinsoft.util.StringEx) IOException(java.io.IOException) SQLException(java.sql.SQLException) 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