use of org.apache.directory.api.ldap.codec.decorators.ExtendedResponseDecorator in project directory-ldap-api by apache.
the class ExtendedResponseTest method testDecodeExtendedRequestNoNameWithControls.
/**
* Test the decoding of a ExtendedRequest with no name and a control
*/
@Test
public void testDecodeExtendedRequestNoNameWithControls() {
Asn1Decoder ldapDecoder = new Asn1Decoder();
ByteBuffer stream = ByteBuffer.allocate(0x2B);
stream.put(new byte[] { 0x30, // LDAPMessage ::= SEQUENCE {
0x29, 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, // responseName [0] LDAPOID,
(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 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 Control
Map<String, Control> controls = extendedResponse.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(control.getValue()));
// Check the encoding
try {
ByteBuffer bb = encoder.encodeMessage(extendedResponse);
// Check the length
assertEquals(0x2B, 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.codec.decorators.ExtendedResponseDecorator in project directory-ldap-api by apache.
the class DefaultLdapCodecService method toJndi.
/**
* {@inheritDoc}
*/
@Override
public javax.naming.ldap.ExtendedRequest toJndi(final ExtendedRequest modelRequest) throws EncoderException {
final String oid = modelRequest.getRequestName();
final byte[] value;
if (modelRequest instanceof ExtendedRequestDecorator) {
ExtendedRequestDecorator<?> decorator = (ExtendedRequestDecorator<?>) modelRequest;
value = decorator.getRequestValue();
} else {
// have to ask the factory to decorate for us - can't do it ourselves
ExtendedOperationFactory extendedRequestFactory = extendedOperationFactories.get(modelRequest.getRequestName());
ExtendedRequestDecorator<?> decorator = (ExtendedRequestDecorator<?>) extendedRequestFactory.decorate(modelRequest);
value = decorator.getRequestValue();
}
return new javax.naming.ldap.ExtendedRequest() {
private static final long serialVersionUID = -4160980385909987475L;
@Override
public String getID() {
return oid;
}
@Override
public byte[] getEncodedValue() {
return value;
}
@Override
public javax.naming.ldap.ExtendedResponse createExtendedResponse(String id, byte[] berValue, int offset, int length) throws NamingException {
ExtendedOperationFactory factory = extendedOperationFactories.get(modelRequest.getRequestName());
try {
final ExtendedResponseDecorator<?> resp = (ExtendedResponseDecorator<?>) factory.newResponse(berValue);
return new javax.naming.ldap.ExtendedResponse() {
private static final long serialVersionUID = -7686354122066100703L;
@Override
public String getID() {
return oid;
}
@Override
public byte[] getEncodedValue() {
return resp.getResponseValue();
}
};
} catch (DecoderException de) {
NamingException ne = new NamingException("Unable to decode encoded response value: " + Strings.dumpBytes(berValue));
ne.setRootCause(de);
throw ne;
}
}
};
}
use of org.apache.directory.api.ldap.codec.decorators.ExtendedResponseDecorator 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);
}
use of org.apache.directory.api.ldap.codec.decorators.ExtendedResponseDecorator in project directory-ldap-api by apache.
the class PasswordModifyFactory method decorate.
/**
* {@inheritDoc}
*/
@Override
public PasswordModifyResponseDecorator decorate(ExtendedResponse decoratedResponse) {
if (decoratedResponse instanceof PasswordModifyResponseDecorator) {
return (PasswordModifyResponseDecorator) decoratedResponse;
}
if (decoratedResponse instanceof PasswordModifyResponse) {
return new PasswordModifyResponseDecorator(codec, (PasswordModifyResponse) 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();
PasswordModifyResponseContainer container = new PasswordModifyResponseContainer();
PasswordModifyResponse pwdModifyResponse;
if (value != null) {
ByteBuffer buffer = ByteBuffer.wrap(value);
try {
decoder.decode(buffer, container);
pwdModifyResponse = container.getPwdModifyResponse();
// Now, update the created response with what we got from the extendedResponse
pwdModifyResponse.getLdapResult().setResultCode(response.getLdapResult().getResultCode());
pwdModifyResponse.getLdapResult().setDiagnosticMessage(response.getLdapResult().getDiagnosticMessage());
pwdModifyResponse.getLdapResult().setMatchedDn(response.getLdapResult().getMatchedDn());
pwdModifyResponse.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.
pwdModifyResponse = new PasswordModifyResponseImpl(decoratedResponse.getMessageId(), ResultCodeEnum.OPERATIONS_ERROR, stackTrace);
}
} else {
pwdModifyResponse = new PasswordModifyResponseImpl();
// Now, update the created response with what we got from the extendedResponse
pwdModifyResponse.getLdapResult().setResultCode(response.getLdapResult().getResultCode());
pwdModifyResponse.getLdapResult().setDiagnosticMessage(response.getLdapResult().getDiagnosticMessage());
pwdModifyResponse.getLdapResult().setMatchedDn(response.getLdapResult().getMatchedDn());
pwdModifyResponse.getLdapResult().setReferral(response.getLdapResult().getReferral());
}
PasswordModifyResponseDecorator decorated = new PasswordModifyResponseDecorator(codec, pwdModifyResponse);
Control ppolicyControl = response.getControl(PasswordPolicy.OID);
if (ppolicyControl != null) {
decorated.addControl(ppolicyControl);
}
return decorated;
}
use of org.apache.directory.api.ldap.codec.decorators.ExtendedResponseDecorator in project directory-ldap-api by apache.
the class InitExtendedResponse method action.
/**
* {@inheritDoc}
*/
public void action(LdapMessageContainer<ExtendedResponseDecorator<?>> container) throws DecoderException {
// Now, we can allocate the ExtendedResponse Object
ExtendedResponseDecorator<?> extendedResponse = new ExtendedResponseDecorator<>(container.getLdapCodecService(), new ExtendedResponseImpl(container.getMessageId()));
container.setMessage(extendedResponse);
LOG.debug("Extended Response");
}
Aggregations