Search in sources :

Example 81 with ShouldNeverHappenException

use of com.serotonin.ShouldNeverHappenException in project ma-core-public by infiniteautomation.

the class Common method encrypt.

public static String encrypt(String plaintext, String alg) {
    try {
        if ("NONE".equals(alg))
            return plaintext;
        if ("BCRYPT".equals(alg)) {
            return BCrypt.hashpw(plaintext, BCrypt.gensalt());
        }
        MessageDigest md = MessageDigest.getInstance(alg);
        if (md == null)
            throw new ShouldNeverHappenException("MessageDigest algorithm " + alg + " not found. Set the 'security.hashAlgorithm' property in env.properties appropriately. " + "Use 'NONE' for no hashing.");
        md.update(plaintext.getBytes(UTF8_CS));
        byte[] raw = md.digest();
        String hash = new String(Base64.encodeBase64(raw));
        return hash;
    } catch (NoSuchAlgorithmException e) {
        // Should never happen, so just wrap in a runtime exception and rethrow
        throw new ShouldNeverHappenException(e);
    }
}
Also used : ShouldNeverHappenException(com.serotonin.ShouldNeverHappenException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) MessageDigest(java.security.MessageDigest)

Example 82 with ShouldNeverHappenException

use of com.serotonin.ShouldNeverHappenException in project ma-core-public by infiniteautomation.

the class Common method getCronTrigger.

public static CronTimerTrigger getCronTrigger(int periodType, int delaySeconds) {
    int delayMinutes = 0;
    if (delaySeconds >= 60) {
        delayMinutes = delaySeconds / 60;
        delaySeconds %= 60;
        if (delayMinutes >= 60)
            delayMinutes = 59;
    }
    try {
        switch(periodType) {
            case TimePeriods.MILLISECONDS:
                throw new ShouldNeverHappenException("Can't create a cron trigger for milliseconds");
            case TimePeriods.SECONDS:
                return new CronTimerTrigger("* * * * * ?");
            case TimePeriods.MINUTES:
                return new CronTimerTrigger(delaySeconds + " * * * * ?");
            case TimePeriods.HOURS:
                return new CronTimerTrigger(delaySeconds + " " + delayMinutes + " * * * ?");
            case TimePeriods.DAYS:
                return new CronTimerTrigger(delaySeconds + " " + delayMinutes + " 0 * * ?");
            case TimePeriods.WEEKS:
                return new CronTimerTrigger(delaySeconds + " " + delayMinutes + " 0 ? * MON");
            case TimePeriods.MONTHS:
                return new CronTimerTrigger(delaySeconds + " " + delayMinutes + " 0 1 * ?");
            case TimePeriods.YEARS:
                return new CronTimerTrigger(delaySeconds + " " + delayMinutes + " 0 1 JAN ?");
            default:
                throw new ShouldNeverHappenException("Invalid cron period type: " + periodType);
        }
    } catch (ParseException e) {
        throw new ShouldNeverHappenException(e);
    }
}
Also used : ShouldNeverHappenException(com.serotonin.ShouldNeverHappenException) CronTimerTrigger(com.serotonin.timer.CronTimerTrigger) ParseException(java.text.ParseException)

Example 83 with ShouldNeverHappenException

use of com.serotonin.ShouldNeverHappenException in project ma-core-public by infiniteautomation.

the class DeltamationCommon method getCronTrigger.

/**
 * Delta implementation of com.serotonin.m2m2.Common.getCronTrigger()
 * Adds an "every" parameter and overloaded methods
 *
 * @param every
 * @param periodType
 * @param delaySeconds
 * @return
 */
public static CronTimerTrigger getCronTrigger(int every, int periodType, int delaySeconds) {
    // could use constraint
    if (every <= 0) {
        every = 1;
    }
    int delayMinutes = 0;
    if (delaySeconds >= 60) {
        delayMinutes = delaySeconds / 60;
        delaySeconds %= 60;
        if (delayMinutes >= 60)
            delayMinutes = 59;
    }
    try {
        switch(periodType) {
            case TimePeriods.MILLISECONDS:
                throw new ShouldNeverHappenException("Can't create a cron trigger for milliseconds");
            case TimePeriods.SECONDS:
                return new CronTimerTrigger("*/" + Integer.toString(every) + " * * * * ?");
            case TimePeriods.MINUTES:
                return new CronTimerTrigger(delaySeconds + " */" + Integer.toString(every) + " * * * ?");
            case TimePeriods.HOURS:
                return new CronTimerTrigger(delaySeconds + " " + delayMinutes + " */" + Integer.toString(every) + " * * ?");
            case TimePeriods.DAYS:
                return new CronTimerTrigger(delaySeconds + " " + delayMinutes + " 0 */" + Integer.toString(every) + " * ?");
            case TimePeriods.WEEKS:
                // TODO cant do every on weeks
                return new CronTimerTrigger(delaySeconds + " " + delayMinutes + " 0 ? * MON");
            case TimePeriods.MONTHS:
                return new CronTimerTrigger(delaySeconds + " " + delayMinutes + " 0 1 */" + Integer.toString(every) + " ?");
            case TimePeriods.YEARS:
                return new CronTimerTrigger(delaySeconds + " " + delayMinutes + " 0 1 JAN ? */" + Integer.toString(every));
            default:
                throw new ShouldNeverHappenException("Invalid cron period type: " + periodType);
        }
    } catch (ParseException e) {
        throw new ShouldNeverHappenException(e);
    }
}
Also used : ShouldNeverHappenException(com.serotonin.ShouldNeverHappenException) CronTimerTrigger(com.serotonin.timer.CronTimerTrigger) ParseException(java.text.ParseException)

Aggregations

ShouldNeverHappenException (com.serotonin.ShouldNeverHappenException)83 IOException (java.io.IOException)20 ArrayList (java.util.ArrayList)10 TranslatableMessage (com.serotonin.m2m2.i18n.TranslatableMessage)9 SQLException (java.sql.SQLException)9 ParseException (java.text.ParseException)8 PointValueTime (com.serotonin.m2m2.rt.dataImage.PointValueTime)6 DataPointVO (com.serotonin.m2m2.vo.DataPointVO)6 FileNotFoundException (java.io.FileNotFoundException)6 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)5 ResultSet (java.sql.ResultSet)5 Statement (java.sql.Statement)5 JsonException (com.serotonin.json.JsonException)4 JsonWriter (com.serotonin.json.JsonWriter)4 ImageValue (com.serotonin.m2m2.rt.dataImage.types.ImageValue)4 NumericValue (com.serotonin.m2m2.rt.dataImage.types.NumericValue)4 CronTimerTrigger (com.serotonin.timer.CronTimerTrigger)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)4 StringWriter (java.io.StringWriter)4 HashMap (java.util.HashMap)4