use of org.apache.directory.api.ldap.model.message.ExtendedResponse in project directory-ldap-api by apache.
the class ExtendedResponseTest method testResponseWithMatchedDNAttribute.
/**
* Test parsing of a response with MatchedDN attribute
*/
@Test
public void testResponseWithMatchedDNAttribute() {
Dsmlv2ResponseParser parser = null;
try {
parser = new Dsmlv2ResponseParser(getCodec());
parser.setInput(ExtendedResponseTest.class.getResource("response_with_matchedDN_attribute.xml").openStream(), "UTF-8");
parser.parse();
} catch (Exception e) {
fail(e.getMessage());
}
ExtendedResponse extendedResponse = (ExtendedResponse) parser.getBatchResponse().getCurrentResponse();
LdapResult ldapResult = extendedResponse.getLdapResult();
assertTrue(ldapResult.getMatchedDn().equals("cn=Bob Rush,ou=Dev,dc=Example,dc=COM"));
}
use of org.apache.directory.api.ldap.model.message.ExtendedResponse in project directory-ldap-api by apache.
the class ExtendedResponseDsml method toDsml.
/**
* {@inheritDoc}
*/
@Override
public Element toDsml(Element root) {
Element element;
if (root != null) {
element = root.addElement(EXTENDED_RESPONSE_TAG);
} else {
element = new DefaultElement(EXTENDED_RESPONSE_TAG);
}
ExtendedResponse extendedResponse = getDecorated();
// LDAP Result
LdapResultDsml ldapResultDsml = new LdapResultDsml(getCodecService(), getDecorated().getLdapResult(), getDecorated());
ldapResultDsml.toDsml(element);
// ResponseName
String responseName = extendedResponse.getResponseName();
if (responseName != null) {
element.addElement("responseName").addText(responseName);
}
// Response
Object responseValue = getResponseValue();
if (responseValue != null) {
if (ParserUtils.needsBase64Encoding(responseValue)) {
Namespace xsdNamespace = new Namespace(ParserUtils.XSD, ParserUtils.XML_SCHEMA_URI);
Namespace xsiNamespace = new Namespace(ParserUtils.XSI, ParserUtils.XML_SCHEMA_INSTANCE_URI);
element.getDocument().getRootElement().add(xsdNamespace);
element.getDocument().getRootElement().add(xsiNamespace);
Element responseElement = element.addElement("response").addText(ParserUtils.base64Encode(responseValue));
responseElement.addAttribute(new QName("type", xsiNamespace), ParserUtils.XSD + ":" + ParserUtils.BASE64BINARY);
} else {
element.addElement("response").addText(Strings.utf8ToString((byte[]) responseValue));
}
}
return element;
}
use of org.apache.directory.api.ldap.model.message.ExtendedResponse in project directory-ldap-api by apache.
the class StartTlsFactory method decorate.
/**
* {@inheritDoc}
*/
@Override
public StartTlsResponseDecorator decorate(ExtendedResponse decoratedResponse) {
if (decoratedResponse instanceof StartTlsResponseDecorator) {
return (StartTlsResponseDecorator) decoratedResponse;
}
if (decoratedResponse instanceof StartTlsResponse) {
return new StartTlsResponseDecorator(codec, (StartTlsResponse) decoratedResponse);
}
// It's an opaque extended operation
@SuppressWarnings("unchecked") ExtendedResponseDecorator<ExtendedResponse> response = (ExtendedResponseDecorator<ExtendedResponse>) decoratedResponse;
// Decode the response, as it's an opaque operation
StartTlsResponse startTlsResponse = new StartTlsResponseImpl(response.getMessageId());
startTlsResponse.getLdapResult().setResultCode(response.getLdapResult().getResultCode());
startTlsResponse.getLdapResult().setDiagnosticMessage(response.getLdapResult().getDiagnosticMessage());
return new StartTlsResponseDecorator(codec, new StartTlsResponseImpl());
}
use of org.apache.directory.api.ldap.model.message.ExtendedResponse in project directory-ldap-api by apache.
the class ExtendedResponseTest method testDecodeExtendedRequestNoName.
/**
* Test the decoding of a ExtendedRequest with no name
*/
@Test
public void testDecodeExtendedRequestNoName() {
Asn1Decoder ldapDecoder = new Asn1Decoder();
ByteBuffer stream = ByteBuffer.allocate(0x0E);
stream.put(new byte[] { // LDAPMessage ::= SEQUENCE {
0x30, // LDAPMessage ::= SEQUENCE {
0x0C, 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 // referral [3] Referral OPTIONAL }
// responseName [0] LDAPOID,
});
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 encoding
try {
ByteBuffer bb = encoder.encodeMessage(extendedResponse);
// Check the length
assertEquals(0x0E, bb.limit());
String encodedPdu = Strings.dumpBytes(bb.array());
assertEquals(encodedPdu, decodedPdu);
} catch (EncoderException ee) {
ee.printStackTrace();
fail(ee.getMessage());
}
}
use of org.apache.directory.api.ldap.model.message.ExtendedResponse in project directory-ldap-api by apache.
the class DefaultLdapCodecService method newExtendedResponse.
/**
* {@inheritDoc}
*/
@SuppressWarnings("unchecked")
@Override
public <E extends ExtendedResponse> E newExtendedResponse(String responseName, int messageId, byte[] serializedResponse) throws DecoderException {
ExtendedResponseDecorator<ExtendedResponse> resp;
ExtendedOperationFactory extendedRequestFactory = extendedOperationFactories.get(responseName);
if (extendedRequestFactory != null) {
resp = (ExtendedResponseDecorator<ExtendedResponse>) extendedRequestFactory.newResponse(serializedResponse);
} else {
resp = new ExtendedResponseDecorator(this, new ExtendedResponseImpl(responseName));
resp.setResponseValue(serializedResponse);
resp.setResponseName(responseName);
}
resp.setMessageId(messageId);
return (E) resp;
}
Aggregations