Search in sources :

Example 11 with StrSubstitutor

use of org.apache.commons.lang.text.StrSubstitutor in project ddf by codice.

the class AbstractIntegrationTest method getFileContent.

/**
 * Variables to be replaced in a resource file should be in the format: $variableName$ The
 * variable to replace in the file should also also match the parameter names of the method
 * calling getFileContent.
 *
 * @param filePath
 * @param params
 * @param classRelativeToResource
 * @return
 */
@SuppressWarnings({ "squid:S00112" /* A generic RuntimeException is perfectly reasonable in this case. */
})
public static String getFileContent(String filePath, ImmutableMap<String, String> params, Class classRelativeToResource) {
    StrSubstitutor strSubstitutor = new StrSubstitutor(params);
    strSubstitutor.setVariablePrefix(RESOURCE_VARIABLE_DELIMETER);
    strSubstitutor.setVariableSuffix(RESOURCE_VARIABLE_DELIMETER);
    String fileContent;
    try {
        fileContent = IOUtils.toString(classRelativeToResource.getClassLoader().getResourceAsStream(filePath), "UTF-8");
    } catch (IOException e) {
        throw new RuntimeException("Failed to read filepath: " + filePath);
    }
    return strSubstitutor.replace(fileContent);
}
Also used : StrSubstitutor(org.apache.commons.lang.text.StrSubstitutor) IOException(java.io.IOException)

Example 12 with StrSubstitutor

use of org.apache.commons.lang.text.StrSubstitutor in project coprhd-controller by CoprHD.

the class LocalCassandraService method createConfig.

/**
 * Generates a configuration for running a local cassandra instance.
 *
 * @param configResource
 *            the configuration resource name.
 * @param rootDir
 *            the root directory for the cassandra instance.
 * @return the configuration location.
 *
 * @throws IOException
 *             if an I/O error occurs.
 */
private static String createConfig(String configResource, String rootDir) throws IOException {
    URL configURL = LocalCassandraService.class.getResource(configResource);
    if (configURL == null) {
        throw new IllegalStateException("Could not find " + configResource);
    }
    File path = FileUtils.toFile(configURL);
    String data = FileUtils.readFileToString(path, "UTF-8");
    StrSubstitutor substitutor = new StrSubstitutor(Collections.singletonMap("rootDir", rootDir));
    String contents = substitutor.replace(data);
    File configFile = File.createTempFile("config", ".yaml");
    configFile.deleteOnExit();
    FileUtils.writeStringToFile(configFile, contents, "UTF-8");
    return "file:" + configFile.getAbsolutePath();
}
Also used : StrSubstitutor(org.apache.commons.lang.text.StrSubstitutor) File(java.io.File) URL(java.net.URL)

Example 13 with StrSubstitutor

use of org.apache.commons.lang.text.StrSubstitutor in project coprhd-controller by CoprHD.

the class TestDbService method createConfig.

private static String createConfig(String rootDir) throws IOException {
    URL configURL = TestDbService.class.getResource("db-test.yaml");
    if (configURL == null) {
        throw new IllegalStateException("Could not find cassandra.yaml");
    }
    File path = FileUtils.toFile(configURL);
    String data = FileUtils.readFileToString(path, "UTF-8");
    StrSubstitutor substitutor = new StrSubstitutor(Collections.singletonMap("rootDir", rootDir));
    String contents = substitutor.replace(data);
    File configFile = File.createTempFile("db-test", ".yaml");
    configFile.deleteOnExit();
    FileUtils.writeStringToFile(configFile, contents, "UTF-8");
    return "file:" + configFile.getAbsolutePath();
}
Also used : StrSubstitutor(org.apache.commons.lang.text.StrSubstitutor) File(java.io.File) URL(java.net.URL)

Example 14 with StrSubstitutor

use of org.apache.commons.lang.text.StrSubstitutor in project mica2 by obiba.

the class MailService method getSubject.

public String getSubject(String subjectFormat, Map<String, String> ctx, String defaultSubject) {
    StrSubstitutor sub = new StrSubstitutor(ctx, "${", "}");
    String temp = // 
    Optional.ofNullable(subjectFormat).filter(// 
    s -> !s.isEmpty()).orElse(defaultSubject);
    return sub.replace(temp);
}
Also used : Async(org.springframework.scheduling.annotation.Async) UriComponentsBuilder(org.springframework.web.util.UriComponentsBuilder) Logger(org.slf4j.Logger) HttpHeaders(org.springframework.http.HttpHeaders) Collection(java.util.Collection) MediaType(org.springframework.http.MediaType) LoggerFactory(org.slf4j.LoggerFactory) HttpMethod(org.springframework.http.HttpMethod) URLEncoder.encode(java.net.URLEncoder.encode) Strings(com.google.common.base.Strings) HttpEntity(org.springframework.http.HttpEntity) Collectors.toList(java.util.stream.Collectors.toList) Stream(java.util.stream.Stream) Service(org.springframework.stereotype.Service) Map(java.util.Map) StrSubstitutor(org.apache.commons.lang.text.StrSubstitutor) Optional(java.util.Optional) ResponseEntity(org.springframework.http.ResponseEntity) UnsupportedEncodingException(java.io.UnsupportedEncodingException) RestTemplate(org.springframework.web.client.RestTemplate) Joiner(com.google.common.base.Joiner) StrSubstitutor(org.apache.commons.lang.text.StrSubstitutor)

Example 15 with StrSubstitutor

use of org.apache.commons.lang.text.StrSubstitutor in project kylo by Teradata.

the class PropertyExpressionResolver method resolveVariables.

/**
 * Resolve the str with values from the supplied {@code properties} This will recursively fill out the str looking back at the properties to get the correct value. NOTE the property values will be
 * overwritten if replacement is found!
 */
public ResolvedVariables resolveVariables(String str, List<NifiProperty> properties) {
    ResolvedVariables variables = new ResolvedVariables(str);
    StrLookup resolver = new StrLookup() {

        @Override
        public String lookup(String key) {
            Optional<NifiProperty> optional = properties.stream().filter(prop -> key.equals(prop.getKey())).findFirst();
            if (optional.isPresent()) {
                NifiProperty property = optional.get();
                String value = StringUtils.isNotBlank(property.getValue()) ? property.getValue().trim() : "";
                variables.getResolvedVariables().put(property.getKey(), value);
                return value;
            } else {
                return null;
            }
        }
    };
    StrSubstitutor ss = new StrSubstitutor(resolver);
    variables.setResolvedString(ss.replace(str));
    Map<String, String> map = variables.getResolvedVariables();
    Map<String, String> vars = map.entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey, entry -> ss.replace(entry.getValue())));
    variables.getResolvedVariables().putAll(vars);
    return variables;
}
Also used : PropertyUtils(org.apache.commons.beanutils.PropertyUtils) SpringEnvironmentProperties(com.thinkbiganalytics.spring.SpringEnvironmentProperties) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) HashMap(java.util.HashMap) FeedMetadata(com.thinkbiganalytics.feedmgr.rest.model.FeedMetadata) StringUtils(org.apache.commons.lang3.StringUtils) StrLookup(org.apache.commons.lang.text.StrLookup) Lists(com.google.common.collect.Lists) ConfigurationPropertyReplacer(com.thinkbiganalytics.nifi.feedmgr.ConfigurationPropertyReplacer) Map(java.util.Map) Nonnull(javax.annotation.Nonnull) Nullable(javax.annotation.Nullable) Logger(org.slf4j.Logger) MetadataFieldAnnotationFieldNameResolver(com.thinkbiganalytics.feedmgr.MetadataFieldAnnotationFieldNameResolver) MetadataFields(com.thinkbiganalytics.feedmgr.MetadataFields) NifiProperty(com.thinkbiganalytics.nifi.rest.model.NifiProperty) Collection(java.util.Collection) MetadataField(com.thinkbiganalytics.metadata.MetadataField) Collectors(java.util.stream.Collectors) AnnotatedFieldProperty(com.thinkbiganalytics.annotations.AnnotatedFieldProperty) List(java.util.List) StrSubstitutor(org.apache.commons.lang.text.StrSubstitutor) Optional(java.util.Optional) Collections(java.util.Collections) StrSubstitutor(org.apache.commons.lang.text.StrSubstitutor) StrLookup(org.apache.commons.lang.text.StrLookup) NifiProperty(com.thinkbiganalytics.nifi.rest.model.NifiProperty) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

StrSubstitutor (org.apache.commons.lang.text.StrSubstitutor)20 File (java.io.File)3 Map (java.util.Map)3 StrLookup (org.apache.commons.lang.text.StrLookup)3 IOException (java.io.IOException)2 URL (java.net.URL)2 Collection (java.util.Collection)2 HashMap (java.util.HashMap)2 Optional (java.util.Optional)2 Logger (org.slf4j.Logger)2 LoggerFactory (org.slf4j.LoggerFactory)2 Context (com.datatorrent.api.Context)1 StreamingContainer (com.datatorrent.stram.engine.StreamingContainer)1 PTOperator (com.datatorrent.stram.plan.physical.PTOperator)1 Joiner (com.google.common.base.Joiner)1 Strings (com.google.common.base.Strings)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 Lists (com.google.common.collect.Lists)1 VirtualFile (com.intellij.openapi.vfs.VirtualFile)1 AnnotatedFieldProperty (com.thinkbiganalytics.annotations.AnnotatedFieldProperty)1