Search in sources :

Example 6 with RbvException

use of com.axway.ats.rbv.model.RbvException in project ats-framework by Axway.

the class DbFolder method refresh.

private void refresh() throws RbvException {
    newMetaDataMap = new HashMap<String, MetaData>();
    //store the current meta data map and clear the map holding all meta data
    //this way we will be able to detect any changes including added and removed
    //meta data
    HashMap<String, MetaData> oldMetaDataMap = allMetaDataMap;
    allMetaDataMap = new HashMap<String, MetaData>();
    log.debug("Run DB query '" + this.searchQuery.getQuery() + "'");
    DbRecordValuesList[] queryResults;
    try {
        queryResults = dbProvider.select(this.searchQuery);
    } catch (DbException dbe) {
        throw new RbvException(dbe);
    }
    if (queryResults != null) {
        for (DbRecordValuesList queryResult : queryResults) {
            DbMetaData currentData = new DbMetaData();
            StringBuffer metaDataHash = new StringBuffer();
            for (DbRecordValue recordValue : queryResult) {
                DbMetaDataKey key = new DbMetaDataKey(recordValue.getDbColumn());
                Object value = recordValue.getValue();
                currentData.putProperty(key.toString(), value);
                //calculate the hash
                metaDataHash.append(key.toString());
                metaDataHash.append(recordValue.getValueAsString());
            }
            try {
                //compute MD5 so we don't keep the whole StringBuffer in memory
                MessageDigest metaDataHashDigest = MessageDigest.getInstance("MD5");
                String metaDataSum = new String(metaDataHashDigest.digest(metaDataHash.toString().getBytes()));
                if (!oldMetaDataMap.containsKey(metaDataSum)) {
                    newMetaDataMap.put(metaDataSum, currentData);
                }
                //always put the record in the map holding all meta data
                allMetaDataMap.put(metaDataSum, currentData);
            } catch (NoSuchAlgorithmException e) {
                throw new RuntimeException(e);
            }
        }
        didPollingOccured = true;
    }
}
Also used : RbvException(com.axway.ats.rbv.model.RbvException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) DbException(com.axway.ats.core.dbaccess.exceptions.DbException) DbRecordValuesList(com.axway.ats.core.dbaccess.DbRecordValuesList) DbRecordValue(com.axway.ats.core.dbaccess.DbRecordValue) MetaData(com.axway.ats.rbv.MetaData) MessageDigest(java.security.MessageDigest)

Example 7 with RbvException

use of com.axway.ats.rbv.model.RbvException in project ats-framework by Axway.

the class DbDateFieldRule method checkDate.

private boolean checkDate(DbMetaData dbMetaData) throws RbvException {
    Timestamp actual;
    try {
        actual = (Timestamp) dbMetaData.getProperty(this.expectedMetaDataKey);
    } catch (ClassCastException cce) {
        throw new MetaDataIncorrectException("Meta data is incorrect - expected Timestamp");
    }
    long timestamp = ((Date) this.expectedValue).getTime();
    Timestamp expected = new Timestamp(timestamp);
    switch(this.relation) {
        case BEFORE_DATE:
            return actual.compareTo(expected) <= 0;
        case AFTER_DATE:
            return actual.compareTo(expected) >= 0;
        case EXACT:
            return actual.compareTo(expected) == 0;
        default:
            throw new RbvException("No implementation for MatchRelation '" + this.relation.toString() + "' in DbDateFieldMatchingTerm");
    }
}
Also used : MetaDataIncorrectException(com.axway.ats.rbv.model.MetaDataIncorrectException) RbvException(com.axway.ats.rbv.model.RbvException) Timestamp(java.sql.Timestamp) Date(java.util.Date)

Example 8 with RbvException

use of com.axway.ats.rbv.model.RbvException in project ats-framework by Axway.

the class DbDateFieldRule method checkString.

private boolean checkString(DbMetaData dbMetaData) throws RbvException {
    String actualValue;
    try {
        actualValue = (String) dbMetaData.getProperty(this.expectedMetaDataKey.toString());
    } catch (ClassCastException cce) {
        throw new MetaDataIncorrectException("Meta data is incorrect - expected String");
    }
    try {
        SimpleDateFormat sdf = new SimpleDateFormat(this.actualValuePattern);
        sdf.setTimeZone(new SimpleTimeZone(0, "GMT"));
        Date expectedDate = new Date(Long.parseLong((String) this.expectedValue) * 1000);
        Date actualDate = sdf.parse(actualValue);
        switch(this.relation) {
            case BEFORE_DATE:
                return expectedDate.before(actualDate);
            case AFTER_DATE:
                return expectedDate.after(actualDate);
            case EXACT:
                return expectedDate.compareTo(actualDate) == 0;
            default:
                throw new RbvException("No implementation for MatchRelation '" + this.relation.toString() + "' in DbDateFieldMatchingTerm");
        }
    } catch (NumberFormatException nfe) {
        throw new RbvException("Expected value '" + this.expectedValue + "' cannot be converted to UNIX timestamp");
    } catch (ParseException pe) {
        //only if the actual value is incorrect
        throw new RbvException("Actual value '" + actualValue + "' cannot be converted to Date");
    }
}
Also used : MetaDataIncorrectException(com.axway.ats.rbv.model.MetaDataIncorrectException) SimpleTimeZone(java.util.SimpleTimeZone) RbvException(com.axway.ats.rbv.model.RbvException) ParseException(java.text.ParseException) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date)

Example 9 with RbvException

use of com.axway.ats.rbv.model.RbvException in project ats-framework by Axway.

the class DbStringFieldRule method performMatch.

@Override
protected boolean performMatch(MetaData metaData) throws RbvException {
    boolean actualResult = false;
    //this cast is safe, as isMatch has already checked the type of meta data
    DbMetaData dbMetaData = (DbMetaData) metaData;
    String actualValue;
    try {
        actualValue = (String) dbMetaData.getProperty(expectedMetaDataKey.toString());
        if (dbEncryptor != null) {
            actualValue = dbEncryptor.decrypt(actualValue);
        }
        log.info("Actual value is '" + actualValue + "'");
    } catch (ClassCastException cce) {
        throw new MetaDataIncorrectException("Meta data is incorrect - expected String");
    }
    if (expectedValue == null) {
        return actualValue == null;
    } else {
        if (actualValue == null) {
            return false;
        }
    }
    if (relation == MatchRelation.EQUALS) {
        actualResult = expectedValue.equals(actualValue);
    } else if (relation == MatchRelation.CONTAINS) {
        actualResult = actualValue.contains((String) expectedValue);
    } else if (relation == MatchRelation.REGEX_MATCH) {
        Pattern regexPattern = Pattern.compile((String) expectedValue);
        actualResult = regexPattern.matcher(actualValue).matches();
    } else {
        throw new RbvException("No implementation for MatchRelation '" + relation.toString() + "' in DbStringFieldMatchingTerm");
    }
    return actualResult;
}
Also used : DbMetaData(com.axway.ats.rbv.db.DbMetaData) Pattern(java.util.regex.Pattern) MetaDataIncorrectException(com.axway.ats.rbv.model.MetaDataIncorrectException) RbvException(com.axway.ats.rbv.model.RbvException)

Example 10 with RbvException

use of com.axway.ats.rbv.model.RbvException in project ats-framework by Axway.

the class FileSystemFolder method getAllMetaData.

public List<MetaData> getAllMetaData() throws RbvException {
    //first check if the folder is already open
    if (!isOpen) {
        throw new MatchableNotOpenException("File system folder is not open");
    }
    newMetaData.clear();
    if (fileName == null) {
        fileName = ".*";
        isRegExp = true;
    }
    HashMap<String, MetaData> tempMetaData = new HashMap<String, MetaData>();
    //fetch dir contents recursively
    String[] fileList;
    try {
        fileList = this.fileSystemOperations.findFiles(path, fileName, isRegExp, true, includeSubDirs);
    } catch (Exception e) {
        final String notExistMessageSuffix = "does not exist or is not a folder";
        if ((e.getMessage() != null && e.getMessage().endsWith(notExistMessageSuffix)) || (e.getCause() != null && e.getCause().getMessage() != null && e.getCause().getMessage().endsWith(notExistMessageSuffix))) {
            log.warn(getDescription() + " does not exist, skipping to next poll attempt");
            return new ArrayList<MetaData>();
        }
        throw new RbvException("Unable to list the contents of " + path, e);
    }
    if (fileList != null) {
        for (String fileName : fileList) {
            try {
                FilePackage file = new FilePackage(atsAgent, fileName.trim(), osType);
                MetaData metaData = new FileSystemMetaData(file);
                // The way files are compared is by combining their name+path,
                // modification time, user and group ID in a hash string
                String hashKey = file.getUniqueIdentifier();
                if (!allMetaData.containsKey(hashKey)) {
                    newMetaData.add(metaData);
                }
                tempMetaData.put(hashKey, metaData);
            } catch (PackageException e) {
                // the creation of the package somehow failed - a simple explanation would be that
                // the filed was removed during the execution of this method or something similar;
                log.warn("Unable to build up metadata for " + fileName, e);
            // either way we need not throw an exception but only continue iterating
            }
        }
    }
    allMetaData.clear();
    allMetaData.putAll(tempMetaData);
    return new ArrayList<MetaData>(allMetaData.values());
}
Also used : MatchableNotOpenException(com.axway.ats.rbv.model.MatchableNotOpenException) HashMap(java.util.HashMap) RbvException(com.axway.ats.rbv.model.RbvException) ArrayList(java.util.ArrayList) MatchableAlreadyOpenException(com.axway.ats.rbv.model.MatchableAlreadyOpenException) MatchableNotOpenException(com.axway.ats.rbv.model.MatchableNotOpenException) RbvException(com.axway.ats.rbv.model.RbvException) PackageException(com.axway.ats.action.objects.model.PackageException) RbvStorageException(com.axway.ats.rbv.model.RbvStorageException) FilePackage(com.axway.ats.action.objects.FilePackage) MetaData(com.axway.ats.rbv.MetaData) PackageException(com.axway.ats.action.objects.model.PackageException)

Aggregations

RbvException (com.axway.ats.rbv.model.RbvException)15 PackageException (com.axway.ats.action.objects.model.PackageException)6 MimePackage (com.axway.ats.action.objects.MimePackage)5 BaseTest (com.axway.ats.rbv.BaseTest)3 MetaData (com.axway.ats.rbv.MetaData)3 MetaDataIncorrectException (com.axway.ats.rbv.model.MetaDataIncorrectException)3 ArrayList (java.util.ArrayList)3 Test (org.junit.Test)3 InputStream (java.io.InputStream)2 Date (java.util.Date)2 FileSystemOperations (com.axway.ats.action.filesystem.FileSystemOperations)1 FilePackage (com.axway.ats.action.objects.FilePackage)1 NoSuchHeaderException (com.axway.ats.action.objects.model.NoSuchHeaderException)1 NoSuchMimePartException (com.axway.ats.action.objects.model.NoSuchMimePartException)1 ConfigurationException (com.axway.ats.config.exceptions.ConfigurationException)1 DbRecordValue (com.axway.ats.core.dbaccess.DbRecordValue)1 DbRecordValuesList (com.axway.ats.core.dbaccess.DbRecordValuesList)1 DbException (com.axway.ats.core.dbaccess.exceptions.DbException)1 Monitor (com.axway.ats.rbv.Monitor)1 PollingParameters (com.axway.ats.rbv.PollingParameters)1