View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.syncope.common.rest.api.beans;
20  
21  import com.fasterxml.jackson.annotation.JsonIgnore;
22  import java.io.Serializable;
23  import java.util.Collection;
24  import java.util.HashSet;
25  import java.util.List;
26  import java.util.Set;
27  import org.apache.commons.lang3.StringUtils;
28  import org.apache.commons.lang3.builder.EqualsBuilder;
29  import org.apache.commons.lang3.builder.HashCodeBuilder;
30  
31  public class ComplianceQuery implements Serializable {
32  
33      private static final long serialVersionUID = -7324275079761880426L;
34  
35      public static class Builder {
36  
37          private final ComplianceQuery instance = new ComplianceQuery();
38  
39          public Builder username(final String username) {
40              instance.setUsername(username);
41              return this;
42          }
43  
44          public Builder password(final String password) {
45              instance.setPassword(password);
46              return this;
47          }
48  
49          public Builder realm(final String realm) {
50              instance.setRealm(realm);
51              return this;
52          }
53  
54          public ComplianceQuery build() {
55              return instance;
56          }
57  
58          public Builder resource(final String resource) {
59              if (resource != null) {
60                  instance.getResources().add(resource);
61              }
62              return this;
63          }
64  
65          public Builder resources(final String... resources) {
66              instance.getResources().addAll(List.of(resources));
67              return this;
68          }
69  
70          public Builder resources(final Collection<String> resources) {
71              if (resources != null) {
72                  instance.getResources().addAll(resources);
73              }
74              return this;
75          }
76      }
77  
78      private String username;
79  
80      private String password;
81  
82      private String realm;
83  
84      private Set<String> resources = new HashSet<>();
85  
86      public String getUsername() {
87          return username;
88      }
89  
90      public void setUsername(final String username) {
91          this.username = username;
92      }
93  
94      public String getPassword() {
95          return password;
96      }
97  
98      public void setPassword(final String password) {
99          this.password = password;
100     }
101 
102     public String getRealm() {
103         return realm;
104     }
105 
106     public void setRealm(final String realm) {
107         this.realm = realm;
108     }
109 
110     public Set<String> getResources() {
111         return resources;
112     }
113 
114     public void setResources(final Set<String> resources) {
115         this.resources = resources;
116     }
117 
118     @JsonIgnore
119     public boolean isEmpty() {
120         if (StringUtils.isBlank(username) && StringUtils.isBlank(password)) {
121             return true;
122         }
123         return StringUtils.isEmpty(realm) && resources.isEmpty();
124     }
125 
126     @Override
127     public boolean equals(final Object obj) {
128         if (this == obj) {
129             return true;
130         }
131         if (obj == null) {
132             return false;
133         }
134         if (getClass() != obj.getClass()) {
135             return false;
136         }
137         ComplianceQuery other = (ComplianceQuery) obj;
138         return new EqualsBuilder().
139                 append(username, other.username).
140                 append(password, other.password).
141                 append(realm, other.realm).
142                 append(resources, other.resources).
143                 build();
144     }
145 
146     @Override
147     public int hashCode() {
148         return new HashCodeBuilder().
149                 append(username).
150                 append(password).
151                 append(realm).
152                 append(resources).
153                 build();
154     }
155 }