Example usage for javax.ejb TransactionAttributeType SUPPORTS

List of usage examples for javax.ejb TransactionAttributeType SUPPORTS

Introduction

In this page you can find the example usage for javax.ejb TransactionAttributeType SUPPORTS.

Prototype

TransactionAttributeType SUPPORTS

To view the source code for javax.ejb TransactionAttributeType SUPPORTS.

Click Source Link

Document

If the client calls with a transaction context, the container performs the same steps as described in the REQUIRED case.

Usage

From source file:org.cesecore.certificates.ca.CaSessionBean.java

@TransactionAttribute(TransactionAttributeType.SUPPORTS)
@Override//from www . j a  v a 2s. c o  m
public List<Integer> getAuthorizedCaIds(final AuthenticationToken admin) {
    final Collection<Integer> availableCaIds = getAllCaIds();
    final ArrayList<Integer> returnval = new ArrayList<Integer>();
    for (Integer caid : availableCaIds) {
        if (authorizedToCANoLogging(admin, caid)) {
            returnval.add(caid);
        }
    }
    return returnval;
}

From source file:org.cesecore.certificates.ca.CaSessionBean.java

@TransactionAttribute(TransactionAttributeType.SUPPORTS)
@Override/*from ww  w  .j  av  a 2s  .c  o m*/
public Collection<String> getAuthorizedCaNames(final AuthenticationToken admin) {
    final Collection<Integer> availableCaIds = getAllCaIds();
    final TreeSet<String> names = new TreeSet<String>();
    for (Integer caid : availableCaIds) {
        if (authorizedToCANoLogging(admin, caid)) {
            try {
                names.add(getCAInfoInternal(caid).getName());
            } catch (CADoesntExistsException e) {
                // NOPMD Should not happen since we just retrieved the ID
            }
        }
    }
    return names;
}

From source file:org.cesecore.certificates.ca.CaSessionBean.java

@TransactionAttribute(TransactionAttributeType.SUPPORTS)
@Override/*w ww.  j a  va 2 s.  com*/
public List<CAInfo> getAuthorizedAndEnabledCaInfos(AuthenticationToken authenticationToken) {
    List<CAInfo> result = new ArrayList<CAInfo>();
    for (int caId : getAuthorizedCaIds(authenticationToken)) {
        CAInfo caInfo;
        try {
            caInfo = getCAInfoInternal(caId);
        } catch (CADoesntExistsException e) {
            throw new IllegalStateException(
                    "CA with ID " + caId + " was not found in spite if just being retrieved.");
        }
        if (caInfo.getStatus() != CAConstants.CA_EXTERNAL && caInfo.getStatus() != CAConstants.CA_UNINITIALIZED
                && caInfo.getStatus() != CAConstants.CA_WAITING_CERTIFICATE_RESPONSE) {
            result.add(caInfo);
        }
    }
    return result;
}

From source file:org.cesecore.certificates.ca.CaSessionBean.java

@TransactionAttribute(TransactionAttributeType.SUPPORTS)
@Override//w  w w.ja va 2s  .  c  o m
public List<CAInfo> getAuthorizedAndNonExternalCaInfos(AuthenticationToken authenticationToken) {
    List<CAInfo> result = new ArrayList<CAInfo>();
    for (Integer caId : getAuthorizedCaIds(authenticationToken)) {
        CAInfo caInfo;
        try {
            caInfo = getCAInfoInternal(caId);
        } catch (CADoesntExistsException e) {
            throw new IllegalStateException(
                    "CA with ID " + caId + " was not found in spite if just being retrieved.");
        }
        if (caInfo.getStatus() != CAConstants.CA_EXTERNAL) {
            result.add(caInfo);
        }
    }
    return result;
}

From source file:org.cesecore.certificates.ca.CaSessionBean.java

@TransactionAttribute(TransactionAttributeType.SUPPORTS)
@Override/*from   w w  w.  j  a  v a2s  .co m*/
public void verifyExistenceOfCA(int caid) throws CADoesntExistsException {
    getCAInternal(caid, null, true);
}

From source file:org.cesecore.certificates.ca.CaSessionBean.java

@TransactionAttribute(TransactionAttributeType.SUPPORTS)
@Override/*from   ww  w.  ja  va2  s .c  o m*/
public HashMap<Integer, String> getCAIdToNameMap() {
    final HashMap<Integer, String> returnval = new HashMap<Integer, String>();
    for (final CAData cadata : CAData.findAll(entityManager)) {
        returnval.put(cadata.getCaId(), cadata.getName());
    }
    return returnval;
}

From source file:org.cesecore.certificates.certificate.CertificateStoreSessionBean.java

License:asdf

@Override
@TransactionAttribute(TransactionAttributeType.SUPPORTS)
public boolean isOnlyUsernameForSubjectKeyIdOrDnAndIssuerDN(final String issuerDN, final byte subjectKeyId[],
        final String subjectDN, final String username) {
    if (log.isTraceEnabled()) {
        log.trace(">isOnlyUsernameForSubjectKeyIdOrDnAndIssuerDN(), issuer='" + issuerDN + "'");
    }/* w  w w .j  a  v  a2 s.  c o  m*/
    // First make a DN in our well-known format
    final String transformedIssuerDN = CertTools.stringToBCDNString(StringTools.strip(issuerDN));
    final String sSubjectKeyId = new String(Base64.encode(subjectKeyId, false));
    final String transformedSubjectDN = CertTools.stringToBCDNString(StringTools.strip(subjectDN));
    if (log.isDebugEnabled()) {
        log.debug("Looking for user with a certificate with issuer DN(transformed) '" + transformedIssuerDN
                + "' and SubjectKeyId '" + sSubjectKeyId + "' OR subject DN(transformed) '"
                + transformedSubjectDN + "'.");
    }
    try {
        final Set<String> usernames = CertificateData.findUsernamesBySubjectKeyIdOrDnAndIssuer(entityManager,
                transformedIssuerDN, sSubjectKeyId, transformedSubjectDN);
        return usernames.size() == 0 || (usernames.size() == 1 && usernames.contains(username));
    } finally {
        if (log.isTraceEnabled()) {
            log.trace("<isOnlyUsernameForSubjectKeyIdOrDnAndIssuerDN(), issuer='" + issuerDN + "'");
        }
    }
}

From source file:org.cesecore.certificates.certificate.CertificateStoreSessionBean.java

License:asdf

@TransactionAttribute(TransactionAttributeType.SUPPORTS)
@Override/*from  w ww  .j  a  va 2 s . co  m*/
public Certificate findMostRecentlyUpdatedActiveCertificate(byte[] subjectKeyId) {
    Certificate certificate = null;
    final String subjectKeyIdString = new String(Base64.encode(subjectKeyId, false));
    log.debug("Searching for subjectKeyIdString " + subjectKeyIdString);
    final Query query = this.entityManager.createQuery(
            "SELECT a FROM CertificateData a WHERE a.subjectKeyId=:subjectKeyId AND a.status=:status ORDER BY a.updateTime DESC");
    query.setParameter("subjectKeyId", subjectKeyIdString);
    query.setParameter("status", CertificateConstants.CERT_ACTIVE);
    query.setMaxResults(1);
    @SuppressWarnings("unchecked")
    final List<CertificateData> resultList = query.getResultList();
    if (resultList.size() == 1) {
        certificate = resultList.get(0).getCertificate(this.entityManager);
        if (certificate == null && log.isDebugEnabled()) {
            log.debug("Reference to an issued certificate with subjectKeyId " + subjectKeyId
                    + " found, but the certificate is not stored in the database.");
        }
    }
    return certificate;
}

From source file:org.cesecore.certificates.certificate.CertificateStoreSessionBean.java

License:asdf

@Override
@TransactionAttribute(TransactionAttributeType.SUPPORTS)
public boolean isUniqueCertificateSerialNumberIndex() {
    // Must always run in a transaction in order to store certificates, EntityManager requires use within a transaction
    if (UniqueSernoHelper.getIsUniqueCertificateSerialNumberIndex() == null) {
        // Only create new transactions to store certificates and call this, if the variable is not initialized.
        // If it is already set we don't have to waste time creating a new transaction

        // Sets variables (but only once) that can be checked with isUniqueCertificateSerialNumberIndex().
        // This part must be called first (at least once).
        final String userName = "checkUniqueIndexTestUserNotToBeUsed_fjasdfjsdjfsad"; // This name should only be used for this test. Made complex so that no one else will use the same.
        // Loading two dummy certificates. These certificates has same serial number and issuer.
        // It should not be possible to store both of them in the DB.
        final X509Certificate cert1 = UniqueSernoHelper.getTestCertificate1();
        final X509Certificate cert2 = UniqueSernoHelper.getTestCertificate2();
        final Certificate c1 = findCertificateByFingerprint(CertTools.getFingerprintAsString(cert1));
        final Certificate c2 = findCertificateByFingerprint(CertTools.getFingerprintAsString(cert2));
        if ((c1 != null) && (c2 != null)) {
            // already proved that not checking index for serial number.
            UniqueSernoHelper.setIsUniqueCertificateSerialNumberIndex(Boolean.FALSE);
        }/*from  w  w  w  .  ja v a2 s.c  o  m*/
        final AuthenticationToken admin = new AlwaysAllowLocalAuthenticationToken(
                new UsernamePrincipal("Internal database constraint test"));
        if (c1 == null) {// storing initial certificate if no test certificate created.
            try {
                // needs to call using "certificateStoreSession." in order to honor the transaction annotations
                certificateStoreSession.checkForUniqueCertificateSerialNumberIndexInTransaction(admin, cert1,
                        userName, "abcdef0123456789", CertificateConstants.CERT_INACTIVE, 0, 0, "",
                        new Date().getTime());
            } catch (Throwable e) { // NOPMD, we really need to catch all, never crash
                throw new RuntimeException("It should always be possible to store initial dummy certificate.",
                        e);
            }
        }
        UniqueSernoHelper.setIsUniqueCertificateSerialNumberIndex(Boolean.FALSE);
        if (c2 == null) { // storing a second certificate with same issuer 
            try {
                // needs to call using "certificateStoreSession." in order to honor the transaction annotations
                certificateStoreSession.checkForUniqueCertificateSerialNumberIndexInTransaction(admin, cert2,
                        userName, "fedcba9876543210", CertificateConstants.CERT_INACTIVE, 0, 0, "",
                        new Date().getTime());
            } catch (Throwable e) { // NOPMD, we really need to catch all, never crash
                log.info(
                        "certificateStoreSession.checkForUniqueCertificateSerialNumberIndexInTransaction threw Throwable (normal if there is a unique issuerDN/serialNumber index): "
                                + e.getMessage());
                log.info("Unique index in CertificateData table for certificate serial number");
                // Exception is thrown when unique index is working and a certificate with same serial number is in the database.
                UniqueSernoHelper.setIsUniqueCertificateSerialNumberIndex(Boolean.TRUE);
            }
        }
        if (!UniqueSernoHelper.getIsUniqueCertificateSerialNumberIndex().booleanValue()) {
            // It was possible to store a second certificate with same serial number. Unique number not working.
            log.info(INTRES.getLocalizedMessage("createcert.not_unique_certserialnumberindex"));
        }
        // Remove potentially stored certificates so anyone can create the unique index if wanted
        try {
            certificateStoreSession.removeUniqueCertificateSerialNumberTestCertificates();
            log.info("Removed rows used during test for unique certificate serial number database constraint.");
        } catch (Throwable e) { // NOPMD, we really need to catch all, never crash
            log.debug("Unable to clean up database rows used during test for unique certificate serial number."
                    + " This is expected if DELETE is not granted to the EJBCA database user.", e);
        }
    }
    return UniqueSernoHelper.getIsUniqueCertificateSerialNumberIndex() != null
            && UniqueSernoHelper.getIsUniqueCertificateSerialNumberIndex().booleanValue();
}

From source file:org.cesecore.certificates.certificate.CertificateStoreSessionBean.java

License:asdf

@Override
@TransactionAttribute(TransactionAttributeType.SUPPORTS)
public void reloadCaCertificateCache() {
    log.info("Reloading CA certificate cache.");
    Collection<Certificate> certs = certificateStoreSession.findCertificatesByType(
            CertificateConstants.CERTTYPE_SUBCA + CertificateConstants.CERTTYPE_ROOTCA, null);
    CaCertificateCache.INSTANCE.loadCertificates(certs);
}