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.ArrayList;
22  import java.util.List;
23  import java.util.stream.Collectors;
24  import org.apache.commons.lang3.StringUtils;
25  import org.apache.syncope.common.lib.SyncopeClientException;
26  import org.apache.syncope.common.lib.to.AnyTypeTO;
27  import org.apache.syncope.common.lib.to.SchemaTO;
28  import org.apache.syncope.common.lib.types.AnyTypeKind;
29  import org.apache.syncope.common.lib.types.SchemaType;
30  import org.apache.syncope.common.rest.api.beans.SchemaQuery;
31  import org.apache.syncope.common.rest.api.service.AnyTypeService;
32  import org.apache.syncope.common.rest.api.service.SchemaService;
33  
34  /**
35   * Console client for invoking rest schema services.
36   */
37  public class SchemaRestClient extends BaseRestClient {
38  
39      private static final long serialVersionUID = -2479730152700312373L;
40  
41      public <T extends SchemaTO> List<T> getSchemas(
42              final SchemaType schemaType, final AnyTypeKind kind) {
43  
44          AnyTypeService client = getService(AnyTypeService.class);
45  
46          List<String> classes = new ArrayList<>();
47  
48          switch (kind) {
49              case USER:
50              case GROUP:
51                  AnyTypeTO type = client.read(kind.name());
52                  if (type != null) {
53                      classes.addAll(type.getClasses());
54                  }
55                  break;
56  
57              default:
58                  getService(AnyTypeService.class).list().stream().filter(
59                          anyType -> anyType.getKind() != AnyTypeKind.USER && anyType.getKind() != AnyTypeKind.GROUP).
60                          forEach(anyType -> classes.addAll(anyType.getClasses()));
61          }
62  
63          return getSchemas(schemaType, null, classes.toArray(String[]::new));
64      }
65  
66      public <T extends SchemaTO> List<T> getSchemas(
67              final SchemaType schemaType, final String keyword, final String... anyTypeClasses) {
68  
69          SchemaQuery.Builder builder = new SchemaQuery.Builder().type(schemaType);
70          if (StringUtils.isNotBlank(keyword)) {
71              builder.keyword(keyword);
72          }
73          if (anyTypeClasses != null && anyTypeClasses.length > 0) {
74              builder.anyTypeClasses(anyTypeClasses);
75          }
76  
77          List<T> schemas = new ArrayList<>();
78          try {
79              schemas.addAll(getService(SchemaService.class).<T>search(builder.build()));
80          } catch (SyncopeClientException e) {
81              LOG.error("While getting all {} schemas for {}", schemaType, anyTypeClasses, e);
82          }
83          return schemas;
84      }
85  
86      public List<String> getSchemaNames(final SchemaType schemaType) {
87          List<String> schemaNames = List.of();
88  
89          try {
90              schemaNames = getSchemas(schemaType, null, new String[0]).stream().
91                      map(SchemaTO::getKey).collect(Collectors.toList());
92          } catch (SyncopeClientException e) {
93              LOG.error("While getting all user schema names", e);
94          }
95  
96          return schemaNames;
97      }
98  
99      public <T extends SchemaTO> T read(final SchemaType schemaType, final String key) {
100         return getService(SchemaService.class).read(schemaType, key);
101     }
102 
103     public void create(final SchemaType schemaType, final SchemaTO modelObject) {
104         getService(SchemaService.class).create(schemaType, modelObject);
105     }
106 
107     public void update(final SchemaType schemaType, final SchemaTO modelObject) {
108         getService(SchemaService.class).update(schemaType, modelObject);
109     }
110 
111     public void delete(final SchemaType schemaType, final String key) {
112         getService(SchemaService.class).delete(schemaType, key);
113     }
114 }