1 /*
2 * $HeadURL: /cvsroot/insight/Insight/remote-protocol/src/com/mindtree/insight/remoteprotocol/PacketDataSizeOverLimitsException.java,v $
3 * $Date: 2005/08/09 18:39:17 $
4 * $Revision: 1.1 $
5 * $Author: m1001025 $
6 *
7 * Copyright (c) 2005 MindTree Consulting Ltd.
8 *
9 * This file is part of Insight Remote Protocol Library.
10 *
11 * Insight Remote Protocol Library is free software: you can redistribute it
12 * and/or modify it under the terms of the GNU General Public License as
13 * published by the Free Software Foundation, either version 3 of the License,
14 * or (at your option) any later version.
15 *
16 * Insight Remote Protocol Library is distributed in the hope that it will be
17 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
19 * Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along with
22 * Insight Remote Protocol Library. If not, see <http://www.gnu.org/licenses/>.
23 */
24
25 package com.mindtree.techworks.insight.remoteprotocol.spi;
26
27 import java.nio.charset.CharacterCodingException;
28
29 import com.mindtree.techworks.insight.remoteprotocol.PacketDataSizeOverLimitsException;
30 import com.mindtree.techworks.insight.remoteprotocol.RemoteProtocolException;
31 import com.mindtree.techworks.insight.remoteprotocol.util.ByteUtils;
32
33 /**
34 * This the data content of a data packet containig log data. The class provides
35 * methods to retrieve the data as a <code>String</code> and also set the data
36 * as string. It takes care of setting the byte array representing the data.
37 *
38 * @see com.mindtree.techworks.insight.remoteprotocol.spi.PacketDataContent
39 * @author <a href="mailto:bindul_bhowmik@mindtree.com">Bindul Bhowmik</a>
40 * @version $Revision: 1.2 $ $Date: 2005/08/09 18:39:16 $
41 */
42 public class LogMessageDataContent extends PacketDataContent {
43
44 // -------------------------------------------------------------------------
45 // Class variables
46 // -------------------------------------------------------------------------
47
48 /**
49 * The serial version UID for the serialized form
50 */
51 private static final long serialVersionUID = 1213532797396843257L;
52
53 // -------------------------------------------------------------------------
54 // Constructors
55 // -------------------------------------------------------------------------
56
57 /**
58 * Creates an instance of LogMessageDataContent object.
59 *
60 * @see PacketDataContent#PacketDataContent()
61 */
62 public LogMessageDataContent() {
63
64 super();
65 this.messageType = MessageType.LOG_DATA_MESSAGE;
66
67 }
68
69 /**
70 * Creates an instance of LogMessageDataContent object
71 *
72 * @see PacketDataContent#PacketDataContent(byte[], PacketHeader)
73 * @param completePacketData
74 * The data for the complete packet.
75 * @param packetHeader
76 * The header for the packet
77 * @throws RemoteProtocolException
78 * If the message type in the PacketHeader is not
79 * <code>MessageType#LOG_DATA_MESSAGE</code>
80 */
81 public LogMessageDataContent(byte[] completePacketData,
82 PacketHeader packetHeader) throws RemoteProtocolException {
83
84 super(completePacketData, packetHeader);
85 if (!MessageType.LOG_DATA_MESSAGE.equals(this.messageType)) {
86 throw new RemoteProtocolException("Incompatible message type");
87 }
88 }
89
90 // -------------------------------------------------------------------------
91 // Accessors
92 // -------------------------------------------------------------------------
93
94 /**
95 * Returns the log message as a string.
96 *
97 * @return The log message as a string
98 * @throws RemoteProtocolException
99 * If the log message cannot be decoded.
100 */
101 public String getLogMessage() throws RemoteProtocolException {
102
103 int dataSize = getLength();
104
105 try {
106 return ByteUtils.readStringFromByteArray(packetData, 0,
107 dataSize - 2);
108 } catch (CharacterCodingException e) {
109 throw new RemoteProtocolException("Unable to decode log message", e);
110 }
111 }
112
113 /**
114 * Sets the log message in the packet data.
115 *
116 * @param logMessage
117 * The log message to set.
118 * @throws PacketDataSizeOverLimitsException
119 * If the log data exceeds the max limit.
120 * @throws RemoteProtocolException
121 * If the character stream cannot be encoded
122 */
123 public void setLogMessage(String logMessage)
124 throws PacketDataSizeOverLimitsException, RemoteProtocolException {
125
126 byte[] logData;
127 try {
128 logData = ByteUtils.getByteArrayForString(logMessage);
129 } catch (CharacterCodingException e) {
130 throw new RemoteProtocolException("Unable to encode log message", e);
131 }
132 checkPacketDataSize(logData);
133 this.packetData = logData;
134 }
135
136 }