All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.coos.messaging.impl.DefaultMessage Maven / Gradle / Ivy

/**
 * COOS - Connected Objects Operating System (www.connectedobjects.org).
 *
 * Copyright (C) 2009 Telenor ASA and Tellu AS. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This library is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published
 * by the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see .
 *
 * You may also contact one of the following for additional information:
 * Telenor ASA, Snaroyveien 30, N-1331 Fornebu, Norway (www.telenor.no)
 * Tellu AS, Hagalokkveien 13, N-1383 Asker, Norway (www.tellu.no)
 */
package org.coos.messaging.impl;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.util.Enumeration;
import java.util.Hashtable;

import org.coos.messaging.Message;
import org.coos.messaging.MessageContext;
import org.coos.messaging.Serializer;
import org.coos.messaging.SerializerFactory;
import org.coos.util.serialize.AFClassLoader;

/**
 * @author Knut Eilif Husa, Tellu AS
 */
public class DefaultMessage implements Message {

	protected String receiverEndpointUri;
	protected String senderEndpointUri;
	protected Hashtable headers = new Hashtable();
	protected Object body;
	protected byte[] serializedbody;
	protected transient MessageContext messageContext = new MessageContext();
	protected byte version;

	public DefaultMessage() {
		setHeader(MESSAGE_NAME, DEFAULT_MESSAGE_NAME);
		setHeader(TYPE, TYPE_MSG);
	}

	public DefaultMessage(String signalName) {
		setHeader(Message.MESSAGE_NAME, signalName);
		setHeader(TYPE, TYPE_MSG);
	}

	public DefaultMessage(String signalName, String type) {
		setHeader(Message.MESSAGE_NAME, signalName);
		setHeader(TYPE, type);
	}

	public DefaultMessage(DataInputStream din) throws Exception {
		deserialize(din);
	}

	public String getReceiverEndpointUri() {
		return receiverEndpointUri;
	}

	public Message setReceiverEndpointUri(String receiverEndpointUri) {
		this.receiverEndpointUri = receiverEndpointUri;
		return this;
	}

	public String getSenderEndpointUri() {
		return senderEndpointUri;
	}

	public Message setSenderEndpointUri(String senderEndpointUri) {
		this.senderEndpointUri = senderEndpointUri;
		return this;
	}

	public String getHeader(String key) {
		return (String) headers.get(key);
	}

	public Message setHeader(String key, String value) {
		headers.put(key, value);
		return this;
	}

	public Hashtable getHeaders() {
		return headers;
	}

	public String getType() {
		return (String) headers.get(TYPE);
	}

	public Message setBody(byte[] bytesBody) {
		headers.put(CONTENT_TYPE, CONTENT_TYPE_BYTES);
		body = bytesBody;
		return this;
	}

	public Message setBody(Hashtable propertyBody) {
		headers.put(CONTENT_TYPE, CONTENT_TYPE_PROPERTY);
		body = propertyBody;
		return this;
	}

	public Message setBody(String stringBody) {
		headers.put(CONTENT_TYPE, CONTENT_TYPE_STRING);
		body = stringBody;
		return this;
	}

	public Message setBody(Object objectBody) {
		headers.put(CONTENT_TYPE, CONTENT_TYPE_OBJECT);
		body = objectBody;
		return this;
	}

	public byte[] getBodyAsBytes() {
		try {
			deserializeBody();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return (byte[]) body;
	}

	public Hashtable getBodyAsProperties() {
		try {
			deserializeBody();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return (Hashtable) body;
	}

	public String getBodyAsString() {
		try {
			deserializeBody();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return (String) body;
	}

	public Object getBody() {
		try {
			deserializeBody();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return body;
	}
	
	public void setDeserializeClassLoader(AFClassLoader cl) {
	   //Classloading mechanism not available in cldc
	}

	public void deserialize(DataInputStream din) throws Exception {
		din.readInt(); // Skip size
		version = din.readByte();

		if (din.readBoolean()) {
			receiverEndpointUri = din.readUTF();
		}
		if (din.readBoolean()) {
			senderEndpointUri = din.readUTF();
		}

		int headerSize = din.readInt();
		for (int i = 0; i < headerSize; i++) {
			String key = din.readUTF();
			String value = din.readUTF();
			headers.put(key, value);
		}

		serializedbody = new byte[din.readInt()];

		if (serializedbody.length == 0) {
			return;
		}
		din.readFully(serializedbody);
	}

	private void deserializeBody() throws Exception {
		if (body == null && serializedbody != null && serializedbody.length > 0) {
			String serMethod = (String) headers.get(SERIALIZATION_METHOD);
			if (serMethod != null) {
				Serializer serializer = SerializerFactory.getSerializer(serMethod);
				body = serializer.deserialize(serializedbody);
			} else {
				throw new Exception("No serialization method indicated in message header");
			}
		}
	}

	public byte[] serialize() throws Exception {
		ByteArrayOutputStream bout = new ByteArrayOutputStream();
		DataOutputStream dout = new DataOutputStream(bout);

		dout.writeByte(version);

		// The addresses
		dout.writeBoolean(receiverEndpointUri != null);
		if (receiverEndpointUri != null) {
			dout.writeUTF(receiverEndpointUri);
		}
		dout.writeBoolean(senderEndpointUri != null);
		if (senderEndpointUri != null) {
			dout.writeUTF(senderEndpointUri);
		}

		// The body

		if (body != null && serializedbody == null) {
			if (body instanceof byte[]) {
				headers.put(CONTENT_TYPE, CONTENT_TYPE_BYTES);
			} else if (body instanceof String) {
				headers.put(CONTENT_TYPE, CONTENT_TYPE_STRING);
			} else if (body instanceof Hashtable) {
				headers.put(CONTENT_TYPE, CONTENT_TYPE_PROPERTY);
			} else {
				headers.put(CONTENT_TYPE, CONTENT_TYPE_OBJECT);
			}

			String serMethod = (String) headers.get(SERIALIZATION_METHOD);
			if (serMethod != null) {
				Serializer serializer = SerializerFactory.getSerializer(serMethod);
				if (serializer != null) {
					serializedbody = serializer.serialize(body);
				} else {
					throw new Exception("Serialization method not registered: " + serMethod);
				}
			} else {
				try {
					Serializer serializer = SerializerFactory.getDefaultSerializer();
					serializedbody = serializer.serialize(body);
					headers.put(SERIALIZATION_METHOD, SERIALIZATION_METHOD_DEFAULT);
				} catch (Exception e) {
					Serializer serializer = SerializerFactory.getSerializer(SERIALIZATION_METHOD_JAVA);
					if (serializer != null) {
						serializedbody = serializer.serialize(body);
						headers.put(SERIALIZATION_METHOD, SERIALIZATION_METHOD_JAVA);
					} else {
						throw new Exception("Serialization failed");
					}
				}
			}

		}

		// The headers
		dout.writeInt(headers.size());
		Enumeration enumer = headers.keys();
		while (enumer.hasMoreElements()) {
			String key = (String) enumer.nextElement();
			String value = (String) headers.get(key);
			dout.writeUTF(key);
			dout.writeUTF(value);
		}

		// The body
		if (serializedbody != null) {
			dout.writeInt(serializedbody.length);
			dout.write(serializedbody);
		} else {
			dout.writeInt(0);
		}

		byte[] msgBytes = bout.toByteArray();
		ByteArrayOutputStream bout2 = new ByteArrayOutputStream();
		DataOutputStream dout2 = new DataOutputStream(bout2);
		dout2.writeInt(msgBytes.length);
		dout2.write(msgBytes);
		return bout2.toByteArray();
	}

	public MessageContext getMessageContext() {
		return messageContext;
	}

	public void setMessageContext(MessageContext ctx) {
		this.messageContext = ctx;

	}

	public String getContentType() {
		return (String) headers.get(CONTENT_TYPE);
	}

	public String getName() {
		return (String) headers.get(MESSAGE_NAME);
	}

	public Message copy() throws Exception {
		ByteArrayInputStream bais = new ByteArrayInputStream(this.serialize());
		DataInputStream din = new DataInputStream(bais);
		return new DefaultMessage(din);
	}

	public String getReceiverEndpointName() {
		return (String) headers.get(RECEIVER_ENDPOINT_NAME);
	}

	public String getSenderEndpointName() {
		return (String) headers.get(SENDER_ENDPOINT_NAME);
	}

	public void setReceiverEndpointName(String endpointName) {
		if (endpointName != null) {
			headers.put(RECEIVER_ENDPOINT_NAME, endpointName);
		}
	}

	public void setSenderEndpointName(String endpointName) {
		if (endpointName != null) {
			headers.put(SENDER_ENDPOINT_NAME, endpointName);
		}
	}

	public String toString() {
		StringBuffer buf = new StringBuffer();
		buf.append("Receiver: ");
		buf.append(receiverEndpointUri);
		buf.append(", Sender: ");
		buf.append(senderEndpointUri);
		Enumeration enumer = headers.keys();
		while (enumer.hasMoreElements()) {
			String key = (String) enumer.nextElement();
			String value = (String) headers.get(key);
			buf.append(", ");
			buf.append(key);
			buf.append(":");
			buf.append(value);

		}
		return buf.toString();
	}

	public byte[] getSerializedBody() {
		return serializedbody;
	}
	
	public void setSerializedBody(byte[] body) {
		serializedbody = body;
	}


	public byte getVersion() {
		return version;
	}

	public void setVersion(byte version) {
		this.version = version;
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy