[lsc-changes] r290 - in lsc/branches/v1.1/src/main/java/org/lsc: beans jndi utils

jclarke at lsc-project.org jclarke at lsc-project.org
Fri Jul 17 14:34:04 CEST 2009


Author: jclarke
Date: 2009-07-17 14:34:04 +0200 (Fri, 17 Jul 2009)
New Revision: 290

Modified:
   lsc/branches/v1.1/src/main/java/org/lsc/beans/AbstractBean.java
   lsc/branches/v1.1/src/main/java/org/lsc/jndi/JndiServices.java
   lsc/branches/v1.1/src/main/java/org/lsc/utils/LocalizedJndiModificationsLayout.java
Log:
Fixes #110. Don't massacre byte[] into String when reading from source. Use Strings for display, however (bean.get*Value and logs)

Modified: lsc/branches/v1.1/src/main/java/org/lsc/beans/AbstractBean.java
===================================================================
--- lsc/branches/v1.1/src/main/java/org/lsc/beans/AbstractBean.java	2009-07-08 07:22:22 UTC (rev 289)
+++ lsc/branches/v1.1/src/main/java/org/lsc/beans/AbstractBean.java	2009-07-17 12:34:04 UTC (rev 290)
@@ -267,22 +267,7 @@
             Iterator<?> iter = values.iterator();
 
             while (iter.hasNext()) {
-            	Object valueObject = iter.next();
-            	
-            	// get a String from the value, whatever syntax it is in
-            	String value = null;
-            	if (valueObject instanceof byte[]) {
-            		value = new String((byte[])valueObject);
-            	} else {
-            		value = (String) valueObject;
-            	}
-                
-            	// clean up value
-            	value = value.trim();
-
-                if (value.length() > 0) {
-                    attr.add(value);
-                }
+                attr.add(iter.next());
             }
         }
 
@@ -473,7 +458,14 @@
 		Attribute attribute = getAttributeById(id);
 		for (int i = 0; attribute != null && i < attribute.size(); i++)
 		{
-			resultsArray.add(attribute.get(i).toString());
+			Object value = attribute.get(i);
+			String stringValue;
+			
+			// convert to String because this method only returns Strings
+			if (value instanceof byte[]) stringValue = new String((byte[]) value);
+			else stringValue = value.toString();
+				
+			resultsArray.add(stringValue);
 		}
 
 		return resultsArray;

Modified: lsc/branches/v1.1/src/main/java/org/lsc/jndi/JndiServices.java
===================================================================
--- lsc/branches/v1.1/src/main/java/org/lsc/jndi/JndiServices.java	2009-07-08 07:22:22 UTC (rev 289)
+++ lsc/branches/v1.1/src/main/java/org/lsc/jndi/JndiServices.java	2009-07-17 12:34:04 UTC (rev 290)
@@ -325,33 +325,37 @@
      */
     public SearchResult getEntry(final String base, final String filter, 
             final SearchControls sc, final int scope) throws NamingException {
+    	//sanity checks
+    	String searchBase = base == null ? "" : base;
+    	String searchFilter = filter == null ? DEFAULT_FILTER : filter;
+    	
         NamingEnumeration<SearchResult> ne = null;
         try {
             sc.setSearchScope(scope);
             String rewrittenBase = null;
-            if (contextDn != null && base.toLowerCase().endsWith(contextDn.toLowerCase())) {
-                if (!base.equalsIgnoreCase(contextDn)) {
-                    rewrittenBase = base.substring(0, base.toLowerCase()
+            if (contextDn != null && searchBase.toLowerCase().endsWith(contextDn.toLowerCase())) {
+                if (!searchBase.equalsIgnoreCase(contextDn)) {
+                    rewrittenBase = searchBase.substring(0, searchBase.toLowerCase()
                             .lastIndexOf(contextDn.toLowerCase()) - 1);
                 } else {
                     rewrittenBase = "";
                 }
             } else {
-                rewrittenBase = base;
+                rewrittenBase = searchBase;
             }
-            ne = ctx.search(rewrittenBase, filter, sc);
+            ne = ctx.search(rewrittenBase, searchFilter, sc);
         } catch (NamingException nex) {
-            LOGGER.error("Error while looking for " + filter + " in " + base + ": " + nex);
+            LOGGER.error("Error while looking for " + searchFilter + " in " + searchBase + ": " + nex);
             throw nex;
         }
         SearchResult sr = null;
         if (ne.hasMoreElements()) {
             sr = (SearchResult) ne.nextElement();
             if (ne.hasMoreElements()) {
-                LOGGER.error("Too many entries returned (base: \"" + base
-                        + "\", filter: \"" + filter + "\"");
-                throw new SizeLimitExceededException("Too many entries returned (base: \"" + base
-                        + "\", filter: \"" + filter + "\"");
+                LOGGER.error("Too many entries returned (base: \"" + searchBase
+                        + "\", filter: \"" + searchFilter + "\"");
+                throw new SizeLimitExceededException("Too many entries returned (base: \"" + searchBase
+                        + "\", filter: \"" + searchFilter + "\"");
             } else {
                 return sr;
             }
@@ -714,6 +718,10 @@
             final String filter, final int scope, final List<String> attrsNames) 
             throws NamingException {
 
+        // sanity checks
+        String searchBase = base == null ? "" : base;
+        String searchFilter = filter == null ? DEFAULT_FILTER : filter;
+    	
         Map<String, LscAttributes> res = new HashMap<String, LscAttributes>();
     	
     	if (attrsNames == null || attrsNames.size() == 0) {
@@ -750,7 +758,7 @@
 
             byte[] pagedResultsResponse = null;
             do {
-                NamingEnumeration<SearchResult> results = ctx.search(base, filter, constraints);
+                NamingEnumeration<SearchResult> results = ctx.search(searchBase, searchFilter, constraints);
 
                 if (results != null) {
                     Map<String, String> attrsValues = null;

Modified: lsc/branches/v1.1/src/main/java/org/lsc/utils/LocalizedJndiModificationsLayout.java
===================================================================
--- lsc/branches/v1.1/src/main/java/org/lsc/utils/LocalizedJndiModificationsLayout.java	2009-07-08 07:22:22 UTC (rev 289)
+++ lsc/branches/v1.1/src/main/java/org/lsc/utils/LocalizedJndiModificationsLayout.java	2009-07-17 12:34:04 UTC (rev 290)
@@ -203,7 +203,7 @@
 
 					// print value and base64 encode it if it's not a
 					// SAFE-STRING per RFC2849
-					String value = ne.next().toString();
+					String value = getStringValue(ne.next());
 					sb.append((isLdifSafeString(value) ? ": " + value : ":: " + toBase64(value)));
 
 					// new line
@@ -217,6 +217,17 @@
         return sb.toString();
     }
 
+	private String getStringValue(Object value) {
+		if (value instanceof byte[])
+		{
+			return new String((byte[]) value);
+		}
+		else
+		{
+			return value.toString();
+		}
+	}
+
 	private String toBase64(String value)
 	{
 		return new String(new Base64().encode(value.getBytes()));



More information about the lsc-changes mailing list