[SYNCOPE-1262] Last matrix parameter still there
[syncope.git] / common / lib / src / main / java / org / apache / syncope / common / lib / types / AuditLoggerName.java
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.text.ParseException;
24 import java.util.Map;
25 import org.apache.commons.lang3.StringUtils;
26 import org.apache.commons.lang3.tuple.Pair;
27 import org.apache.syncope.common.lib.AbstractBaseBean;
28 import org.apache.syncope.common.lib.log.EventCategoryTO;
29 import org.apache.syncope.common.lib.types.AuditElements.EventCategoryType;
30 import org.apache.syncope.common.lib.types.AuditElements.Result;
31
32 public class AuditLoggerName extends AbstractBaseBean {
33
34 private static final long serialVersionUID = -647989486671786839L;
35
36 public static String getAuditLoggerName(final String domain) {
37 return LoggerType.AUDIT.getPrefix() + "." + domain;
38 }
39
40 public static String getAuditEventLoggerName(final String domain, final String loggerName) {
41 return domain + "." + loggerName;
42 }
43
44 private final AuditElements.EventCategoryType type;
45
46 private final String category;
47
48 private final String subcategory;
49
50 private final String event;
51
52 private final Result result;
53
54 @JsonCreator
55 public AuditLoggerName(
56 @JsonProperty("type") final AuditElements.EventCategoryType type,
57 @JsonProperty("category") final String category,
58 @JsonProperty("subcategory") final String subcategory,
59 @JsonProperty("event") final String event,
60 @JsonProperty("result") final Result result) {
61
62 super();
63
64 this.type = type == null ? AuditElements.EventCategoryType.CUSTOM : type;
65 this.category = category;
66 this.subcategory = subcategory;
67 this.event = event;
68 this.result = result == null ? Result.SUCCESS : result;
69 }
70
71 public AuditElements.EventCategoryType getType() {
72 return type;
73 }
74
75 public String getEvent() {
76 return event;
77 }
78
79 public String getCategory() {
80 return category;
81 }
82
83 public Result getResult() {
84 return result;
85 }
86
87 public String getSubcategory() {
88 return subcategory;
89 }
90
91 public String toLoggerName() {
92 return new StringBuilder().append(LoggerType.AUDIT.getPrefix()).append('.').
93 append(buildEvent(type, category, subcategory, event, result)).toString();
94 }
95
96 public static AuditLoggerName fromLoggerName(final String loggerName)
97 throws ParseException {
98
99 if (StringUtils.isBlank(loggerName)) {
100 throw new IllegalArgumentException("Null value not permitted");
101 }
102
103 if (!loggerName.startsWith(LoggerType.AUDIT.getPrefix())) {
104 throw new ParseException("Audit logger name must start with " + LoggerType.AUDIT.getPrefix(), 0);
105 }
106
107 final Map.Entry<EventCategoryTO, Result> eventCategory = parseEventCategory(
108 loggerName.replaceAll(LoggerType.AUDIT.getPrefix() + ".", ""));
109
110 return new AuditLoggerName(
111 eventCategory.getKey().getType(),
112 eventCategory.getKey().getCategory(),
113 eventCategory.getKey().getSubcategory(),
114 eventCategory.getKey().getEvents().isEmpty()
115 ? StringUtils.EMPTY : eventCategory.getKey().getEvents().iterator().next(),
116 eventCategory.getValue());
117 }
118
119 public static Pair<EventCategoryTO, Result> parseEventCategory(final String event) {
120 EventCategoryTO eventCategoryTO = new EventCategoryTO();
121
122 Result condition = null;
123
124 if (StringUtils.isNotEmpty(event)) {
125 final String[] elements = event.substring(1, event.length() - 1).split("\\]:\\[");
126
127 if (elements.length == 1) {
128 eventCategoryTO.setType(EventCategoryType.CUSTOM);
129 condition = Result.SUCCESS;
130 eventCategoryTO.getEvents().add(event);
131 } else {
132 EventCategoryType type;
133
134 if (EventCategoryType.PROPAGATION.toString().equals(elements[0])) {
135 type = EventCategoryType.PROPAGATION;
136 } else if (EventCategoryType.PULL.toString().equals(elements[0])) {
137 type = EventCategoryType.PULL;
138 } else if (EventCategoryType.PUSH.toString().equals(elements[0])) {
139 type = EventCategoryType.PUSH;
140 } else {
141 try {
142 type = EventCategoryType.valueOf(elements[0]);
143 } catch (Exception e) {
144 type = EventCategoryType.CUSTOM;
145 }
146 }
147
148 eventCategoryTO.setType(type);
149
150 eventCategoryTO.setCategory(StringUtils.isNotEmpty(elements[1]) ? elements[1] : null);
151
152 eventCategoryTO.setSubcategory(StringUtils.isNotEmpty(elements[2]) ? elements[2] : null);
153
154 if (elements.length > 3 && StringUtils.isNotEmpty(elements[3])) {
155 eventCategoryTO.getEvents().add(elements[3]);
156 }
157
158 if (elements.length > 4) {
159 condition = Result.valueOf(elements[4].toUpperCase());
160 }
161 }
162 }
163
164 return Pair.of(eventCategoryTO, condition);
165 }
166
167 /**
168 * Build event string with the following syntax [type]:[category]:[subcategory]:[event]:[maybe result value cond].
169 *
170 * @param type event type.
171 * @param category event category.
172 * @param subcategory event subcategory.
173 * @param event event.
174 * @param condition result value condition.
175 * @return event string.
176 */
177 public static String buildEvent(
178 final AuditElements.EventCategoryType type,
179 final String category,
180 final String subcategory,
181 final String event,
182 final AuditElements.Result condition) {
183
184 final StringBuilder eventBuilder = new StringBuilder();
185
186 eventBuilder.append('[');
187 if (type != null) {
188 eventBuilder.append(type.toString());
189 }
190 eventBuilder.append("]:[");
191 if (StringUtils.isNotBlank(category)) {
192 eventBuilder.append(category);
193 }
194 eventBuilder.append("]:[");
195 if (StringUtils.isNotBlank(subcategory)) {
196 eventBuilder.append(subcategory);
197 }
198 eventBuilder.append("]:[");
199 if (StringUtils.isNotBlank(event)) {
200 eventBuilder.append(event);
201 }
202 eventBuilder.append(']');
203
204 if (condition != null) {
205 eventBuilder.append(":[").
206 append(condition).
207 append(']');
208 }
209
210 return eventBuilder.toString();
211 }
212 }