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.List;
22  import java.util.Optional;
23  import javax.ws.rs.core.GenericType;
24  import org.apache.commons.lang3.StringUtils;
25  import org.apache.cxf.jaxrs.client.WebClient;
26  import org.apache.cxf.transport.http.asyncclient.AsyncHTTPConduit;
27  import org.apache.syncope.client.enduser.SyncopeEnduserSession;
28  import org.apache.syncope.common.lib.to.ProvisioningResult;
29  import org.apache.syncope.common.lib.to.UserRequest;
30  import org.apache.syncope.common.lib.to.UserRequestForm;
31  import org.apache.syncope.common.lib.to.UserTO;
32  import org.apache.syncope.common.rest.api.beans.UserRequestQuery;
33  import org.apache.syncope.common.rest.api.service.UserRequestService;
34  import org.apache.wicket.extensions.markup.html.repeater.util.SortParam;
35  
36  public class UserRequestRestClient extends BaseRestClient {
37  
38      private static final long serialVersionUID = -4785231164900813921L;
39  
40      public int countRequests() {
41          return getService(UserRequestService.class).
42                  listRequests(new UserRequestQuery.Builder()
43                          .user(SyncopeEnduserSession.get().getSelfTO().getUsername())
44                          .page(1)
45                          .size(0)
46                          .build()).getTotalCount();
47      }
48  
49      public List<UserRequest> listRequests(
50              final int page,
51              final int size,
52              final String username,
53              final SortParam<String> sort) {
54  
55          return getService(UserRequestService.class).listRequests(new UserRequestQuery.Builder().
56                  user(StringUtils.isBlank(username)
57                          ? SyncopeEnduserSession.get().getSelfTO().getUsername()
58                          : username).
59                  page(page).size(size).orderBy(toOrderBy(sort)).build()).getResult();
60      }
61  
62      public void cancelRequest(final String executionId, final String reason) {
63          getService(UserRequestService.class).cancelRequest(executionId, reason);
64      }
65  
66      public int countForms() {
67          return getService(UserRequestService.class).
68                  listForms(new UserRequestQuery.Builder().page(1).size(0).build()).
69                  getTotalCount();
70      }
71  
72      public List<UserRequestForm> listForms(final int page, final int size, final SortParam<String> sort) {
73          return getService(UserRequestService.class).
74                  listForms(new UserRequestQuery.Builder().page(page).size(size).orderBy(toOrderBy(sort)).build()).
75                  getResult();
76      }
77  
78      public Optional<UserRequestForm> getForm(final String username, final String taskId) {
79          return Optional.ofNullable(getService(UserRequestService.class).getForm(StringUtils.isBlank(username)
80                  ? SyncopeEnduserSession.get().getSelfTO().getUsername()
81                  : username,
82                  taskId));
83      }
84  
85      public ProvisioningResult<UserTO> submitForm(final UserRequestForm form) {
86          return getService(UserRequestService.class).submitForm(form).readEntity(
87                  new GenericType<>() {
88          });
89      }
90  
91      public void startRequest(final String bpmnProcess, final String user) {
92          UserRequestService service = getService(UserRequestService.class);
93          WebClient.getConfig(WebClient.client(service)).
94                  getRequestContext().put(AsyncHTTPConduit.USE_ASYNC, Boolean.FALSE);
95  
96          service.startRequest(bpmnProcess, user, null);
97  
98          WebClient.getConfig(WebClient.client(service)).
99                  getRequestContext().put(AsyncHTTPConduit.USE_ASYNC, Boolean.TRUE);
100     }
101 
102     public UserRequestForm claimForm(final String taskKey) {
103         return getService(UserRequestService.class).claimForm(taskKey);
104     }
105 }