View Javadoc
1   /*
2    * Copyright (c) 2002-2025, 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.accountgenerator.web.request;
35  
36  import fr.paris.lutece.plugins.accountgenerator.service.IdentityAccountGeneratorService;
37  import fr.paris.lutece.plugins.identitystore.v3.web.rs.AbstractIdentityStoreRequest;
38  import fr.paris.lutece.plugins.identitystore.v3.web.rs.dto.account.generator.AccountGenerationDto;
39  import fr.paris.lutece.plugins.identitystore.v3.web.rs.dto.account.generator.AccountGenerationRequest;
40  import fr.paris.lutece.plugins.identitystore.v3.web.rs.dto.account.generator.AccountGenerationResponse;
41  import fr.paris.lutece.plugins.identitystore.v3.web.rs.dto.account.generator.GeneratedAccountDto;
42  import fr.paris.lutece.plugins.identitystore.v3.web.rs.util.Constants;
43  import fr.paris.lutece.plugins.identitystore.v3.web.rs.util.ResponseStatusFactory;
44  import fr.paris.lutece.plugins.identitystore.web.exception.*;
45  import fr.paris.lutece.portal.service.util.AppPropertiesService;
46  
47  import java.util.List;
48  
49  public class AccountGeneratorCreateRequest extends AbstractIdentityStoreRequest
50  {
51      private static final int generationLimit = AppPropertiesService.getPropertyInt( "accountgenerator.generation.limit", 50 );
52      private static String JSON_EXAMPLE = "{\n" + "    \"generation\": {\n" + "        \"generateAccount\": true,\n"
53              + "        \"generationPattern\": \"XYZ\",\n" + "        \"generationIncrementOffset\": 30,\n" + "        \"nbDaysOfValidity\": 300,\n"
54              + "        \"batchSize\": 50\n" + "    }\n" + "}";
55      private final AccountGenerationRequest _accountGenerationRequest;
56  
57      public AccountGeneratorCreateRequest( AccountGenerationRequest request, String strClientAppCode, String authorName, String authorType )
58              throws IdentityStoreException
59      {
60          super( strClientAppCode, authorName, authorType );
61          _accountGenerationRequest = request;
62      }
63  
64      @Override
65      protected void fetchResources() throws ResourceNotFoundException {
66  
67      }
68  
69      @Override
70      protected void validateRequestFormat() throws RequestFormatException {
71          if ( _accountGenerationRequest == null || _accountGenerationRequest.getAccountGenerationDto( ) == null )
72          {
73              throw new RequestFormatException( "The request must specify an account generation. I.e: " + JSON_EXAMPLE, Constants.PROPERTY_REST_ERROR_ACCOUNT_GENERATION_REQUEST_FORMAT );
74          }
75      }
76  
77      @Override
78      protected void validateClientAuthorization() throws ClientAuthorizationException {
79  
80      }
81  
82      @Override
83      protected void validateResourcesConsistency() throws ResourceConsistencyException {
84  
85      }
86  
87      @Override
88      protected void formatRequestContent() throws RequestContentFormattingException {
89  
90      }
91  
92      @Override
93      protected void checkDuplicatesConsistency() throws DuplicatesConsistencyException {
94  
95      }
96  
97      @Override
98      protected AccountGenerationResponse doSpecificRequest( ) throws IdentityStoreException
99      {
100         final AccountGenerationResponse response = new AccountGenerationResponse( );
101         final AccountGenerationDto accountGenerationDto = _accountGenerationRequest.getAccountGenerationDto( );
102         String message = "";
103         final int batchSize = Math.min( accountGenerationDto.getBatchSize( ), generationLimit );
104         if ( batchSize != accountGenerationDto.getBatchSize( ) )
105         {
106             accountGenerationDto.setBatchSize( batchSize );
107             message = "The number of accounts that can be generated in a single request is limited to " + generationLimit
108                     + ", so the batch size has been override to this value.";
109         }
110 
111         final List<GeneratedAccountDto> generatedAccounts = IdentityAccountGeneratorService.instance( ).createIdentityAccountBatch( accountGenerationDto );
112         response.setGeneratedAccountList( generatedAccounts );
113         response.setStatus( ResponseStatusFactory.success( ) );
114         final long accountsCount = generatedAccounts.stream( ).filter( GeneratedAccountDto::hasAccount ).count( );
115         final long identitiesCount = generatedAccounts.stream( ).filter( GeneratedAccountDto::hasIdentity ).count( );
116         response.getStatus( ).setMessage(
117                 message + " Generated " + accountsCount + " accounts and/or " + identitiesCount + " identities. See each account status if some are missing." );
118         return response;
119     }
120 }