Search in sources :

Example 76 with APIException

use of org.openmrs.api.APIException in project openmrs-core by openmrs.

the class ObsValidator method validate.

/**
 * @see org.springframework.validation.Validator#validate(java.lang.Object,
 *      org.springframework.validation.Errors)
 * @should fail validation if personId is null
 * @should fail validation if obsDatetime is null
 * @should fail validation if concept is null
 * @should fail validation if concept datatype is boolean and valueBoolean is null
 * @should fail validation if concept datatype is coded and valueCoded is null
 * @should fail validation if concept datatype is date and valueDatetime is null
 * @should fail validation if concept datatype is numeric and valueNumeric is null
 * @should fail validation if concept datatype is text and valueText is null
 * @should fail validation if obs ancestors contains obs
 * @should pass validation if all values present
 * @should fail validation if the parent obs has values
 * @should reject an invalid concept and drug combination
 * @should pass if answer concept and concept of value drug match
 * @should pass validation if field lengths are correct
 * @should fail validation if field lengths are not correct
 * @should not validate if obs is voided
 * @should not validate a voided child obs
 * @should fail for a null object
 */
@Override
public void validate(Object obj, Errors errors) {
    Obs obs = (Obs) obj;
    if (obs == null) {
        throw new APIException("Obs can't be null");
    } else if (obs.getVoided()) {
        return;
    }
    List<Obs> ancestors = new ArrayList<>();
    validateHelper(obs, errors, ancestors, true);
    ValidateUtil.validateFieldLengths(errors, obj.getClass(), "accessionNumber", "valueModifier", "valueComplex", "comment", "voidReason");
}
Also used : Obs(org.openmrs.Obs) APIException(org.openmrs.api.APIException) ArrayList(java.util.ArrayList)

Example 77 with APIException

use of org.openmrs.api.APIException in project openmrs-core by openmrs.

the class OpenmrsUtil method getDirectoryInApplicationDataDirectory.

/**
 * Find the given folderName in the application data directory. Or, treat folderName like an
 * absolute url to a directory
 *
 * @param folderName
 * @return folder capable of storing information
 */
public static File getDirectoryInApplicationDataDirectory(String folderName) throws APIException {
    // try to load the repository folder straight away.
    File folder = new File(folderName);
    // application directory
    if (!folder.isAbsolute()) {
        folder = new File(OpenmrsUtil.getApplicationDataDirectory(), folderName);
    }
    // now create the directory folder if it doesn't exist
    if (!folder.exists()) {
        log.warn("'" + folder.getAbsolutePath() + "' doesn't exist.  Creating directories now.");
        folder.mkdirs();
    }
    if (!folder.isDirectory()) {
        throw new APIException("should.be.directory", new Object[] { folder.getAbsolutePath() });
    }
    return folder;
}
Also used : APIException(org.openmrs.api.APIException) File(java.io.File) JarFile(java.util.jar.JarFile)

Example 78 with APIException

use of org.openmrs.api.APIException in project openmrs-core by openmrs.

the class Security method incorrectlyEncodeString.

/**
 * This method will hash <code>strToEncode</code> using SHA-1 and the incorrect hashing method
 * that sometimes dropped out leading zeros.
 *
 * @param strToEncode string to encode
 * @return the SHA-1 encryption of a given string
 */
private static String incorrectlyEncodeString(String strToEncode) throws APIException {
    String algorithm = "SHA1";
    MessageDigest md;
    byte[] input;
    try {
        md = MessageDigest.getInstance(algorithm);
        input = strToEncode.getBytes(StandardCharsets.UTF_8);
    } catch (NoSuchAlgorithmException e) {
        // Yikes! Can't encode password...what to do?
        log.error(getPasswordEncodeFailMessage(algorithm), e);
        throw new APIException("system.cannot.find.encryption.algorithm", null, e);
    }
    return incorrectHexString(md.digest(input));
}
Also used : APIException(org.openmrs.api.APIException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) MessageDigest(java.security.MessageDigest)

Example 79 with APIException

use of org.openmrs.api.APIException in project openmrs-core by openmrs.

the class Security method decrypt.

/**
 * decrypt text to a string with specific initVector and secretKey; rarely used except in
 * testing and where specifically necessary
 *
 * @see #decrypt(String)
 *
 * @param text text to be decrypted
 * @param initVector custom init vector byte array
 * @param secretKey custom secret key byte array
 * @return decrypted text
 * @since 1.9
 */
public static String decrypt(String text, byte[] initVector, byte[] secretKey) {
    IvParameterSpec initVectorSpec = new IvParameterSpec(initVector);
    SecretKeySpec secret = new SecretKeySpec(secretKey, OpenmrsConstants.ENCRYPTION_KEY_SPEC);
    String decrypted;
    try {
        Cipher cipher = Cipher.getInstance(OpenmrsConstants.ENCRYPTION_CIPHER_CONFIGURATION);
        cipher.init(Cipher.DECRYPT_MODE, secret, initVectorSpec);
        byte[] original = cipher.doFinal(Base64.getDecoder().decode(text));
        decrypted = new String(original, StandardCharsets.UTF_8);
    } catch (GeneralSecurityException e) {
        throw new APIException("could.not.decrypt.text", null, e);
    }
    return decrypted;
}
Also used : APIException(org.openmrs.api.APIException) SecretKeySpec(javax.crypto.spec.SecretKeySpec) GeneralSecurityException(java.security.GeneralSecurityException) IvParameterSpec(javax.crypto.spec.IvParameterSpec) Cipher(javax.crypto.Cipher)

Example 80 with APIException

use of org.openmrs.api.APIException in project openmrs-core by openmrs.

the class Security method encodeString.

/**
 *	/**
 * This method will hash <code>strToEncode</code> using the preferred algorithm. Currently,
 * OpenMRS's preferred algorithm is hard coded to be SHA-512.
 *
 * @param strToEncode string to encode
 * @return the SHA-512 encryption of a given string
 * @should encode strings to 128 characters
 */
public static String encodeString(String strToEncode) throws APIException {
    String algorithm = "SHA-512";
    MessageDigest md;
    byte[] input;
    try {
        md = MessageDigest.getInstance(algorithm);
        input = strToEncode.getBytes(StandardCharsets.UTF_8);
    } catch (NoSuchAlgorithmException e) {
        // Yikes! Can't encode password...what to do?
        log.error(getPasswordEncodeFailMessage(algorithm), e);
        throw new APIException("system.cannot.find.password.encryption.algorithm", null, e);
    }
    return hexString(md.digest(input));
}
Also used : APIException(org.openmrs.api.APIException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) MessageDigest(java.security.MessageDigest)

Aggregations

APIException (org.openmrs.api.APIException)86 Date (java.util.Date)11 IOException (java.io.IOException)10 ArrayList (java.util.ArrayList)10 File (java.io.File)9 Obs (org.openmrs.Obs)7 List (java.util.List)6 Map (java.util.Map)6 User (org.openmrs.User)6 FileInputStream (java.io.FileInputStream)5 FileOutputStream (java.io.FileOutputStream)5 Concept (org.openmrs.Concept)5 OpenmrsObject (org.openmrs.OpenmrsObject)5 InputStream (java.io.InputStream)4 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)4 Order (org.openmrs.Order)4 FileNotFoundException (java.io.FileNotFoundException)3 OutputStreamWriter (java.io.OutputStreamWriter)3 MessageDigest (java.security.MessageDigest)3 HashMap (java.util.HashMap)3