use of com.mindbright.asn1.ASN1OctetString in project ldapsdk by pingidentity.
the class Filter method hashCode.
/**
* Generates a hash code for this search filter.
*
* @return The generated hash code for this search filter.
*/
@Override()
public int hashCode() {
final CaseIgnoreStringMatchingRule matchingRule = CaseIgnoreStringMatchingRule.getInstance();
int hashCode = filterType;
switch(filterType) {
case FILTER_TYPE_AND:
case FILTER_TYPE_OR:
for (final Filter f : filterComps) {
hashCode += f.hashCode();
}
break;
case FILTER_TYPE_NOT:
hashCode += notComp.hashCode();
break;
case FILTER_TYPE_EQUALITY:
case FILTER_TYPE_GREATER_OR_EQUAL:
case FILTER_TYPE_LESS_OR_EQUAL:
case FILTER_TYPE_APPROXIMATE_MATCH:
hashCode += StaticUtils.toLowerCase(attrName).hashCode();
hashCode += matchingRule.normalize(assertionValue).hashCode();
break;
case FILTER_TYPE_SUBSTRING:
hashCode += StaticUtils.toLowerCase(attrName).hashCode();
if (subInitial != null) {
hashCode += matchingRule.normalizeSubstring(subInitial, MatchingRule.SUBSTRING_TYPE_SUBINITIAL).hashCode();
}
for (final ASN1OctetString s : subAny) {
hashCode += matchingRule.normalizeSubstring(s, MatchingRule.SUBSTRING_TYPE_SUBANY).hashCode();
}
if (subFinal != null) {
hashCode += matchingRule.normalizeSubstring(subFinal, MatchingRule.SUBSTRING_TYPE_SUBFINAL).hashCode();
}
break;
case FILTER_TYPE_PRESENCE:
hashCode += StaticUtils.toLowerCase(attrName).hashCode();
break;
case FILTER_TYPE_EXTENSIBLE_MATCH:
if (attrName != null) {
hashCode += StaticUtils.toLowerCase(attrName).hashCode();
}
if (matchingRuleID != null) {
hashCode += StaticUtils.toLowerCase(matchingRuleID).hashCode();
}
if (dnAttributes) {
hashCode++;
}
hashCode += matchingRule.normalize(assertionValue).hashCode();
break;
}
return hashCode;
}
use of com.mindbright.asn1.ASN1OctetString in project ldapsdk by pingidentity.
the class Filter method toCode.
/**
* Appends a number of lines comprising the Java source code that can be used
* to recreate this filter to the given list. Note that unless a first line
* prefix and/or last line suffix are provided, this will just include the
* code for the static method used to create the filter, starting with
* "Filter.createXFilter(" and ending with the closing parenthesis for that
* method call.
*
* @param lineList The list to which the source code lines should be
* added.
* @param indentSpaces The number of spaces that should be used to indent
* the generated code. It must not be negative.
* @param firstLinePrefix An optional string that should precede the static
* method call (e.g., it could be used for an
* attribute assignment, like "Filter f = "). It may
* be {@code null} or empty if there should be no
* first line prefix.
* @param lastLineSuffix An optional suffix that should follow the closing
* parenthesis of the static method call (e.g., it
* could be a semicolon to represent the end of a
* Java statement). It may be {@code null} or empty
* if there should be no last line suffix.
*/
public void toCode(@NotNull final List<String> lineList, final int indentSpaces, @Nullable final String firstLinePrefix, @Nullable final String lastLineSuffix) {
// Generate a string with the appropriate indent.
final StringBuilder buffer = new StringBuilder();
for (int i = 0; i < indentSpaces; i++) {
buffer.append(' ');
}
final String indent = buffer.toString();
// Start the first line, including any appropriate prefix.
buffer.setLength(0);
buffer.append(indent);
if (firstLinePrefix != null) {
buffer.append(firstLinePrefix);
}
// that type of filter.
switch(filterType) {
case FILTER_TYPE_AND:
case FILTER_TYPE_OR:
if (filterType == FILTER_TYPE_AND) {
buffer.append("Filter.createANDFilter(");
} else {
buffer.append("Filter.createORFilter(");
}
if (filterComps.length == 0) {
buffer.append(')');
if (lastLineSuffix != null) {
buffer.append(lastLineSuffix);
}
lineList.add(buffer.toString());
return;
}
for (int i = 0; i < filterComps.length; i++) {
String suffix;
if (i == (filterComps.length - 1)) {
suffix = ")";
if (lastLineSuffix != null) {
suffix += lastLineSuffix;
}
} else {
suffix = ",";
}
filterComps[i].toCode(lineList, indentSpaces + 5, null, suffix);
}
return;
case FILTER_TYPE_NOT:
buffer.append("Filter.createNOTFilter(");
lineList.add(buffer.toString());
final String suffix;
if (lastLineSuffix == null) {
suffix = ")";
} else {
suffix = ')' + lastLineSuffix;
}
notComp.toCode(lineList, indentSpaces + 5, null, suffix);
return;
case FILTER_TYPE_PRESENCE:
buffer.append("Filter.createPresenceFilter(");
lineList.add(buffer.toString());
buffer.setLength(0);
buffer.append(indent);
buffer.append(" \"");
buffer.append(attrName);
buffer.append("\")");
if (lastLineSuffix != null) {
buffer.append(lastLineSuffix);
}
lineList.add(buffer.toString());
return;
case FILTER_TYPE_EQUALITY:
case FILTER_TYPE_GREATER_OR_EQUAL:
case FILTER_TYPE_LESS_OR_EQUAL:
case FILTER_TYPE_APPROXIMATE_MATCH:
if (filterType == FILTER_TYPE_EQUALITY) {
buffer.append("Filter.createEqualityFilter(");
} else if (filterType == FILTER_TYPE_GREATER_OR_EQUAL) {
buffer.append("Filter.createGreaterOrEqualFilter(");
} else if (filterType == FILTER_TYPE_LESS_OR_EQUAL) {
buffer.append("Filter.createLessOrEqualFilter(");
} else {
buffer.append("Filter.createApproximateMatchFilter(");
}
lineList.add(buffer.toString());
buffer.setLength(0);
buffer.append(indent);
buffer.append(" \"");
buffer.append(attrName);
buffer.append("\",");
lineList.add(buffer.toString());
buffer.setLength(0);
buffer.append(indent);
buffer.append(" ");
if (StaticUtils.isSensitiveToCodeAttribute(attrName)) {
buffer.append("\"---redacted-value---\"");
} else if (StaticUtils.isPrintableString(assertionValue.getValue())) {
buffer.append('"');
buffer.append(assertionValue.stringValue());
buffer.append('"');
} else {
StaticUtils.byteArrayToCode(assertionValue.getValue(), buffer);
}
buffer.append(')');
if (lastLineSuffix != null) {
buffer.append(lastLineSuffix);
}
lineList.add(buffer.toString());
return;
case FILTER_TYPE_SUBSTRING:
buffer.append("Filter.createSubstringFilter(");
lineList.add(buffer.toString());
buffer.setLength(0);
buffer.append(indent);
buffer.append(" \"");
buffer.append(attrName);
buffer.append("\",");
lineList.add(buffer.toString());
final boolean isRedacted = StaticUtils.isSensitiveToCodeAttribute(attrName);
boolean isPrintable = true;
if (subInitial != null) {
isPrintable = StaticUtils.isPrintableString(subInitial.getValue());
}
if (isPrintable && (subAny != null)) {
for (final ASN1OctetString s : subAny) {
if (!StaticUtils.isPrintableString(s.getValue())) {
isPrintable = false;
break;
}
}
}
if (isPrintable && (subFinal != null)) {
isPrintable = StaticUtils.isPrintableString(subFinal.getValue());
}
buffer.setLength(0);
buffer.append(indent);
buffer.append(" ");
if (subInitial == null) {
buffer.append("null");
} else if (isRedacted) {
buffer.append("\"---redacted-subInitial---\"");
} else if (isPrintable) {
buffer.append('"');
buffer.append(subInitial.stringValue());
buffer.append('"');
} else {
StaticUtils.byteArrayToCode(subInitial.getValue(), buffer);
}
buffer.append(',');
lineList.add(buffer.toString());
buffer.setLength(0);
buffer.append(indent);
buffer.append(" ");
if ((subAny == null) || (subAny.length == 0)) {
buffer.append("null,");
lineList.add(buffer.toString());
} else if (isRedacted) {
buffer.append("new String[]");
lineList.add(buffer.toString());
lineList.add(indent + " {");
for (int i = 0; i < subAny.length; i++) {
buffer.setLength(0);
buffer.append(indent);
buffer.append(" \"---redacted-subAny-");
buffer.append(i + 1);
buffer.append("---\"");
if (i < (subAny.length - 1)) {
buffer.append(',');
}
lineList.add(buffer.toString());
}
lineList.add(indent + " },");
} else if (isPrintable) {
buffer.append("new String[]");
lineList.add(buffer.toString());
lineList.add(indent + " {");
for (int i = 0; i < subAny.length; i++) {
buffer.setLength(0);
buffer.append(indent);
buffer.append(" \"");
buffer.append(subAny[i].stringValue());
buffer.append('"');
if (i < (subAny.length - 1)) {
buffer.append(',');
}
lineList.add(buffer.toString());
}
lineList.add(indent + " },");
} else {
buffer.append("new String[]");
lineList.add(buffer.toString());
lineList.add(indent + " {");
for (int i = 0; i < subAny.length; i++) {
buffer.setLength(0);
buffer.append(indent);
buffer.append(" ");
StaticUtils.byteArrayToCode(subAny[i].getValue(), buffer);
if (i < (subAny.length - 1)) {
buffer.append(',');
}
lineList.add(buffer.toString());
}
lineList.add(indent + " },");
}
buffer.setLength(0);
buffer.append(indent);
buffer.append(" ");
if (subFinal == null) {
buffer.append("null)");
} else if (isRedacted) {
buffer.append("\"---redacted-subFinal---\")");
} else if (isPrintable) {
buffer.append('"');
buffer.append(subFinal.stringValue());
buffer.append("\")");
} else {
StaticUtils.byteArrayToCode(subFinal.getValue(), buffer);
buffer.append(')');
}
if (lastLineSuffix != null) {
buffer.append(lastLineSuffix);
}
lineList.add(buffer.toString());
return;
case FILTER_TYPE_EXTENSIBLE_MATCH:
buffer.append("Filter.createExtensibleMatchFilter(");
lineList.add(buffer.toString());
buffer.setLength(0);
buffer.append(indent);
buffer.append(" ");
if (attrName == null) {
buffer.append("null, // Attribute Description");
} else {
buffer.append('"');
buffer.append(attrName);
buffer.append("\",");
}
lineList.add(buffer.toString());
buffer.setLength(0);
buffer.append(indent);
buffer.append(" ");
if (matchingRuleID == null) {
buffer.append("null, // Matching Rule ID");
} else {
buffer.append('"');
buffer.append(matchingRuleID);
buffer.append("\",");
}
lineList.add(buffer.toString());
buffer.setLength(0);
buffer.append(indent);
buffer.append(" ");
buffer.append(dnAttributes);
buffer.append(", // DN Attributes");
lineList.add(buffer.toString());
buffer.setLength(0);
buffer.append(indent);
buffer.append(" ");
if ((attrName != null) && StaticUtils.isSensitiveToCodeAttribute(attrName)) {
buffer.append("\"---redacted-value---\")");
} else {
if (StaticUtils.isPrintableString(assertionValue.getValue())) {
buffer.append('"');
buffer.append(assertionValue.stringValue());
buffer.append("\")");
} else {
StaticUtils.byteArrayToCode(assertionValue.getValue(), buffer);
buffer.append(')');
}
}
if (lastLineSuffix != null) {
buffer.append(lastLineSuffix);
}
lineList.add(buffer.toString());
return;
}
}
use of com.mindbright.asn1.ASN1OctetString in project ldapsdk by pingidentity.
the class IntermediateResponse method readFrom.
/**
* Creates a new intermediate response object with the provided message ID and
* with the protocol op and controls read from the given ASN.1 stream reader.
*
* @param messageID The LDAP message ID for the LDAP message that is
* associated with this intermediate response.
* @param messageSequence The ASN.1 stream reader sequence used in the
* course of reading the LDAP message elements.
* @param reader The ASN.1 stream reader from which to read the
* protocol op and controls.
*
* @return The decoded intermediate response.
*
* @throws LDAPException If a problem occurs while reading or decoding data
* from the ASN.1 stream reader.
*/
@NotNull()
static IntermediateResponse readFrom(final int messageID, @NotNull final ASN1StreamReaderSequence messageSequence, @NotNull final ASN1StreamReader reader) throws LDAPException {
try {
String oid = null;
ASN1OctetString value = null;
final ASN1StreamReaderSequence opSequence = reader.beginSequence();
while (opSequence.hasMoreElements()) {
final byte type = (byte) reader.peek();
switch(type) {
case TYPE_INTERMEDIATE_RESPONSE_OID:
oid = reader.readString();
break;
case TYPE_INTERMEDIATE_RESPONSE_VALUE:
value = new ASN1OctetString(type, reader.readBytes());
break;
default:
throw new LDAPException(ResultCode.DECODING_ERROR, ERR_INTERMEDIATE_RESPONSE_INVALID_ELEMENT.get(StaticUtils.toHex(type)));
}
}
final Control[] controls;
if (messageSequence.hasMoreElements()) {
final ArrayList<Control> controlList = new ArrayList<>(1);
final ASN1StreamReaderSequence controlSequence = reader.beginSequence();
while (controlSequence.hasMoreElements()) {
controlList.add(Control.readFrom(reader));
}
controls = new Control[controlList.size()];
controlList.toArray(controls);
} else {
controls = NO_CONTROLS;
}
return new IntermediateResponse(messageID, oid, value, controls);
} catch (final LDAPException le) {
Debug.debugException(le);
throw le;
} catch (final Exception e) {
Debug.debugException(e);
throw new LDAPException(ResultCode.DECODING_ERROR, ERR_INTERMEDIATE_RESPONSE_CANNOT_DECODE.get(StaticUtils.getExceptionMessage(e)), e);
}
}
use of com.mindbright.asn1.ASN1OctetString in project ldapsdk by pingidentity.
the class EXTERNALBindRequest method process.
/**
* Sends this bind request to the target server over the provided connection
* and returns the corresponding response.
*
* @param connection The connection to use to send this bind request to the
* server and read the associated response.
* @param depth The current referral depth for this request. It should
* always be one for the initial request, and should only
* be incremented when following referrals.
*
* @return The bind response read from the server.
*
* @throws LDAPException If a problem occurs while sending the request or
* reading the response.
*/
@Override()
@NotNull()
protected BindResult process(@NotNull final LDAPConnection connection, final int depth) throws LDAPException {
// Create the LDAP message.
messageID = connection.nextMessageID();
final ASN1OctetString creds;
if (authzID == null) {
creds = null;
} else {
creds = new ASN1OctetString(authzID);
}
return sendBindRequest(connection, "", creds, getControls(), getResponseTimeoutMillis(connection));
}
use of com.mindbright.asn1.ASN1OctetString in project ldapsdk by pingidentity.
the class Entry method toLDIFString.
/**
* Appends an LDIF-formatted string representation of this entry to the
* provided buffer. No extra blank lines will be added.
*
* @param buffer The buffer to which to append the LDIF representation
* of this entry.
* @param wrapColumn The column at which long lines should be wrapped. A
* value less than or equal to two indicates that no
* wrapping should be performed.
*/
@Override()
public final void toLDIFString(@NotNull final StringBuilder buffer, final int wrapColumn) {
LDIFWriter.encodeNameAndValue("dn", new ASN1OctetString(dn), buffer, wrapColumn);
buffer.append(StaticUtils.EOL);
for (final Attribute a : attributes.values()) {
final String name = a.getName();
if (a.hasValue()) {
for (final ASN1OctetString value : a.getRawValues()) {
LDIFWriter.encodeNameAndValue(name, value, buffer, wrapColumn);
buffer.append(StaticUtils.EOL);
}
} else {
LDIFWriter.encodeNameAndValue(name, EMPTY_OCTET_STRING, buffer, wrapColumn);
buffer.append(StaticUtils.EOL);
}
}
}
Aggregations