Search in sources :

Example 6 with ExtendedResponseDecorator

use of org.apache.directory.api.ldap.codec.decorators.ExtendedResponseDecorator in project directory-ldap-api by apache.

the class ExtendedResponseTest method testDecodeExtendedRequestNoNameWithControls.

/**
 * Test the decoding of a ExtendedRequest with no name and a control
 */
@Test
public void testDecodeExtendedRequestNoNameWithControls() {
    Asn1Decoder ldapDecoder = new Asn1Decoder();
    ByteBuffer stream = ByteBuffer.allocate(0x2B);
    stream.put(new byte[] { 0x30, // LDAPMessage ::= SEQUENCE {
    0x29, 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, // responseName [0] LDAPOID,
    (byte) 0xA0, // A control
    0x1B, 0x30, 0x19, 0x04, 0x17, 0x32, 0x2E, 0x31, 0x36, 0x2E, 0x38, 0x34, 0x30, 0x2E, 0x31, 0x2E, 0x31, 0x31, 0x33, 0x37, 0x33, 0x30, 0x2E, 0x33, 0x2E, 0x34, 0x2E, 0x32 });
    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 Control
    Map<String, Control> controls = extendedResponse.getControls();
    assertEquals(1, controls.size());
    @SuppressWarnings("unchecked") CodecControl<Control> control = (org.apache.directory.api.ldap.codec.api.CodecControl<Control>) controls.get("2.16.840.1.113730.3.4.2");
    assertEquals("2.16.840.1.113730.3.4.2", control.getOid());
    assertEquals("", Strings.dumpBytes(control.getValue()));
    // Check the encoding
    try {
        ByteBuffer bb = encoder.encodeMessage(extendedResponse);
        // Check the length
        assertEquals(0x2B, 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) ByteBuffer(java.nio.ByteBuffer) DecoderException(org.apache.directory.api.asn1.DecoderException) EncoderException(org.apache.directory.api.asn1.EncoderException) Control(org.apache.directory.api.ldap.model.message.Control) CodecControl(org.apache.directory.api.ldap.codec.api.CodecControl) 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) CodecControl(org.apache.directory.api.ldap.codec.api.CodecControl) Test(org.junit.Test) AbstractCodecServiceTest(org.apache.directory.api.ldap.codec.osgi.AbstractCodecServiceTest)

Example 7 with ExtendedResponseDecorator

use of org.apache.directory.api.ldap.codec.decorators.ExtendedResponseDecorator in project directory-ldap-api by apache.

the class DefaultLdapCodecService method toJndi.

/**
 * {@inheritDoc}
 */
@Override
public javax.naming.ldap.ExtendedRequest toJndi(final ExtendedRequest modelRequest) throws EncoderException {
    final String oid = modelRequest.getRequestName();
    final byte[] value;
    if (modelRequest instanceof ExtendedRequestDecorator) {
        ExtendedRequestDecorator<?> decorator = (ExtendedRequestDecorator<?>) modelRequest;
        value = decorator.getRequestValue();
    } else {
        // have to ask the factory to decorate for us - can't do it ourselves
        ExtendedOperationFactory extendedRequestFactory = extendedOperationFactories.get(modelRequest.getRequestName());
        ExtendedRequestDecorator<?> decorator = (ExtendedRequestDecorator<?>) extendedRequestFactory.decorate(modelRequest);
        value = decorator.getRequestValue();
    }
    return new javax.naming.ldap.ExtendedRequest() {

        private static final long serialVersionUID = -4160980385909987475L;

        @Override
        public String getID() {
            return oid;
        }

        @Override
        public byte[] getEncodedValue() {
            return value;
        }

        @Override
        public javax.naming.ldap.ExtendedResponse createExtendedResponse(String id, byte[] berValue, int offset, int length) throws NamingException {
            ExtendedOperationFactory factory = extendedOperationFactories.get(modelRequest.getRequestName());
            try {
                final ExtendedResponseDecorator<?> resp = (ExtendedResponseDecorator<?>) factory.newResponse(berValue);
                return new javax.naming.ldap.ExtendedResponse() {

                    private static final long serialVersionUID = -7686354122066100703L;

                    @Override
                    public String getID() {
                        return oid;
                    }

                    @Override
                    public byte[] getEncodedValue() {
                        return resp.getResponseValue();
                    }
                };
            } catch (DecoderException de) {
                NamingException ne = new NamingException("Unable to decode encoded response value: " + Strings.dumpBytes(berValue));
                ne.setRootCause(de);
                throw ne;
            }
        }
    };
}
Also used : ExtendedOperationFactory(org.apache.directory.api.ldap.codec.api.ExtendedOperationFactory) ExtendedRequestDecorator(org.apache.directory.api.ldap.codec.decorators.ExtendedRequestDecorator) DecoderException(org.apache.directory.api.asn1.DecoderException) ExtendedResponse(org.apache.directory.api.ldap.model.message.ExtendedResponse) ExtendedRequest(org.apache.directory.api.ldap.model.message.ExtendedRequest) ExtendedResponseDecorator(org.apache.directory.api.ldap.codec.decorators.ExtendedResponseDecorator) NamingException(javax.naming.NamingException)

Example 8 with ExtendedResponseDecorator

use of org.apache.directory.api.ldap.codec.decorators.ExtendedResponseDecorator in project directory-ldap-api by apache.

the class WhoAmIFactory method decorate.

/**
 * {@inheritDoc}
 */
@Override
public WhoAmIResponseDecorator decorate(ExtendedResponse decoratedResponse) {
    if (decoratedResponse instanceof WhoAmIResponseDecorator) {
        return (WhoAmIResponseDecorator) decoratedResponse;
    }
    if (decoratedResponse instanceof WhoAmIResponse) {
        return new WhoAmIResponseDecorator(codec, (WhoAmIResponse) decoratedResponse);
    }
    // It's an opaque extended operation
    @SuppressWarnings("unchecked") ExtendedResponseDecorator<ExtendedResponse> response = (ExtendedResponseDecorator<ExtendedResponse>) decoratedResponse;
    // Decode the response, as it's an opaque operation
    Asn1Decoder decoder = new Asn1Decoder();
    byte[] value = response.getResponseValue();
    ByteBuffer buffer = ByteBuffer.wrap(value);
    WhoAmIResponseContainer container = new WhoAmIResponseContainer();
    WhoAmIResponse whoAmIResponse = null;
    try {
        decoder.decode(buffer, container);
        whoAmIResponse = container.getWhoAmIResponse();
        // Now, update the created response with what we got from the extendedResponse
        whoAmIResponse.getLdapResult().setResultCode(response.getLdapResult().getResultCode());
        whoAmIResponse.getLdapResult().setDiagnosticMessage(response.getLdapResult().getDiagnosticMessage());
        whoAmIResponse.getLdapResult().setMatchedDn(response.getLdapResult().getMatchedDn());
        whoAmIResponse.getLdapResult().setReferral(response.getLdapResult().getReferral());
    } catch (DecoderException de) {
        StringWriter sw = new StringWriter();
        de.printStackTrace(new PrintWriter(sw));
        String stackTrace = sw.toString();
        // Error while decoding the value.
        whoAmIResponse = new WhoAmIResponseImpl(decoratedResponse.getMessageId(), ResultCodeEnum.OPERATIONS_ERROR, stackTrace);
    }
    return new WhoAmIResponseDecorator(codec, whoAmIResponse);
}
Also used : WhoAmIResponseImpl(org.apache.directory.api.ldap.extras.extended.whoAmI.WhoAmIResponseImpl) WhoAmIResponse(org.apache.directory.api.ldap.extras.extended.whoAmI.WhoAmIResponse) ByteBuffer(java.nio.ByteBuffer) DecoderException(org.apache.directory.api.asn1.DecoderException) StringWriter(java.io.StringWriter) ExtendedResponse(org.apache.directory.api.ldap.model.message.ExtendedResponse) ExtendedResponseDecorator(org.apache.directory.api.ldap.codec.decorators.ExtendedResponseDecorator) Asn1Decoder(org.apache.directory.api.asn1.ber.Asn1Decoder) PrintWriter(java.io.PrintWriter)

Example 9 with ExtendedResponseDecorator

use of org.apache.directory.api.ldap.codec.decorators.ExtendedResponseDecorator in project directory-ldap-api by apache.

the class PasswordModifyFactory method decorate.

/**
 * {@inheritDoc}
 */
@Override
public PasswordModifyResponseDecorator decorate(ExtendedResponse decoratedResponse) {
    if (decoratedResponse instanceof PasswordModifyResponseDecorator) {
        return (PasswordModifyResponseDecorator) decoratedResponse;
    }
    if (decoratedResponse instanceof PasswordModifyResponse) {
        return new PasswordModifyResponseDecorator(codec, (PasswordModifyResponse) decoratedResponse);
    }
    // It's an opaque extended operation
    @SuppressWarnings("unchecked") ExtendedResponseDecorator<ExtendedResponse> response = (ExtendedResponseDecorator<ExtendedResponse>) decoratedResponse;
    // Decode the response, as it's an opaque operation
    Asn1Decoder decoder = new Asn1Decoder();
    byte[] value = response.getResponseValue();
    PasswordModifyResponseContainer container = new PasswordModifyResponseContainer();
    PasswordModifyResponse pwdModifyResponse;
    if (value != null) {
        ByteBuffer buffer = ByteBuffer.wrap(value);
        try {
            decoder.decode(buffer, container);
            pwdModifyResponse = container.getPwdModifyResponse();
            // Now, update the created response with what we got from the extendedResponse
            pwdModifyResponse.getLdapResult().setResultCode(response.getLdapResult().getResultCode());
            pwdModifyResponse.getLdapResult().setDiagnosticMessage(response.getLdapResult().getDiagnosticMessage());
            pwdModifyResponse.getLdapResult().setMatchedDn(response.getLdapResult().getMatchedDn());
            pwdModifyResponse.getLdapResult().setReferral(response.getLdapResult().getReferral());
        } catch (DecoderException de) {
            StringWriter sw = new StringWriter();
            de.printStackTrace(new PrintWriter(sw));
            String stackTrace = sw.toString();
            // Error while decoding the value.
            pwdModifyResponse = new PasswordModifyResponseImpl(decoratedResponse.getMessageId(), ResultCodeEnum.OPERATIONS_ERROR, stackTrace);
        }
    } else {
        pwdModifyResponse = new PasswordModifyResponseImpl();
        // Now, update the created response with what we got from the extendedResponse
        pwdModifyResponse.getLdapResult().setResultCode(response.getLdapResult().getResultCode());
        pwdModifyResponse.getLdapResult().setDiagnosticMessage(response.getLdapResult().getDiagnosticMessage());
        pwdModifyResponse.getLdapResult().setMatchedDn(response.getLdapResult().getMatchedDn());
        pwdModifyResponse.getLdapResult().setReferral(response.getLdapResult().getReferral());
    }
    PasswordModifyResponseDecorator decorated = new PasswordModifyResponseDecorator(codec, pwdModifyResponse);
    Control ppolicyControl = response.getControl(PasswordPolicy.OID);
    if (ppolicyControl != null) {
        decorated.addControl(ppolicyControl);
    }
    return decorated;
}
Also used : ByteBuffer(java.nio.ByteBuffer) DecoderException(org.apache.directory.api.asn1.DecoderException) Control(org.apache.directory.api.ldap.model.message.Control) PasswordModifyResponse(org.apache.directory.api.ldap.extras.extended.pwdModify.PasswordModifyResponse) StringWriter(java.io.StringWriter) ExtendedResponse(org.apache.directory.api.ldap.model.message.ExtendedResponse) ExtendedResponseDecorator(org.apache.directory.api.ldap.codec.decorators.ExtendedResponseDecorator) Asn1Decoder(org.apache.directory.api.asn1.ber.Asn1Decoder) PasswordModifyResponseImpl(org.apache.directory.api.ldap.extras.extended.pwdModify.PasswordModifyResponseImpl) PrintWriter(java.io.PrintWriter)

Example 10 with ExtendedResponseDecorator

use of org.apache.directory.api.ldap.codec.decorators.ExtendedResponseDecorator in project directory-ldap-api by apache.

the class InitExtendedResponse method action.

/**
 * {@inheritDoc}
 */
public void action(LdapMessageContainer<ExtendedResponseDecorator<?>> container) throws DecoderException {
    // Now, we can allocate the ExtendedResponse Object
    ExtendedResponseDecorator<?> extendedResponse = new ExtendedResponseDecorator<>(container.getLdapCodecService(), new ExtendedResponseImpl(container.getMessageId()));
    container.setMessage(extendedResponse);
    LOG.debug("Extended Response");
}
Also used : ExtendedResponseImpl(org.apache.directory.api.ldap.model.message.ExtendedResponseImpl) ExtendedResponseDecorator(org.apache.directory.api.ldap.codec.decorators.ExtendedResponseDecorator)

Aggregations

ExtendedResponseDecorator (org.apache.directory.api.ldap.codec.decorators.ExtendedResponseDecorator)18 DecoderException (org.apache.directory.api.asn1.DecoderException)15 ByteBuffer (java.nio.ByteBuffer)13 Asn1Decoder (org.apache.directory.api.asn1.ber.Asn1Decoder)13 LdapMessageContainer (org.apache.directory.api.ldap.codec.api.LdapMessageContainer)11 AbstractCodecServiceTest (org.apache.directory.api.ldap.codec.osgi.AbstractCodecServiceTest)11 Test (org.junit.Test)11 EncoderException (org.apache.directory.api.asn1.EncoderException)8 ExtendedResponse (org.apache.directory.api.ldap.model.message.ExtendedResponse)8 Control (org.apache.directory.api.ldap.model.message.Control)5 CodecControl (org.apache.directory.api.ldap.codec.api.CodecControl)4 PrintWriter (java.io.PrintWriter)2 StringWriter (java.io.StringWriter)2 ExtendedOperationFactory (org.apache.directory.api.ldap.codec.api.ExtendedOperationFactory)2 ExtendedResponseImpl (org.apache.directory.api.ldap.model.message.ExtendedResponseImpl)2 NamingException (javax.naming.NamingException)1 TLV (org.apache.directory.api.asn1.ber.tlv.TLV)1 ExtendedRequestDecorator (org.apache.directory.api.ldap.codec.decorators.ExtendedRequestDecorator)1 PasswordModifyResponse (org.apache.directory.api.ldap.extras.extended.pwdModify.PasswordModifyResponse)1 PasswordModifyResponseImpl (org.apache.directory.api.ldap.extras.extended.pwdModify.PasswordModifyResponseImpl)1