Search in sources :

Example 1 with CacheException

use of org.apache.jcs.access.exception.CacheException in project nhin-d by DirectProject.

the class CertCacheFactory method getCertCache.

/**
	 * Retrieves a cert cache by name.  Caches are created using a singleton pattern meaning one and only once instance of a cache for a given name
	 * is ever created.
	 * @param cacheName The name of the cache to retrieve.
	 * @param cachePolicy Policy to apply to the cache
	 * @return The certificate cache for the given cache name.
	 * @throws CacheException Thrown if the cache cannot be created.
	 */
public synchronized JCS getCertCache(String cacheName, CertStoreCachePolicy cachePolicy) throws CacheException {
    JCS retVal = certCacheMap.get(cacheName);
    if (retVal == null) {
        try {
            // create instance
            retVal = JCS.getInstance(cacheName);
            if (cachePolicy != null)
                applyCachePolicy(retVal, cachePolicy);
            certCacheMap.put(cacheName, retVal);
        } catch (CacheException e) {
            LOGGER.warn("Failed to create JCS cache " + cacheName, e);
            throw e;
        }
    }
    return retVal;
}
Also used : CacheException(org.apache.jcs.access.exception.CacheException) JCS(org.apache.jcs.JCS)

Example 2 with CacheException

use of org.apache.jcs.access.exception.CacheException in project nhin-d by DirectProject.

the class LDAPCertificateStore method getCertificates.

/**
	 * {@inheritDoc}
	 */
@SuppressWarnings("unchecked")
@Override
public Collection<X509Certificate> getCertificates(String subjectName) {
    String realSubjectName;
    int index;
    if ((index = subjectName.indexOf("EMAILADDRESS=")) > -1)
        realSubjectName = subjectName.substring(index + "EMAILADDRESS=".length());
    else
        realSubjectName = subjectName;
    Collection<X509Certificate> retVal;
    JCS cache = getCache();
    if (cache != null) {
        // try to get it from the cache first
        retVal = (Collection<X509Certificate>) cache.get(realSubjectName);
        // the certificate is not in the cache, so now hit the real server
        if (retVal == null || retVal.size() == 0) {
            retVal = ldapCertUtil.ldapSearch(realSubjectName);
            // add or update the cache and the local cert store
            if (retVal != null && retVal.size() > 0) {
                // don't cache wildcard searches
                if (!subjectName.contains("*")) {
                    try {
                        // first add the certificates to the cache 
                        cache.putSafe(realSubjectName, retVal);
                    } catch (CacheException e) {
                        // TODO: handle exception
                        LOGGER.error("Error adding certificates to the cache: " + e.getMessage(), e);
                    }
                    // now add or update the local cert store
                    if (localStoreDelegate != null) {
                        addOrUpdateLocalStoreDelegate(retVal);
                    }
                }
            } else // couldn't retrieve the certificate from the real server, so have to go to the bootstrap
            {
                if (localStoreDelegate != null) {
                    // last ditch effort is to go to the bootstrap cache
                    retVal = localStoreDelegate.getCertificates(realSubjectName);
                }
            }
            if (retVal == null || retVal.size() == 0) {
                LOGGER.info("getCertificates(String subjectName) - Could not find an LDAP certificate for subject " + subjectName);
            }
        }
    } else // cache miss
    {
        retVal = ldapCertUtil.ldapSearch(realSubjectName);
        if (localStoreDelegate != null) {
            if (retVal == null || retVal.size() == 0) {
                // last ditch effort is to go to the bootstrap cache
                retVal = localStoreDelegate.getCertificates(realSubjectName);
            } else if (!subjectName.contains("*")) {
                // now add or update the local cert store
                addOrUpdateLocalStoreDelegate(retVal);
            }
        }
        if (retVal == null || retVal.size() == 0) {
            LOGGER.info("getCertificates(String subjectName) - Could not find an LDAP certificate for subject " + subjectName);
        }
    }
    return retVal;
}
Also used : CacheException(org.apache.jcs.access.exception.CacheException) JCS(org.apache.jcs.JCS) Thumbprint(org.nhindirect.stagent.cert.Thumbprint) X509Certificate(java.security.cert.X509Certificate)

Example 3 with CacheException

use of org.apache.jcs.access.exception.CacheException in project nhin-d by DirectProject.

the class CacheableKeyStoreManagerCertificateStore method applyCachePolicy.

private void applyCachePolicy(CertStoreCachePolicy policy) {
    if (getCache() != null) {
        try {
            ICompositeCacheAttributes attributes = cache.getCacheAttributes();
            attributes.setMaxObjects(policy.getMaxItems());
            attributes.setUseLateral(false);
            attributes.setUseRemote(false);
            cache.setCacheAttributes(attributes);
            IElementAttributes eattributes = cache.getDefaultElementAttributes();
            eattributes.setMaxLifeSeconds(policy.getSubjectTTL());
            eattributes.setIsEternal(false);
            eattributes.setIsLateral(false);
            eattributes.setIsRemote(false);
            cache.setDefaultElementAttributes(eattributes);
        } catch (CacheException e) {
        // no-op
        }
    }
}
Also used : CacheException(org.apache.jcs.access.exception.CacheException) ICompositeCacheAttributes(org.apache.jcs.engine.behavior.ICompositeCacheAttributes) IElementAttributes(org.apache.jcs.engine.behavior.IElementAttributes)

Example 4 with CacheException

use of org.apache.jcs.access.exception.CacheException in project nhin-d by DirectProject.

the class CacheableKeyStoreManagerCertificateStore method getCertificates.

///CLOVER:ON
/**
	 * {@inheritDoc}
	 */
@SuppressWarnings("unchecked")
@Override
public Collection<X509Certificate> getCertificates(String subjectName) {
    String realSubjectName;
    int index;
    if ((index = subjectName.indexOf("EMAILADDRESS=")) > -1)
        realSubjectName = subjectName.substring(index + "EMAILADDRESS=".length());
    else
        realSubjectName = subjectName;
    Collection<X509Certificate> retVal;
    final JCS cache = getCache();
    if (cache != null) {
        retVal = (Collection<X509Certificate>) cache.get(realSubjectName);
        if (retVal == null || retVal.size() == 0)
            retVal = super.getCertificates(subjectName);
    } else
        // cache miss
        retVal = super.getCertificates(subjectName);
    if (retVal == null || retVal.size() == 0) {
        LOGGER.info("getCertificates(String subjectName) - Could not find a PKCS11 certificate for subject " + subjectName);
    } else {
        try {
            if (cache != null)
                cache.put(realSubjectName, retVal);
        } catch (CacheException e) {
        /*
				 * no-opss
				 */
        }
    }
    return retVal;
}
Also used : CacheException(org.apache.jcs.access.exception.CacheException) JCS(org.apache.jcs.JCS) X509Certificate(java.security.cert.X509Certificate)

Example 5 with CacheException

use of org.apache.jcs.access.exception.CacheException in project nhin-d by DirectProject.

the class DNSCertificateStore method applyCachePolicy.

private void applyCachePolicy(CertStoreCachePolicy policy) {
    if (getCache() != null) {
        try {
            ICompositeCacheAttributes attributes = cache.getCacheAttributes();
            attributes.setMaxObjects(policy.getMaxItems());
            attributes.setUseLateral(false);
            attributes.setUseRemote(false);
            cache.setCacheAttributes(attributes);
            IElementAttributes eattributes = cache.getDefaultElementAttributes();
            eattributes.setMaxLifeSeconds(policy.getSubjectTTL());
            eattributes.setIsEternal(false);
            eattributes.setIsLateral(false);
            eattributes.setIsRemote(false);
            cache.setDefaultElementAttributes(eattributes);
        } catch (CacheException e) {
        // TODO: Handle exception
        }
    }
}
Also used : CacheException(org.apache.jcs.access.exception.CacheException) ICompositeCacheAttributes(org.apache.jcs.engine.behavior.ICompositeCacheAttributes) IElementAttributes(org.apache.jcs.engine.behavior.IElementAttributes)

Aggregations

CacheException (org.apache.jcs.access.exception.CacheException)13 X509Certificate (java.security.cert.X509Certificate)7 ArrayList (java.util.ArrayList)5 ICompositeCacheAttributes (org.apache.jcs.engine.behavior.ICompositeCacheAttributes)5 IElementAttributes (org.apache.jcs.engine.behavior.IElementAttributes)5 NHINDException (org.nhindirect.stagent.NHINDException)5 JCS (org.apache.jcs.JCS)3 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 MalformedURLException (java.net.MalformedURLException)1 UnknownHostException (java.net.UnknownHostException)1 Certificate (java.security.cert.Certificate)1 Thumbprint (org.nhindirect.stagent.cert.Thumbprint)1 CERTRecord (org.xbill.DNS.CERTRecord)1 CNAMERecord (org.xbill.DNS.CNAMERecord)1 ExtendedResolver (org.xbill.DNS.ExtendedResolver)1 Lookup (org.xbill.DNS.Lookup)1 NSRecord (org.xbill.DNS.NSRecord)1 Name (org.xbill.DNS.Name)1 Record (org.xbill.DNS.Record)1