You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@juneau.apache.org by ja...@apache.org on 2017/10/09 23:21:50 UTC
incubator-juneau git commit: Code cleanup.
Repository: incubator-juneau
Updated Branches:
refs/heads/master 9c746e647 -> 593f92b22
Code cleanup.
Project: http://git-wip-us.apache.org/repos/asf/incubator-juneau/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-juneau/commit/593f92b2
Tree: http://git-wip-us.apache.org/repos/asf/incubator-juneau/tree/593f92b2
Diff: http://git-wip-us.apache.org/repos/asf/incubator-juneau/diff/593f92b2
Branch: refs/heads/master
Commit: 593f92b2272c8d384d756c47ac37763c0cf783a6
Parents: 9c746e6
Author: JamesBognar <ja...@apache.org>
Authored: Mon Oct 9 19:21:46 2017 -0400
Committer: JamesBognar <ja...@apache.org>
Committed: Mon Oct 9 19:21:46 2017 -0400
----------------------------------------------------------------------
.../apache/juneau/svl/ResolvingObjectMap.java | 72 +---------------
.../apache/juneau/svl/VarResolverSession.java | 89 +++++++++++++++++++-
.../org/apache/juneau/rest/RestRequest.java | 8 +-
3 files changed, 91 insertions(+), 78 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/593f92b2/juneau-core/juneau-svl/src/main/java/org/apache/juneau/svl/ResolvingObjectMap.java
----------------------------------------------------------------------
diff --git a/juneau-core/juneau-svl/src/main/java/org/apache/juneau/svl/ResolvingObjectMap.java b/juneau-core/juneau-svl/src/main/java/org/apache/juneau/svl/ResolvingObjectMap.java
index 0890e52..a31ae91 100644
--- a/juneau-core/juneau-svl/src/main/java/org/apache/juneau/svl/ResolvingObjectMap.java
+++ b/juneau-core/juneau-svl/src/main/java/org/apache/juneau/svl/ResolvingObjectMap.java
@@ -12,9 +12,6 @@
// ***************************************************************************************************************************
package org.apache.juneau.svl;
-import java.lang.reflect.*;
-import java.util.*;
-
import org.apache.juneau.*;
/**
@@ -32,7 +29,7 @@ import org.apache.juneau.*;
* <p>
* All other data types are left as-is.
*/
-@SuppressWarnings({"serial","unchecked","rawtypes"})
+@SuppressWarnings({"serial"})
public class ResolvingObjectMap extends ObjectMap {
private final VarResolverSession varResolver;
@@ -49,71 +46,6 @@ public class ResolvingObjectMap extends ObjectMap {
@Override /* Map */
public Object get(Object key) {
- return resolve(super.get(key));
- }
-
- private Object resolve(Object o) {
- if (o == null)
- return null;
- if (o instanceof CharSequence)
- return varResolver.resolve(o.toString());
- if (o.getClass().isArray()) {
- if (! containsVars(o))
- return o;
- Object o2 = Array.newInstance(o.getClass().getComponentType(), Array.getLength(o));
- for (int i = 0; i < Array.getLength(o); i++)
- Array.set(o2, i, resolve(Array.get(o, i)));
- return o2;
- }
- if (o instanceof Collection) {
- try {
- Collection c = (Collection)o;
- if (! containsVars(c))
- return o;
- Collection c2 = c.getClass().newInstance();
- for (Object o2 : c)
- c2.add(resolve(o2));
- return c2;
- } catch (Exception e) {
- return o;
- }
- }
- if (o instanceof Map) {
- try {
- Map m = (Map)o;
- if (! containsVars(m))
- return o;
- Map m2 = m.getClass().newInstance();
- for (Map.Entry e : (Set<Map.Entry>)m.entrySet())
- m2.put(e.getKey(), resolve(e.getValue()));
- return m2;
- } catch (Exception e) {
- return o;
- }
- }
- return o;
- }
-
- private static boolean containsVars(Object array) {
- for (int i = 0; i < Array.getLength(array); i++) {
- Object o = Array.get(array, i);
- if (o instanceof CharSequence && o.toString().contains("$"))
- return true;
- }
- return false;
- }
-
- private static boolean containsVars(Collection c) {
- for (Object o : c)
- if (o instanceof CharSequence && o.toString().contains("$"))
- return true;
- return false;
- }
-
- private static boolean containsVars(Map m) {
- for (Object o : m.values())
- if (o instanceof CharSequence && o.toString().contains("$"))
- return true;
- return false;
+ return varResolver.resolve(super.get(key));
}
}
http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/593f92b2/juneau-core/juneau-svl/src/main/java/org/apache/juneau/svl/VarResolverSession.java
----------------------------------------------------------------------
diff --git a/juneau-core/juneau-svl/src/main/java/org/apache/juneau/svl/VarResolverSession.java b/juneau-core/juneau-svl/src/main/java/org/apache/juneau/svl/VarResolverSession.java
index f9da289..65b847b 100644
--- a/juneau-core/juneau-svl/src/main/java/org/apache/juneau/svl/VarResolverSession.java
+++ b/juneau-core/juneau-svl/src/main/java/org/apache/juneau/svl/VarResolverSession.java
@@ -15,6 +15,7 @@ package org.apache.juneau.svl;
import static org.apache.juneau.internal.StringUtils.*;
import java.io.*;
+import java.lang.reflect.*;
import java.util.*;
import org.apache.juneau.*;
@@ -71,7 +72,7 @@ public class VarResolverSession {
/**
* Resolve all variables in the specified string.
*
- * @param s
+ * @param s
* The string to resolve variables in.
* @return
* The new string with all variables resolved, or the same string if no variables were found.
@@ -116,6 +117,92 @@ public class VarResolverSession {
}
}
+ /**
+ * Convenience method for resolving variables in arbitrary objects.
+ *
+ * <p>
+ * Supports resolving variables in the following object types:
+ * <ul>
+ * <li>{@link CharSequence}
+ * <li>Arrays containing values of type {@link CharSequence}.
+ * <li>Collections containing values of type {@link CharSequence}.
+ * <br>Collection class must have a no-arg constructor.
+ * <li>Maps containing values of type {@link CharSequence}.
+ * <br>Map class must have a no-arg constructor.
+ * </ul>
+ *
+ * @param o The object.
+ * @return The same object if no resolution was needed, otherwise a new object or data structure if resolution was
+ * needed.
+ */
+ @SuppressWarnings({ "rawtypes", "unchecked" })
+ public <T> T resolve(T o) {
+ if (o == null)
+ return null;
+ if (o instanceof CharSequence)
+ return (T)resolve(o.toString());
+ if (o.getClass().isArray()) {
+ if (! containsVars(o))
+ return o;
+ Object o2 = Array.newInstance(o.getClass().getComponentType(), Array.getLength(o));
+ for (int i = 0; i < Array.getLength(o); i++)
+ Array.set(o2, i, resolve(Array.get(o, i)));
+ return (T)o2;
+ }
+ if (o instanceof Collection) {
+ try {
+ Collection c = (Collection)o;
+ if (! containsVars(c))
+ return o;
+ Collection c2 = c.getClass().newInstance();
+ for (Object o2 : c)
+ c2.add(resolve(o2));
+ return (T)c2;
+ } catch (Exception e) {
+ return o;
+ }
+ }
+ if (o instanceof Map) {
+ try {
+ Map m = (Map)o;
+ if (! containsVars(m))
+ return o;
+ Map m2 = m.getClass().newInstance();
+ for (Map.Entry e : (Set<Map.Entry>)m.entrySet())
+ m2.put(e.getKey(), resolve(e.getValue()));
+ return (T)m2;
+ } catch (Exception e) {
+ return o;
+ }
+ }
+ return o;
+ }
+
+ private static boolean containsVars(Object array) {
+ for (int i = 0; i < Array.getLength(array); i++) {
+ Object o = Array.get(array, i);
+ if (o instanceof CharSequence && o.toString().contains("$"))
+ return true;
+ }
+ return false;
+ }
+
+ @SuppressWarnings("rawtypes")
+ private static boolean containsVars(Collection c) {
+ for (Object o : c)
+ if (o instanceof CharSequence && o.toString().contains("$"))
+ return true;
+ return false;
+ }
+
+ @SuppressWarnings("rawtypes")
+ private static boolean containsVars(Map m) {
+ for (Object o : m.values())
+ if (o instanceof CharSequence && o.toString().contains("$"))
+ return true;
+ return false;
+ }
+
/*
* Checks to see if string is of the simple form "$X{...}" with no embedded variables.
* This is a common case, and we can avoid using StringWriters.
http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/593f92b2/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/RestRequest.java
----------------------------------------------------------------------
diff --git a/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/RestRequest.java b/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/RestRequest.java
index e635521..30c2180 100644
--- a/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/RestRequest.java
+++ b/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/RestRequest.java
@@ -964,13 +964,7 @@ public final class RestRequest extends HttpServletRequestWrapper {
* @return A copy of the array with variables resolved.
*/
public String[] resolveVars(String[] input) {
- VarResolverSession vs = getVarResolverSession();
- if (input == null || input.length == 0)
- return input;
- input = Arrays.copyOf(input, input.length);
- for (int i = 0; i < input.length; i++)
- input[i] = vs.resolve(input[i]);
- return input;
+ return getVarResolverSession().resolve(input);
}
/**