Skip to content

Commit

Permalink
#26391 possible fix in 23.01.8 LTS
Browse files Browse the repository at this point in the history
  • Loading branch information
erickgonzalez committed Oct 12, 2023
1 parent e144ab6 commit 0c143c2
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 17 deletions.
16 changes: 9 additions & 7 deletions dotCMS/src/main/java/com/dotcms/rest/api/CorsFilter.java
Expand Up @@ -30,16 +30,17 @@ public class CorsFilter implements ContainerResponseFilter {

public CorsFilter() {
Map<String, List<String[]>> loadingMap = new HashMap<>();
Config.subset(CORS_PREFIX ).forEachRemaining(key -> {
final String[] splitter = key.split("\\.", 2);
final String mapping = splitter[0];
final String header = fixHeaderCase(splitter[1]);
final List<String> props = Config.subsetContainsAsList(CORS_PREFIX);
props.forEach(key -> {
final String[] splitter = key.split("_", 5);
final String mapping = splitter[4];
final String header = fixHeaderCase(splitter[5]);
List<String[]> keys = loadingMap.getOrDefault(mapping, new ArrayList<>());

keys.add(new String[] {header, Config.getStringProperty(CORS_PREFIX + "." + key, "")});
keys.add(new String[] {header, Config.getStringProperty(key, "")});

loadingMap.put(mapping, keys);

});
this.headerMap = ImmutableMap.copyOf(loadingMap);
}
Expand Down Expand Up @@ -73,8 +74,9 @@ protected List<String[]> getHeaders(final String mapping) {
}


protected final String fixHeaderCase(final String propertyName) {
protected final String fixHeaderCase(String propertyName) {

propertyName = propertyName.toLowerCase().replace("_","-");
final StringWriter sw = new StringWriter();
boolean upperCaseNextChar = true;
for (final char c : propertyName.toCharArray()) {
Expand Down
50 changes: 40 additions & 10 deletions dotCMS/src/main/java/com/dotmarketing/util/Config.java
Expand Up @@ -8,11 +8,10 @@
import java.lang.reflect.Array;
import java.net.URL;
import java.nio.file.Files;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.*;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.stream.Collectors;

import org.apache.commons.configuration.Configuration;
import org.apache.commons.configuration.PropertiesConfiguration;
import com.dotcms.repackage.com.google.common.base.Supplier;
Expand Down Expand Up @@ -311,14 +310,45 @@ private static void readEnvironmentVariables() {
}


private static String envKey(final String theKey) {
public static String envKey(final String theKey) {

String envKey = ENV_PREFIX + theKey.toUpperCase().replace(".", "_");
while (envKey.contains("__")) {
envKey = envKey.replace("__", "_");
}
return envKey.endsWith("_") ? envKey.substring(0, envKey.length() - 1) : envKey;
if(!theKey.startsWith(ENV_PREFIX)) {
String envKey = ENV_PREFIX + theKey.toUpperCase().replace(".", "_").replace("-","_");
while (envKey.contains("__")) {
envKey = envKey.replace("__", "_");
}
return envKey.endsWith("_") ? envKey.substring(0, envKey.length() - 1) : envKey;
}
return theKey;

}

/**
* Returns a list of properties that contains the given String.
* Also gives priority to the System Env over the ones in the properties file.
* @param containsString
* @return list of properties
*/
public static List<String> subsetContainsAsList(final String containsString){
final List<String> fullListProps = new ArrayList<String>();
props.getKeys().forEachRemaining(fullListProps::add);

//List with all system env props that contains the pattern
final String envContainsString = envKey(containsString);
final List<String> propList = fullListProps.stream().filter(prop -> prop.contains(envContainsString)).collect(Collectors.toList());

//List with all props with . (dotmarketing.properties) that contains the pattern
final List<String> configList = fullListProps.stream().filter(prop -> prop.contains(containsString)).collect(Collectors.toList());

//Final list union of the env list + configList which aren't set by envList
for(final String prop : configList){
final String keyRefactor = envKey(prop);
if(!propList.contains(keyRefactor)){
propList.add(prop);
}
}

return propList;
}

/**
Expand Down

0 comments on commit 0c143c2

Please sign in to comment.