View Javadoc
1   /*
2    * Copyright (c) 2002-2024, City of Paris
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions
7    * are met:
8    *
9    *  1. Redistributions of source code must retain the above copyright notice
10   *     and the following disclaimer.
11   *
12   *  2. Redistributions in binary form must reproduce the above copyright notice
13   *     and the following disclaimer in the documentation and/or other materials
14   *     provided with the distribution.
15   *
16   *  3. Neither the name of 'Mairie de Paris' nor 'Lutece' nor the names of its
17   *     contributors may be used to endorse or promote products derived from
18   *     this software without specific prior written permission.
19   *
20   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
24   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30   * POSSIBILITY OF SUCH DAMAGE.
31   *
32   * License 1.0
33   */
34  package fr.paris.lutece.plugins.identitystore.cache;
35  
36  import fr.paris.lutece.plugins.identitystore.business.application.ClientApplication;
37  import fr.paris.lutece.plugins.identitystore.business.application.ClientApplicationHome;
38  import fr.paris.lutece.plugins.identitystore.business.contract.ServiceContract;
39  import fr.paris.lutece.plugins.identitystore.business.contract.ServiceContractHome;
40  import fr.paris.lutece.plugins.identitystore.v3.web.rs.util.Constants;
41  import fr.paris.lutece.plugins.identitystore.web.exception.ClientAuthorizationException;
42  import fr.paris.lutece.plugins.identitystore.web.exception.ResourceNotFoundException;
43  import fr.paris.lutece.portal.service.cache.AbstractCacheableService;
44  import fr.paris.lutece.portal.service.util.AppLogService;
45  import org.apache.commons.collections.CollectionUtils;
46  
47  import java.util.List;
48  import java.util.Objects;
49  
50  public class ActiveServiceContractCache extends AbstractCacheableService
51  {
52  
53      public static final String SERVICE_NAME = "ActiveServiceContractCache";
54  
55      public ActiveServiceContractCache( )
56      {
57          this.initCache( );
58      }
59  
60      public void refresh( )
61      {
62          AppLogService.debug( "Init service contract cache" );
63          this.resetCache( );
64          final List<ClientApplication> clientApplications = ClientApplicationHome.selectApplicationList( );
65          clientApplications.forEach( clientApplication -> {
66              try
67              {
68                  final ServiceContract activeServiceContract = this.getActiveServiceContractFromDatabase( clientApplication.getClientCode( ) );
69                  this.put( clientApplication.getClientCode( ), activeServiceContract );
70              }
71              catch( final ClientAuthorizationException e )
72              {
73                  AppLogService.debug( e.getMessage( ) );
74              }
75          } );
76      }
77  
78      public void put( final String clientCode, final ServiceContract serviceContract )
79      {
80          if ( this.getKeys( ).contains( clientCode ) )
81          {
82              this.removeKey( clientCode );
83          }
84          this.putInCache( clientCode, serviceContract );
85          AppLogService.debug( "An active service contract has been added for client application with code : " + clientCode );
86      }
87  
88      /**
89       * Deletes a {@link ServiceContract} by its id in the database
90       * 
91       * @param id
92       */
93      public void deleteById( final Integer id )
94      {
95          this.getKeys( ).forEach( key -> {
96              try
97              {
98                  ServiceContract contract = this.get( key );
99                  if ( Objects.equals( contract.getId( ), id ) )
100                 {
101                     this.removeKey( key );
102                 }
103             }
104             catch( final ClientAuthorizationException e )
105             {
106                 AppLogService.error( "Cannot delete service contract with id" + id + " : {}", e );
107             }
108         } );
109     }
110 
111     public ServiceContract get( final String clientCode ) throws ClientAuthorizationException
112     {
113         ServiceContractfr/paris/lutece/plugins/identitystore/business/contract/ServiceContract.html#ServiceContract">ServiceContract serviceContract = (ServiceContract) this.getFromCache( clientCode );
114         if ( serviceContract == null )
115         {
116             serviceContract = this.getActiveServiceContractFromDatabase( clientCode );
117             this.put( clientCode, serviceContract );
118         }
119         return serviceContract;
120     }
121 
122     private ServiceContract getActiveServiceContractFromDatabase( final String clientCode ) throws ClientAuthorizationException
123     {
124         final List<ServiceContract> serviceContracts = ClientApplicationHome.selectActiveServiceContract( clientCode );
125         if ( CollectionUtils.isEmpty( serviceContracts ) )
126         {
127             throw new ClientAuthorizationException( "No contract service found for client application with code " + clientCode,
128                     Constants.PROPERTY_REST_ERROR_SERVICE_CONTRACT_NOT_FOUND );
129         }
130         else
131             if ( CollectionUtils.size( serviceContracts ) > 1 )
132             {
133                 throw new ClientAuthorizationException(
134                         "There is more than one active service contract for the application with code " + clientCode + ". There must be only one",
135                         Constants.PROPERTY_REST_ERROR_MULTIPLE_ACTIVE_SERVICE_CONTRACTS );
136             }
137 
138         final ServiceContract serviceContract = serviceContracts.get( 0 );
139         serviceContract.setAttributeRights( ServiceContractHome.selectApplicationRights( serviceContract ) );
140         serviceContract.setAttributeCertifications( ServiceContractHome.selectAttributeCertifications( serviceContract ) );
141         serviceContract.setAttributeRequirements( ServiceContractHome.selectAttributeRequirements( serviceContract ) );
142 
143         return serviceContract;
144     }
145 
146     @Override
147     public String getName( )
148     {
149         return SERVICE_NAME;
150     }
151 }