001 /*
002 GRANITE DATA SERVICES
003 Copyright (C) 2011 GRANITE DATA SERVICES S.A.S.
004
005 This file is part of Granite Data Services.
006
007 Granite Data Services is free software; you can redistribute it and/or modify
008 it under the terms of the GNU Library General Public License as published by
009 the Free Software Foundation; either version 2 of the License, or (at your
010 option) any later version.
011
012 Granite Data Services is distributed in the hope that it will be useful, but
013 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
014 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License
015 for more details.
016
017 You should have received a copy of the GNU Library General Public License
018 along with this library; if not, see <http://www.gnu.org/licenses/>.
019 */
020
021 package flex.messaging.messages;
022
023 import java.util.Collection;
024 import java.util.HashMap;
025 import java.util.Map;
026
027 import org.granite.util.StringUtil;
028 import org.granite.util.UUIDUtil;
029
030
031 /**
032 * @author Franck WOLFF
033 */
034 public abstract class AbstractMessage implements Message {
035
036 private static final long serialVersionUID = 1L;
037
038 private Object body = null;
039 private Object clientId = null;
040 private String destination = null;
041 private Map<String, Object> headers = null;
042 private String messageId = null;
043 private long timestamp = 0L;
044 private long timeToLive = 0L;
045
046 public AbstractMessage() {
047 super();
048 }
049
050 public AbstractMessage(Message request) {
051 this(request, false);
052 }
053
054 public AbstractMessage(Message request, boolean keepClientId) {
055 super();
056 this.messageId = UUIDUtil.randomUUID();
057 this.timestamp = System.currentTimeMillis();
058 this.clientId = (keepClientId && request.getClientId() != null ? request.getClientId() : UUIDUtil.randomUUID());
059 }
060
061 public Object getBody() {
062 return body;
063 }
064
065 public void setBody(Object body) {
066 this.body = body;
067 }
068
069 public Object getClientId() {
070 return clientId;
071 }
072
073 public void setClientId(Object clientId) {
074 this.clientId = clientId;
075 }
076
077 public String getDestination() {
078 return destination;
079 }
080
081 public void setDestination(String destination) {
082 this.destination = destination;
083 }
084
085 public Map<String, Object> getHeaders() {
086 return headers;
087 }
088
089 public void setHeaders(Map<String, Object> headers) {
090 this.headers = headers;
091 }
092
093 public Object getHeader(String name) {
094 return (headers != null ? headers.get(name) : null);
095 }
096
097 public boolean headerExists(String name) {
098 return (headers != null ? headers.containsKey(name) : false);
099 }
100
101 public void setHeader(String name, Object value) {
102 if (headers == null)
103 headers = new HashMap<String, Object>();
104 headers.put(name, value);
105 }
106
107 public String getMessageId() {
108 return messageId;
109 }
110
111 public void setMessageId(String messageId) {
112 this.messageId = messageId;
113 }
114
115 public long getTimestamp() {
116 return timestamp;
117 }
118
119 public void setTimestamp(long timestamp) {
120 this.timestamp = timestamp;
121 }
122
123 public long getTimeToLive() {
124 return timeToLive;
125 }
126
127 public void setTimeToLive(long timeToLive) {
128 this.timeToLive = timeToLive;
129 }
130
131 protected void toString(StringBuilder sb, String indent, String bodyAlternative) {
132 sb.append('\n').append(indent).append(" destination = ").append(destination);
133
134 if (headers != null && headers.containsKey(REMOTE_CREDENTIALS_HEADER)) {
135 Map<String, Object> headersCopy = new HashMap<String, Object>(headers);
136 headersCopy.put(REMOTE_CREDENTIALS_HEADER, HIDDEN_CREDENTIALS);
137 sb.append('\n').append(indent).append(" headers = ").append(headersCopy);
138 }
139 else
140 sb.append('\n').append(indent).append(" headers = ").append(headers);
141
142 sb.append('\n').append(indent).append(" messageId = ").append(messageId);
143 sb.append('\n').append(indent).append(" timestamp = ").append(timestamp);
144 sb.append('\n').append(indent).append(" clientId = ").append(clientId);
145 sb.append('\n').append(indent).append(" timeToLive = ").append(timeToLive);
146 sb.append('\n').append(indent).append(" body = ").append(printBody(body, bodyAlternative));
147 }
148
149 private static String printBody(Object body, String bodyAlternative) {
150
151 body = (bodyAlternative != null ? bodyAlternative : body);
152 if (body == null)
153 return null;
154
155 if (body.getClass().isArray() || body instanceof Collection<?> || body instanceof Map<?, ?>)
156 return StringUtil.toString(body, 100); // limit to first 100 elements.
157
158 return body.toString();
159 }
160 }