/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License, Version 1.0 only
* (the "License"). You may not use this file except in compliance
* with the License.
*
* You can obtain a copy of the license at legal-notices/CDDLv1_0.txt
* or http://forgerock.org/license/CDDLv1.0.html.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at legal-notices/CDDLv1_0.txt.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information:
* Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*
*
* Copyright 2013 ForgeRock AS.
*/
package org.forgerock.opendj.adapter.server2x;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import org.forgerock.opendj.ldap.Attribute;
import org.forgerock.opendj.ldap.ByteString;
import org.forgerock.opendj.ldap.DN;
import org.forgerock.opendj.ldap.DecodeException;
import org.forgerock.opendj.ldap.DereferenceAliasesPolicy;
import org.forgerock.opendj.ldap.ErrorResultException;
import org.forgerock.opendj.ldap.LinkedAttribute;
import org.forgerock.opendj.ldap.ResultCode;
import org.forgerock.opendj.ldap.controls.Control;
import org.forgerock.opendj.ldap.controls.GenericControl;
import org.forgerock.opendj.ldap.responses.Responses;
import org.forgerock.opendj.ldap.responses.Result;
import org.forgerock.opendj.ldap.responses.SearchResultEntry;
import org.opends.server.protocols.asn1.ASN1;
import org.opends.server.protocols.asn1.ASN1Exception;
import org.opends.server.protocols.asn1.ASN1Writer;
import org.opends.server.protocols.ldap.LDAPAttribute;
import org.opends.server.protocols.ldap.LDAPControl;
import org.opends.server.protocols.ldap.LDAPFilter;
import org.opends.server.protocols.ldap.LDAPModification;
import org.opends.server.types.AttributeValue;
import org.opends.server.types.ByteStringBuilder;
import org.opends.server.types.DereferencePolicy;
import org.opends.server.types.LDAPException;
import org.opends.server.types.ModificationType;
import org.opends.server.types.Operation;
import org.opends.server.types.SearchResultReference;
import org.opends.server.types.SearchScope;
/**
* Common utility methods.
*/
public final class Converters
{
// Prevent instantiation.
private Converters() {
throw new AssertionError();
}
/**
* Converts from OpenDJ LDAP SDK {@link DereferenceAliasesPolicy} to OpenDJ
* server {@link DereferencePolicy}.
*
* @param dereferenceAliasesPolicy
* value to convert
* @return the converted value
*/
static final org.opends.server.types.DereferencePolicy to(
final DereferenceAliasesPolicy dereferenceAliasesPolicy) {
return DereferencePolicy.values()[dereferenceAliasesPolicy.intValue()];
}
/**
* Converts from OpenDJ LDAP SDK {@link DN} to OpenDJ server
* {@link org.opends.server.types.DN}.
*
* @param userDn
* value to convert
* @return the converted value
*/
static final org.opends.server.types.DN to(final DN userDn) {
try {
return org.opends.server.types.DN.decode(userDn.toString());
} catch (Exception e) {
throw new IllegalStateException(e.getMessage());
}
}
/**
* Converts from OpenDJ LDAP SDK {@link ByteString} to OpenDJ server
* {@link org.opends.server.types.ByteString}.
*
* @param value
* value to convert
* @return the converted value
*/
static final org.opends.server.types.ByteString to(ByteString value) {
if (value != null) {
return org.opends.server.types.ByteString.wrap(value.toByteArray());
}
return null;
}
/**
* Converts from OpenDJ LDAP SDK {@link org.forgerock.opendj.ldap.SearchScope}
* to OpenDJ server {@link org.opends.server.types.SearchScope}.
*
* @param searchScope
* value to convert
* @return the converted value
*/
static final org.opends.server.types.SearchScope to(
final org.forgerock.opendj.ldap.SearchScope searchScope) {
return SearchScope.values()[searchScope.intValue()];
}
/**
* Converts from OpenDJ LDAP SDK {@link org.forgerock.opendj.ldap.Filter} to
* OpenDJ server {@link org.opends.server.types.RawFilter}.
*
* @param filter
* value to convert
* @return the converted value
*/
static final org.opends.server.types.RawFilter to(final org.forgerock.opendj.ldap.Filter filter) {
org.opends.server.protocols.ldap.LDAPFilter ldapFilter = null;
try {
ldapFilter = LDAPFilter.decode(filter.toString());
} catch (LDAPException e) {
throw new IllegalStateException(e);
}
return ldapFilter;
}
/**
* Converts from OpenDJ LDAP SDK
* {@link org.forgerock.opendj.ldap.responses.SearchResultReference} to OpenDJ
* server {@link org.opends.server.types.SearchResultReference}.
*
* @param searchResultReference
* value to convert
* @return the converted value
*/
static final org.opends.server.types.SearchResultReference to(
final org.forgerock.opendj.ldap.responses.SearchResultReference searchResultReference) {
return new SearchResultReference(searchResultReference.getURIs(), to(searchResultReference
.getControls()));
}
/**
* Converts from OpenDJ LDAP SDK {@link String} to OpenDJ server
* {@link org.opends.server.types.ByteString}.
*
* @param value
* value to convert
* @return the converted value
*/
static final org.opends.server.types.ByteString to(final String value) {
return org.opends.server.types.ByteString.valueOf(value);
}
/**
* Converts from OpenDJ LDAP SDK {@link Control} to OpenDJ server
* {@link org.opends.server.protocols.ldap.LDAPControl}.
*
* @param control
* value to convert
* @return the converted value
*/
static final org.opends.server.protocols.ldap.LDAPControl to(final Control control) {
return new LDAPControl(control.getOID(), control.isCritical(), to(control.getValue()));
}
/**
* Converts from a List of OpenDJ LDAP SDK
* {@link org.forgerock.opendj.ldap.controls.Control} to a List
* of OpenDJ server {@link org.opends.server.types.Control}.
*
* @param listOfControl
* value to convert
* @return the converted value
*/
static final List to(
final List listOfControl) {
List toListofControl =
new ArrayList(listOfControl.size());
for (org.forgerock.opendj.ldap.controls.Control c : listOfControl) {
toListofControl.add(to(c));
}
return toListofControl;
}
/**
* Converts from OpenDJ LDAP SDK {@link org.forgerock.opendj.ldap.Attribute}
* to OpenDJ server {@link org.opends.server.types.RawAttribute}.
*
* @param attribute
* value to convert
* @return the converted value
*/
static final org.opends.server.types.RawAttribute to(
final org.forgerock.opendj.ldap.Attribute attribute) {
ArrayList listAttributeValues =
new ArrayList(attribute.size());
for (ByteString b : attribute.toArray()) {
listAttributeValues.add(to(b));
}
return new LDAPAttribute(attribute.getAttributeDescriptionAsString(), listAttributeValues);
}
/**
* Converts from an Iterable of OpenDJ LDAP SDK
* {@link org.forgerock.opendj.ldap.Attribute} to a List of
* OpenDJ server {@link org.opends.server.types.RawAttribute}.
*
* @param listOfAttributes
* value to convert
* @return the converted value
*/
static final List to(
final Iterable listOfAttributes) {
List toListofAttributes =
new ArrayList(
((Collection) listOfAttributes).size());
for (org.forgerock.opendj.ldap.Attribute a : listOfAttributes) {
toListofAttributes.add(to(a));
}
return toListofAttributes;
}
/**
* Converts from OpenDJ LDAP SDK
* {@link org.forgerock.opendj.ldap.Modification} to OpenDJ server
* {@link org.opends.server.types.RawModification}.
*
* @param modification
* value to convert
* @return the converted value
*/
static final org.opends.server.types.RawModification to(
final org.forgerock.opendj.ldap.Modification modification) {
return new LDAPModification(to(modification.getModificationType()), to(modification
.getAttribute()));
}
/**
* Converts from a List of OpenDJ LDAP SDK
* {@link org.forgerock.opendj.ldap.Modification} to a List of
* OpenDJ server {@link org.opends.server.types.RawModification}.
*
* @param listOfModifications
* value to convert
* @return the converted value
*/
static final List toModifications(
final List listOfModifications) {
List toListofModifications =
new ArrayList(listOfModifications.size());
for (org.forgerock.opendj.ldap.Modification m : listOfModifications) {
toListofModifications.add(to(m));
}
return toListofModifications;
}
/**
* Converts from OpenDJ LDAP SDK
* {@link org.forgerock.opendj.ldap.ModificationType} to OpenDJ server
* {@link org.opends.server.types.ModificationType}.
*
* @param modificationType
* value to convert
* @return the converted value
*/
static final org.opends.server.types.ModificationType to(
final org.forgerock.opendj.ldap.ModificationType modificationType) {
return ModificationType.values()[modificationType.intValue()];
}
/**
* Converts from OpenDJ server {@link org.opends.server.types.ByteString} to
* OpenDJ LDAP SDK {@link ByteString}.
*
* @param value
* value to convert
* @return the converted value
*/
static final ByteString from(final org.opends.server.types.ByteString value) {
if (value != null) {
return ByteString.wrap(value.toByteArray());
}
return null;
}
/**
* Converts from OpenDJ server
* {@link org.opends.server.protocols.ldap.LDAPControl} to OpenDJ LDAP SDK
* {@link Control}.
*
* @param ldapControl
* value to convert
* @return the converted value
*/
static final Control from(final org.opends.server.protocols.ldap.LDAPControl ldapControl) {
return GenericControl.newControl(ldapControl.getOID(), ldapControl.isCritical(),
from(ldapControl.getValue()));
}
/**
* Converts from OpenDJ server {@link org.opends.server.types.Control} to
* OpenDJ LDAP SDK {@link Control}.
*
* @param control
* value to convert
* @return the converted value
*/
static final Control from(final org.opends.server.types.Control control) {
String oid = null;
boolean isCritical = false;
ByteString value = null;
// The server control doesn't have a method for extracting directly the value so, we need to ASN1 it.
ByteStringBuilder builder = new ByteStringBuilder();
final ASN1Writer writer = ASN1.getWriter(builder);
try {
control.write(writer);
} catch (IOException e) {
// Nothing to do.
}
final ByteString sdkByteString = from(builder.toByteString());
final org.forgerock.opendj.asn1.ASN1Reader sdkReaderASN1 =
org.forgerock.opendj.asn1.ASN1.getReader(sdkByteString.toByteArray());
// Reads the ASN1 SDK byte string.
try {
sdkReaderASN1.readStartSequence();
oid = sdkReaderASN1.readOctetStringAsString();
if (sdkReaderASN1.hasNextElement()
&& (sdkReaderASN1.peekType() == org.forgerock.opendj.asn1.ASN1.UNIVERSAL_BOOLEAN_TYPE)) {
isCritical = sdkReaderASN1.readBoolean();
}
if (sdkReaderASN1.hasNextElement()
&& (sdkReaderASN1.peekType() == org.forgerock.opendj.asn1.ASN1.UNIVERSAL_OCTET_STRING_TYPE)) {
value = sdkReaderASN1.readOctetString();
}
sdkReaderASN1.readEndSequence();
} catch (DecodeException e) {
// Nothing to do.
} catch (IOException e) {
// Nothing to do.
}
// Creates the control
return GenericControl.newControl(oid, isCritical, value);
}
/**
* Converts from a List of OpenDJ server
* {@link org.opends.server.types.Control} to a List of OpenDJ
* LDAP SDK {@link org.forgerock.opendj.ldap.controls.Control}.
*
* @param listOfControl
* value to convert
* @return the converted value
*/
static final List from(
final List listOfControl) {
List fromListofControl =
new ArrayList(listOfControl.size());
for (org.opends.server.types.Control c : listOfControl) {
fromListofControl.add(from(c));
}
return fromListofControl;
}
/**
* Converts from OpenDJ server
* {@link org.opends.server.types.SearchResultReference} to OpenDJ LDAP SDK
* {@link org.forgerock.opendj.ldap.responses.SearchResultReference}.
*
* @param srvResultReference
* value to convert
* @return the converted value
*/
static final org.forgerock.opendj.ldap.responses.SearchResultReference from(
final org.opends.server.types.SearchResultReference srvResultReference) {
return Responses.newSearchResultReference(srvResultReference.getReferralURLString());
}
/**
* Converts from OpenDJ server {@link org.opends.server.types.Attribute} to
* OpenDJ LDAP SDK {@link org.forgerock.opendj.ldap.Attribute}.
*
* @param attribute
* value to convert
* @return the converted value
*/
static final org.forgerock.opendj.ldap.Attribute from(
final org.opends.server.types.Attribute attribute) {
Attribute sdkAttribute = new LinkedAttribute(attribute.getNameWithOptions());
for (AttributeValue value : attribute) {
sdkAttribute.add(value);
}
return sdkAttribute;
}
/**
* Converts from an Iterable of OpenDJ server
* {@link org.opends.server.types.Attribute} to a List of OpenDJ
* LDAP SDK {@link org.forgerock.opendj.ldap.Attribute}.
*
* @param listOfAttributes
* value to convert
* @return the converted value
*/
static final List from(
final Iterable listOfAttributes) {
List fromListofAttributes =
new ArrayList(
((Collection) listOfAttributes).size());
for (org.opends.server.types.Attribute a : listOfAttributes) {
fromListofAttributes.add(from(a));
}
return fromListofAttributes;
}
/**
* Converts from OpenDJ server
* {@link org.opends.server.types.SearchResultEntry} to OpenDJ LDAP SDK
* {@link org.forgerock.opendj.ldap.responses.SearchResultEntry}.
*
* @param srvResultEntry
* value to convert
* @return the converted value
*/
static final org.forgerock.opendj.ldap.responses.SearchResultEntry from(
final org.opends.server.types.SearchResultEntry srvResultEntry) {
final SearchResultEntry searchResultEntry =
Responses.newSearchResultEntry(srvResultEntry.getDN().toString());
if (srvResultEntry.getAttributes() != null) {
for (org.opends.server.types.Attribute a : srvResultEntry.getAttributes()) {
searchResultEntry.addAttribute(from(a));
}
}
if (srvResultEntry.getControls() != null) {
for (org.opends.server.types.Control c : srvResultEntry.getControls()) {
searchResultEntry.addControl(from(c));
}
}
return searchResultEntry;
}
/**
* Populates the result object with the operation details and return the
* result object if it was successful. Otherwise, it throws an
* {@link ErrorResultException}.
*
* @param
* the type of the result object
* @param operation
* used to populate the result
* @param result
* the result object to populate from the Operation
* @return the result if successful, an {@link ErrorResultException} is thrown
* otherwise
* @throws ErrorResultException
* when an error occurs
*/
static final T getResponseResult(final Operation operation, final T result)
throws ErrorResultException {
if (operation.getReferralURLs() != null) {
for (String ref : operation.getReferralURLs()) {
result.addReferralURI(ref);
}
}
if (operation.getResponseControls() != null) {
for (org.opends.server.types.Control c : operation.getResponseControls()) {
result.addControl(from(c));
}
}
result.setDiagnosticMessage((operation.getErrorMessage() != null ? operation
.getErrorMessage().toString() : null));
result.setMatchedDN((operation.getMatchedDN() != null) ? operation.getMatchedDN()
.toString() : null);
if (result.isSuccess()) {
return result;
} else {
throw ErrorResultException.newErrorResult(result);
}
}
/**
* Converts the OpenDJ server {@link Operation} object into an OpenDJ LDAP SDK
* {@link Result} object.
*
* @param operation
* value to convert
* @return the converted value
* @throws ErrorResultException
* when an error occurs
*/
static final Result getResponseResult(final Operation operation) throws ErrorResultException {
Result result = Responses.newResult(getResultCode(operation));
return getResponseResult(operation, result);
}
/**
* Returns the OpenDJ LDAP SDK {@link ResultCode} extracted out of the OpenDJ
* server {@link Operation}.
*
* @param operation
* value to convert
* @return the converted value
*/
static final ResultCode getResultCode(final Operation operation) {
return ResultCode.valueOf(operation.getResultCode().getIntValue());
}
/**
* Converts from byte[] to OpenDJ server
* {@link org.opends.server.types.ByteString}.
*
* @param authenticationValue
* value to convert
* @return the converted value
*/
static final org.opends.server.types.ByteString getCredentials(final byte[] authenticationValue) {
final org.opends.server.protocols.asn1.ASN1Reader reader =
ASN1.getReader(authenticationValue);
org.opends.server.types.ByteString saslCred = org.opends.server.types.ByteString.empty();
try {
reader.readOctetStringAsString(); // Reads SASL Mechanism - RFC 4511 4.2
if (reader.hasNextElement()) {
saslCred = reader.readOctetString(); // Reads credentials.
}
} catch (ASN1Exception e) {
// Nothing to do.
}
return saslCred.toByteString();
}
}