Skip to content

Commit

Permalink
LDEV-5138 - parse placeholders in cache/custom and other places using…
Browse files Browse the repository at this point in the history
… url string format before
  • Loading branch information
michaeloffner committed Nov 5, 2024
1 parent 777842c commit e25bd1a
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 67 deletions.
65 changes: 3 additions & 62 deletions core/src/main/java/lucee/runtime/config/ConfigWebFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -2631,7 +2631,6 @@ else if (hasCS) {

{
Struct custom = ConfigWebUtil.getAsStruct(data, true, "custom");

// Workaround for old EHCache class definitions
if (cd.getClassName() != null && cd.getClassName().endsWith(".EHCacheLite")) {
cd = new ClassDefinitionImpl("org.lucee.extension.cache.eh.EHCache");
Expand Down Expand Up @@ -5752,85 +5751,27 @@ public static String getAttr(Struct data, String name) {
return null;
}
if (StringUtil.isEmpty(v)) return "";
return replaceConfigPlaceHolder(v);
return ConfigWebUtil.replaceConfigPlaceHolder(v);
}

public static String getAttr(Struct data, String name, String alias) {
String v = ConfigWebUtil.getAsString(name, data, null);
if (v == null) v = ConfigWebUtil.getAsString(alias, data, null);
if (v == null) return null;
if (StringUtil.isEmpty(v)) return "";
return replaceConfigPlaceHolder(v);
return ConfigWebUtil.replaceConfigPlaceHolder(v);
}

public static String getAttr(Struct data, String[] names) {
String v;
for (String name: names) {
v = ConfigWebUtil.getAsString(name, data, null);
if (!StringUtil.isEmpty(v)) return replaceConfigPlaceHolder(v);
if (!StringUtil.isEmpty(v)) return ConfigWebUtil.replaceConfigPlaceHolder(v);
}
return null;

}

public static String replaceConfigPlaceHolder(String v) {
if (StringUtil.isEmpty(v) || v.indexOf('{') == -1) return v;

int s = -1, e = -1, d = -1;
int prefixLen, start = -1, end;
String _name, _prop;
while ((s = v.indexOf("{system:", start)) != -1 | /* don't change */
(e = v.indexOf("{env:", start)) != -1 | /* don't change */
(d = v.indexOf("${", start)) != -1) {
boolean isSystem = false, isDollar = false;
// system
if (s > -1 && (e == -1 || e > s)) {
start = s;
prefixLen = 8;
isSystem = true;
}
// env
else if (e > -1) {
start = e;
prefixLen = 5;
}
// dollar
else {
start = d;
prefixLen = 2;
isDollar = true;
}

end = v.indexOf('}', start);
/*
* print.edate("----------------"); print.edate(s+"-"+e); print.edate(v); print.edate(start);
* print.edate(end);
*/
if (end > prefixLen) {
_name = v.substring(start + prefixLen, end);
// print.edate(_name);
if (isDollar) {
String[] _parts = _name.split(":");
_prop = SystemUtil.getSystemPropOrEnvVar(_parts[0], (_parts.length > 1) ? _parts[1] : null);
}
else {
_prop = isSystem ? System.getProperty(_name) : System.getenv(_name);
}

if (_prop != null) {
v = new StringBuilder().append(v.substring(0, start)).append(_prop).append(v.substring(end + 1)).toString();
start += _prop.length();
}
else start = end;
}
else start = end; // set start to end for the next round
s = -1;
e = -1; // reset index
d = -1; // I don't think we need this?
}
return v;
}

public static class Path {
public final String str;
public final Resource res;
Expand Down
93 changes: 90 additions & 3 deletions core/src/main/java/lucee/runtime/config/ConfigWebUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
import lucee.runtime.monitor.Monitor;
import lucee.runtime.net.http.ReqRspUtil;
import lucee.runtime.op.Caster;
import lucee.runtime.op.Decision;
import lucee.runtime.osgi.BundleBuilderFactory;
import lucee.runtime.osgi.BundleFile;
import lucee.runtime.osgi.OSGiUtil;
Expand Down Expand Up @@ -825,6 +826,93 @@ protected static FunctionLib[] duplicate(FunctionLib[] flds, boolean deepCopy) {
return rst;
}

public static Object replaceConfigPlaceHolders(Object obj) {
if (obj == null) return obj;

// handle simple value
if (Decision.isSimpleValue(obj)) {
if (obj instanceof CharSequence) return replaceConfigPlaceHolder(obj.toString());
return obj;
}

// handle collection
if (obj instanceof lucee.runtime.type.Collection) {
return replaceConfigPlaceHolders((lucee.runtime.type.Collection) obj);
}

return obj;
}

public static lucee.runtime.type.Collection replaceConfigPlaceHolders(lucee.runtime.type.Collection data) {
if (data == null) return data;

lucee.runtime.type.Collection repl;
if (data instanceof Struct) repl = new StructImpl();
else if (data instanceof Array) repl = new ArrayImpl();
else return data;
Iterator<Entry<Key, Object>> it = data.entryIterator();
Entry<Key, Object> e;
while (it.hasNext()) {
e = it.next();
repl.setEL(e.getKey(), replaceConfigPlaceHolders(e.getValue()));
}
return repl;
}

public static String replaceConfigPlaceHolder(String v) {
if (StringUtil.isEmpty(v) || v.indexOf('{') == -1) return v;

int s = -1, e = -1, d = -1;
int prefixLen, start = -1, end;
String _name, _prop;
while ((s = v.indexOf("{system:", start)) != -1 | /* don't change */
(e = v.indexOf("{env:", start)) != -1 | /* don't change */
(d = v.indexOf("${", start)) != -1) {
boolean isSystem = false, isDollar = false;
// system
if (s > -1 && (e == -1 || e > s)) {
start = s;
prefixLen = 8;
isSystem = true;
}
// env
else if (e > -1) {
start = e;
prefixLen = 5;
}
// dollar
else {
start = d;
prefixLen = 2;
isDollar = true;
}

end = v.indexOf('}', start);
if (end > prefixLen) {
_name = v.substring(start + prefixLen, end);
// print.edate(_name);
if (isDollar) {
String[] _parts = _name.split(":");
_prop = SystemUtil.getSystemPropOrEnvVar(_parts[0], (_parts.length > 1) ? _parts[1] : null);
}
else {
_prop = isSystem ? System.getProperty(_name) : System.getenv(_name);
}

if (_prop != null) {
v = new StringBuilder().append(v.substring(0, start)).append(_prop).append(v.substring(end + 1)).toString();
start += _prop.length();
}
else start = end;
}
else start = end; // set start to end for the next round
s = -1;
e = -1; // reset index
d = -1; // I don't think we need this?
}
return v;
}

public static Array getAsArray(String parent, String child, Struct sct) {
return getAsArray(child, getAsStruct(parent, sct));
}
Expand Down Expand Up @@ -857,19 +945,18 @@ public static Struct getAsStruct(Struct input, boolean allowCSSString, String...
input.put(names[0], sct);
return sct;
}
return sct;
return (Struct) replaceConfigPlaceHolders(sct);
}

public static Struct toStruct(String str) {

Struct sct = new StructImpl(StructImpl.TYPE_LINKED);
try {
String[] arr = ListUtil.toStringArray(ListUtil.listToArrayRemoveEmpty(str, '&'));

String[] item;
for (int i = 0; i < arr.length; i++) {
item = ListUtil.toStringArray(ListUtil.listToArrayRemoveEmpty(arr[i], '='));
if (item.length == 2) sct.setEL(KeyImpl.init(URLDecoder.decode(item[0], true).trim()), URLDecoder.decode(item[1], true));
if (item.length == 2) sct.setEL(KeyImpl.init(URLDecoder.decode(item[0], true).trim()), replaceConfigPlaceHolder(URLDecoder.decode(item[1], true)));
else if (item.length == 1) sct.setEL(KeyImpl.init(URLDecoder.decode(item[0], true).trim()), "");
}
}
Expand Down
2 changes: 1 addition & 1 deletion loader/build.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<project default="core" basedir="." name="Lucee"
xmlns:resolver="antlib:org.apache.maven.resolver.ant">

<property name="version" value="6.1.1.112-SNAPSHOT"/>
<property name="version" value="6.1.1.113-SNAPSHOT"/>

<taskdef uri="antlib:org.apache.maven.resolver.ant" resource="org/apache/maven/resolver/ant/antlib.xml">
<classpath>
Expand Down
2 changes: 1 addition & 1 deletion loader/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

<groupId>org.lucee</groupId>
<artifactId>lucee</artifactId>
<version>6.1.1.112-SNAPSHOT</version>
<version>6.1.1.113-SNAPSHOT</version>
<packaging>jar</packaging>

<name>Lucee Loader Build</name>
Expand Down

0 comments on commit e25bd1a

Please sign in to comment.