Search in sources :

Example 1 with DefaultDERBuffer

use of org.ldaptive.asn1.DefaultDERBuffer in project ldaptive by vt-middleware.

the class DefaultDnParser method parse.

/**
 * Parses the supplied DN into a list of RDNs.
 *
 * @param  dn  to parse
 *
 * @return  unmodifiable list of RDNs
 */
public List<RDn> parse(final String dn) {
    if (dn.trim().isEmpty()) {
        return Collections.emptyList();
    }
    final List<RDn> rdns = new ArrayList<>();
    final List<NameValue> nameValues = new ArrayList<>();
    int pos = 0;
    while (pos < dn.length()) {
        final int[] endAttrNamePos = readToChar(dn, new char[] { '=' }, pos);
        final String attrName = dn.substring(pos, endAttrNamePos[0]).trim();
        if (attrName.isEmpty()) {
            throw new IllegalArgumentException("Invalid RDN: no attribute name found for " + dn);
        } else if (attrName.contains("+") || attrName.contains(",")) {
            throw new IllegalArgumentException("Invalid RDN: unexpected '" + attrName.charAt(0) + "' for " + dn);
        }
        pos = endAttrNamePos[0];
        // error if char isn't an '='
        if (pos >= dn.length() || dn.charAt(pos++) != '=') {
            throw new IllegalArgumentException("Invalid RDN: no equals found for " + dn);
        }
        final int[] endAttrValuePos = readToChar(dn, new char[] { '+', ',' }, pos);
        final String attrValue = dn.substring(pos, endAttrValuePos[0]).trim();
        if (attrValue.isEmpty()) {
            nameValues.add(new NameValue(attrName, ""));
        } else if (attrValue.startsWith("#")) {
            final DERParser parser = new DERParser();
            final OctetStringHandler handler = new OctetStringHandler();
            parser.registerHandler(HEX_PATH, handler);
            final String hexData = attrValue.substring(1);
            parser.parse(new DefaultDERBuffer(decodeHexValue(hexData.toCharArray())));
            nameValues.add(new NameValue(attrName, handler.getDecodedValue()));
        } else {
            nameValues.add(new NameValue(attrName, decodeStringValue(attrValue)));
        }
        if (endAttrValuePos[1] == -1 || endAttrValuePos[1] == ',') {
            rdns.add(new RDn(nameValues));
            nameValues.clear();
        }
        pos = endAttrValuePos[0] + 1;
        if (pos == dn.length() && endAttrValuePos[1] != -1) {
            // dangling match character
            throw new IllegalArgumentException("Invalid RDN: attribute value ends with '" + endAttrValuePos[1] + "' for " + dn);
        }
    }
    return Collections.unmodifiableList(rdns);
}
Also used : DefaultDERBuffer(org.ldaptive.asn1.DefaultDERBuffer) ArrayList(java.util.ArrayList) DERParser(org.ldaptive.asn1.DERParser)

Example 2 with DefaultDERBuffer

use of org.ldaptive.asn1.DefaultDERBuffer in project ldaptive by vt-middleware.

the class SyncInfoMessageTest method encode.

/**
 * @param  berValue  encoded response.
 * @param  response  expected decoded response.
 *
 * @throws  Exception  On test failure.
 */
@Test(dataProvider = "response")
public void encode(final byte[] berValue, final SyncInfoMessage response) throws Exception {
    final SyncInfoMessage info = new SyncInfoMessage();
    info.getResponseValueParseHandler().handle(null, new DefaultDERBuffer(berValue));
    Assert.assertEquals(info, response);
}
Also used : DefaultDERBuffer(org.ldaptive.asn1.DefaultDERBuffer) Test(org.testng.annotations.Test)

Example 3 with DefaultDERBuffer

use of org.ldaptive.asn1.DefaultDERBuffer in project ldaptive by vt-middleware.

the class PasswordModifyResponseParser method parse.

/**
 * Parse the supplied extended operation response.
 *
 * @param  response  from a password modify extended operation
 *
 * @return  generated password
 */
public static String parse(final ExtendedResponse response) {
    final StringBuilder sb = new StringBuilder();
    final DERParser p = new DERParser();
    p.registerHandler(GenPasswdHandler.PATH, new GenPasswdHandler(sb));
    p.parse(new DefaultDERBuffer(response.getResponseValue()));
    return sb.toString();
}
Also used : DefaultDERBuffer(org.ldaptive.asn1.DefaultDERBuffer) DERParser(org.ldaptive.asn1.DERParser)

Example 4 with DefaultDERBuffer

use of org.ldaptive.asn1.DefaultDERBuffer in project ldaptive by vt-middleware.

the class ResponseParserTest method parse.

/**
 * @param  berValue  to parse.
 * @param  response  expected response.
 *
 * @throws  Exception  On test failure.
 */
@Test(groups = "transport", dataProvider = "response")
public void parse(final byte[] berValue, final Message response) throws Exception {
    final ResponseParser parser = new ResponseParser();
    Assert.assertEquals(parser.parse(new DefaultDERBuffer(berValue)).get(), response);
}
Also used : DefaultDERBuffer(org.ldaptive.asn1.DefaultDERBuffer) Test(org.testng.annotations.Test)

Example 5 with DefaultDERBuffer

use of org.ldaptive.asn1.DefaultDERBuffer in project ldaptive by vt-middleware.

the class ResponseParserBenchmark method parse.

/**
 * Benchmark {@link ResponseParser#parse(DERBuffer)}.
 *
 * @param  blackhole  to consume objects
 */
@Benchmark
public void parse(final Blackhole blackhole) {
    final ResponseParser parser = new ResponseParser();
    final Optional<Message> message = parser.parse(new DefaultDERBuffer(SEARCH_RESULT));
    blackhole.consume(message.get());
}
Also used : Message(org.ldaptive.Message) DefaultDERBuffer(org.ldaptive.asn1.DefaultDERBuffer) Benchmark(org.openjdk.jmh.annotations.Benchmark)

Aggregations

DefaultDERBuffer (org.ldaptive.asn1.DefaultDERBuffer)7 ArrayList (java.util.ArrayList)3 DERParser (org.ldaptive.asn1.DERParser)3 Test (org.testng.annotations.Test)2 Message (org.ldaptive.Message)1 Dn (org.ldaptive.dn.Dn)1 NameValue (org.ldaptive.dn.NameValue)1 RDn (org.ldaptive.dn.RDn)1 Benchmark (org.openjdk.jmh.annotations.Benchmark)1