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.Date;
24  import java.util.LinkedHashMap;
25  import java.util.List;
26  import java.util.Map;
27  import org.apache.commons.lang3.StringUtils;
28  import org.apache.syncope.client.lib.batch.BatchRequest;
29  import org.apache.syncope.client.ui.commons.DateOps;
30  import org.apache.syncope.common.lib.form.SyncopeForm;
31  import org.apache.syncope.common.lib.to.ExecTO;
32  import org.apache.syncope.common.lib.to.JobTO;
33  import org.apache.syncope.common.lib.to.NotificationTaskTO;
34  import org.apache.syncope.common.lib.to.PagedResult;
35  import org.apache.syncope.common.lib.to.PropagationTaskTO;
36  import org.apache.syncope.common.lib.to.SchedTaskTO;
37  import org.apache.syncope.common.lib.to.TaskTO;
38  import org.apache.syncope.common.lib.types.AnyTypeKind;
39  import org.apache.syncope.common.lib.types.JobAction;
40  import org.apache.syncope.common.lib.types.TaskType;
41  import org.apache.syncope.common.rest.api.batch.BatchRequestItem;
42  import org.apache.syncope.common.rest.api.batch.BatchResponseItem;
43  import org.apache.syncope.common.rest.api.beans.ExecQuery;
44  import org.apache.syncope.common.rest.api.beans.ExecSpecs;
45  import org.apache.syncope.common.rest.api.beans.TaskQuery;
46  import org.apache.syncope.common.rest.api.service.TaskService;
47  import org.apache.wicket.extensions.markup.html.repeater.util.SortParam;
48  
49  /**
50   * Console client for invoking Rest Tasks services.
51   */
52  public class TaskRestClient extends BaseRestClient implements ExecutionRestClient {
53  
54      private static final long serialVersionUID = 6284485820911028843L;
55  
56      public JobTO getJob(final String key) {
57          return getService(TaskService.class).getJob(key);
58      }
59  
60      public List<JobTO> listJobs() {
61          return getService(TaskService.class).listJobs();
62      }
63  
64      public void actionJob(final String refKey, final JobAction jobAction) {
65          getService(TaskService.class).actionJob(refKey, jobAction);
66      }
67  
68      public int count(final TaskType kind) {
69          return getService(TaskService.class).search(
70                  new TaskQuery.Builder(kind).page(1).size(0).build()).getTotalCount();
71      }
72  
73      public int count(final String resource, final TaskType kind) {
74          return getService(TaskService.class).search(
75                  new TaskQuery.Builder(kind).resource(resource).page(1).size(0).build()).getTotalCount();
76      }
77  
78      public int count(final AnyTypeKind anyTypeKind, final String entityKey, final TaskType kind) {
79          return getService(TaskService.class).search(
80                  new TaskQuery.Builder(kind).anyTypeKind(anyTypeKind).entityKey(entityKey).page(1).size(0).build()).
81                  getTotalCount();
82      }
83  
84      public int count(final AnyTypeKind anyTypeKind, final String entityKey, final String notification) {
85          return getService(TaskService.class).search(
86                  new TaskQuery.Builder(TaskType.NOTIFICATION).notification(notification).
87                          anyTypeKind(anyTypeKind).entityKey(entityKey).page(1).size(0).build()).
88                  getTotalCount();
89      }
90  
91      @Override
92      public int countExecutions(final String taskKey) {
93          return getService(TaskService.class).
94                  listExecutions(new ExecQuery.Builder().key(taskKey).page(1).size(0).build()).getTotalCount();
95      }
96  
97      public List<PropagationTaskTO> listPropagationTasks(
98              final String resource, final int page, final int size, final SortParam<String> sort) {
99  
100         return getService(TaskService.class).
101                 <PropagationTaskTO>search(new TaskQuery.Builder(TaskType.PROPAGATION).
102                         resource(resource).
103                         page(page).size(size).
104                         orderBy(toOrderBy(sort)).build()).
105                 getResult();
106     }
107 
108     public List<PropagationTaskTO> listPropagationTasks(
109             final AnyTypeKind anyTypeKind, final String entityKey,
110             final int page, final int size, final SortParam<String> sort) {
111 
112         return getService(TaskService.class).
113                 <PropagationTaskTO>search(new TaskQuery.Builder(TaskType.PROPAGATION).
114                         anyTypeKind(anyTypeKind).entityKey(entityKey).
115                         page(page).size(size).
116                         orderBy(toOrderBy(sort)).build()).
117                 getResult();
118     }
119 
120     public List<NotificationTaskTO> listNotificationTasks(
121             final String notification,
122             final AnyTypeKind anyTypeKind,
123             final String entityKey,
124             final int page,
125             final int size,
126             final SortParam<String> sort) {
127 
128         TaskQuery.Builder builder = new TaskQuery.Builder(TaskType.NOTIFICATION);
129         if (notification != null) {
130             builder.notification(notification);
131         }
132 
133         if (anyTypeKind != null) {
134             builder.anyTypeKind(anyTypeKind);
135         }
136 
137         if (entityKey != null) {
138             builder.entityKey(entityKey);
139         }
140 
141         PagedResult<NotificationTaskTO> list = getService(TaskService.class).
142                 search(builder.page(page).size(size).orderBy(toOrderBy(sort)).build());
143         return list.getResult();
144     }
145 
146     public <T extends TaskTO> List<T> list(
147             final TaskType taskType, final int page, final int size, final SortParam<String> sort) {
148 
149         return getService(TaskService.class).<T>search(
150                 new TaskQuery.Builder(taskType).
151                         page(page).
152                         size(size).
153                         orderBy(toOrderBy(sort)).
154                         build()).
155                 getResult();
156     }
157 
158     public <T extends TaskTO> List<T> list(
159             final String resource,
160             final TaskType taskType,
161             final int page,
162             final int size,
163             final SortParam<String> sort) {
164 
165         return getService(TaskService.class).<T>search(
166                 new TaskQuery.Builder(taskType).
167                         page(page).
168                         size(size).
169                         resource(resource).
170                         orderBy(toOrderBy(sort)).
171                         build()).
172                 getResult();
173     }
174 
175     @Override
176     public List<ExecTO> listExecutions(
177             final String taskKey, final int page, final int size, final SortParam<String> sort) {
178 
179         return getService(TaskService.class).
180                 listExecutions(new ExecQuery.Builder().key(taskKey).page(page).size(size).
181                         orderBy(toOrderBy(sort)).build()).getResult();
182     }
183 
184     public PropagationTaskTO readPropagationTask(final String taskKey) {
185         return getService(TaskService.class).read(TaskType.PROPAGATION, taskKey, false);
186     }
187 
188     public NotificationTaskTO readNotificationTask(final String taskKey) {
189         return getService(TaskService.class).read(TaskType.NOTIFICATION, taskKey, false);
190     }
191 
192     public <T extends TaskTO> T readTask(final TaskType type, final String taskKey) {
193         return getService(TaskService.class).read(type, taskKey, false);
194     }
195 
196     public SyncopeForm getMacroTaskForm(final String taskKey) {
197         return getService(TaskService.class).getMacroTaskForm(taskKey);
198     }
199 
200     public void delete(final TaskType type, final String taskKey) {
201         getService(TaskService.class).delete(type, taskKey);
202     }
203 
204     @Override
205     public void startExecution(final String taskKey, final Date startAt) {
206         startExecution(taskKey, startAt, false);
207     }
208 
209     public void startExecution(final String taskKey, final Date startAt, final boolean dryRun) {
210         getService(TaskService.class).execute(
211                 new ExecSpecs.Builder().key(taskKey).startAt(DateOps.toOffsetDateTime(startAt)).dryRun(dryRun).build());
212     }
213 
214     public void startExecution(
215             final String taskKey,
216             final Date startAt,
217             final boolean dryRun,
218             final SyncopeForm macroTaskForm) {
219 
220         getService(TaskService.class).execute(
221                 new ExecSpecs.Builder().key(taskKey).startAt(DateOps.toOffsetDateTime(startAt)).dryRun(dryRun).build(),
222                 macroTaskForm);
223     }
224 
225     @Override
226     public void deleteExecution(final String taskExecKey) {
227         getService(TaskService.class).deleteExecution(taskExecKey);
228     }
229 
230     @Override
231     public List<ExecTO> listRecentExecutions(final int max) {
232         return getService(TaskService.class).listRecentExecutions(max);
233     }
234 
235     public void create(final TaskType type, final SchedTaskTO taskTO) {
236         getService(TaskService.class).create(type, taskTO);
237     }
238 
239     public void update(final TaskType type, final SchedTaskTO taskTO) {
240         getService(TaskService.class).update(type, taskTO);
241     }
242 
243     @Override
244     public Map<String, String> batch(final BatchRequest batchRequest) {
245         List<BatchRequestItem> batchRequestItems = new ArrayList<>(batchRequest.getItems());
246 
247         Map<String, String> result = new LinkedHashMap<>();
248         try {
249             List<BatchResponseItem> batchResponseItems = batchRequest.commit().getItems();
250             for (int i = 0; i < batchResponseItems.size(); i++) {
251                 String status = getStatus(batchResponseItems.get(i).getStatus());
252 
253                 if (batchRequestItems.get(i).getRequestURI().contains("/execute")) {
254                     result.put(StringUtils.substringAfterLast(
255                             StringUtils.substringBefore(batchRequestItems.get(i).getRequestURI(), "/execute"), "/"),
256                             status);
257                 } else {
258                     result.put(StringUtils.substringAfterLast(
259                             batchRequestItems.get(i).getRequestURI(), "/"), status);
260                 }
261             }
262         } catch (IOException e) {
263             LOG.error("While processing Batch response", e);
264         }
265 
266         return result;
267     }
268 }