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.client.console.rest;
20  
21  import java.util.List;
22  import java.util.Objects;
23  import java.util.stream.Collectors;
24  import org.apache.syncope.common.lib.SyncopeClientException;
25  import org.apache.syncope.common.lib.audit.AuditEntry;
26  import org.apache.syncope.common.lib.audit.EventCategory;
27  import org.apache.syncope.common.lib.to.AuditConfTO;
28  import org.apache.syncope.common.lib.types.AuditElements;
29  import org.apache.syncope.common.lib.types.AuditLoggerName;
30  import org.apache.syncope.common.lib.types.ClientExceptionType;
31  import org.apache.syncope.common.rest.api.beans.AuditQuery;
32  import org.apache.syncope.common.rest.api.service.AuditService;
33  import org.apache.wicket.extensions.markup.html.repeater.util.SortParam;
34  
35  public class AuditRestClient extends BaseRestClient {
36  
37      private static final long serialVersionUID = 4579786978763032240L;
38  
39      public List<AuditLoggerName> list() {
40          return getService(AuditService.class).list().stream().
41                  map(a -> {
42                      try {
43                          return AuditLoggerName.fromAuditKey(a.getKey());
44                      } catch (Exception e) {
45                          LOG.error("Unexpected when parsing {}", a.getKey(), e);
46                          return null;
47                      }
48                  }).
49                  filter(Objects::nonNull).
50                  collect(Collectors.toList());
51      }
52  
53      public void enable(final AuditLoggerName auditLoggerName) {
54          AuditConfTO audit = new AuditConfTO();
55          audit.setKey(auditLoggerName.toAuditKey());
56          audit.setActive(true);
57          getService(AuditService.class).set(audit);
58      }
59  
60      public void delete(final AuditLoggerName auditLoggerName) {
61          try {
62              getService(AuditService.class).delete(auditLoggerName.toAuditKey());
63          } catch (SyncopeClientException e) {
64              if (e.getType() != ClientExceptionType.NotFound) {
65                  LOG.error("Unexpected error when deleting {}", auditLoggerName.toAuditKey(), e);
66              }
67          }
68      }
69  
70      public List<EventCategory> listEvents() {
71          try {
72              return getService(AuditService.class).events();
73          } catch (Exception e) {
74              return List.of();
75          }
76      }
77  
78      public List<AuditEntry> search(
79              final String key,
80              final int page,
81              final int size,
82              final AuditElements.EventCategoryType type,
83              final String category,
84              final List<String> events,
85              final AuditElements.Result result,
86              final SortParam<String> sort) {
87  
88          AuditQuery query = new AuditQuery.Builder().
89                  entityKey(key).
90                  size(size).
91                  page(page).
92                  type(type).
93                  category(category).
94                  events(events).
95                  result(result).
96                  orderBy(toOrderBy(sort)).
97                  build();
98  
99          return getService(AuditService.class).search(query).getResult();
100     }
101 
102     public int count(
103             final String key,
104             final AuditElements.EventCategoryType type,
105             final String category,
106             final List<String> events,
107             final AuditElements.Result result) {
108 
109         AuditQuery query = new AuditQuery.Builder().
110                 entityKey(key).
111                 page(1).
112                 size(0).
113                 type(type).
114                 category(category).
115                 events(events).
116                 result(result).
117                 build();
118         return getService(AuditService.class).search(query).getTotalCount();
119     }
120 }