📄 schemaops.java
字号:
if (bracket_pos != -1 && bracket_pos < quote_pos) // multiple names...
{
bracket_pos = ldapSyntaxDescription.indexOf(')', bracket_pos); // get end bracket pos
ArrayList newList = new ArrayList(5);
while (quote_pos < bracket_pos && quote_pos != -1) // iterate through grabbing 'quoted' substrings until we get to the end of the bracketed expression
{
int start = ++quote_pos;
quote_pos = ldapSyntaxDescription.indexOf('\'', quote_pos);
String temp = ldapSyntaxDescription.substring(start, quote_pos);
newList.add(temp);
quote_pos++;
quote_pos = ldapSyntaxDescription.indexOf('\'', quote_pos);
}
return (String[]) newList.toArray(new String[]{});
}
else // return the single name
{
quote_pos++;
int next_quote = ldapSyntaxDescription.indexOf('\'', quote_pos);
String temp = ldapSyntaxDescription.substring(quote_pos, next_quote);
return new String[]{temp};
}
}
catch (StringIndexOutOfBoundsException e)
{
log.log(Level.WARNING, "unable to parse line: " + ldapSyntaxDescription, e);
return new String[]{"syntax_error"};
}
}
/**
* Takes a DXAttributes set representing attribute schema defs,
* and translates the oids into human friendly strings...
*/
/*
protected Attributes addAttributeInfo(Attributes attdefs)
{
try
{
Attribute syntax = attdefs.get("SYNTAX"); // get syntax attribute
String oid = syntax.get().toString(); // convert oid value to string
if (oid.indexOf('{') > -1)
oid = oid.substring(0, oid.indexOf('{'));
String syntaxdesc = (String) oids.get(oid); // look up description for oid
attdefs.put("(SYNTAX-DESC)", syntaxdesc); // stick in synthetic attribute
return attdefs;
}
catch (NamingException e) { return attdefs; }
catch (NullPointerException e) { return attdefs; }
}
*/
/**
* This returns a sorted list of all known object classes, as read from the schema
*
* @return an array list of strings
* @throws NamingException
*/
public ArrayList objectClasses()
throws NamingException
{
if (fullObjectClassArray == null)
{
ArrayList temp = listEntryNames("schema=objectClasses,cn=schema");
if (temp == null)
throw new NamingException("unable to read list of object classes from schema");
String[] OCs = (String[]) temp.toArray(new String[temp.size()]);
Arrays.sort(OCs, new Comparator()
{
public int compare(Object a, Object b)
{
return ((String) a).compareToIgnoreCase((String) b);
}
public boolean equals(Object a, Object b)
{
return ((String) a).equalsIgnoreCase((String) b);
}
});
int size = OCs.length;
fullObjectClassArray = new ArrayList(size);
for (int i = 0; i < size; i++)
fullObjectClassArray.add(i, OCs[i]);
}
return fullObjectClassArray;
}
/**
* This returns a sorted list of all known attribute names, as read from the schema
*
* @return an array list of strings
* @throws NamingException
*/
public ArrayList attributeNames()
throws NamingException
{
if (fullAttributeNameArray == null)
{
ArrayList temp = listEntryNames("schema=attributeTypes,cn=schema");
if (temp == null)
throw new NamingException("unable to read list of attribute types from schema");
String[] ATs = (String[]) temp.toArray(new String[temp.size()]);
Arrays.sort(ATs, new Comparator()
{
public int compare(Object a, Object b)
{
return ((String) a).compareToIgnoreCase((String) b);
}
public boolean equals(Object a, Object b)
{
return ((String) a).equalsIgnoreCase((String) b);
}
});
int size = ATs.length;
fullAttributeNameArray = new ArrayList(size);
for (int i = 0; i < size; i++)
fullAttributeNameArray.add(i, ATs[i]);
}
return fullAttributeNameArray;
}
/**
* Gets a list of the object classes most likely
* to be used for the next Level of the DN...
*
* @param dn the dn of the parent to determine likely
* child object classes for
* @return list of recommended object classes...
*/
public ArrayList getRecommendedObjectClasses(String dn)
{
try
{
if ((dn != null) && ctx != null)
{
Attributes atts = ctx.getAttributes(dn);
if (atts == null)
{
log.log(Level.WARNING, "error reading object classes for " + dn);
}
else
{
Attribute objectClasses = atts.get("objectclass");
if (objectClasses == null) // hack. Shouldn't have to do this. Bad Server! Spank!
objectClasses = atts.get("objectClass");
if (objectClasses == null) // still! - try 'oc'
objectClasses = atts.get("oc");
if (objectClasses == null) // aargh! Give up.
{
log.log(Level.WARNING, "unable to recognize object classes for " + dn);
}
else
{
NamingEnumeration names = objectClasses.getAll();
if (names == null)
log.log(Level.WARNING, "object class has no attributes!");
ArrayList returnArray = new ArrayList(10);
while (names.hasMore())
returnArray.add(names.next());
return returnArray;
}
}
}
}
catch (NamingException e)
{
log.log(Level.WARNING, "error reading object classes for " + dn + "\n internal error III: ", e);
}
return null;
}
/**
* Gets a list of all attribute definitions that have a 'binary' syntax
* (currently defined as: SYNTAX = 1.3.6.1.4.1.1466.115.121.1.5, 1.3.6.1.4.1.1466.115.121.1.40,
* 1.3.6.1.4.1.1466.115.121.1.28 or 1.3.6.1.4.1.1466.115.121.1.8)
*
* @return list of space separated attribute names as a string array - used to set ctx.addToEnvironment("java.naming.ldap.attributes.binary", -String- );
*/
// Performance note - this might be sped up a tad if required...
public String getNewBinaryAttributes()
{
if (rawSchemaAttributes == null)
return "";
try
{
StringBuffer binaryAttributeList = new StringBuffer(1000);
Attribute rawSyntaxAttribute = getAttributeTypes();
if (rawSyntaxAttribute == null)
return "";
NamingEnumeration values = rawSyntaxAttribute.getAll();
while (values.hasMore())
{
String attributeDescription = (String) values.next(); // something like "( 1.3.6.1.4.1.453.7.6.2.1 NAME 'mhsX400Domain' SYNTAX 1.3.6.1.4.1.1466.115.121.1.5 )"
if ((attributeDescription.indexOf("1.3.6.1.4.1.1466.115.121.1.5 ") > 0) || // binary
(attributeDescription.indexOf("1.3.6.1.4.1.1466.115.121.1.40") > 0) || // octet string
(attributeDescription.indexOf("1.3.6.1.4.1.1466.115.121.1.28") > 0) || // jpeg
(attributeDescription.indexOf("1.3.6.1.4.1.1466.115.121.1.8") > 0)) // certificate
{
String[] names = getNames(attributeDescription);
for (int i = 0; i < names.length; i++)
{
binaryAttributeList.append(names[i]);
binaryAttributeList.append(' ');
}
}
}
return binaryAttributeList.toString();
}
catch (NamingException e)
{
log.log(Level.WARNING, "unable to get binary attributes from schema", e);
return "";
}
}
/**
* Convenience method to get the objectClasses Attribute, which represents the syntax of
* attributeTypes.
*
* @return an Attribute with multiple values, each representing a particular attributeType
* represented as complex string of values that must be parsed: e.g. the surname value might be:
* 2.5.4.4 NAME ( 'sn' 'surname' ) SYNTAX 1.3.6.1.4.1.1466.115.121.1.15
*/
public Attribute getAttributeTypes()
{
return rawSchemaAttributes.get("attributeTypes");
}
/**
* Finds the syntax of the corresponding attribute
*
* @param attID the undecorated attribute name - e.g. 'commonName'
* @return returns the attribute syntax, or null if not found.
*/
public String getAttributeSyntax(String attID)
{
if(attID == null)
return null;
if (attID.indexOf(';') > 0)
attID = attID.substring(0, attID.indexOf(';')); //TE: for example: userCertificate;binary.
return schemaLookup("schema=" + attID + ",schema=attributeTypes", "SYNTAX");
}
/**
* Looks up a particular value in a particular schema attribute
*
* @param entryName the entry to lookup; e.g. 'schema=person, schema=objectClass'
* @param schemaAttribute the actual field to look up, e.g. "DESC"
* @return the looked up value (or the first one found, if multiple)
*/
public String schemaLookup(String entryName, String schemaAttribute)
{
entryName = mangleEntryName(entryName);
try
{
Attributes schemaAtts = getAttributes(entryName);
Attribute schemaAtt = schemaAtts.get(schemaAttribute);
String att = (String) schemaAtt.get();
return att;
}
catch (NamingException e)
{
log.log(Level.WARNING, "unable to get value for " + entryName + " value: " + schemaAttribute, e);
}
catch (NullPointerException e2)
{
if ("DESC".equals(schemaAttribute) == false) // ignore frequent error encountered searching for option 'DESC' schema paramater
log.log(Level.WARNING, "unable to read any schema entry for " + entryName + "and attribute: " + schemaAttribute, e2);
}
catch (NoSuchElementException e)
{
if ("DESC".equals(schemaAttribute) == false) // ignore frequent error encountered searching for option 'DESC' schema paramater
log.log(Level.WARNING, "unable to read any schema entry for " + entryName + "and attribute: " + schemaAttribute, e);
}
return null;
}
public String getNameOfObjectClassAttribute()
{
return schemaLookup("schema=objectClass,schema=attributeTypes", "NAME");
}
/**
* Tries to determine if the attribute is a SINGLE-VALUE attribute.
* The rawSchemaAttributes Attributes object represents each attribute
* similar to
* <br><br>
* ( 1.3.6.1.4.1.3327.77.4.1.2 NAME 'uNSPSCTitle' SYNTAX 1.3.6.1.4.1.1466.115.121.1.15 SINGLE-VALUE )
* <br><br>
* This method gets each value (see above string) in the attribute, then each of their names in turn
* until the name of the attribute we are seeking is found. Then checks if 'SINGLE-VALUE'
* is present in that value.
*
* @param name the name of the attribute, for example uNSPCSTitle.
* @return true if the attribute is a SINGLE-VALUE attribute, false otherwise.
*/
public boolean isAttributeSingleValued(String name)
{/* TE */
if (rawSchemaAttributes == null)
return false;
try
{
Attribute attributeTypes = getAttributeTypes();
NamingEnumeration en = attributeTypes.getAll();
while (en.hasMore())
{
String attr = (String) en.next();
String[] attrName = getNames(attr);
for (int i = 0; i < attrName.length; i++)
if (attrName[i].equals(name) && (attr.indexOf("SINGLE-VALUE") >= 0))
return true;
}
}
catch (NamingException e)
{
log.log(Level.WARNING, "Unable to determine if attribute '" + name + "' is SINGLE-VALUE." + e);
}
return false;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -