Search in sources :

Example 6 with ExtendedResponse

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

the class ExtendedResponseTest method testResponseWithEmptyResponseName.

/**
 * Test parsing of a response with empty Response Name
 */
@Test
public void testResponseWithEmptyResponseName() {
    Dsmlv2ResponseParser parser = null;
    try {
        parser = new Dsmlv2ResponseParser(getCodec());
        parser.setInput(ExtendedResponseTest.class.getResource("response_with_empty_responseName.xml").openStream(), "UTF-8");
        parser.parse();
    } catch (Exception e) {
        fail(e.getMessage());
    }
    ExtendedResponse extendedResponse = (ExtendedResponse) parser.getBatchResponse().getCurrentResponse();
    assertEquals("", extendedResponse.getResponseName().toString());
}
Also used : Dsmlv2ResponseParser(org.apache.directory.api.dsmlv2.Dsmlv2ResponseParser) 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 7 with ExtendedResponse

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

the class ExtendedResponseTest method testResponseWith1Control.

/**
 * Test parsing of a response with a (optional) Control element
 */
@Test
public void testResponseWith1Control() {
    Dsmlv2ResponseParser parser = null;
    try {
        parser = new Dsmlv2ResponseParser(getCodec());
        parser.setInput(ExtendedResponseTest.class.getResource("response_with_1_control.xml").openStream(), "UTF-8");
        parser.parse();
    } catch (Exception e) {
        fail(e.getMessage());
    }
    ExtendedResponse extendedResponse = (ExtendedResponse) parser.getBatchResponse().getCurrentResponse();
    Map<String, Control> controls = extendedResponse.getControls();
    assertEquals(1, extendedResponse.getControls().size());
    Control control = controls.get("1.2.840.113556.1.4.643");
    assertNotNull(control);
    assertTrue(control.isCritical());
    assertEquals("1.2.840.113556.1.4.643", control.getOid());
    assertEquals("Some text", Strings.utf8ToString(((DsmlControl<?>) control).getValue()));
}
Also used : DsmlControl(org.apache.directory.api.dsmlv2.DsmlControl) Dsmlv2ResponseParser(org.apache.directory.api.dsmlv2.Dsmlv2ResponseParser) DsmlControl(org.apache.directory.api.dsmlv2.DsmlControl) Control(org.apache.directory.api.ldap.model.message.Control) 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 8 with ExtendedResponse

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

the class ExtendedResponseTest method testResponseWith3ControlsWithoutValue.

/**
 * Test parsing of a response with 3 (optional) Control elements without value
 */
@Test
public void testResponseWith3ControlsWithoutValue() {
    Dsmlv2ResponseParser parser = null;
    try {
        parser = new Dsmlv2ResponseParser(getCodec());
        parser.setInput(ExtendedResponseTest.class.getResource("response_with_3_controls_without_value.xml").openStream(), "UTF-8");
        parser.parse();
    } catch (Exception e) {
        fail(e.getMessage());
    }
    ExtendedResponse extendedResponse = (ExtendedResponse) parser.getBatchResponse().getCurrentResponse();
    Map<String, Control> controls = extendedResponse.getControls();
    assertEquals(3, extendedResponse.getControls().size());
    Control control = controls.get("1.2.840.113556.1.4.456");
    assertNotNull(control);
    assertTrue(control.isCritical());
    assertEquals("1.2.840.113556.1.4.456", control.getOid());
    assertFalse(((DsmlControl<?>) control).hasValue());
}
Also used : Dsmlv2ResponseParser(org.apache.directory.api.dsmlv2.Dsmlv2ResponseParser) DsmlControl(org.apache.directory.api.dsmlv2.DsmlControl) Control(org.apache.directory.api.ldap.model.message.Control) 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 9 with ExtendedResponse

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

the class BatchResponseTest method testResponseWith1ExtendedResponse.

/**
 * Test parsing of a Response with the 1 ExtendedResponse
 */
@Test
public void testResponseWith1ExtendedResponse() {
    Dsmlv2ResponseParser parser = null;
    try {
        parser = new Dsmlv2ResponseParser(getCodec());
        parser.setInput(BatchResponseTest.class.getResource("response_with_1_ExtendedResponse.xml").openStream(), "UTF-8");
        parser.parse();
    } catch (Exception e) {
        fail(e.getMessage());
    }
    BatchResponseDsml batchResponse = parser.getBatchResponse();
    assertEquals(1, batchResponse.getResponses().size());
    DsmlDecorator<? extends Response> response = batchResponse.getCurrentResponse();
    if (response instanceof ExtendedResponse) {
        assertTrue(true);
    } else {
        fail();
    }
}
Also used : BatchResponseDsml(org.apache.directory.api.dsmlv2.response.BatchResponseDsml) Dsmlv2ResponseParser(org.apache.directory.api.dsmlv2.Dsmlv2ResponseParser) ExtendedResponse(org.apache.directory.api.ldap.model.message.ExtendedResponse) Test(org.junit.Test) AbstractResponseTest(org.apache.directory.api.dsmlv2.AbstractResponseTest)

Example 10 with ExtendedResponse

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

the class Dsmlv2Engine method processRequest.

/**
 * Processes a single request
 *
 * @param request the request to process
 * @param respWriter The writer used to store the DSML response
 * @exception Exception If we had an error while processing the request
 */
protected void processRequest(DsmlDecorator<? extends Request> request, BufferedWriter respWriter) throws Exception {
    ResultCodeEnum resultCode = null;
    switch(request.getDecorated().getType()) {
        case ABANDON_REQUEST:
            connection.abandon((AbandonRequest) request);
            return;
        case ADD_REQUEST:
            AddResponse response = connection.add((AddRequest) request);
            resultCode = response.getLdapResult().getResultCode();
            AddResponseDsml addResponseDsml = new AddResponseDsml(connection.getCodecService(), response);
            writeResponse(respWriter, addResponseDsml);
            break;
        case BIND_REQUEST:
            BindResponse bindResponse = connection.bind((BindRequest) request);
            resultCode = bindResponse.getLdapResult().getResultCode();
            BindResponseDsml authResponseDsml = new BindResponseDsml(connection.getCodecService(), bindResponse);
            writeResponse(respWriter, authResponseDsml);
            break;
        case COMPARE_REQUEST:
            CompareResponse compareResponse = connection.compare((CompareRequest) request);
            resultCode = compareResponse.getLdapResult().getResultCode();
            CompareResponseDsml compareResponseDsml = new CompareResponseDsml(connection.getCodecService(), compareResponse);
            writeResponse(respWriter, compareResponseDsml);
            break;
        case DEL_REQUEST:
            DeleteResponse delResponse = connection.delete((DeleteRequest) request);
            resultCode = delResponse.getLdapResult().getResultCode();
            DelResponseDsml delResponseDsml = new DelResponseDsml(connection.getCodecService(), delResponse);
            writeResponse(respWriter, delResponseDsml);
            break;
        case EXTENDED_REQUEST:
            ExtendedResponse extendedResponse = connection.extended((ExtendedRequest) request);
            resultCode = extendedResponse.getLdapResult().getResultCode();
            ExtendedResponseDsml extendedResponseDsml = new ExtendedResponseDsml(connection.getCodecService(), extendedResponse);
            writeResponse(respWriter, extendedResponseDsml);
            break;
        case MODIFY_REQUEST:
            ModifyResponse modifyResponse = connection.modify((ModifyRequest) request);
            resultCode = modifyResponse.getLdapResult().getResultCode();
            ModifyResponseDsml modifyResponseDsml = new ModifyResponseDsml(connection.getCodecService(), modifyResponse);
            writeResponse(respWriter, modifyResponseDsml);
            break;
        case MODIFYDN_REQUEST:
            ModifyDnResponse modifyDnResponse = connection.modifyDn((ModifyDnRequest) request);
            resultCode = modifyDnResponse.getLdapResult().getResultCode();
            ModDNResponseDsml modDNResponseDsml = new ModDNResponseDsml(connection.getCodecService(), modifyDnResponse);
            writeResponse(respWriter, modDNResponseDsml);
            break;
        case SEARCH_REQUEST:
            SearchCursor searchResponses = connection.search((SearchRequest) request);
            SearchResponseDsml searchResponseDsml = new SearchResponseDsml(connection.getCodecService());
            if (respWriter != null) {
                StringBuilder sb = new StringBuilder();
                sb.append("<searchResponse");
                if (request.getDecorated().getMessageId() > 0) {
                    sb.append(" requestID=\"");
                    sb.append(request.getDecorated().getMessageId());
                    sb.append('"');
                }
                sb.append('>');
                respWriter.write(sb.toString());
            }
            while (searchResponses.next()) {
                Response searchResponse = searchResponses.get();
                if (searchResponse.getType() == MessageTypeEnum.SEARCH_RESULT_ENTRY) {
                    SearchResultEntry searchResultEntry = (SearchResultEntry) searchResponse;
                    SearchResultEntryDsml searchResultEntryDsml = new SearchResultEntryDsml(connection.getCodecService(), searchResultEntry);
                    searchResponseDsml = new SearchResponseDsml(connection.getCodecService(), searchResultEntryDsml);
                    if (respWriter != null) {
                        writeResponse(respWriter, searchResultEntryDsml);
                    } else {
                        searchResponseDsml.addResponse(searchResultEntryDsml);
                    }
                } else if (searchResponse.getType() == MessageTypeEnum.SEARCH_RESULT_REFERENCE) {
                    SearchResultReference searchResultReference = (SearchResultReference) searchResponse;
                    SearchResultReferenceDsml searchResultReferenceDsml = new SearchResultReferenceDsml(connection.getCodecService(), searchResultReference);
                    searchResponseDsml = new SearchResponseDsml(connection.getCodecService(), searchResultReferenceDsml);
                    if (respWriter != null) {
                        writeResponse(respWriter, searchResultReferenceDsml);
                    } else {
                        searchResponseDsml.addResponse(searchResultReferenceDsml);
                    }
                }
            }
            SearchResultDone srDone = searchResponses.getSearchResultDone();
            if (srDone != null) {
                resultCode = srDone.getLdapResult().getResultCode();
                SearchResultDoneDsml srdDsml = new SearchResultDoneDsml(connection.getCodecService(), srDone);
                if (respWriter != null) {
                    writeResponse(respWriter, srdDsml);
                    respWriter.write("</searchResponse>");
                } else {
                    searchResponseDsml.addResponse(srdDsml);
                    batchResponse.addResponse(searchResponseDsml);
                }
            }
            break;
        case UNBIND_REQUEST:
            connection.unBind();
            break;
        default:
            throw new IllegalStateException("Unexpected request tpye " + request.getDecorated().getType());
    }
    if ((!continueOnError) && (resultCode != null) && (resultCode != ResultCodeEnum.SUCCESS) && (resultCode != ResultCodeEnum.COMPARE_TRUE) && (resultCode != ResultCodeEnum.COMPARE_FALSE) && (resultCode != ResultCodeEnum.REFERRAL)) {
        // Turning on Exit flag
        exit = true;
    }
}
Also used : DelResponseDsml(org.apache.directory.api.dsmlv2.response.DelResponseDsml) SearchCursor(org.apache.directory.api.ldap.model.cursor.SearchCursor) SearchResultReference(org.apache.directory.api.ldap.model.message.SearchResultReference) BindResponse(org.apache.directory.api.ldap.model.message.BindResponse) SearchResultDoneDsml(org.apache.directory.api.dsmlv2.response.SearchResultDoneDsml) ResultCodeEnum(org.apache.directory.api.ldap.model.message.ResultCodeEnum) ModifyResponseDsml(org.apache.directory.api.dsmlv2.response.ModifyResponseDsml) CompareResponseDsml(org.apache.directory.api.dsmlv2.response.CompareResponseDsml) ModifyDnResponse(org.apache.directory.api.ldap.model.message.ModifyDnResponse) ModifyResponse(org.apache.directory.api.ldap.model.message.ModifyResponse) CompareResponse(org.apache.directory.api.ldap.model.message.CompareResponse) SearchResponseDsml(org.apache.directory.api.dsmlv2.response.SearchResponseDsml) SearchResultEntryDsml(org.apache.directory.api.dsmlv2.response.SearchResultEntryDsml) AddResponseDsml(org.apache.directory.api.dsmlv2.response.AddResponseDsml) SearchResultReferenceDsml(org.apache.directory.api.dsmlv2.response.SearchResultReferenceDsml) SearchResultDone(org.apache.directory.api.ldap.model.message.SearchResultDone) AddResponse(org.apache.directory.api.ldap.model.message.AddResponse) BindResponseDsml(org.apache.directory.api.dsmlv2.response.BindResponseDsml) ErrorResponse(org.apache.directory.api.dsmlv2.response.ErrorResponse) ExtendedResponse(org.apache.directory.api.ldap.model.message.ExtendedResponse) CompareResponse(org.apache.directory.api.ldap.model.message.CompareResponse) AddResponse(org.apache.directory.api.ldap.model.message.AddResponse) DeleteResponse(org.apache.directory.api.ldap.model.message.DeleteResponse) ModifyResponse(org.apache.directory.api.ldap.model.message.ModifyResponse) Response(org.apache.directory.api.ldap.model.message.Response) ModifyDnResponse(org.apache.directory.api.ldap.model.message.ModifyDnResponse) BindResponse(org.apache.directory.api.ldap.model.message.BindResponse) ExtendedResponseDsml(org.apache.directory.api.dsmlv2.response.ExtendedResponseDsml) DeleteResponse(org.apache.directory.api.ldap.model.message.DeleteResponse) ModDNResponseDsml(org.apache.directory.api.dsmlv2.response.ModDNResponseDsml) ExtendedResponse(org.apache.directory.api.ldap.model.message.ExtendedResponse) SearchResultEntry(org.apache.directory.api.ldap.model.message.SearchResultEntry)

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