Search in sources :

Example 11 with LdapURLEncodingException

use of org.apache.directory.api.ldap.model.exception.LdapURLEncodingException in project directory-ldap-api by apache.

the class SearchResultDoneTest method testResponseWith1ReferralAndAnErrorMessage.

/**
 * Test parsing of a response with a Referral and an Error Message
 */
@Test
public void testResponseWith1ReferralAndAnErrorMessage() {
    Dsmlv2ResponseParser parser = null;
    try {
        parser = new Dsmlv2ResponseParser(getCodec());
        parser.setInput(SearchResultDoneTest.class.getResource("response_with_1_referral_and_error_message.xml").openStream(), "UTF-8");
        parser.parse();
    } catch (Exception e) {
        fail(e.getMessage());
    }
    SearchResultDone searchResultDone = ((SearchResponse) parser.getBatchResponse().getCurrentResponse().getDecorated()).getSearchResultDone();
    LdapResult ldapResult = searchResultDone.getLdapResult();
    Collection<String> referrals = ldapResult.getReferral().getLdapUrls();
    assertEquals(1, referrals.size());
    try {
        assertTrue(referrals.contains(new LdapUrl("ldap://www.apache.org/").toString()));
    } catch (LdapURLEncodingException e) {
        fail();
    }
}
Also used : LdapUrl(org.apache.directory.api.ldap.model.url.LdapUrl) LdapURLEncodingException(org.apache.directory.api.ldap.model.exception.LdapURLEncodingException) Dsmlv2ResponseParser(org.apache.directory.api.dsmlv2.Dsmlv2ResponseParser) LdapResult(org.apache.directory.api.ldap.model.message.LdapResult) SearchResultDone(org.apache.directory.api.ldap.model.message.SearchResultDone) LdapURLEncodingException(org.apache.directory.api.ldap.model.exception.LdapURLEncodingException) SearchResponse(org.apache.directory.api.dsmlv2.response.SearchResponse) Test(org.junit.Test) AbstractResponseTest(org.apache.directory.api.dsmlv2.AbstractResponseTest)

Example 12 with LdapURLEncodingException

use of org.apache.directory.api.ldap.model.exception.LdapURLEncodingException in project directory-ldap-api by apache.

the class StoreReference method action.

/**
 * {@inheritDoc}
 */
public void action(LdapMessageContainer<SearchResultReferenceDecorator> container) throws DecoderException {
    SearchResultReference searchResultReference = container.getMessage();
    // Get the Value and store it in the BindRequest
    TLV tlv = container.getCurrentTLV();
    // Get the referral, or create it if not existing
    Referral referral = searchResultReference.getReferral();
    if (referral == null) {
        referral = new ReferralImpl();
        searchResultReference.setReferral(referral);
    }
    // We have to handle the special case of a 0 length list of referrals
    LdapUrl url = LdapUrl.EMPTY_URL;
    if (tlv.getLength() == 0) {
        referral.addLdapUrl("");
    } else {
        String urlStr = Strings.utf8ToString(tlv.getValue().getData());
        try {
            url = new LdapUrl(urlStr);
            referral.addLdapUrl(urlStr);
        } catch (LdapURLEncodingException luee) {
            LOG.error(I18n.err(I18n.ERR_04021, urlStr, luee.getMessage()));
            throw new DecoderException(I18n.err(I18n.ERR_04016, luee.getMessage()), luee);
        }
    }
    if (IS_DEBUG) {
        LOG.debug("Search reference URL found : {}", url);
    }
    // We can have an END transition
    container.setGrammarEndAllowed(true);
}
Also used : LdapUrl(org.apache.directory.api.ldap.model.url.LdapUrl) LdapURLEncodingException(org.apache.directory.api.ldap.model.exception.LdapURLEncodingException) DecoderException(org.apache.directory.api.asn1.DecoderException) Referral(org.apache.directory.api.ldap.model.message.Referral) ReferralImpl(org.apache.directory.api.ldap.model.message.ReferralImpl) SearchResultReference(org.apache.directory.api.ldap.model.message.SearchResultReference) TLV(org.apache.directory.api.asn1.ber.tlv.TLV)

Example 13 with LdapURLEncodingException

use of org.apache.directory.api.ldap.model.exception.LdapURLEncodingException in project directory-ldap-api by apache.

the class AddReferral method action.

/**
 * {@inheritDoc}
 */
public void action(LdapMessageContainer<MessageDecorator<? extends Message>> container) throws DecoderException {
    TLV tlv = container.getCurrentTLV();
    Message response = container.getMessage();
    LdapResult ldapResult = ((ResultResponse) response).getLdapResult();
    Referral referral = ldapResult.getReferral();
    if (tlv.getLength() == 0) {
        referral.addLdapUrl("");
    } else {
        if (ldapResult.getResultCode() == ResultCodeEnum.REFERRAL) {
            try {
                String url = Strings.utf8ToString(tlv.getValue().getData());
                referral.addLdapUrl(new LdapUrl(url).toString());
            } catch (LdapURLEncodingException luee) {
                String badUrl = Strings.utf8ToString(tlv.getValue().getData());
                LOG.error(I18n.err(I18n.ERR_04015, badUrl, luee.getMessage()));
                throw new DecoderException(I18n.err(I18n.ERR_04016, luee.getMessage()), luee);
            }
        } else {
            LOG.warn("The Referral error message is not allowed when havind an error code no equals to REFERRAL");
            referral.addLdapUrl(LdapUrl.EMPTY_URL.toString());
        }
    }
    if (IS_DEBUG) {
        StringBuilder sb = new StringBuilder();
        boolean isFirst = true;
        for (String url : ldapResult.getReferral().getLdapUrls()) {
            if (isFirst) {
                isFirst = false;
            } else {
                sb.append(", ");
            }
            sb.append(url);
        }
        LOG.debug("The referral error message is set to " + sb.toString());
    }
    // We can have an END transition
    container.setGrammarEndAllowed(true);
}
Also used : ResultResponse(org.apache.directory.api.ldap.model.message.ResultResponse) LdapUrl(org.apache.directory.api.ldap.model.url.LdapUrl) LdapURLEncodingException(org.apache.directory.api.ldap.model.exception.LdapURLEncodingException) DecoderException(org.apache.directory.api.asn1.DecoderException) LdapResult(org.apache.directory.api.ldap.model.message.LdapResult) Message(org.apache.directory.api.ldap.model.message.Message) Referral(org.apache.directory.api.ldap.model.message.Referral) TLV(org.apache.directory.api.asn1.ber.tlv.TLV)

Example 14 with LdapURLEncodingException

use of org.apache.directory.api.ldap.model.exception.LdapURLEncodingException in project directory-ldap-api by apache.

the class LdapUrlTest method testLdapURLNoHostDN.

/**
 * test a LdapUrl without a host but with a Dn
 */
@Test
public void testLdapURLNoHostDN() throws LdapURLEncodingException {
    try {
        LdapUrl url = new LdapUrl("ldap:///ou=system");
        assertEquals("ldap:///ou=system", url.toString());
    } catch (LdapURLEncodingException luee) {
        fail();
    }
}
Also used : LdapUrl(org.apache.directory.api.ldap.model.url.LdapUrl) LdapURLEncodingException(org.apache.directory.api.ldap.model.exception.LdapURLEncodingException) Test(org.junit.Test)

Example 15 with LdapURLEncodingException

use of org.apache.directory.api.ldap.model.exception.LdapURLEncodingException in project directory-ldap-api by apache.

the class SearchResultDoneTest method testResponseWith2Referrals.

/**
 * Test parsing of a response with 2 Referral elements
 */
@Test
public void testResponseWith2Referrals() {
    Dsmlv2ResponseParser parser = null;
    try {
        parser = new Dsmlv2ResponseParser(getCodec());
        parser.setInput(SearchResultDoneTest.class.getResource("response_with_2_referrals.xml").openStream(), "UTF-8");
        parser.parse();
    } catch (Exception e) {
        fail(e.getMessage());
    }
    SearchResultDone searchResultDone = ((SearchResponse) parser.getBatchResponse().getCurrentResponse().getDecorated()).getSearchResultDone();
    LdapResult ldapResult = searchResultDone.getLdapResult();
    Collection<String> referrals = ldapResult.getReferral().getLdapUrls();
    assertEquals(2, referrals.size());
    try {
        assertTrue(referrals.contains(new LdapUrl("ldap://www.apache.org/").toString()));
    } catch (LdapURLEncodingException e) {
        fail();
    }
    try {
        assertTrue(referrals.contains(new LdapUrl("ldap://www.apple.com/").toString()));
    } catch (LdapURLEncodingException e) {
        fail();
    }
}
Also used : LdapUrl(org.apache.directory.api.ldap.model.url.LdapUrl) LdapURLEncodingException(org.apache.directory.api.ldap.model.exception.LdapURLEncodingException) Dsmlv2ResponseParser(org.apache.directory.api.dsmlv2.Dsmlv2ResponseParser) LdapResult(org.apache.directory.api.ldap.model.message.LdapResult) SearchResultDone(org.apache.directory.api.ldap.model.message.SearchResultDone) LdapURLEncodingException(org.apache.directory.api.ldap.model.exception.LdapURLEncodingException) SearchResponse(org.apache.directory.api.dsmlv2.response.SearchResponse) Test(org.junit.Test) AbstractResponseTest(org.apache.directory.api.dsmlv2.AbstractResponseTest)

Aggregations

LdapURLEncodingException (org.apache.directory.api.ldap.model.exception.LdapURLEncodingException)27 LdapUrl (org.apache.directory.api.ldap.model.url.LdapUrl)27 Test (org.junit.Test)25 AbstractResponseTest (org.apache.directory.api.dsmlv2.AbstractResponseTest)24 Dsmlv2ResponseParser (org.apache.directory.api.dsmlv2.Dsmlv2ResponseParser)24 LdapResult (org.apache.directory.api.ldap.model.message.LdapResult)23 DecoderException (org.apache.directory.api.asn1.DecoderException)5 SearchResponse (org.apache.directory.api.dsmlv2.response.SearchResponse)5 BindResponse (org.apache.directory.api.ldap.model.message.BindResponse)3 CompareResponse (org.apache.directory.api.ldap.model.message.CompareResponse)3 DeleteResponse (org.apache.directory.api.ldap.model.message.DeleteResponse)3 ExtendedResponse (org.apache.directory.api.ldap.model.message.ExtendedResponse)3 ModifyDnResponse (org.apache.directory.api.ldap.model.message.ModifyDnResponse)3 ModifyResponse (org.apache.directory.api.ldap.model.message.ModifyResponse)3 SearchResultDone (org.apache.directory.api.ldap.model.message.SearchResultDone)3 SearchResultReference (org.apache.directory.api.ldap.model.message.SearchResultReference)3 TLV (org.apache.directory.api.asn1.ber.tlv.TLV)2 Referral (org.apache.directory.api.ldap.model.message.Referral)2 AddResponse (org.apache.directory.api.ldap.model.message.AddResponse)1 Message (org.apache.directory.api.ldap.model.message.Message)1