Bump versions to 0.14.2-SNAPSHOT
[aaa.git] / aaa-shiro / impl / src / main / java / org / opendaylight / aaa / shiro / tokenauthrealm / auth / ClaimBuilder.java
1 /*
2  * Copyright (c) 2014, 2017 Hewlett-Packard Development Company, L.P. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.aaa.shiro.tokenauthrealm.auth;
9
10 import com.google.common.base.Strings;
11 import com.google.common.collect.ImmutableSet;
12 import java.io.Serializable;
13 import java.util.LinkedHashSet;
14 import java.util.Objects;
15 import java.util.Set;
16 import org.opendaylight.aaa.api.Claim;
17
18 /**
19  * Builder for a {@link Claim}. The userId, user, and roles information is
20  * mandatory.
21  *
22  * @author liemmn
23  */
24 public class ClaimBuilder {
25     private String userId = "";
26     private String user = "";
27     private final Set<String> roles = new LinkedHashSet<>();
28     private String clientId = "";
29     private String domain = "";
30
31     public ClaimBuilder() {
32     }
33
34     public ClaimBuilder(Claim claim) {
35         clientId = claim.clientId();
36         userId = claim.userId();
37         user = claim.user();
38         domain = claim.domain();
39         roles.addAll(claim.roles());
40     }
41
42     public ClaimBuilder setClientId(String clientId) {
43         this.clientId = Strings.nullToEmpty(clientId).trim();
44         return this;
45     }
46
47     public ClaimBuilder setUserId(String userId) {
48         this.userId = Strings.nullToEmpty(userId).trim();
49         return this;
50     }
51
52     public ClaimBuilder setUser(String userName) {
53         user = Strings.nullToEmpty(userName).trim();
54         return this;
55     }
56
57     public ClaimBuilder setDomain(String domain) {
58         this.domain = Strings.nullToEmpty(domain).trim();
59         return this;
60     }
61
62     public ClaimBuilder addRoles(Set<String> theRoles) {
63         for (String role : theRoles) {
64             addRole(role);
65         }
66         return this;
67     }
68
69     public ClaimBuilder addRole(String role) {
70         roles.add(Strings.nullToEmpty(role).trim());
71         return this;
72     }
73
74     public Claim build() {
75         return new ImmutableClaim(this);
76     }
77
78     protected static class ImmutableClaim implements Claim, Serializable {
79         private static final long serialVersionUID = -8115027645190209129L;
80         private int hashCode = 0;
81         protected String clientId;
82         protected String userId;
83         protected String user;
84         protected String domain;
85         protected ImmutableSet<String> roles;
86
87         protected ImmutableClaim(ClaimBuilder base) {
88             clientId = base.clientId;
89             userId = base.userId;
90             user = base.user;
91             domain = base.domain;
92             roles = ImmutableSet.<String>builder().addAll(base.roles).build();
93
94             if (userId.isEmpty() || user.isEmpty() || roles.isEmpty() || roles.contains("")) {
95                 throw new IllegalStateException(
96                         "The Claim is missing one or more of the required fields.");
97             }
98         }
99
100         @Override
101         public String clientId() {
102             return clientId;
103         }
104
105         @Override
106         public String userId() {
107             return userId;
108         }
109
110         @Override
111         public String user() {
112             return user;
113         }
114
115         @Override
116         public String domain() {
117             return domain;
118         }
119
120         @Override
121         public Set<String> roles() {
122             return roles;
123         }
124
125         @Override
126         public boolean equals(Object object) {
127             if (this == object) {
128                 return true;
129             }
130             if (!(object instanceof Claim)) {
131                 return false;
132             }
133             Claim claim = (Claim) object;
134             return Objects.equals(roles, claim.roles())
135                     && Objects.equals(domain, claim.domain())
136                     && Objects.equals(userId, claim.userId())
137                     && Objects.equals(user, claim.user())
138                     && Objects.equals(clientId, claim.clientId());
139         }
140
141         @Override
142         public int hashCode() {
143             if (hashCode == 0) {
144                 hashCode = Objects.hash(clientId, userId, user, domain, roles);
145             }
146             return hashCode;
147         }
148
149         @Override
150         public String toString() {
151             return "clientId:" + clientId + "," + "userId:" + userId + "," + "userName:" + user
152                     + "," + "domain:" + domain + "," + "roles:" + roles;
153         }
154     }
155 }