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