Search in sources :

Example 26 with ExtendedResponse

use of org.apache.directory.api.ldap.model.message.ExtendedResponse in project directory-ldap-api by apache.

the class ExtendedResponseTest method testResponseWithMatchedDNAttribute.

/**
 * Test parsing of a response with MatchedDN attribute
 */
@Test
public void testResponseWithMatchedDNAttribute() {
    Dsmlv2ResponseParser parser = null;
    try {
        parser = new Dsmlv2ResponseParser(getCodec());
        parser.setInput(ExtendedResponseTest.class.getResource("response_with_matchedDN_attribute.xml").openStream(), "UTF-8");
        parser.parse();
    } catch (Exception e) {
        fail(e.getMessage());
    }
    ExtendedResponse extendedResponse = (ExtendedResponse) parser.getBatchResponse().getCurrentResponse();
    LdapResult ldapResult = extendedResponse.getLdapResult();
    assertTrue(ldapResult.getMatchedDn().equals("cn=Bob Rush,ou=Dev,dc=Example,dc=COM"));
}
Also used : Dsmlv2ResponseParser(org.apache.directory.api.dsmlv2.Dsmlv2ResponseParser) LdapResult(org.apache.directory.api.ldap.model.message.LdapResult) ExtendedResponse(org.apache.directory.api.ldap.model.message.ExtendedResponse) LdapURLEncodingException(org.apache.directory.api.ldap.model.exception.LdapURLEncodingException) DecoderException(org.apache.directory.api.asn1.DecoderException) AbstractResponseTest(org.apache.directory.api.dsmlv2.AbstractResponseTest) Test(org.junit.Test)

Example 27 with ExtendedResponse

use of org.apache.directory.api.ldap.model.message.ExtendedResponse in project directory-ldap-api by apache.

the class ExtendedResponseDsml method toDsml.

/**
 * {@inheritDoc}
 */
@Override
public Element toDsml(Element root) {
    Element element;
    if (root != null) {
        element = root.addElement(EXTENDED_RESPONSE_TAG);
    } else {
        element = new DefaultElement(EXTENDED_RESPONSE_TAG);
    }
    ExtendedResponse extendedResponse = getDecorated();
    // LDAP Result
    LdapResultDsml ldapResultDsml = new LdapResultDsml(getCodecService(), getDecorated().getLdapResult(), getDecorated());
    ldapResultDsml.toDsml(element);
    // ResponseName
    String responseName = extendedResponse.getResponseName();
    if (responseName != null) {
        element.addElement("responseName").addText(responseName);
    }
    // Response
    Object responseValue = getResponseValue();
    if (responseValue != null) {
        if (ParserUtils.needsBase64Encoding(responseValue)) {
            Namespace xsdNamespace = new Namespace(ParserUtils.XSD, ParserUtils.XML_SCHEMA_URI);
            Namespace xsiNamespace = new Namespace(ParserUtils.XSI, ParserUtils.XML_SCHEMA_INSTANCE_URI);
            element.getDocument().getRootElement().add(xsdNamespace);
            element.getDocument().getRootElement().add(xsiNamespace);
            Element responseElement = element.addElement("response").addText(ParserUtils.base64Encode(responseValue));
            responseElement.addAttribute(new QName("type", xsiNamespace), ParserUtils.XSD + ":" + ParserUtils.BASE64BINARY);
        } else {
            element.addElement("response").addText(Strings.utf8ToString((byte[]) responseValue));
        }
    }
    return element;
}
Also used : DefaultElement(org.dom4j.tree.DefaultElement) ExtendedResponse(org.apache.directory.api.ldap.model.message.ExtendedResponse) QName(org.dom4j.QName) DefaultElement(org.dom4j.tree.DefaultElement) Element(org.dom4j.Element) Namespace(org.dom4j.Namespace)

Example 28 with ExtendedResponse

use of org.apache.directory.api.ldap.model.message.ExtendedResponse in project directory-ldap-api by apache.

the class StartTlsFactory method decorate.

/**
 * {@inheritDoc}
 */
@Override
public StartTlsResponseDecorator decorate(ExtendedResponse decoratedResponse) {
    if (decoratedResponse instanceof StartTlsResponseDecorator) {
        return (StartTlsResponseDecorator) decoratedResponse;
    }
    if (decoratedResponse instanceof StartTlsResponse) {
        return new StartTlsResponseDecorator(codec, (StartTlsResponse) decoratedResponse);
    }
    // It's an opaque extended operation
    @SuppressWarnings("unchecked") ExtendedResponseDecorator<ExtendedResponse> response = (ExtendedResponseDecorator<ExtendedResponse>) decoratedResponse;
    // Decode the response, as it's an opaque operation
    StartTlsResponse startTlsResponse = new StartTlsResponseImpl(response.getMessageId());
    startTlsResponse.getLdapResult().setResultCode(response.getLdapResult().getResultCode());
    startTlsResponse.getLdapResult().setDiagnosticMessage(response.getLdapResult().getDiagnosticMessage());
    return new StartTlsResponseDecorator(codec, new StartTlsResponseImpl());
}
Also used : StartTlsResponseImpl(org.apache.directory.api.ldap.extras.extended.startTls.StartTlsResponseImpl) StartTlsResponse(org.apache.directory.api.ldap.extras.extended.startTls.StartTlsResponse) ExtendedResponse(org.apache.directory.api.ldap.model.message.ExtendedResponse) ExtendedResponseDecorator(org.apache.directory.api.ldap.codec.decorators.ExtendedResponseDecorator)

Example 29 with ExtendedResponse

use of org.apache.directory.api.ldap.model.message.ExtendedResponse in project directory-ldap-api by apache.

the class ExtendedResponseTest method testDecodeExtendedRequestNoName.

/**
 * Test the decoding of a ExtendedRequest with no name
 */
@Test
public void testDecodeExtendedRequestNoName() {
    Asn1Decoder ldapDecoder = new Asn1Decoder();
    ByteBuffer stream = ByteBuffer.allocate(0x0E);
    stream.put(new byte[] { // LDAPMessage ::= SEQUENCE {
    0x30, // LDAPMessage ::= SEQUENCE {
    0x0C, 0x02, 0x01, // messageID MessageID
    0x01, // CHOICE { ..., extendedResp Response, ...
    0x78, // ExtendedResponse ::= [APPLICATION 24] SEQUENCE {
    0x07, // COMPONENTS OF LDAPResult,
    0x0A, 0x01, // LDAPResult ::= SEQUENCE {
    0x00, // },
    0x04, // matchedDN LDAPDN,
    0x00, 0x04, // errorMessage LDAPString,
    0x00 // referral [3] Referral OPTIONAL }
    // responseName [0] LDAPOID,
    });
    String decodedPdu = Strings.dumpBytes(stream.array());
    stream.flip();
    // Allocate a LdapMessage Container
    LdapMessageContainer<ExtendedResponseDecorator<?>> container = new LdapMessageContainer<ExtendedResponseDecorator<?>>(codec);
    // Decode the ExtendedResponse PDU
    try {
        ldapDecoder.decode(stream, container);
    } catch (DecoderException de) {
        de.printStackTrace();
        fail(de.getMessage());
    }
    // Check the decoded ExtendedResponse PDU
    ExtendedResponse extendedResponse = container.getMessage();
    assertEquals(1, extendedResponse.getMessageId());
    assertEquals(ResultCodeEnum.SUCCESS, extendedResponse.getLdapResult().getResultCode());
    assertEquals("", extendedResponse.getLdapResult().getMatchedDn().getName());
    assertEquals("", extendedResponse.getLdapResult().getDiagnosticMessage());
    // Check the encoding
    try {
        ByteBuffer bb = encoder.encodeMessage(extendedResponse);
        // Check the length
        assertEquals(0x0E, bb.limit());
        String encodedPdu = Strings.dumpBytes(bb.array());
        assertEquals(encodedPdu, decodedPdu);
    } catch (EncoderException ee) {
        ee.printStackTrace();
        fail(ee.getMessage());
    }
}
Also used : LdapMessageContainer(org.apache.directory.api.ldap.codec.api.LdapMessageContainer) DecoderException(org.apache.directory.api.asn1.DecoderException) EncoderException(org.apache.directory.api.asn1.EncoderException) ExtendedResponse(org.apache.directory.api.ldap.model.message.ExtendedResponse) Asn1Decoder(org.apache.directory.api.asn1.ber.Asn1Decoder) ExtendedResponseDecorator(org.apache.directory.api.ldap.codec.decorators.ExtendedResponseDecorator) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test) AbstractCodecServiceTest(org.apache.directory.api.ldap.codec.osgi.AbstractCodecServiceTest)

Example 30 with ExtendedResponse

use of org.apache.directory.api.ldap.model.message.ExtendedResponse in project directory-ldap-api by apache.

the class DefaultLdapCodecService method newExtendedResponse.

/**
 * {@inheritDoc}
 */
@SuppressWarnings("unchecked")
@Override
public <E extends ExtendedResponse> E newExtendedResponse(String responseName, int messageId, byte[] serializedResponse) throws DecoderException {
    ExtendedResponseDecorator<ExtendedResponse> resp;
    ExtendedOperationFactory extendedRequestFactory = extendedOperationFactories.get(responseName);
    if (extendedRequestFactory != null) {
        resp = (ExtendedResponseDecorator<ExtendedResponse>) extendedRequestFactory.newResponse(serializedResponse);
    } else {
        resp = new ExtendedResponseDecorator(this, new ExtendedResponseImpl(responseName));
        resp.setResponseValue(serializedResponse);
        resp.setResponseName(responseName);
    }
    resp.setMessageId(messageId);
    return (E) resp;
}
Also used : ExtendedResponse(org.apache.directory.api.ldap.model.message.ExtendedResponse) ExtendedOperationFactory(org.apache.directory.api.ldap.codec.api.ExtendedOperationFactory) ExtendedResponseImpl(org.apache.directory.api.ldap.model.message.ExtendedResponseImpl) ExtendedResponseDecorator(org.apache.directory.api.ldap.codec.decorators.ExtendedResponseDecorator)

Aggregations

ExtendedResponse (org.apache.directory.api.ldap.model.message.ExtendedResponse)30 DecoderException (org.apache.directory.api.asn1.DecoderException)22 Test (org.junit.Test)19 AbstractResponseTest (org.apache.directory.api.dsmlv2.AbstractResponseTest)17 Dsmlv2ResponseParser (org.apache.directory.api.dsmlv2.Dsmlv2ResponseParser)17 LdapURLEncodingException (org.apache.directory.api.ldap.model.exception.LdapURLEncodingException)15 LdapResult (org.apache.directory.api.ldap.model.message.LdapResult)8 ExtendedResponseDecorator (org.apache.directory.api.ldap.codec.decorators.ExtendedResponseDecorator)7 Control (org.apache.directory.api.ldap.model.message.Control)6 ByteBuffer (java.nio.ByteBuffer)4 Asn1Decoder (org.apache.directory.api.asn1.ber.Asn1Decoder)4 DsmlControl (org.apache.directory.api.dsmlv2.DsmlControl)4 LdapUrl (org.apache.directory.api.ldap.model.url.LdapUrl)3 IOException (java.io.IOException)2 PrintWriter (java.io.PrintWriter)2 StringWriter (java.io.StringWriter)2 ConnectException (java.net.ConnectException)2 UnresolvedAddressException (java.nio.channels.UnresolvedAddressException)2 EncoderException (org.apache.directory.api.asn1.EncoderException)2 BatchResponseDsml (org.apache.directory.api.dsmlv2.response.BatchResponseDsml)2