001: /**
002: * EasyBeans
003: * Copyright (C) 2006 Bull S.A.S.
004: * Contact: easybeans@ow2.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: *
021: * --------------------------------------------------------------------------
022: * $Id: AbsMessage.java 1970 2007-10-16 11:49:25Z benoitf $
023: * --------------------------------------------------------------------------
024: */package org.ow2.easybeans.component.smartclient.message;
025:
026: import java.io.UnsupportedEncodingException;
027: import java.nio.ByteBuffer;
028: import java.nio.CharBuffer;
029: import java.nio.charset.CharacterCodingException;
030: import java.nio.charset.Charset;
031: import java.nio.charset.CharsetDecoder;
032:
033: import org.ow2.easybeans.component.smartclient.api.Message;
034: import org.ow2.easybeans.component.smartclient.api.ProtocolConstants;
035:
036: /**
037: * Abstract class that needs to be used for every message that are exchanged between client and endpoint.
038: * @author Florent Benoit
039: *
040: */
041: public abstract class AbsMessage implements Message {
042:
043: /**
044: * Gets a message to send.
045: * @return the bytebuffer to send
046: */
047: public ByteBuffer getMessage() {
048: ByteBuffer subMessage = getSubMessage();
049:
050: // compute length
051: int length = HEADER_SIZE;
052: if (subMessage != null) {
053: length += subMessage.capacity();
054: }
055:
056: // Create ByteBuffer
057: ByteBuffer byteBuffer = ByteBuffer.allocate(length);
058:
059: // Append header
060: byteBuffer.put(ProtocolConstants.PROTOCOL_VERSION);
061: byteBuffer.put(getOpCode());
062: if (subMessage != null) {
063: byteBuffer.putInt(subMessage.capacity());
064: }
065:
066: // append inner message (go to position 0 first)
067: if (subMessage != null) {
068: subMessage.position(0);
069: byteBuffer.put(subMessage);
070: }
071:
072: // reset our position
073: byteBuffer.position(0);
074:
075: // return buffer
076: return byteBuffer;
077: }
078:
079: /**
080: * Gets the OpCode of this message.
081: * @return the operation code.
082: */
083: public abstract byte getOpCode();
084:
085: /**
086: * Gets the content of this message (only this part, not the header).
087: * @return the content of this message.
088: */
089: public abstract ByteBuffer getSubMessage();
090:
091: /**
092: * Encode the given string into a bytebuffer.
093: * @param str the given string
094: * @return a bytebuffer with UTF-8 encoded string
095: */
096: protected ByteBuffer encode(final String str) {
097: byte[] bytes = null;
098: try {
099: bytes = str.getBytes("UTF-8");
100: } catch (UnsupportedEncodingException e) {
101: // TODO Auto-generated catch block
102: e.printStackTrace();
103: }
104: ByteBuffer buffer = ByteBuffer.allocate(bytes.length);
105: buffer.put(bytes);
106: return buffer;
107:
108: }
109:
110: /**
111: * Decode the string encoded in the bytebuffer in UTF-8 format.
112: * @param buffer the given buffer to analyze.
113: * @return the decoded string
114: */
115: protected String decode(final ByteBuffer buffer) {
116: Charset charset = Charset.forName("UTF-8");
117: CharsetDecoder charsetDecoder = charset.newDecoder();
118:
119: CharBuffer charBuffer = null;
120: try {
121: charBuffer = charsetDecoder.decode(buffer);
122: } catch (CharacterCodingException e) {
123: throw new IllegalStateException(
124: "Invalid characted encoding", e);
125: }
126: return charBuffer.toString();
127: }
128:
129: }
|