Search in sources :

Example 1 with HashingException

use of org.wso2.apimgt.gateway.cli.exception.HashingException in project product-microgateway by wso2.

the class HashUtils method getMD5Hex.

/**
 * Calculates the MD5 hash for a given String.
 *
 * @param inputString input string
 * @return calculated md5 hash value
 * @throws HashingException error while calculating the md5 hash value
 */
private static String getMD5Hex(final String inputString) throws HashingException {
    MessageDigest md;
    byte[] digest;
    try {
        md = MessageDigest.getInstance(HashingConstants.HASH_ALGORITHM);
        md.update(inputString.getBytes(StandardCharsets.UTF_8));
        digest = md.digest();
    } catch (NoSuchAlgorithmException e) {
        throw new HashingException("Error getting md5 hash of " + inputString, e);
    }
    return convertByteToHex(digest);
}
Also used : NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) MessageDigest(java.security.MessageDigest) HashingException(org.wso2.apimgt.gateway.cli.exception.HashingException)

Example 2 with HashingException

use of org.wso2.apimgt.gateway.cli.exception.HashingException in project product-microgateway by wso2.

the class HashUtils method detectChanges.

/**
 * Generate hashes for the specified apis, subscription and application policies, then compare with the previously
 * generated hashes and detect if there are changes with them.
 *
 * @param apis                 APIs list
 * @param subscriptionPolicies Subscription Policies list
 * @param appPolicies          Application policies list
 * @param projectName          Name of the project
 * @return true if there are changes detected vs the previous check
 * @throws HashingException error while change detection
 */
public static boolean detectChanges(List<ExtendedAPI> apis, List<SubscriptionThrottlePolicyDTO> subscriptionPolicies, List<ApplicationThrottlePolicyDTO> appPolicies, String projectName) throws HashingException {
    boolean hasChanges = true;
    Map<String, String> allHashesMap = new HashMap<>();
    Map<String, String> apiHashesMap = getMapOfHashes(apis);
    Map<String, String> appPolicyHashesMap = getMapOfHashes(appPolicies);
    Map<String, String> subsPolicyHashesMap = getMapOfHashes(subscriptionPolicies);
    logger.debug("API calculated hashes {}", apiHashesMap);
    logger.debug("App policy calculated hashes {}", appPolicyHashesMap);
    logger.debug("Subscription policy calculated hashes {}", subsPolicyHashesMap);
    allHashesMap.putAll(apiHashesMap);
    allHashesMap.putAll(appPolicyHashesMap);
    allHashesMap.putAll(subsPolicyHashesMap);
    try {
        Map<String, String> storedHashes = loadStoredResourceHashes(projectName);
        if (equalMaps(storedHashes, allHashesMap)) {
            logger.debug("No changes detected after calculating hashes.");
            hasChanges = false;
        } else {
            logger.debug("Storing calculated resource hashes.");
            storeResourceHashes(allHashesMap, projectName);
            logger.debug("Storing calculated resource hashes success.");
        }
    } catch (IOException e) {
        throw new HashingException("Error while resource change detection", e);
    }
    return hasChanges;
}
Also used : HashMap(java.util.HashMap) IOException(java.io.IOException) HashingException(org.wso2.apimgt.gateway.cli.exception.HashingException)

Example 3 with HashingException

use of org.wso2.apimgt.gateway.cli.exception.HashingException in project product-microgateway by wso2.

the class HashUtils method getAnnotatedHash.

/**
 * Calculates the hash of the object using the Hash annotations added to the getter methods of the object.
 *
 * @param obj object whose hash needs to be calculated.
 * @return calculated hash value
 * @throws HashingException error while calculating hash
 */
private static String getAnnotatedHash(Object obj) throws HashingException {
    ObjectMapper mapper = new ObjectMapper();
    TreeSet<String> sortedSet = new TreeSet<>();
    for (Method method : obj.getClass().getMethods()) {
        if (method.isAnnotationPresent(Hash.class)) {
            try {
                Object value = method.invoke(obj);
                String stringifiedField = mapper.writeValueAsString(value);
                // The method name needs to be added here. The reason is, the array of methods returned from
                // getClass().getMethods() is not always in a particular order. If the order changes, this would
                // result in change of the hash even through the object didn't change. To fix this, a TreeSet is
                // used which will insert entries sorted in natural ordering. The method name appended in the front
                // to always have a fixed string at the beginning.
                sortedSet.add(method.getName() + HashingConstants.HASH_SEPARATOR + stringifiedField);
            } catch (JsonProcessingException | IllegalAccessException | InvocationTargetException e) {
                throw new HashingException("Error while generating hash for " + obj, e);
            }
        }
    }
    StringBuilder builder = new StringBuilder();
    for (String entry : sortedSet) {
        builder.append(entry);
        builder.append(HashingConstants.HASH_SEPARATOR);
    }
    String val = builder.toString();
    return getMD5Hex(val);
}
Also used : TreeSet(java.util.TreeSet) Method(java.lang.reflect.Method) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) InvocationTargetException(java.lang.reflect.InvocationTargetException) HashingException(org.wso2.apimgt.gateway.cli.exception.HashingException)

Aggregations

HashingException (org.wso2.apimgt.gateway.cli.exception.HashingException)3 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 IOException (java.io.IOException)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Method (java.lang.reflect.Method)1 MessageDigest (java.security.MessageDigest)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 HashMap (java.util.HashMap)1 TreeSet (java.util.TreeSet)1