Skip to content

Commit

Permalink
#26391 include in 22.03.11
Browse files Browse the repository at this point in the history
  • Loading branch information
erickgonzalez committed Nov 9, 2023
1 parent 06cf8c4 commit e1c72f9
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,47 @@ private static void removeStaticFinalAndSetValue(Field field, Object value) thro
field.set(null, value);
}

/**
* Method to test {@link Config#envKey(String)}
* Checks if the method is converting the key to env key successfully.
*/
@Test
public void test_envKey_keyWithoutPrefix_returnsEnvKey() {
final String MY_ENV_VAR_PROPERTY = "my.env.var.property";
final String convertedProperty = Config.envKey(MY_ENV_VAR_PROPERTY);

assertTrue(convertedProperty.contains("DOT_"));
assertFalse(convertedProperty.contains("."));
}

/**
* Method to test {@link Config#envKey(String)}
* If the key is already an Env Key it doesn't need to be converted.
*/
@Test
public void test_envKey_keyWithPrefix_returnsEnvKey() {
final String DOT_MY_ENV_VAR_PROPERTY = "DOT_MY_ENV_VAR_PROPERTY";
final String convertedProperty = Config.envKey(DOT_MY_ENV_VAR_PROPERTY);

assertTrue(convertedProperty.contains("DOT_"));
assertFalse(convertedProperty.contains("."));
assertFalse(convertedProperty.contains("DOT_DOT_"));
}

/**
* Method to test {@link Config#subsetContainsAsList(String)}
* Pull out all the properties that contains the given string, in this case testSubset.
*/
@Test
public void test_subsetContainsAsList(){
Config.props.addProperty("DOT_testSubset_anyKey","anyValue");
Config.props.addProperty("testSubset.anyKey","anyValue");

final List<String> properties = Config.subsetContainsAsList("testSubset");
assertTrue(properties.size()>=2);
assertTrue(properties.contains("DOT_testSubset_anyKey"));
assertTrue(properties.contains("testSubset.anyKey"));
}



Expand Down
15 changes: 9 additions & 6 deletions dotCMS/src/main/java/com/dotcms/rest/api/CorsFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,15 @@ 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 convertedKeyToEnvKey = Config.envKey(key);
final String[] splitter = convertedKeyToEnvKey.split("_", 5);
final String mapping = splitter[3].toLowerCase();
final String header = fixHeaderCase(splitter[4]);
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);

Expand Down Expand Up @@ -73,8 +75,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
53 changes: 42 additions & 11 deletions dotCMS/src/main/java/com/dotmarketing/util/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,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.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 @@ -307,16 +307,47 @@ private static void readEnvironmentVariables() {
.forEach(e -> props.setProperty(e.getKey(), e.getValue()));
}
}


private 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;

public static String envKey(final String theKey) {

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
3 changes: 2 additions & 1 deletion hotfix_tracking.md
Original file line number Diff line number Diff line change
Expand Up @@ -206,4 +206,5 @@ This maintenance release includes the following code fixes:
172. https://github.com/dotCMS/core/issues/25411 : Showing a binary field to showFields in a Relationship Field shows path instead of thumbnail #25411
173. https://github.com/dotCMS/core/issues/25827 : Recreating a field with same name diff type uses the same id #25827
174. https://github.com/dotCMS/core/issues/25870 : Using showFields field variable replicates title to all items #25870
175. https://github.com/dotCMS/core/issues/26374 : Use of Png filter on images results in a 404 #26374
175. https://github.com/dotCMS/core/issues/26374 : Use of Png filter on images results in a 404 #26374
176. https://github.com/dotCMS/core/issues/26391 : custom REST CORS header configuration is not applied to responses #26391

0 comments on commit e1c72f9

Please sign in to comment.