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.IOException;
22  import java.util.ArrayList;
23  import java.util.LinkedHashMap;
24  import java.util.List;
25  import java.util.Map;
26  import java.util.Optional;
27  import org.apache.commons.lang3.StringUtils;
28  import org.apache.syncope.client.lib.batch.BatchRequest;
29  import org.apache.syncope.common.lib.SyncopeClientException;
30  import org.apache.syncope.common.lib.to.PagedResult;
31  import org.apache.syncope.common.lib.to.UserRequest;
32  import org.apache.syncope.common.lib.to.UserRequestForm;
33  import org.apache.syncope.common.rest.api.batch.BatchRequestItem;
34  import org.apache.syncope.common.rest.api.batch.BatchResponseItem;
35  import org.apache.syncope.common.rest.api.beans.UserRequestQuery;
36  import org.apache.syncope.common.rest.api.service.UserRequestService;
37  import org.apache.wicket.extensions.markup.html.repeater.util.SortParam;
38  
39  public class UserRequestRestClient extends BaseRestClient {
40  
41      private static final long serialVersionUID = -4785231164900813921L;
42  
43      public int countRequests(final String keyword) {
44          try {
45              return getService(UserRequestService.class).
46                      listRequests(new UserRequestQuery.Builder().user(keyword).page(1).size(0).build()).
47                      getTotalCount();
48          } catch (Exception e) {
49              return 0;
50          }
51      }
52  
53      public List<UserRequest> listRequests(
54              final String keyword, final int page, final int size, final SortParam<String> sort) {
55  
56          try {
57              return getService(UserRequestService.class).listRequests(
58                      new UserRequestQuery.Builder().user(keyword).page(page).size(size).
59                              orderBy(toOrderBy(sort)).build()).
60                      getResult();
61          } catch (SyncopeClientException e) {
62              return List.of();
63          }
64      }
65  
66      public void cancelRequest(final String executionId, final String reason) {
67          getService(UserRequestService.class).cancelRequest(executionId, reason);
68      }
69  
70      public int countForms(final String keyword) {
71          try {
72              return getService(UserRequestService.class).
73                      listForms(new UserRequestQuery.Builder().user(keyword).page(1).size(0).build()).
74                      getTotalCount();
75          } catch (SyncopeClientException e) {
76              return 0;
77          }
78      }
79  
80      public List<UserRequestForm> listForms(
81              final String keyword, final int page, final int size, final SortParam<String> sort) {
82  
83          try {
84              return getService(UserRequestService.class).listForms(
85                      new UserRequestQuery.Builder().user(keyword).page(page).size(size).
86                              orderBy(toOrderBy(sort)).build()).
87                      getResult();
88          } catch (SyncopeClientException e) {
89              return List.of();
90          }
91      }
92  
93      public Optional<UserRequestForm> getForm(final String userKey) {
94          PagedResult<UserRequestForm> forms = getService(UserRequestService.class).
95                  listForms(new UserRequestQuery.Builder().user(userKey).page(1).size(1).build());
96          UserRequestForm form = forms.getResult().isEmpty()
97                  ? null
98                  : forms.getResult().get(0);
99          return Optional.ofNullable(form);
100     }
101 
102     public UserRequestForm claimForm(final String taskKey) {
103         return getService(UserRequestService.class).claimForm(taskKey);
104     }
105 
106     public UserRequestForm unclaimForm(final String taskKey) {
107         return getService(UserRequestService.class).unclaimForm(taskKey);
108     }
109 
110     public void submitForm(final UserRequestForm form) {
111         getService(UserRequestService.class).submitForm(form);
112     }
113 
114     public Map<String, String> batch(final BatchRequest batchRequest) {
115         List<BatchRequestItem> batchRequestItems = new ArrayList<>(batchRequest.getItems());
116 
117         Map<String, String> result = new LinkedHashMap<>();
118         try {
119             List<BatchResponseItem> batchResponseItems = batchRequest.commit().getItems();
120             for (int i = 0; i < batchResponseItems.size(); i++) {
121                 String status = getStatus(batchResponseItems.get(i).getStatus());
122                 result.put(StringUtils.substringAfterLast(batchRequestItems.get(i).getRequestURI(), "/"), status);
123             }
124         } catch (IOException e) {
125             LOG.error("While processing Batch response", e);
126         }
127 
128         return result;
129     }
130 }