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");
}
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;
}
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));
}
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;
}
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));
}
Aggregations