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 import org.opends.server.types.DN; 034 import org.opends.server.types.DirectoryException; 035 036 /** 037 * This message is part of the replication protocol. 038 * This message is sent by a server to another server in order to 039 * request this other server to do an export to the server sender 040 * of this message. 041 */ 042 public class InitializeRequestMessage extends RoutableMessage implements 043 Serializable 044 { 045 private static final long serialVersionUID = 8303271162942249215L; 046 047 private String baseDn = null; 048 049 /** 050 * Creates a InitializeRequestMessage message. 051 * 052 * @param baseDn The base DN of the replication domain. 053 * @param destination destination of this message 054 * @param senderID serverID of the server that will send this message 055 */ 056 public InitializeRequestMessage(DN baseDn, short senderID, short destination) 057 { 058 super(senderID, destination); 059 this.baseDn = baseDn.toNormalizedString(); 060 } 061 062 /** 063 * Creates a new InitializeRequestMessage by decoding the provided byte array. 064 * @param in A byte array containing the encoded information for the Message 065 * @throws DataFormatException If the in does not contain a properly 066 * encoded InitializeMessage. 067 */ 068 public InitializeRequestMessage(byte[] in) throws DataFormatException 069 { 070 super(); 071 try 072 { 073 /* first byte is the type */ 074 if (in[0] != MSG_TYPE_INITIALIZE_REQUEST) 075 throw new DataFormatException( 076 "input is not a valid InitializeRequestMessage"); 077 int pos = 1; 078 079 // baseDn 080 int length = getNextLength(in, pos); 081 baseDn = new String(in, pos, length, "UTF-8"); 082 pos += length +1; 083 084 // sender 085 length = getNextLength(in, pos); 086 String sourceServerIdString = new String(in, pos, length, "UTF-8"); 087 senderID = Short.valueOf(sourceServerIdString); 088 pos += length +1; 089 090 // destination 091 length = getNextLength(in, pos); 092 String destinationServerIdString = new String(in, pos, length, "UTF-8"); 093 destination = Short.valueOf(destinationServerIdString); 094 pos += length +1; 095 096 097 } catch (UnsupportedEncodingException e) 098 { 099 throw new DataFormatException("UTF-8 is not supported by this jvm."); 100 } 101 } 102 103 /** 104 * Get the base DN from this InitializeRequestMessage. 105 * 106 * @return the base DN from this InitializeRequestMessage. 107 */ 108 public DN getBaseDn() 109 { 110 if (baseDn == null) 111 return null; 112 try 113 { 114 return DN.decode(baseDn); 115 } catch (DirectoryException e) 116 { 117 return null; 118 } 119 } 120 121 /** 122 * {@inheritDoc} 123 */ 124 @Override 125 public byte[] getBytes() 126 { 127 try { 128 byte[] baseDNBytes = baseDn.getBytes("UTF-8"); 129 byte[] senderBytes = String.valueOf(senderID).getBytes("UTF-8"); 130 byte[] destinationBytes = String.valueOf(destination). 131 getBytes("UTF-8"); 132 133 int length = 1 + baseDNBytes.length + 1 + senderBytes.length + 1 134 + destinationBytes.length + 1; 135 136 byte[] resultByteArray = new byte[length]; 137 138 // type of the operation 139 resultByteArray[0] = MSG_TYPE_INITIALIZE_REQUEST; 140 int pos = 1; 141 142 // baseDN 143 pos = addByteArray(baseDNBytes, resultByteArray, pos); 144 145 // sender 146 pos = addByteArray(senderBytes, resultByteArray, pos); 147 148 // destination 149 pos = addByteArray(destinationBytes, resultByteArray, pos); 150 151 return resultByteArray; 152 } 153 catch (UnsupportedEncodingException e) 154 { 155 return null; 156 } 157 } 158 159 /** 160 * Get a string representation of this object. 161 * @return A string representation of this object. 162 */ 163 public String toString() 164 { 165 return "InitializeRequestMessage: baseDn="+baseDn+" senderId="+senderID + 166 " destination=" + destination; 167 } 168 }