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.logic;
20  
21  import java.lang.reflect.Method;
22  import java.util.List;
23  import java.util.stream.Collectors;
24  import org.apache.commons.lang3.RandomUtils;
25  import org.apache.syncope.common.keymaster.client.api.model.NetworkService;
26  import org.apache.syncope.common.lib.to.EntityTO;
27  import org.apache.syncope.core.persistence.api.dao.NetworkServiceDAO;
28  import org.apache.syncope.core.persistence.api.dao.NotFoundException;
29  import org.apache.syncope.core.persistence.api.entity.NetworkServiceEntity;
30  import org.apache.syncope.core.persistence.api.entity.SelfKeymasterEntityFactory;
31  import org.springframework.security.access.prepost.PreAuthorize;
32  import org.springframework.transaction.annotation.Transactional;
33  
34  public class NetworkServiceLogic extends AbstractTransactionalLogic<EntityTO> {
35  
36      protected final NetworkServiceDAO serviceDAO;
37  
38      protected final SelfKeymasterEntityFactory entityFactory;
39  
40      public NetworkServiceLogic(final NetworkServiceDAO serviceDAO, final SelfKeymasterEntityFactory entityFactory) {
41          this.serviceDAO = serviceDAO;
42          this.entityFactory = entityFactory;
43      }
44  
45      protected NetworkService toNetworkService(
46              final NetworkService.Type serviceType,
47              final NetworkServiceEntity service) {
48  
49          NetworkService ns = new NetworkService();
50          ns.setType(serviceType);
51          ns.setAddress(service.getAddress());
52          return ns;
53      }
54  
55      @PreAuthorize("@environment.getProperty('keymaster.username') == authentication.name")
56      @Transactional(readOnly = true)
57      public List<NetworkService> list(final NetworkService.Type serviceType) {
58          return serviceDAO.findAll(serviceType).stream().
59                  map(service -> toNetworkService(serviceType, service)).collect(Collectors.toList());
60      }
61  
62      @PreAuthorize("@environment.getProperty('keymaster.username') == authentication.name")
63      @Transactional(readOnly = true)
64      public NetworkService get(final NetworkService.Type serviceType) {
65          List<NetworkService> list = list(serviceType);
66          if (list.isEmpty()) {
67              throw new NotFoundException("No registered services for type " + serviceType);
68          }
69  
70          return list.size() == 1
71                  ? list.get(0)
72                  : list.get(RandomUtils.nextInt(0, list.size()));
73      }
74  
75      @PreAuthorize("@environment.getProperty('keymaster.username') == authentication.name")
76      public void register(final NetworkService networkService) {
77          if (serviceDAO.findAll(networkService.getType()).stream().
78                  noneMatch(s -> s.getAddress().equals(networkService.getAddress()))) {
79  
80              NetworkServiceEntity service = entityFactory.newNetworkService();
81              service.setType(networkService.getType());
82              service.setAddress(networkService.getAddress());
83              serviceDAO.save(service);
84          }
85      }
86  
87      @PreAuthorize("@environment.getProperty('keymaster.username') == authentication.name")
88      public void unregister(final NetworkService networkService) {
89          serviceDAO.deleteAll(networkService);
90      }
91  
92      @Override
93      protected EntityTO resolveReference(final Method method, final Object... args) throws UnresolvedReferenceException {
94          throw new UnsupportedOperationException();
95      }
96  }