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.enduser.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.DerSchemaTO;
28  import org.apache.syncope.common.lib.to.PlainSchemaTO;
29  import org.apache.syncope.common.lib.to.SchemaTO;
30  import org.apache.syncope.common.lib.to.VirSchemaTO;
31  import org.apache.syncope.common.lib.types.AnyTypeKind;
32  import org.apache.syncope.common.lib.types.SchemaType;
33  import org.apache.syncope.common.rest.api.beans.SchemaQuery;
34  import org.apache.syncope.common.rest.api.service.AnyTypeService;
35  import org.apache.syncope.common.rest.api.service.SchemaService;
36  
37  /**
38   * Enduser client for invoking rest schema services.
39   */
40  public class SchemaRestClient extends BaseRestClient {
41  
42      private static final long serialVersionUID = -2479730152700312373L;
43  
44      public <T extends SchemaTO> List<T> getSchemas(final SchemaType schemaType, final AnyTypeKind kind) {
45          final AnyTypeService client = getService(AnyTypeService.class);
46  
47          final List<String> classes = new ArrayList<>();
48  
49          switch (kind) {
50              case USER:
51              case GROUP:
52                  final AnyTypeTO type = client.read(kind.name());
53                  if (type != null) {
54                      classes.addAll(type.getClasses());
55                  }
56                  break;
57  
58              default:
59                  getService(AnyTypeService.class).list().stream().filter(
60                          anyTypeTO -> (anyTypeTO.getKind() != AnyTypeKind.USER
61                          && anyTypeTO.getKind() != AnyTypeKind.GROUP)).
62                          forEach((anyTypeTO) -> classes.addAll(anyTypeTO.getClasses()));
63          }
64          return getSchemas(schemaType, null, classes.toArray(String[]::new));
65      }
66  
67      public <T extends SchemaTO> List<T> getSchemas(
68              final SchemaType schemaType, final String keyword, final String... anyTypeClasses) {
69  
70          SchemaQuery.Builder builder = new SchemaQuery.Builder().type(schemaType);
71          if (StringUtils.isNotBlank(keyword)) {
72              builder.keyword(keyword);
73          }
74          if (anyTypeClasses != null && anyTypeClasses.length > 0) {
75              builder.anyTypeClasses(anyTypeClasses);
76          }
77  
78          List<T> schemas = new ArrayList<>();
79          try {
80              schemas.addAll(getService(SchemaService.class).<T>search(builder.build()));
81          } catch (SyncopeClientException e) {
82              LOG.error("While getting all {} schemas for {}", schemaType, anyTypeClasses, e);
83          }
84          return schemas;
85      }
86  
87      public List<String> getSchemaNames(final SchemaType schemaType) {
88          List<String> schemaNames = List.of();
89  
90          try {
91              schemaNames = getSchemas(schemaType, null, new String[0]).stream().
92                      map(SchemaTO::getKey).collect(Collectors.toList());
93          } catch (SyncopeClientException e) {
94              LOG.error("While getting all user schema names", e);
95          }
96  
97          return schemaNames;
98      }
99  
100     public List<String> getPlainSchemaNames() {
101         return getSchemaNames(SchemaType.PLAIN);
102     }
103 
104     public List<String> getDerSchemaNames() {
105         return getSchemaNames(SchemaType.DERIVED);
106     }
107 
108     public List<String> getVirSchemaNames() {
109         return getSchemaNames(SchemaType.VIRTUAL);
110     }
111 
112     public <T extends SchemaTO> T read(final SchemaType schemaType, final String key) {
113         return getService(SchemaService.class).read(schemaType, key);
114     }
115 
116     public void create(final SchemaType schemaType, final SchemaTO modelObject) {
117         getService(SchemaService.class).create(schemaType, modelObject);
118     }
119 
120     public void update(final SchemaType schemaType, final SchemaTO modelObject) {
121         getService(SchemaService.class).update(schemaType, modelObject);
122     }
123 
124     public PlainSchemaTO deletePlainSchema(final String name) {
125         PlainSchemaTO response = getService(SchemaService.class).read(SchemaType.PLAIN, name);
126         getService(SchemaService.class).delete(SchemaType.PLAIN, name);
127         return response;
128     }
129 
130     public DerSchemaTO deleteDerSchema(final String name) {
131         DerSchemaTO schemaTO = getService(SchemaService.class).read(SchemaType.DERIVED, name);
132         getService(SchemaService.class).delete(SchemaType.DERIVED, name);
133         return schemaTO;
134     }
135 
136     public VirSchemaTO deleteVirSchema(final String name) {
137         VirSchemaTO schemaTO = getService(SchemaService.class).read(SchemaType.VIRTUAL, name);
138         getService(SchemaService.class).delete(SchemaType.VIRTUAL, name);
139         return schemaTO;
140     }
141 }