View Javadoc

1   /* This file is part of COPAL (COntext Provisioning for All).
2    *
3    * COPAL is a part of SM4All (Smart hoMes for All) project.
4    *
5    * COPAL is free software: you can redistribute it and/or modify
6    * it under the terms of the GNU Lesser General Public License as published by
7    * the Free Software Foundation, either version 3 of the License, or
8    * (at your option) any later version.
9    *
10   * COPAL is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   * GNU Lesser General Public License for more details.
14   *
15   * You should have received a copy of the GNU Lesser General Public License
16   * along with COPAL. If not, see <http://www.gnu.org/licenses/>.
17   */
18  package at.ac.tuwien.infosys.sm4all.copal.api.security;
19  
20  import java.util.Arrays;
21  import at.ac.tuwien.infosys.sm4all.copal.api.event.ContextEvent;
22  import at.ac.tuwien.infosys.sm4all.copal.api.util.Attribute;
23  
24  /**
25   * Representation of a single authorization request of a {@link ContextEvent}.
26   * 
27   * @author sanjin
28   */
29  public class Authorization {
30  
31      private final String method;
32      private final Attribute[] attributes;
33  
34      /**
35       * Create instance of {@link Authorization} with specified name of
36       * {@link AuthorizationMethod} that should handle this {@link Authorization}
37       * and specified {@link Attribute}s to authorize.
38       * 
39       * @param method the name of {@link AuthorizationMethod} that should handle
40       *        this {@link Authorization}
41       * @param attributes the {@link Attribute}s for {@link AuthorizationMethod}.
42       * @throws NullPointerException if specified name of
43       *         {@link AuthorizationMethod} or {@link Attribute}s is
44       *         <code>null</code>.
45       * @throws IllegalArgumentException if specified name of
46       *         {@link AuthorizationMethod} is an empty or blank string or if
47       *         specified array of {@link Attribute}s is empty.
48       */
49      public Authorization(final String method, final Attribute... attributes) {
50          super();
51  
52          if (null == method) {
53              throw new NullPointerException("Method cannot be null.");
54          }
55          if (method.trim().isEmpty()) {
56              throw new IllegalArgumentException(
57                      "Method cannot be an empty or blank string.");
58          }
59          if (null == attributes) {
60              throw new NullPointerException("Attributes cannot be null.");
61          }
62          if (0 == attributes.length) {
63              throw new IllegalArgumentException("Attributes cannot be empty.");
64          }
65  
66          for (final Attribute attribute : attributes) {
67              if (null == attribute) {
68                  throw new NullPointerException("Attribute cannot be null.");
69              }
70          }
71  
72          this.method = method;
73          this.attributes = Arrays.copyOf(attributes, attributes.length);
74      }
75  
76      /**
77       * Returns the name of {@link AuthorizationMethod} that should handle this
78       * {@link Authorization}.
79       * 
80       * @return the name of {@link AuthorizationMethod} that should handle this
81       *         {@link Authorization}.
82       */
83      public String getMethod() {
84          return this.method;
85      }
86  
87      /**
88       * Returns the additional {@link Attribute}s for {@link AuthorizationMethod}
89       * .
90       * 
91       * @return the additional {@link Attribute}s for {@link AuthorizationMethod}
92       *         .
93       */
94      public Attribute[] getAttributes() {
95          return Arrays.copyOf(this.attributes, this.attributes.length);
96      }
97  
98      /**
99       * Returns hash code for this {@link Authorization}. The hash code for an
100      * {@link Authorization} object is hash code of its {@link #getMethod()}.
101      * 
102      * @return a hash code value for this {@link Authorization}.
103      */
104     @Override
105     public int hashCode() {
106         return this.method.hashCode();
107     }
108 
109     /**
110      * Compares this {@link Authorization} to the specified {@link Object}. The
111      * result is <code>true</code> if and only if the argument is not
112      * <code>null</code> and is an {@link Authorization} object that has same
113      * name of the {@link AuthorizationMethod} as this {@link Authorization}.
114      * 
115      * @param obj the {@link Object} to compare this {@link Authorization}
116      *        against.
117      * @return <code>true</code> if {@link Authorization}s are equal;
118      *         <code>false</code> otherwise.
119      */
120     @Override
121     public boolean equals(final Object obj) {
122         boolean result = false;
123 
124         if (null != obj) {
125             if (this == obj) {
126                 result = true;
127             } else if (obj instanceof Authorization) {
128                 final Authorization other = (Authorization) obj;
129 
130                 result = this.method.equals(other.method);
131             }
132         }
133 
134         return result;
135     }
136 }