| | |
| | | package org.opends.server.synchronization; |
| | | |
| | | import org.opends.server.core.AddOperation; |
| | | import org.opends.server.core.DirectoryServer; |
| | | import org.opends.server.protocols.asn1.ASN1Element; |
| | | import org.opends.server.protocols.asn1.ASN1Exception; |
| | | import org.opends.server.protocols.asn1.ASN1OctetString; |
| | | |
| | | import java.io.UnsupportedEncodingException; |
| | | import java.util.ArrayList; |
| | | import java.util.Collection; |
| | | import java.util.LinkedHashSet; |
| | | import java.util.List; |
| | | import java.util.zip.DataFormatException; |
| | | |
| | | import org.opends.server.protocols.internal.InternalClientConnection; |
| | | import org.opends.server.protocols.ldap.LDAPAttribute; |
| | | import org.opends.server.protocols.ldap.LDAPException; |
| | | import org.opends.server.types.Attribute; |
| | | import org.opends.server.types.AttributeValue; |
| | | |
| | | import static org.opends.server.synchronization.SynchMessages.*; |
| | | import static org.opends.server.protocols.ldap.LDAPConstants.*; |
| | | import static org.opends.server.util.StaticUtils.toLowerCase; |
| | | |
| | | /** |
| | | * This class is used to exchange Add operation between LDAP servers |
| | |
| | | */ |
| | | public AddMsg(AddOperation op) |
| | | { |
| | | dn = op.getRawEntryDN().stringValue(); |
| | | List<LDAPAttribute> attrs = op.getRawAttributes(); |
| | | ArrayList<ASN1Element> elems = new ArrayList<ASN1Element>(attrs.size()); |
| | | // Encode the object classes (SET OF LDAPString). |
| | | LinkedHashSet<AttributeValue> ocValues = |
| | | new LinkedHashSet<AttributeValue>(op.getObjectClasses().size()); |
| | | for (String s : op.getObjectClasses().values()) |
| | | { |
| | | ocValues.add(new AttributeValue(new ASN1OctetString(s), |
| | | new ASN1OctetString(toLowerCase(s)))); |
| | | } |
| | | |
| | | /* |
| | | * encode each LDAPAttribute into an ASN1Element |
| | | * then add each ASN1Element into an ArrayList, |
| | | * then encode the Arraylist of ASN1Element into a byte[] |
| | | */ |
| | | for (LDAPAttribute attr : attrs) |
| | | elems.add(attr.encode()); |
| | | Attribute attr = new Attribute( |
| | | DirectoryServer.getObjectClassAttributeType(), |
| | | "objectClass", ocValues); |
| | | |
| | | ArrayList<ASN1Element> elems = new ArrayList<ASN1Element>(); |
| | | |
| | | elems.add(new LDAPAttribute(attr).encode()); |
| | | |
| | | // Encode the user attributes (AttributeList). |
| | | for (List<Attribute> list : op.getUserAttributes().values()) |
| | | { |
| | | for (Attribute a : list) |
| | | { |
| | | elems.add(new LDAPAttribute(a).encode()); |
| | | } |
| | | } |
| | | |
| | | // Encode the operational attributes (AttributeList). |
| | | for (List<Attribute> list : op.getOperationalAttributes().values()) |
| | | { |
| | | for (Attribute a : list) |
| | | { |
| | | elems.add(new LDAPAttribute(a).encode()); |
| | | } |
| | | } |
| | | |
| | | dn = op.getRawEntryDN().stringValue(); |
| | | |
| | | // Encode the sequence. |
| | | encodedAttributes = ASN1Element.encodeValue(elems); |
| | | |
| | | changeNumber = (ChangeNumber) op.getAttachment(SYNCHRONIZATION); |
| | | } |
| | | |
| | | /** |
| | | * Creates a new AddMessage. |
| | | * |
| | | * @param cn ChangeNumber of the add. |
| | | * @param dn DN of the added entry. |
| | | * @param objectClass objectclass of the added entry. |
| | | * @param userAttributes user attributes of the added entry. |
| | | * @param operationalAttributes operational attributes of the added entry. |
| | | */ |
| | | public AddMsg(ChangeNumber cn, |
| | | String dn, |
| | | Attribute objectClass, |
| | | Collection<Attribute> userAttributes, |
| | | Collection<Attribute> operationalAttributes) |
| | | { |
| | | this.dn = dn; |
| | | this.changeNumber = cn; |
| | | |
| | | ArrayList<ASN1Element> elems = new ArrayList<ASN1Element>(); |
| | | elems.add(new LDAPAttribute(objectClass).encode()); |
| | | |
| | | for (Attribute a : userAttributes) |
| | | elems.add(new LDAPAttribute(a).encode()); |
| | | |
| | | for (Attribute a : operationalAttributes) |
| | | elems.add(new LDAPAttribute(a).encode()); |
| | | |
| | | encodedAttributes = ASN1Element.encodeValue(elems); |
| | | } |
| | | |
| | | /** |
| | | * Creates a new Add message from a byte[]. |
| | | * |
| | | * @param in The byte[] from which the operation must be read. |
| | | * @throws Exception The input byte[] is not a valid AddMsg |
| | | * @throws DataFormatException The input byte[] is not a valid AddMsg |
| | | */ |
| | | public AddMsg(byte[] in) throws Exception |
| | | public AddMsg(byte[] in) throws DataFormatException |
| | | { |
| | | /* first byte is the type */ |
| | | if (in[0] != OP_TYPE_ADD_REQUEST) |
| | | throw new Exception("byte[] is not a valid add msg"); |
| | | if (in[0] != MSG_TYPE_ADD_REQUEST) |
| | | throw new DataFormatException("byte[] is not a valid add msg"); |
| | | int pos = 1; |
| | | |
| | | /* read the dn |
| | |
| | | while (in[pos++] != 0) |
| | | { |
| | | if (pos > in.length) |
| | | throw new Exception("byte[] is not a valid add msg"); |
| | | throw new DataFormatException("byte[] is not a valid add msg"); |
| | | length++; |
| | | } |
| | | dn = new String(in, offset, length, "UTF-8"); |
| | | try |
| | | { |
| | | dn = new String(in, offset, length, "UTF-8"); |
| | | |
| | | /* read the changeNumber |
| | | * it is always 24 characters long |
| | | */ |
| | | String changenumberStr = new String(in, pos, 24, "UTF-8"); |
| | | changeNumber = new ChangeNumber(changenumberStr); |
| | | pos +=24; |
| | | /* read the changeNumber |
| | | * it is always 24 characters long |
| | | */ |
| | | String changenumberStr = new String(in, pos, 24, "UTF-8"); |
| | | changeNumber = new ChangeNumber(changenumberStr); |
| | | pos +=24; |
| | | } catch (UnsupportedEncodingException e ) { |
| | | throw new DataFormatException("UTF-8 is not supported by this jvm."); |
| | | } |
| | | |
| | | /* Read the attributes : all the remaining bytes */ |
| | | encodedAttributes = new byte[in.length-pos]; |
| | |
| | | * @return the byte array representation of this Message. |
| | | */ |
| | | @Override |
| | | public byte[] getByte() |
| | | public byte[] getBytes() |
| | | { |
| | | byte[] byteDn; |
| | | try |
| | |
| | | int pos = 1; |
| | | |
| | | /* put the type of the operation */ |
| | | resultByteArray[0] = OP_TYPE_ADD_REQUEST; |
| | | resultByteArray[0] = MSG_TYPE_ADD_REQUEST; |
| | | /* put the DN and a terminating 0 */ |
| | | for (int i = 0; i< byteDn.length; i++,pos++) |
| | | { |