001    /*
002     * CDDL HEADER START
003     *
004     * The contents of this file are subject to the terms of the
005     * Common Development and Distribution License, Version 1.0 only
006     * (the "License").  You may not use this file except in compliance
007     * with the License.
008     *
009     * You can obtain a copy of the license at
010     * trunk/opends/resource/legal-notices/OpenDS.LICENSE
011     * or https://OpenDS.dev.java.net/OpenDS.LICENSE.
012     * See the License for the specific language governing permissions
013     * and limitations under the License.
014     *
015     * When distributing Covered Code, include this CDDL HEADER in each
016     * file and include the License file at
017     * trunk/opends/resource/legal-notices/OpenDS.LICENSE.  If applicable,
018     * add the following below this CDDL HEADER, with the fields enclosed
019     * by brackets "[]" replaced with your own identifying information:
020     *      Portions Copyright [yyyy] [name of copyright owner]
021     *
022     * CDDL HEADER END
023     *
024     *
025     *      Copyright 2006-2008 Sun Microsystems, Inc.
026     */
027    package org.opends.server.replication.protocol;
028    
029    import java.io.Serializable;
030    import java.io.UnsupportedEncodingException;
031    import java.util.zip.DataFormatException;
032    
033    /**
034     * This message is part of the replication protocol.
035     * This message is sent by a server to one or several other servers and
036     * contain one entry to be sent over the protocol in the context of
037     * an import/export over the protocol.
038     */
039    public class EntryMessage extends RoutableMessage implements
040        Serializable
041    {
042      private static final long serialVersionUID = 6116955858351992926L;
043    
044      // The byte array containing the bytes of the entry transported
045      private byte[] entryByteArray;
046    
047      /**
048       * Creates a new EntryMessage.
049       *
050       * @param sender The sender of this message.
051       * @param destination The destination of this message.
052       * @param entryBytes The bytes of the entry.
053       */
054      public EntryMessage(short sender, short destination, byte[] entryBytes)
055      {
056        super(sender, destination);
057        this.entryByteArray = entryBytes.clone();
058      }
059    
060      /**
061       * Creates a new EntryMessage from its encoded form.
062       *
063       * @param in The byte array containing the encoded form of the message.
064       * @throws DataFormatException If the byte array does not contain a valid
065       *                             encoded form of the ServerStartMessage.
066       */
067      public EntryMessage(byte[] in) throws DataFormatException
068      {
069        try
070        {
071          /* first byte is the type */
072          if (in[0] != MSG_TYPE_ENTRY)
073            throw new DataFormatException("input is not a valid ServerStart msg");
074          int pos = 1;
075    
076          // sender
077          int length = getNextLength(in, pos);
078          String senderIDString = new String(in, pos, length, "UTF-8");
079          this.senderID = Short.valueOf(senderIDString);
080          pos += length +1;
081    
082          // destination
083          length = getNextLength(in, pos);
084          String destinationString = new String(in, pos, length, "UTF-8");
085          this.destination = Short.valueOf(destinationString);
086          pos += length +1;
087    
088          // entry
089          length = getNextLength(in, pos);
090          this.entryByteArray = new byte[length];
091          for (int i=0; i<length; i++)
092          {
093            entryByteArray[i] = in[pos+i];
094          }
095        }
096        catch (UnsupportedEncodingException e)
097        {
098          throw new DataFormatException("UTF-8 is not supported by this jvm.");
099        }
100      }
101    
102      /**
103       * Returns the entry bytes.
104       * @return The entry bytes.
105       */
106      public byte[] getEntryBytes()
107      {
108        return entryByteArray;
109      }
110    
111      /**
112       * {@inheritDoc}
113       */
114      @Override
115      public byte[] getBytes()
116      {
117        try {
118          byte[] senderBytes = String.valueOf(senderID).getBytes("UTF-8");
119          byte[] destinationBytes = String.valueOf(destination).getBytes("UTF-8");
120          byte[] entryBytes = entryByteArray;
121    
122          int length = 1 + senderBytes.length +
123                       1 + destinationBytes.length +
124                       1 + entryBytes.length + 1;
125    
126          byte[] resultByteArray = new byte[length];
127    
128          /* put the type of the operation */
129          resultByteArray[0] = MSG_TYPE_ENTRY;
130          int pos = 1;
131    
132          pos = addByteArray(senderBytes, resultByteArray, pos);
133          pos = addByteArray(destinationBytes, resultByteArray, pos);
134          pos = addByteArray(entryBytes, resultByteArray, pos);
135          return resultByteArray;
136        }
137        catch (UnsupportedEncodingException e)
138        {
139          return null;
140        }
141      }
142    }