Class TextInjector

java.lang.Object
overit.geocall.util.TextInjector

public class TextInjector extends Object
The class is used to inject into a string, that contains a reference to a file, a part or the entire contents of the referenced file. The string to be injected will replace the string portion containing the reference.
To create a reference to a file within a string, it is necessary to insert the content inside square brackets and insert as the first attribute 'inject'.

The allowed attributes are:
  • 'inject': reference to the file to be used in the search for the string to be extrapolated. This must be the first of the attributes and is the only one required;
  • 'encoding': encoding to be used to interpret the file. If not set the default system is taken;
  • 'line': line number to be extrapolated; If the line number is greater than the length of the file, is returned an empty string. To be used as an alternative to the 'var' attribute;
  • 'var': variable to search within the file. If a match is found, the associated value is returned; otherwise return an empty string. To be used alternatively to the 'line' attribute.
  • 'where': can be "fs", that is the default value, to define that the reference to the file is an external reference, "cp" to define that the reference to the file is a classpath or "awsSecret" to define that the reference of the value must be fetched from AWS Secrets Manager service
The character to separate the name of the attribute from its value is ':'. Each attribute-value pair must be separated from the others with the character ';' and spaces must not be inserted.

In case you want to use the mode with the reference to a variable, the file must comply with the rules for defining the Properties.
If neither the 'line' attribute nor the 'var' attribute is inserted, it will be loaded entirely the file.

Below is an example to clarify its use:

Examples
"text [inject:etc/file.conf] text"Inject all text of the file
"text [inject:/overit/geocall/basic/Password.properties;where:cp] text"Inject all the text of the property file located in the classpath overit.geocall.basic
"text [inject:etc/file.conf;encoding:UTF-8] text"Inject all text of the file forcing the encoding "UTF-8"
"text [inject:etc/file.conf;line:10] text"Inject the line 10 of the file
"text [inject:etc/file.conf;var:VARNAME;encoding:UTF-8] text"Inject the value of the variable VARNAME of the file forcing the encoding "UTF-8"
"text [inject:/overit/geocall/basic/Password.properties;var:VARNAME;encoding:UTF-8;where:cp] text"Inject the value of the variable VARNAME of the property file located in the classpath overit.geocall.basic, forcing the encoding "UTF-8"
"text [inject:etc/file1.conf;var:VARNAME] text [inject:etc/file2.txt;line:10] text"Inject the value of the variable VARNAME extracted from the "file1.conf" and inject the line 10 extracted from the "file2.txt"
"text [inject:my-secret;where:awsSecret] text"Fetch the secrets from AWS Secret manager and inject the my-secret key's value
  • Field Details

  • Constructor Details

    • TextInjector

      public TextInjector(String s)
      Creates an instance of the TextInjector.
      Parameters:
      s - String contains a reference to a file. If the string is null, the method inject() will return null; if it not contains a reference, the method will return the same string.
    • TextInjector

      public TextInjector(String s, String cache)
      Creates an instance of the TextInjector, that will use a cache mechanism, basing the injections on the cache with the name passed as parameter
      Parameters:
      s - String contains a reference to a file. If the string is null, the method inject() will return null; if it not contains a reference, the method will return the same string.
      cache - The name of the cache to use to retrieve and store the injections
  • Method Details

    • inject

      public String inject()
      Resolve the references contained in the string used to create the instance (see TextInjector()), injecting the string extracted from the file referenced. If requested, it uses the cache
      Returns:
      The string with the reference resolved. If the string is null return null; if it not contains a reference, return the same string.
    • parse

      protected String parse(String s)
    • parse

      protected String parse(String s, String start, String end)
      Injects the string, loading the value from the referenced file
      Parameters:
      s - The string to parse
      start - The start delimiter
      end - The end delimiter
      Returns:
      The string injected
    • doInject

      public String doInject(String injection) throws IOException
      Choose the mode used.
      Parameters:
      injection - String in the format: "inject:..."
      Returns:
      The string resolved
      Throws:
      IOException - in case of injection IO errors like for example not founding injection file
    • loadInject

      protected String loadInject(String path, Charset encoding, String variable, String where) throws IOException
      Load the value of the variable read from the file.
      Parameters:
      path - Path of the property file
      encoding - Encoding used in reading
      variable - The name of the variable
      where - Defines if the path is a classpath ("cp") or an external path ("fs", default)
      Returns:
      The string resolved
      Throws:
      IOException - in case of injection IO errors like for example not founding injection file
    • loadInject

      protected String loadInject(String path, Charset encoding, int l, String where) throws IOException
      Load the line read from the file.
      Parameters:
      path - Path of the file
      encoding - Encoding used in reading
      l - Line number
      where - Defines if the path is a classpath ("cp") or an external path ("fs", default)
      Returns:
      The string resolved
      Throws:
      IOException - in case of injection IO errors like for example not founding injection file
    • loadInject

      protected String loadInject(String path, Charset encoding, String where) throws IOException
      Load the entire file.
      Parameters:
      path - Path of the file
      encoding - Encoding used in reading
      where - Defines if the path is a classpath ("cp") or an external path ("fs", default)
      Returns:
      The string resolved
      Throws:
      IOException - in case of injection IO errors like for example not founding injection file