Coverage Report - org.openpermis.builder.AuthorizationServiceBuilder
 
Classes in this File Line Coverage Branch Coverage Complexity
AuthorizationServiceBuilder
100%
32/32
93%
15/16
2.3
 
 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.builder;
 11  
 
 12  
 import java.util.ArrayList;
 13  
 import java.util.List;
 14  
 
 15  
 import org.openpermis.AuthorizationService;
 16  
 import org.openpermis.PolicyDecisionPoint;
 17  
 import org.openpermis.audit.AuditPolicyDecisionPoint;
 18  
 import org.openpermis.audit.VetoableAccessDecisionListener;
 19  
 import org.openpermis.basic.BasicAuthorizationService;
 20  
 import org.openpermis.basic.Clock;
 21  
 import org.openpermis.policy.PartFactory;
 22  
 import org.openpermis.policy.bean.basic.BasicPartBeanFactory;
 23  
 import org.openpermis.repository.SubjectRepository;
 24  
 import org.openpermis.repository.basic.InternalSubjectRepository;
 25  
 
 26  
 
 27  
 /**
 28  
  * Builds an authorization service.
 29  
  * <p>The builder chooses sensible defaults for parts that an application does not
 30  
  * explicitly configure.
 31  
  * @since 0.1.0
 32  
  */
 33  
 public class AuthorizationServiceBuilder {
 34  
 
 35  
         //---- State
 36  
         
 37  
         /**
 38  
          * @since 0.1.0
 39  
          */
 40  
         private PartFactory partFactory;
 41  
         
 42  
         /**
 43  
          * @since 0.1.0
 44  
          */
 45  
         private PolicyDecisionPoint policyDecisionPoint;
 46  
         
 47  
         /**
 48  
          * @since 0.1.0
 49  
          */
 50  
         private SubjectRepository subjectRepository;
 51  
 
 52  
         /**
 53  
          * @since 0.1.0
 54  
          */
 55  
         private Clock clock;
 56  
         
 57  
         /**
 58  
          * @since 0.3.0
 59  
          */
 60  
         private final List<VetoableAccessDecisionListener> listeners;
 61  
         
 62  
         //---- Constructors
 63  
         
 64  
         /**
 65  
          * Creates a new authorization service builder.
 66  
          * @since 0.3.0
 67  
          */
 68  7
         public AuthorizationServiceBuilder () {
 69  7
                 this.listeners = new ArrayList<VetoableAccessDecisionListener>();
 70  7
         }
 71  
         
 72  
         //---- Methods
 73  
         
 74  
         /**
 75  
          * Adds a vetoable access decision listener that will be used by the created PDP.
 76  
          * @param listener the listener to add.
 77  
          * @return this builder.
 78  
          * @since 0.3.0
 79  
          */
 80  
         public AuthorizationServiceBuilder withListener (VetoableAccessDecisionListener listener) {
 81  1
                 this.listeners.add(listener);
 82  1
                 return this;
 83  
         }
 84  
         
 85  
         /**
 86  
          * Configures the subject repository from which the created PDP will retrieve subjects.
 87  
          * @param repository a {@link SubjectRepository}.
 88  
          * @return this builder.
 89  
          * @since 0.1.0
 90  
          */
 91  
         public AuthorizationServiceBuilder withSubjectsFrom (SubjectRepository repository) {
 92  2
                 if (repository == null) {
 93  1
                         throw new IllegalArgumentException("Repository must not be null.");
 94  
                 }
 95  1
                 this.subjectRepository = repository;
 96  1
                 return this;
 97  
         }
 98  
         
 99  
         /**
 100  
          * Configures the clock service that the created PDP will use to get time values.
 101  
          * @param clockService a {@link Clock} service.
 102  
          * @since 0.1.0
 103  
          */
 104  
         public AuthorizationServiceBuilder withClock (Clock clockService) {
 105  2
                 if (clockService == null) {
 106  1
                         throw new IllegalArgumentException("Clock must not be null.");
 107  
                 }
 108  1
                 this.clock = clockService;
 109  1
                 return this;
 110  
         }
 111  
 
 112  
         /**
 113  
          * Configures the policy that the created PDP will use for access decisions.
 114  
          * @param accessPolicyDecisionPoint the {@link PolicyDecisionPoint} to use for access decisions.
 115  
          * @return this builder.
 116  
          * @since 0.3.0
 117  
          */
 118  
         public AuthorizationServiceBuilder forPolicyDecisionPoint (
 119  
                 PolicyDecisionPoint accessPolicyDecisionPoint
 120  
         ) {
 121  2
                 if (accessPolicyDecisionPoint == null) {
 122  1
                         throw new IllegalArgumentException("Access policy decision point must not be null.");
 123  
                 }
 124  1
                 this.policyDecisionPoint = accessPolicyDecisionPoint;
 125  1
                 return this;
 126  
         }
 127  
 
 128  
         /**
 129  
          * Returns an authorization service that matches the builder's configuration.
 130  
          * @return a new {@link AuthorizationService}.
 131  
          * @since 0.1.0
 132  
          */
 133  
         public AuthorizationService build () {
 134  4
                 return new BasicAuthorizationService(
 135  
                         getPolicyDecisionPoint(), getSubjectRepository(), getClock()
 136  
                 );
 137  
         }
 138  
 
 139  
         /**
 140  
          * @since 0.1.0
 141  
          * @since 0.3.0 Added support for {@link VetoableAccessDecisionListener}. 
 142  
          */
 143  
         private PolicyDecisionPoint getPolicyDecisionPoint () {
 144  4
                 if (this.policyDecisionPoint == null) {
 145  3
                         this.policyDecisionPoint = getPartFactory().createPolicy();
 146  
                 }
 147  4
                 if (this.listeners.isEmpty()) {
 148  3
                         return this.policyDecisionPoint;
 149  
                 }
 150  1
                 return new AuditPolicyDecisionPoint(this.policyDecisionPoint, this.listeners);
 151  
         }
 152  
 
 153  
         /**
 154  
          * @since 0.1.0
 155  
          */
 156  
         private PartFactory getPartFactory () {
 157  3
                 if (this.partFactory == null) {
 158  3
                         this.partFactory = new BasicPartBeanFactory();
 159  
                 }
 160  3
                 return this.partFactory;
 161  
         }
 162  
 
 163  
         /**
 164  
          * @since 0.1.0
 165  
          */
 166  
         private SubjectRepository getSubjectRepository () {
 167  4
                 if (this.subjectRepository == null) {
 168  3
                         this.subjectRepository = new InternalSubjectRepository();
 169  
                 }
 170  4
                 return this.subjectRepository;
 171  
         }
 172  
 
 173  
         /**
 174  
          * @since 0.1.0
 175  
          */
 176  
         private Clock getClock () {
 177  4
                 if (this.clock == null) {
 178  3
                         return SystemClock.INSTANCE;
 179  
                 }
 180  1
                 return this.clock;
 181  
         }
 182  
 
 183  
 }