Search in sources :

Example 1 with WhoAmIResponse

use of org.apache.directory.api.ldap.extras.extended.whoAmI.WhoAmIResponse 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)

Aggregations

PrintWriter (java.io.PrintWriter)1 StringWriter (java.io.StringWriter)1 ByteBuffer (java.nio.ByteBuffer)1 DecoderException (org.apache.directory.api.asn1.DecoderException)1 Asn1Decoder (org.apache.directory.api.asn1.ber.Asn1Decoder)1 ExtendedResponseDecorator (org.apache.directory.api.ldap.codec.decorators.ExtendedResponseDecorator)1 WhoAmIResponse (org.apache.directory.api.ldap.extras.extended.whoAmI.WhoAmIResponse)1 WhoAmIResponseImpl (org.apache.directory.api.ldap.extras.extended.whoAmI.WhoAmIResponseImpl)1 ExtendedResponse (org.apache.directory.api.ldap.model.message.ExtendedResponse)1