View Javadoc
1   /*
2    * Copyright (c) 2002-2023, 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.temporarycode.utils;
35  
36  import java.io.IOException;
37  import java.sql.Timestamp;
38  import java.util.Calendar;
39  
40  import org.apache.commons.lang3.RandomStringUtils;
41  
42  import com.fasterxml.jackson.core.JsonGenerationException;
43  import com.fasterxml.jackson.databind.JsonMappingException;
44  import com.fasterxml.jackson.databind.ObjectMapper;
45  
46  import fr.paris.lutece.plugins.temporarycode.business.EnumCharacterType;
47  import fr.paris.lutece.plugins.temporarycode.business.TemporaryCodeConfig;
48  import fr.paris.lutece.portal.service.util.AppLogService;
49  import fr.paris.lutece.util.json.AbstractJsonResponse;
50  
51  /**
52   * 
53   * TemporaryCodeUtils
54   *
55   */
56  public class TemporaryCodeUtils
57  {
58      
59      /**
60       * Private constructor
61       */
62      private TemporaryCodeUtils ( )
63      {
64          //Do nothing
65      }
66      
67      //ObjectMapper
68      private static ObjectMapper _mapper;
69      
70      /**
71       * Generate code
72       * @param config
73       * @return the code generated
74       */
75      public static String generateCode( TemporaryCodeConfig config )
76      {
77          int length = config.getCodeLength( );      
78          boolean useLetters = EnumCharacterType.ALPHANUMERIC.name( ).equals( config.getCharacterType( ) );
79          boolean useNumbers = EnumCharacterType.NUMERIC.name( ).equals( config.getCharacterType( ) );
80          
81          if( useLetters )
82          {
83              useNumbers = true;
84          }
85          
86          return RandomStringUtils.random( length, useLetters, useNumbers ).toLowerCase( );      
87      }
88      
89      /**
90       * Add minute to date
91       * @param config
92       * @return timsestamp
93       */
94      public static Timestamp addMinuteToDate ( TemporaryCodeConfig config  )
95      {
96          Calendar c = Calendar.getInstance( );
97          c.add( Calendar.MINUTE, + config.getValidityTime( ) );
98  
99          return new Timestamp( c.getTime( ).getTime( ) );
100     }
101      
102     /**
103      * return a string containing the JSON flow
104      * @param jsonResponse the JSON Response Object
105      * @return return a string containing the JSON flow
106      */
107     public static String buildJsonResponse( AbstractJsonResponse jsonResponse )
108     {
109         String strJsonResponse = null;
110 
111         if ( _mapper == null )
112         {
113             initMapper(  );
114         }
115 
116         try
117         {
118             strJsonResponse = _mapper.writeValueAsString( jsonResponse );
119         }
120         catch ( JsonGenerationException e )
121         {
122             AppLogService.error( e );
123         }
124         catch ( JsonMappingException e )
125         {
126             AppLogService.error( e );
127         }
128         catch ( IOException e )
129         {
130             AppLogService.error( e );
131         }
132 
133         return strJsonResponse;
134     }
135 
136     private static void initMapper(  )
137     {
138         _mapper = new ObjectMapper(  );
139         //_mapper.setPropertyNamingStrategy( PropertyNamingStrategy. );
140     }
141 }