KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > easybeans > component > smartclient > message > AbsMessage


1 /**
2  * EasyBeans
3  * Copyright (C) 2006 Bull S.A.S.
4  * Contact: easybeans@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * --------------------------------------------------------------------------
22  * $Id:$
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.easybeans.component.smartclient.message;
27
28 import java.io.UnsupportedEncodingException JavaDoc;
29 import java.nio.ByteBuffer JavaDoc;
30 import java.nio.CharBuffer JavaDoc;
31 import java.nio.charset.CharacterCodingException JavaDoc;
32 import java.nio.charset.Charset JavaDoc;
33 import java.nio.charset.CharsetDecoder JavaDoc;
34
35 import org.objectweb.easybeans.component.smartclient.api.Message;
36 import org.objectweb.easybeans.component.smartclient.api.ProtocolConstants;
37
38 /**
39  * Abstract class that needs to be used for every message that are exchanged between client and endpoint.
40  * @author Florent Benoit
41  *
42  */

43 public abstract class AbsMessage implements Message {
44
45     /**
46      * Gets a message to send.
47      * @return the bytebuffer to send
48      */

49     public ByteBuffer JavaDoc getMessage() {
50         ByteBuffer JavaDoc subMessage = getSubMessage();
51
52         // compute length
53
int length = HEADER_SIZE;
54         if (subMessage != null) {
55             length += subMessage.capacity();
56         }
57
58         // Create ByteBuffer
59
ByteBuffer JavaDoc byteBuffer = ByteBuffer.allocate(length);
60
61         // Append header
62
byteBuffer.put(ProtocolConstants.PROTOCOL_VERSION);
63         byteBuffer.put(getOpCode());
64         if (subMessage != null) {
65             byteBuffer.putInt(subMessage.capacity());
66         }
67
68         // append inner message (go to position 0 first)
69
if (subMessage != null) {
70             subMessage.position(0);
71             byteBuffer.put(subMessage);
72         }
73
74         // reset our position
75
byteBuffer.position(0);
76
77         // return buffer
78
return byteBuffer;
79     }
80
81     /**
82      * Gets the OpCode of this message.
83      * @return the operation code.
84      */

85     public abstract byte getOpCode();
86
87     /**
88      * Gets the content of this message (only this part, not the header).
89      * @return the content of this message.
90      */

91     public abstract ByteBuffer JavaDoc getSubMessage();
92
93
94     /**
95      * Encode the given string into a bytebuffer.
96      * @param str the given string
97      * @return a bytebuffer with UTF-8 encoded string
98      */

99     protected ByteBuffer JavaDoc encode(final String JavaDoc str) {
100         byte[] bytes = null;
101         try {
102             bytes = str.getBytes("UTF-8");
103         } catch (UnsupportedEncodingException JavaDoc e) {
104             // TODO Auto-generated catch block
105
e.printStackTrace();
106         }
107         ByteBuffer JavaDoc buffer = ByteBuffer.allocate(bytes.length);
108         buffer.put(bytes);
109         return buffer;
110
111     }
112
113     /**
114      * Decode the string encoded in the bytebuffer in UTF-8 format.
115      * @param buffer the given buffer to analyze.
116      * @return the decoded string
117      */

118     protected String JavaDoc decode(final ByteBuffer JavaDoc buffer) {
119         Charset JavaDoc charset = Charset.forName("UTF-8");
120         CharsetDecoder JavaDoc charsetDecoder = charset.newDecoder();
121
122         CharBuffer JavaDoc charBuffer = null;
123         try {
124             charBuffer = charsetDecoder.decode(buffer);
125         } catch (CharacterCodingException JavaDoc e) {
126            throw new IllegalStateException JavaDoc("Invalid characted encoding", e);
127         }
128         return charBuffer.toString();
129     }
130
131 }
132
Popular Tags