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