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.io.InputStream;
22  import java.nio.charset.StandardCharsets;
23  import java.util.List;
24  import java.util.stream.Collectors;
25  import org.apache.commons.io.IOUtils;
26  import org.apache.commons.lang3.StringUtils;
27  import org.apache.syncope.client.console.SyncopeConsoleSession;
28  import org.apache.syncope.common.lib.to.RoleTO;
29  import org.apache.syncope.common.rest.api.service.RoleService;
30  
31  /**
32   * Console client for invoking Rest Role's services.
33   */
34  public class RoleRestClient extends BaseRestClient {
35  
36      private static final long serialVersionUID = -3161863874876938094L;
37  
38      public void delete(final String key) {
39          getService(RoleService.class).delete(key);
40      }
41  
42      public RoleTO read(final String key) {
43          return getService(RoleService.class).read(key);
44      }
45  
46      public void update(final RoleTO roleTO) {
47          getService(RoleService.class).update(roleTO);
48      }
49  
50      public void create(final RoleTO roleTO) {
51          getService(RoleService.class).create(roleTO);
52      }
53  
54      public List<RoleTO> list() {
55          return getService(RoleService.class).list();
56      }
57  
58      public String readAnyLayout(final String roleKey) {
59          try {
60              return IOUtils.toString(InputStream.class.cast(
61                      getService(RoleService.class).getAnyLayout(roleKey).getEntity()),
62                      StandardCharsets.UTF_8);
63          } catch (Exception e) {
64              LOG.error("Error retrieving console layout info for role {}", roleKey, e);
65              return StringUtils.EMPTY;
66          }
67      }
68  
69      public void setAnyLayout(final String roleKey, final String content) {
70          getService(RoleService.class).setAnyLayout(
71                  roleKey, IOUtils.toInputStream(content, StandardCharsets.UTF_8));
72      }
73  
74      public void removeAnyLayout(final String roleKey) {
75          getService(RoleService.class).removeAnyLayout(roleKey);
76      }
77  
78      public List<String> getAllAvailableEntitlements() {
79          return SyncopeConsoleSession.get().getAnonymousClient().platform().getEntitlements().
80                  stream().sorted().collect(Collectors.toList());
81      }
82  }