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.lib.types;
20  
21  import com.fasterxml.jackson.annotation.JsonCreator;
22  import com.fasterxml.jackson.annotation.JsonProperty;
23  import java.util.Map;
24  import java.util.Optional;
25  import org.apache.commons.lang3.StringUtils;
26  import org.apache.commons.lang3.builder.EqualsBuilder;
27  import org.apache.commons.lang3.builder.HashCodeBuilder;
28  import org.apache.commons.lang3.tuple.Pair;
29  import org.apache.syncope.common.lib.BaseBean;
30  import org.apache.syncope.common.lib.audit.EventCategory;
31  import org.apache.syncope.common.lib.types.AuditElements.EventCategoryType;
32  import org.apache.syncope.common.lib.types.AuditElements.Result;
33  
34  public class AuditLoggerName implements BaseBean {
35  
36      private static final long serialVersionUID = -647989486671786839L;
37  
38      public static final String AUDIT_PREFIX = "syncope.audit";
39  
40      public static String getAuditLoggerName(final String domain) {
41          return AUDIT_PREFIX + '.' + domain;
42      }
43  
44      public static String getAuditEventLoggerName(final String domain, final String loggerName) {
45          return domain + '.' + loggerName;
46      }
47  
48      public static AuditLoggerName fromAuditKey(final String key) {
49          if (StringUtils.isBlank(key)) {
50              throw new IllegalArgumentException("Null value not permitted");
51          }
52  
53          if (!key.startsWith(AUDIT_PREFIX)) {
54              throw new IllegalArgumentException("Audit logger name must start with " + AUDIT_PREFIX);
55          }
56  
57          Map.Entry<EventCategory, Result> eventCategory = parseEventCategory(key.replace(AUDIT_PREFIX + '.', ""));
58  
59          return new AuditLoggerName(
60                  eventCategory.getKey().getType(),
61                  eventCategory.getKey().getCategory(),
62                  eventCategory.getKey().getSubcategory(),
63                  eventCategory.getKey().getEvents().isEmpty()
64                  ? StringUtils.EMPTY : eventCategory.getKey().getEvents().iterator().next(),
65                  eventCategory.getValue());
66      }
67  
68      public static Pair<EventCategory, Result> parseEventCategory(final String event) {
69          EventCategory eventCategory = new EventCategory();
70  
71          Result condition = null;
72  
73          if (StringUtils.isNotEmpty(event)) {
74              String[] elements = event.substring(1, event.length() - 1).split("\\]:\\[");
75  
76              if (elements.length == 1) {
77                  eventCategory.setType(EventCategoryType.CUSTOM);
78                  condition = Result.SUCCESS;
79                  eventCategory.getEvents().add(event);
80              } else {
81                  eventCategory.setType(EventCategoryType.valueOf(elements[0]));
82  
83                  eventCategory.setCategory(StringUtils.isNotEmpty(elements[1]) ? elements[1] : null);
84  
85                  eventCategory.setSubcategory(StringUtils.isNotEmpty(elements[2]) ? elements[2] : null);
86  
87                  if (elements.length > 3 && StringUtils.isNotEmpty(elements[3])) {
88                      eventCategory.getEvents().add(elements[3]);
89                  }
90  
91                  if (elements.length > 4) {
92                      condition = Result.valueOf(elements[4].toUpperCase());
93                  }
94              }
95          }
96  
97          return Pair.of(eventCategory, condition);
98      }
99  
100     /**
101      * Build event string with the following syntax [type]:[category]:[subcategory]:[event]:[maybe result value cond].
102      *
103      * @param type event type.
104      * @param category event category.
105      * @param subcategory event subcategory.
106      * @param event event.
107      * @param result result value condition.
108      * @return event string.
109      */
110     public static String buildEvent(
111             final AuditElements.EventCategoryType type,
112             final String category,
113             final String subcategory,
114             final String event,
115             final AuditElements.Result result) {
116 
117         StringBuilder eventBuilder = new StringBuilder();
118 
119         eventBuilder.append('[');
120         if (type != null) {
121             eventBuilder.append(type.name());
122         }
123         eventBuilder.append("]:[");
124         if (StringUtils.isNotBlank(category)) {
125             eventBuilder.append(category);
126         }
127         eventBuilder.append("]:[");
128         if (StringUtils.isNotBlank(subcategory)) {
129             eventBuilder.append(subcategory);
130         }
131         eventBuilder.append("]:[");
132         if (StringUtils.isNotBlank(event)) {
133             eventBuilder.append(event);
134         }
135         eventBuilder.append(']');
136 
137         if (result != null) {
138             eventBuilder.append(":[").
139                     append(result).
140                     append(']');
141         }
142 
143         return eventBuilder.toString();
144     }
145 
146     private final EventCategoryType type;
147 
148     private final String category;
149 
150     private final String subcategory;
151 
152     private final String event;
153 
154     private final Result result;
155 
156     @JsonCreator
157     public AuditLoggerName(
158             @JsonProperty("type") final AuditElements.EventCategoryType type,
159             @JsonProperty("category") final String category,
160             @JsonProperty("subcategory") final String subcategory,
161             @JsonProperty("event") final String event,
162             @JsonProperty("result") final Result result) {
163 
164         super();
165 
166         this.type = Optional.ofNullable(type).orElse(EventCategoryType.CUSTOM);
167         this.category = category;
168         this.subcategory = subcategory;
169         this.event = event;
170         this.result = Optional.ofNullable(result).orElse(Result.SUCCESS);
171     }
172 
173     public AuditElements.EventCategoryType getType() {
174         return type;
175     }
176 
177     public String getEvent() {
178         return event;
179     }
180 
181     public String getCategory() {
182         return category;
183     }
184 
185     public Result getResult() {
186         return result;
187     }
188 
189     public String getSubcategory() {
190         return subcategory;
191     }
192 
193     @Override
194     public int hashCode() {
195         return new HashCodeBuilder().
196                 append(type).
197                 append(category).
198                 append(subcategory).
199                 append(event).
200                 append(result).
201                 build();
202     }
203 
204     @Override
205     public boolean equals(final Object obj) {
206         if (this == obj) {
207             return true;
208         }
209         if (obj == null) {
210             return false;
211         }
212         if (getClass() != obj.getClass()) {
213             return false;
214         }
215         final AuditLoggerName other = (AuditLoggerName) obj;
216         return new EqualsBuilder().
217                 append(type, other.type).
218                 append(category, other.category).
219                 append(subcategory, other.subcategory).
220                 append(event, other.event).
221                 append(result, other.result).
222                 build();
223     }
224 
225     @Override
226     public String toString() {
227         return buildEvent(type, category, subcategory, event, result);
228     }
229 
230     public String toAuditKey() {
231         return AUDIT_PREFIX + "." + toString();
232     }
233 }