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.core.rest.cxf.service;
20  
21  import java.net.URI;
22  import java.util.List;
23  import java.util.Optional;
24  import javax.ws.rs.core.Response;
25  import org.apache.commons.lang3.StringUtils;
26  import org.apache.commons.lang3.tuple.Pair;
27  import org.apache.syncope.common.lib.SyncopeConstants;
28  import org.apache.syncope.common.lib.to.PagedResult;
29  import org.apache.syncope.common.lib.to.ProvisioningResult;
30  import org.apache.syncope.common.lib.to.RealmTO;
31  import org.apache.syncope.common.rest.api.RESTHeaders;
32  import org.apache.syncope.common.rest.api.beans.RealmQuery;
33  import org.apache.syncope.common.rest.api.service.RealmService;
34  import org.apache.syncope.core.logic.RealmLogic;
35  import org.springframework.stereotype.Service;
36  
37  @Service
38  public class RealmServiceImpl extends AbstractService implements RealmService {
39  
40      protected final RealmLogic logic;
41  
42      public RealmServiceImpl(final RealmLogic logic) {
43          this.logic = logic;
44      }
45  
46      @Override
47      public PagedResult<RealmTO> search(final RealmQuery query) {
48          Pair<Integer, List<RealmTO>> result = logic.search(
49                  Optional.ofNullable(query.getKeyword()).map(k -> k.replace('*', '%')).orElse(null),
50                  query.getBase(),
51                  query.getPage(),
52                  query.getSize());
53          return buildPagedResult(result.getRight(), 1, result.getRight().size(), result.getLeft());
54      }
55  
56      @Override
57      public Response create(final String parentPath, final RealmTO realmTO) {
58          ProvisioningResult<RealmTO> created =
59                  logic.create(StringUtils.prependIfMissing(parentPath, SyncopeConstants.ROOT_REALM), realmTO);
60          URI location = uriInfo.getAbsolutePathBuilder().path(created.getEntity().getName()).build();
61          Response.ResponseBuilder builder = Response.created(location).
62                  header(RESTHeaders.RESOURCE_KEY, created.getEntity().getFullPath());
63  
64          return applyPreference(created, builder).build();
65      }
66  
67      @Override
68      public Response update(final RealmTO realmTO) {
69          realmTO.setFullPath(StringUtils.prependIfMissing(realmTO.getFullPath(), SyncopeConstants.ROOT_REALM));
70          ProvisioningResult<RealmTO> updated = logic.update(realmTO);
71          return modificationResponse(updated);
72      }
73  
74      @Override
75      public Response delete(final String fullPath) {
76          ProvisioningResult<RealmTO> deleted =
77                  logic.delete(StringUtils.prependIfMissing(fullPath, SyncopeConstants.ROOT_REALM));
78          return modificationResponse(deleted);
79      }
80  }