1 28 package net.sf.jguard.ext.util; 29 30 import java.util.logging.Logger ; 31 32 import javax.naming.NamingEnumeration ; 33 import javax.naming.NamingException ; 34 import javax.naming.directory.Attribute ; 35 import javax.naming.directory.SearchResult ; 36 37 38 public class JNDIUtils { 39 private static final Logger logger = Logger.getLogger(JNDIUtils.class.getName()); 40 41 50 public static String getValueInNameSpace(SearchResult result, int level) { 51 String value = null; 52 String nameSpace = result.getName(); 53 String [] tokens = nameSpace.split(","); 54 String [] tok = tokens[level].split("="); 55 value = tok[1]; 56 return value; 57 58 } 59 60 66 public static String escapeDn(String name) { 67 final char[] META_CHARS = { '+', '"', '<', '>', ';', '/' }; 69 String escapedString = new String (name); 70 escapedString = escapedString.replaceAll("\\\\", "\\\\"); 72 73 escapedString = escapedString.replaceAll("^#", "\\\\#"); 75 escapedString = escapedString.replaceAll("^ | $", "\\\\ "); 76 77 for (int i = 0; i < META_CHARS.length; i++) { 78 escapedString = escapedString.replaceAll("\\" + META_CHARS[i], "\\\\" + META_CHARS[i]); 79 } 80 return escapedString; 81 } 82 83 89 public static String escapeSearchFilter(String filterExpression) { 90 String escapedString = new String (filterExpression); 92 escapedString = escapedString.replaceAll("\\\\", "\\\\5c"); 93 escapedString = escapedString.replaceAll("\\*", "\\\\2a"); 94 escapedString = escapedString.replaceAll("\\(", "\\\\28"); 95 escapedString = escapedString.replaceAll("\\)", "\\\\29"); 96 return escapedString; 97 } 98 99 100 101 108 public static String getAttributeValue(Attribute attribute) throws NamingException { 109 NamingEnumeration nameEnum = null; 110 StringBuffer attributeValue = new StringBuffer (); 111 nameEnum = attribute.getAll(); 112 113 int i = 0; 114 115 while (nameEnum.hasMore()) { 116 if (i != 0) { 117 attributeValue.append(","); 118 } 119 Object obj = (Object ) nameEnum.next(); 120 if (obj instanceof byte[]) { 121 byte[] bytes = (byte[]) obj; 122 obj = new String (bytes); 123 } 124 125 attributeValue.append(obj.toString()); 126 i++; 127 } 128 129 nameEnum.close(); 130 131 return attributeValue.toString(); 132 } 133 134 135 } 136 | Popular Tags |