Handle byte[] to String conversions for nodes in the binary store
@@ -30,15 +30,14 @@ import org.apache.commons.lang3.reflect.ConstructorUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
+import java.io.UnsupportedEncodingException;
import java.lang.reflect.InvocationTargetException;
import java.security.AccessControlException;
import java.util.Collection;
-import java.util.Collections;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
-import javax.jcr.AccessDeniedException;
import javax.jcr.Node;
import javax.jcr.RepositoryException;
@@ -46,8 +45,6 @@ import javax.jcr.RepositoryException;
*/
public class JcrObject {
- private static final Logger log = LoggerFactory.getLogger(JcrObject.class);
-
protected final Node node;
private String versionName;
@@ -223,6 +220,7 @@ public class JcrObject {
return getPropertyFromNode(this.node, name, type, allowNotFound);
}
+ @SuppressWarnings("unchecked")
public <T> T getPropertyFromNode(Node node, String name, Class<T> type, boolean allowNotFound) {
Object o = JcrPropertyUtil.getProperty(node, name, allowNotFound);
if (allowNotFound && o == null) {
@@ -236,18 +234,21 @@ public class JcrObject {
}
}
if (!o.getClass().isAssignableFrom(type)) {
- //if it cant be matched and it is a Node > JcrObject, do the conversion
+ // conversion for Node to JcrObject
if (o instanceof Node && JcrObject.class.isAssignableFrom(type)) {
return JcrUtil.constructNodeObject((Node) o, type, null);
- } else {
- if(name.toLowerCase().contains("password")) {
- throw new MetadataRepositoryException("Unable to convert Property \"UNKNOWN\" to type " + type);
}
- else {
- throw new MetadataRepositoryException("Unable to convert Property " + name + " to type " + type);
+ // conversion for byte[] to String
+ if (o instanceof byte[] && String.class.equals(type)) {
+ try {
+ return (T) new String((byte[]) o, "UTF-8");
+ } catch (final UnsupportedEncodingException e) {
+ throw new MetadataRepositoryException("Unable to decode String property '" + name + "'", e);
}
-
}
+ // unable to convert
+ final String safeName = name.toLowerCase().contains("password") ? "UNKNOWN" : name;
+ throw new MetadataRepositoryException("Unable to convert Property " + safeName + " to type " + type);
} else {
return (T) o;
}
@@ -260,8 +261,7 @@ public class JcrObject {
// Re-throw with possibly a better message. Filter out passwords
if (name.toLowerCase().contains("password")) {
throw new AccessControlException("You do not have the permission to set property \"UNKNOWN\"");
- }
- else {
+ } else {
throw new AccessControlException("You do not have the permission to set property \"" + name + "\"");
}
}
