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 javax.ws.rs.core.GenericType;
23  import javax.ws.rs.core.Response;
24  import org.apache.commons.lang3.StringUtils;
25  import org.apache.syncope.common.lib.SyncopeConstants;
26  import org.apache.syncope.common.lib.request.GroupCR;
27  import org.apache.syncope.common.lib.request.GroupUR;
28  import org.apache.syncope.common.lib.to.GroupTO;
29  import org.apache.syncope.common.lib.to.ProvisioningResult;
30  import org.apache.syncope.common.rest.api.beans.AnyQuery;
31  import org.apache.syncope.common.rest.api.service.AnyService;
32  import org.apache.syncope.common.rest.api.service.GroupService;
33  import org.apache.syncope.common.rest.api.service.SyncopeService;
34  
35  /**
36   * Enduser client for invoking Rest Group's services.
37   */
38  public class GroupRestClient extends AbstractAnyRestClient<GroupTO> {
39  
40      private static final long serialVersionUID = -8549081557283519638L;
41  
42      @Override
43      protected Class<? extends AnyService<GroupTO>> getAnyServiceClass() {
44          return GroupService.class;
45      }
46  
47      public ProvisioningResult<GroupTO> create(final GroupCR groupTO) {
48          Response response = getService(GroupService.class).create(groupTO);
49          return response.readEntity(new GenericType<>() {
50          });
51      }
52  
53      public ProvisioningResult<GroupTO> update(final String etag, final GroupUR groupPatch) {
54          ProvisioningResult<GroupTO> result;
55          synchronized (this) {
56              result = getService(etag, GroupService.class).update(groupPatch).
57                      readEntity(new GenericType<>() {
58                      });
59              resetClient(getAnyServiceClass());
60          }
61          return result;
62      }
63  
64      public List<GroupTO> searchAssignableGroups(
65              final String realm,
66              final String term,
67              final int page,
68              final int size) {
69  
70          return getService(SyncopeService.class).searchAssignableGroups(
71                  StringUtils.isNotEmpty(realm) ? realm : SyncopeConstants.ROOT_REALM, term, page, size).getResult();
72      }
73  
74      @Override
75      public int count(final String realm, final String fiql, final String type) {
76          return getService(GroupService.class).
77                  search(new AnyQuery.Builder().realm(realm).fiql(fiql).page(1).size(0).details(false).build()).
78                  getTotalCount();
79      }
80  }