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.core.persistence.jpa.dao;
20  
21  import com.fasterxml.jackson.databind.json.JsonMapper;
22  import com.fasterxml.jackson.databind.node.ObjectNode;
23  import java.util.ArrayList;
24  import java.util.List;
25  import java.util.Optional;
26  import org.apache.commons.lang3.StringUtils;
27  import org.apache.syncope.common.lib.types.AuditElements;
28  import org.springframework.util.CollectionUtils;
29  
30  public abstract class AbstractJPAJSONLoggerDAO extends JPAAuditConfDAO {
31  
32      private static final JsonMapper MAPPER = JsonMapper.builder().findAndAddModules().build();
33  
34      protected abstract static class JSONMessageCriteriaBuilder extends MessageCriteriaBuilder {
35  
36          protected String entityKey;
37  
38          private AuditElements.EventCategoryType type;
39  
40          private String category;
41  
42          private String subcategory;
43  
44          private List<String> events;
45  
46          private AuditElements.Result result;
47  
48          @Override
49          protected MessageCriteriaBuilder entityKey(final String entityKey) {
50              this.entityKey = entityKey;
51              return this;
52          }
53  
54          @Override
55          public MessageCriteriaBuilder type(final AuditElements.EventCategoryType type) {
56              this.type = type;
57              return this;
58          }
59  
60          @Override
61          public MessageCriteriaBuilder category(final String category) {
62              this.category = category;
63              return this;
64          }
65  
66          @Override
67          public MessageCriteriaBuilder subcategory(final String subcategory) {
68              this.subcategory = subcategory;
69              return this;
70          }
71  
72          @Override
73          public MessageCriteriaBuilder events(final List<String> events) {
74              this.events = events;
75              return this;
76          }
77  
78          @Override
79          public MessageCriteriaBuilder result(final AuditElements.Result result) {
80              this.result = result;
81              return this;
82          }
83  
84          private Optional<ObjectNode> buildContainer() {
85              ObjectNode logger = MAPPER.createObjectNode();
86              if (type != null) {
87                  logger.put("type", type.name());
88              }
89              if (StringUtils.isNotBlank(category)) {
90                  logger.put("category", category);
91              }
92              if (StringUtils.isNotBlank(subcategory)) {
93                  logger.put("subcategory", subcategory);
94              }
95              if (result != null) {
96                  logger.put("result", result.name());
97              }
98  
99              if (!logger.isEmpty()) {
100                 ObjectNode container = MAPPER.createObjectNode();
101                 container.set("logger", logger);
102                 return Optional.of(container);
103             }
104 
105             return Optional.empty();
106         }
107 
108         protected abstract String doBuild(List<ObjectNode> containers);
109 
110         @Override
111         public String build() {
112             List<ObjectNode> containers = new ArrayList<>();
113             if (CollectionUtils.isEmpty(events)) {
114                 buildContainer().ifPresent(containers::add);
115             } else {
116                 events.forEach(event -> buildContainer().ifPresent(container -> {
117                     ((ObjectNode) container.get("logger")).put("event", event);
118                     containers.add(container);
119                 }));
120             }
121 
122             return doBuild(containers);
123         }
124     }
125 }