Coverage Report - org.openpermis.repository.basic.UrlSubjectRepository
 
Classes in this File Line Coverage Branch Coverage Complexity
UrlSubjectRepository
41%
20/48
34%
9/26
7.75
 
 1  
 /*
 2  
  * Copyright (c) 2009, Swiss Federal Department of Defence Civil Protection and Sport
 3  
  *                     (http://www.vbs.admin.ch)
 4  
  * Copyright (c) 2009, Ergon Informatik AG (http://www.ergon.ch)
 5  
  * All rights reserved.
 6  
  *
 7  
  * Licensed under the Open Permis License which accompanies this distribution,
 8  
  * and is available at http://www.openpermis.org/BSDlicenceKent.txt
 9  
  */
 10  
 package org.openpermis.repository.basic;
 11  
 
 12  
 import java.io.BufferedInputStream;
 13  
 import java.io.IOException;
 14  
 import java.io.InputStream;
 15  
 import java.net.URL;
 16  
 import java.security.NoSuchAlgorithmException;
 17  
 import java.security.NoSuchProviderException;
 18  
 
 19  
 import org.slf4j.Logger;
 20  
 import org.slf4j.LoggerFactory;
 21  
 
 22  
 import org.openpermis.cert.CertificateVerifier;
 23  
 import org.openpermis.repository.SubjectRepositoryException;
 24  
 
 25  
 
 26  
 /**
 27  
  * A subject repository containing certificates from a list of {@link URL}s.
 28  
  * @since 0.3.0
 29  
  */
 30  
 public class UrlSubjectRepository
 31  
         extends StreamSubjectRepository
 32  
 {
 33  
         
 34  
         //---- Static
 35  
 
 36  
         /**
 37  
          * The logger object of this class.
 38  
          * @since 0.3.0
 39  
          */
 40  1
         private static final Logger LOGGER =
 41  
                 LoggerFactory.getLogger(UrlSubjectRepository.class);
 42  
         
 43  
         /**
 44  
          * Creates a subject repository for a list of attribute certificates loaded as resources
 45  
          * from a class loader.
 46  
          * <p>Uses the {@link Thread#getContextClassLoader()} as a fallback if a resource cannot
 47  
          * be retrieved using the specified class loader.</p>
 48  
          * @param certificateVerifier the certificate verifier user to verify the the attribute
 49  
          * certificates, must not be {@code null}.
 50  
          * @param classLoader the class loader to use for retrieving the resource URLs.
 51  
          * @param resources list of resource locations to process, must not be {@code null} or empty.
 52  
          * @return the subject repository created.
 53  
          * @throws SubjectRepositoryException if the repository cannot be built or if there
 54  
          * is a problem retrieving a resource.
 55  
          * @see ClassLoader#getResource(String)
 56  
          * @since 0.3.0
 57  
          */
 58  
         public static UrlSubjectRepository createClasspathSubjectRepository (
 59  
                 CertificateVerifier certificateVerifier, ClassLoader classLoader, String... resources
 60  
         ) throws SubjectRepositoryException {
 61  0
                 if (resources == null || resources.length == 0) {
 62  0
                         throw new IllegalArgumentException("Resources must not be [null] or emptry.");
 63  
                 }
 64  0
                 final URL[] urls = new URL[resources.length];
 65  0
                 final ClassLoader fallbackClassLoader = Thread.currentThread().getContextClassLoader();
 66  0
                 for (int i = 0 ; i < resources.length ; i++) {
 67  0
                         if (resources[i] == null) {
 68  0
                                 throw new IllegalArgumentException("Resource must not be [null].");
 69  
                         }
 70  0
                         URL url = classLoader.getResource(resources[i]);
 71  0
                         if (url == null) {
 72  0
                                 fallbackClassLoader.getResource(resources[i]);
 73  
                         }
 74  0
                         if (url == null) {
 75  0
                                 throw new SubjectRepositoryException(
 76  
                                         "Failed to retrieve URL for resource [" + resources[i] + "]."
 77  
                                 );
 78  
                         }
 79  0
                         urls[i] = url;
 80  
                 }
 81  0
                 return new UrlSubjectRepository(certificateVerifier, urls);
 82  
         }
 83  
         
 84  
         /**
 85  
          * Creates a subject repository for a list of attribute certificates loaded as resources
 86  
          * from a class loader.
 87  
          * @param certificateVerifier the certificate verifier user to verify the the attribute
 88  
          * certificates, must not be {@code null}.
 89  
          * @param clazz the class to use for retrieving the resource URLs.
 90  
          * @param resources list of resource locations to process, must not be {@code null} or empty.
 91  
          * @return the subject repository created.
 92  
          * @throws SubjectRepositoryException if the repository cannot be built or if there
 93  
          * is a problem retrieving a resource.
 94  
          * @see Class#getResource(String)
 95  
          * @since 0.3.0
 96  
          */
 97  
         public static UrlSubjectRepository createClasspathSubjectRepository (
 98  
                 CertificateVerifier certificateVerifier, Class<?> clazz, String... resources
 99  
         ) throws SubjectRepositoryException {
 100  3
                 if (resources == null || resources.length == 0) {
 101  0
                         throw new IllegalArgumentException("Resources must not be [null] or emptry.");
 102  
                 }
 103  3
                 final URL[] urls = new URL[resources.length];
 104  9
                 for (int i = 0 ; i < resources.length ; i++) {
 105  6
                         if (resources[i] == null) {
 106  0
                                 throw new IllegalArgumentException("Resource must not be [null].");
 107  
                         }
 108  6
                         final URL url = clazz.getResource(resources[i]);
 109  6
                         if (url == null) {
 110  0
                                 throw new SubjectRepositoryException(
 111  
                                         "Failed to retrieve URL for resource [" + resources[i] + "]."
 112  
                                 );
 113  
                         }
 114  6
                         urls[i] = url;
 115  
                 }
 116  3
                 return new UrlSubjectRepository(certificateVerifier, urls);
 117  
         }
 118  
         
 119  
         //---- Constructors
 120  
         
 121  
         /**
 122  
          * Creates a file directory subject repository from directory and the specified certificate
 123  
          * verifier.
 124  
          * @param certificateVerifier the certificate verifier user to verify the the attribute
 125  
          * certificates, must not be {@code null}.
 126  
          * @param urls a list of URLs pointing to attribute certificates to be added,
 127  
          * must not be {@code null}.
 128  
          * @throws SubjectRepositoryException if the repository cannot be built.
 129  
          * @since 0.3.0
 130  
          */
 131  
         public UrlSubjectRepository (CertificateVerifier certificateVerifier, URL... urls) 
 132  
                 throws SubjectRepositoryException 
 133  
         {
 134  3
                 super(certificateVerifier);
 135  9
                 for (URL url : urls) {
 136  6
                         if (url == null) {
 137  0
                                 throw new IllegalArgumentException("URL must not be [null].");
 138  
                         }
 139  
                         try {
 140  6
                                 addAttributeCertificate(url);
 141  0
                         } catch (NoSuchAlgorithmException e) {
 142  0
                                 throw new SubjectRepositoryException(
 143  
                                         "Cannot decode attribute certificate because a " +
 144  
                                         "crypto algorithm is not available from the crypto provider.",
 145  
                                         e
 146  
                                 );
 147  0
                         } catch (NoSuchProviderException e) {
 148  0
                                 throw new SubjectRepositoryException(
 149  
                                         "Cannot decode attribute certificate because " +
 150  
                                         "there is no default crypto provider.",
 151  
                                         e
 152  
                                 );
 153  0
                         } catch (IOException e) {
 154  0
                                 throw new SubjectRepositoryException(
 155  
                                         "Cannot read attribute certificates from URL [" + url + "].",
 156  
                                         e
 157  
                                 );
 158  6
                         }
 159  
                 }
 160  3
         }
 161  
         
 162  
         //---- Methods
 163  
         
 164  
         /**
 165  
          * Adds an attribute certificate for the specified file.
 166  
          * @param url the URL that points to a resource that contains the DER encoded attribute
 167  
          * certificate, must not be {@code null}.
 168  
          * @throws NoSuchAlgorithmException passed on.
 169  
          * @throws NoSuchProviderException passed on.
 170  
          * @throws IOException if the URL stream cannot be opened or 
 171  
          * {@link #addAttributeCertificate(InputStream)} reports an error.
 172  
          * @see #addAttributeCertificate(InputStream)
 173  
          * @since 0.3.0
 174  
          */
 175  
         private void addAttributeCertificate (
 176  
                 URL url
 177  
         ) throws NoSuchAlgorithmException, NoSuchProviderException, IOException {
 178  6
                 final InputStream is = new BufferedInputStream(url.openStream());
 179  
                 try {
 180  6
                         addAttributeCertificate(is);
 181  
                 } finally {
 182  0
                         try {
 183  6
                                 is.close();
 184  0
                         } catch (IOException e) {
 185  0
                                 LOGGER.warn("Failed to close input stream of URL [" + url + "].", e);
 186  6
                         }
 187  0
                 }
 188  6
         }
 189  
                 
 190  
 }