View Javadoc
1   /*
2    * Copyright (c) 2002-2022, 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.util.env;
35  
36  import java.io.IOException;
37  import java.nio.file.Files;
38  import java.nio.file.Paths;
39  import java.util.Map;
40  import java.util.regex.Matcher;
41  import java.util.regex.Pattern;
42  
43  /**
44   * EnvUtil
45   */
46  public final class EnvUtil
47  {
48      public static final String PREFIX_ENV = "env.";
49      private static final String SUFFIX_FILE = "_FILE";
50      private static final String PATTERN_MARKER = "\\$\\{(.+?)\\}";
51  
52      private static Pattern _pattern = Pattern.compile( PATTERN_MARKER );
53  
54      private static Map<String, String> _mapEnv = System.getenv( );
55  
56      // Private constructor
57      private EnvUtil( )
58      {
59      }
60  
61      /**
62       * Evaluate a string that may contain a reference to an environement variable enclosed by ${...}
63       * 
64       * @param strSource
65       *            The source string
66       * @return The source string or the env variable's value if found.
67       */
68      public static String evaluate( String strSource )
69      {
70          return evaluate( strSource, "" );
71      }
72  
73      /**
74       * Evaluate a string that may contain a reference to an environement variable enclosed by ${...}. If the environement variable has a suffix _FILE the
75       * evaluation will take the content of the file as value.
76       * 
77       * @param strSource
78       *            The source string
79       * @param strEnvPrefix
80       *            A prefix of the environment
81       * @return The source string or the env variable's value if found.
82       */
83      public static String evaluate( String strSource, String strEnvPrefix )
84      {
85          String strOutput = ( strSource != null ) ? strSource : "";
86          Matcher matcher = _pattern.matcher( strOutput );
87          int offset = 0;
88  
89          while ( matcher.find( ) )
90          {
91              String strMarker = matcher.group( );
92              String strEnvVariable = strMarker.substring( 2 + strEnvPrefix.length( ), strMarker.length( ) - 1 );
93              String strValue = _mapEnv.get( strEnvVariable );
94              if ( strEnvVariable.endsWith( SUFFIX_FILE ) )
95              {
96                  strValue = getFileContent( strValue );
97              }
98              strValue = strValue == null ? "" : strValue;
99              strOutput = strOutput.substring( 0, matcher.start( ) + offset ) + strValue + strOutput.substring( matcher.end( ) + offset );
100             offset += strValue.length( ) - strMarker.length( );
101         }
102         return strOutput;
103     }
104 
105     /**
106      * Mock Env map for unit testing
107      * 
108      * @param mapEnv
109      *            a mock map
110      */
111     static void setMockMapEnv( Map<String, String> mapEnv )
112     {
113         _mapEnv = mapEnv;
114     }
115 
116     /**
117      * Gets the content of a file as a String
118      * 
119      * @param strFilePath
120      *            The file path
121      * @return The file content or the error message while reading the message
122      */
123     private static String getFileContent( String strFilePath )
124     {
125         String strData;
126         try
127         {
128             strData = new String( Files.readAllBytes( Paths.get( strFilePath ) ) );
129         }
130         catch( IOException e )
131         {
132             return e.getMessage( );
133         }
134         return strData;
135     }
136 
137 }