Search in sources :

Example 1 with Control

use of org.apache.directory.api.ldap.model.message.Control in project ldapchai by ldapchai.

the class ApacheLdapProviderImpl method extendedOperation.

public ExtendedResponse extendedOperation(final ExtendedRequest request) throws ChaiOperationException, ChaiUnavailableException, IllegalStateException {
    final org.apache.directory.api.ldap.model.message.ExtendedRequest apacheRequest = new org.apache.directory.api.ldap.model.message.ExtendedRequest() {

        public String getRequestName() {
            return request.getID();
        }

        public org.apache.directory.api.ldap.model.message.ExtendedRequest setRequestName(final String oid) {
            return this;
        }

        public org.apache.directory.api.ldap.model.message.ExtendedRequest setMessageId(final int messageId) {
            return this;
        }

        public org.apache.directory.api.ldap.model.message.ExtendedRequest addControl(final Control control) {
            return null;
        }

        public org.apache.directory.api.ldap.model.message.ExtendedRequest addAllControls(final Control[] controls) {
            return null;
        }

        public org.apache.directory.api.ldap.model.message.ExtendedRequest removeControl(final Control control) {
            return null;
        }

        public MessageTypeEnum getResponseType() {
            return null;
        }

        public ResultResponse getResultResponse() {
            return null;
        }

        public boolean hasResponse() {
            return false;
        }

        public MessageTypeEnum getType() {
            return null;
        }

        public Map<String, Control> getControls() {
            return null;
        }

        public Control getControl(final String oid) {
            return null;
        }

        public boolean hasControl(final String oid) {
            return false;
        }

        public int getMessageId() {
            return 0;
        }

        public Object get(final Object key) {
            return null;
        }

        public Object put(final Object key, final Object value) {
            return null;
        }
    };
    try {
        final org.apache.directory.api.ldap.model.message.ExtendedResponse apacheResponse = connection.extended(apacheRequest);
        final ExtendedResponse extendedResponse = new ExtendedResponse() {

            public String getID() {
                return apacheResponse.getResponseName();
            }

            public byte[] getEncodedValue() {
                return null;
            }
        };
        return extendedResponse;
    } catch (LdapException e) {
        throw ChaiOperationException.forErrorMessage(e.getMessage());
    }
}
Also used : Control(org.apache.directory.api.ldap.model.message.Control) ChaiRequestControl(com.novell.ldapchai.ChaiRequestControl) ExtendedResponse(javax.naming.ldap.ExtendedResponse) ExtendedRequest(javax.naming.ldap.ExtendedRequest) LdapException(org.apache.directory.api.ldap.model.exception.LdapException)

Example 2 with Control

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

the class SearchResultEntryTest method testDecodeSearchResultEntryEmptyAttributeValueWithControls.

/**
 * Test the decoding of a SearchResultEntry with an empty attribute value
 * with controls
 */
@Test
public void testDecodeSearchResultEntryEmptyAttributeValueWithControls() throws NamingException {
    Asn1Decoder ldapDecoder = new Asn1Decoder();
    ByteBuffer stream = ByteBuffer.allocate(0x56);
    stream.put(new byte[] { 0x30, // LDAPMessage ::=SEQUENCE {
    0x54, 0x02, 0x01, // messageID MessageID
    0x01, 0x64, // CHOICE { ..., searchResEntry SearchResultEntry,
    0x32, // objectName LDAPDN,
    0x04, 0x1b, 'o', 'u', '=', 'c', 'o', 'n', 't', 'a', 'c', 't', 's', ',', 'd', 'c', '=', 'i', 'k', 't', 'e', 'k', ',', 'd', 'c', '=', 'c', 'o', 'm', // PartialAttributeList ::= SEQUENCE OF SEQUENCE {
    0x30, 0x13, 0x30, 0x11, // type AttributeDescription,
    0x04, 0x0b, 'o', 'b', 'j', 'e', 'c', 't', 'c', 'l', 'a', 's', 's', // vals SET OF AttributeValue }
    0x31, 0x02, // AttributeValue ::= OCTET STRING
    0x04, 0x00, (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 BindRequest Container
    LdapMessageContainer<SearchResultEntryDecorator> ldapMessageContainer = new LdapMessageContainer<SearchResultEntryDecorator>(codec);
    try {
        ldapDecoder.decode(stream, ldapMessageContainer);
    } catch (DecoderException de) {
        de.printStackTrace();
        fail(de.getMessage());
    }
    SearchResultEntry searchResultEntry = ldapMessageContainer.getMessage();
    assertEquals(1, searchResultEntry.getMessageId());
    assertEquals("ou=contacts,dc=iktek,dc=com", searchResultEntry.getObjectName().toString());
    Entry entry = searchResultEntry.getEntry();
    assertEquals(1, entry.size());
    for (int i = 0; i < entry.size(); i++) {
        Attribute attribute = entry.get("objectclass");
        assertEquals(Strings.toLowerCaseAscii("objectClass"), Strings.toLowerCaseAscii(attribute.getUpId()));
        assertTrue(attribute.contains(""));
    }
    // Check the Control
    Map<String, Control> controls = searchResultEntry.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((byte[]) control.getValue()));
    // Check the encoding
    try {
        ByteBuffer bb = encoder.encodeMessage(searchResultEntry);
        // Check the length
        assertEquals(0x56, 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) Attribute(org.apache.directory.api.ldap.model.entry.Attribute) SearchResultEntryDecorator(org.apache.directory.api.ldap.codec.decorators.SearchResultEntryDecorator) ByteBuffer(java.nio.ByteBuffer) DecoderException(org.apache.directory.api.asn1.DecoderException) EncoderException(org.apache.directory.api.asn1.EncoderException) SearchResultEntry(org.apache.directory.api.ldap.model.message.SearchResultEntry) Entry(org.apache.directory.api.ldap.model.entry.Entry) Control(org.apache.directory.api.ldap.model.message.Control) CodecControl(org.apache.directory.api.ldap.codec.api.CodecControl) Asn1Decoder(org.apache.directory.api.asn1.ber.Asn1Decoder) CodecControl(org.apache.directory.api.ldap.codec.api.CodecControl) SearchResultEntry(org.apache.directory.api.ldap.model.message.SearchResultEntry) Test(org.junit.Test) AbstractCodecServiceTest(org.apache.directory.api.ldap.codec.osgi.AbstractCodecServiceTest)

Example 3 with Control

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

the class SearchResultReferenceTest method testDecodeSearchResultReferenceSuccessWithControls.

/**
 * Test the decoding of a SearchResultReference with controls
 */
@Test
public void testDecodeSearchResultReferenceSuccessWithControls() {
    Asn1Decoder ldapDecoder = new Asn1Decoder();
    ByteBuffer stream = ByteBuffer.allocate(0x3F5);
    String[] ldapUrls = new String[] { "ldap:///", "ldap://directory.apache.org:80/", "ldap://d-a.org:80/", "ldap://1.2.3.4/", "ldap://1.2.3.4:80/", "ldap://1.1.1.100000.a/", "ldap://directory.apache.org:389/dc=example,dc=org/", "ldap://directory.apache.org:389/dc=example", "ldap://directory.apache.org:389/dc=example%202,dc=org", "ldap://directory.apache.org:389/dc=example,dc=org?ou", "ldap://directory.apache.org:389/dc=example,dc=org?ou,objectclass,dc", "ldap://directory.apache.org:389/dc=example,dc=org?ou,dc,ou", "ldap:///o=University%20of%20Michigan,c=US", "ldap://ldap.itd.umich.edu/o=University%20of%20Michigan,c=US", "ldap://ldap.itd.umich.edu/o=University%20of%20Michigan,c=US?postalAddress", "ldap://host.com:6666/o=University%20of%20Michigan,c=US??sub?(cn=Babs%20Jensen)", "ldap://ldap.itd.umich.edu/c=GB?objectClass?one", "ldap://ldap.question.com/o=Question%3f,c=US?mail", "ldap://ldap.netscape.com/o=Babsco,c=US???(int=%5c00%5c00%5c00%5c04)", "ldap:///??sub??bindname=cn=Manager%2co=Foo", "ldap:///??sub??!bindname=cn=Manager%2co=Foo" };
    stream.put(new byte[] { // LDAPMessage
    0x30, // LDAPMessage
    (byte) 0x82, // LDAPMessage
    0x03, // LDAPMessage
    (byte) 0xF1, // ::=SEQUENCE {
    0x02, 0x01, // messageID MessageID
    0x01, 0x73, (byte) 0x82, 0x03, // CHOICE { ...,
    (byte) 0xcd // searchResEntry
    // SearchResultEntry,
    // ...
    // SearchResultReference ::= [APPLICATION 19] SEQUENCE OF LDAPURL
    });
    for (int i = 0; i < ldapUrls.length; i++) {
        stream.put((byte) 0x04);
        stream.put((byte) Strings.getBytesUtf8(ldapUrls[i]).length);
        byte[] bytes = Strings.getBytesUtf8(ldapUrls[i]);
        for (int j = 0; j < bytes.length; j++) {
            stream.put(bytes[j]);
        }
    }
    byte[] controlBytes = new byte[] { (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 };
    for (int i = 0; i < controlBytes.length; i++) {
        stream.put(controlBytes[i]);
    }
    String decodedPdu = Strings.dumpBytes(stream.array());
    stream.flip();
    // Allocate a BindRequest Container
    LdapMessageContainer<SearchResultReferenceDecorator> ldapMessageContainer = new LdapMessageContainer<SearchResultReferenceDecorator>(codec);
    try {
        ldapMessageContainer.clean();
        ldapDecoder.decode(stream, ldapMessageContainer);
    } catch (DecoderException de) {
        de.printStackTrace();
        fail(de.getMessage());
    }
    stream.flip();
    SearchResultReference searchResultReference = ldapMessageContainer.getMessage();
    assertEquals(1, searchResultReference.getMessageId());
    Set<String> ldapUrlsSet = new HashSet<String>();
    for (int i = 0; i < ldapUrls.length; i++) {
        ldapUrlsSet.add(ldapUrls[i]);
    }
    Referral referral = searchResultReference.getReferral();
    assertNotNull(referral);
    for (String ldapUrl : referral.getLdapUrls()) {
        if (ldapUrlsSet.contains(ldapUrl)) {
            ldapUrlsSet.remove(ldapUrl);
        } else {
            fail(ldapUrl.toString() + " is not present");
        }
    }
    assertTrue(ldapUrlsSet.size() == 0);
    // Check the Control
    Map<String, Control> controls = searchResultReference.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((byte[]) control.getValue()));
    // Check the encoding
    try {
        ByteBuffer bb = encoder.encodeMessage(searchResultReference);
        // Check the length
        assertEquals(0x3F5, 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) SearchResultReference(org.apache.directory.api.ldap.model.message.SearchResultReference) 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) Referral(org.apache.directory.api.ldap.model.message.Referral) Asn1Decoder(org.apache.directory.api.asn1.ber.Asn1Decoder) SearchResultReferenceDecorator(org.apache.directory.api.ldap.codec.decorators.SearchResultReferenceDecorator) CodecControl(org.apache.directory.api.ldap.codec.api.CodecControl) HashSet(java.util.HashSet) Test(org.junit.Test) AbstractCodecServiceTest(org.apache.directory.api.ldap.codec.osgi.AbstractCodecServiceTest)

Example 4 with Control

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

the class UpdateControls method toString.

/**
 * @see Object#toString()
 */
@Override
public String toString() {
    StringBuilder sb = new StringBuilder();
    sb.append("UpdateControl :");
    sb.append("\n    messageId : ").append(messageId);
    if (controls.isEmpty()) {
        sb.append("\n    No controls");
    } else {
        sb.append("\n    Controls: [");
        boolean isFirst = true;
        for (Control control : controls) {
            if (isFirst) {
                isFirst = false;
            } else {
                sb.append(", ");
            }
            sb.append(control.getOid());
        }
        sb.append(']');
    }
    return sb.toString();
}
Also used : Control(org.apache.directory.api.ldap.model.message.Control)

Example 5 with Control

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

the class UpdateControls method hashCode.

/**
 * {@inheritDoc}
 */
@Override
public int hashCode() {
    int hash = 37;
    hash = hash * 17 + messageId;
    for (Control control : controls) {
        hash = hash * 17 + control.hashCode();
    }
    return hash;
}
Also used : Control(org.apache.directory.api.ldap.model.message.Control)

Aggregations

Control (org.apache.directory.api.ldap.model.message.Control)145 Test (org.junit.Test)124 DsmlControl (org.apache.directory.api.dsmlv2.DsmlControl)85 DecoderException (org.apache.directory.api.asn1.DecoderException)45 AbstractTest (org.apache.directory.api.dsmlv2.AbstractTest)45 Dsmlv2Parser (org.apache.directory.api.dsmlv2.Dsmlv2Parser)45 AbstractResponseTest (org.apache.directory.api.dsmlv2.AbstractResponseTest)40 Dsmlv2ResponseParser (org.apache.directory.api.dsmlv2.Dsmlv2ResponseParser)40 ByteBuffer (java.nio.ByteBuffer)39 CodecControl (org.apache.directory.api.ldap.codec.api.CodecControl)38 EncoderException (org.apache.directory.api.asn1.EncoderException)37 Asn1Decoder (org.apache.directory.api.asn1.ber.Asn1Decoder)37 AbstractCodecServiceTest (org.apache.directory.api.ldap.codec.osgi.AbstractCodecServiceTest)36 LdapURLEncodingException (org.apache.directory.api.ldap.model.exception.LdapURLEncodingException)36 LdapMessageContainer (org.apache.directory.api.ldap.codec.api.LdapMessageContainer)35 SearchResponse (org.apache.directory.api.dsmlv2.response.SearchResponse)12 LdapException (org.apache.directory.api.ldap.model.exception.LdapException)12 BindRequest (org.apache.directory.api.ldap.model.message.BindRequest)11 AbandonRequest (org.apache.directory.api.ldap.model.message.AbandonRequest)9 SearchRequest (org.apache.directory.api.ldap.model.message.SearchRequest)9