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     * @author Franck WOLFF
032     */
033    public abstract class AbstractMessage implements Message {
034    
035            private static final long serialVersionUID = 1L;
036    
037            private Object body = null;
038        private Object clientId = null;
039        private String destination = null;
040        private Map<String, Object> headers = null;
041        private String messageId = null;
042        private long timestamp = 0L;
043        private long timeToLive = 0L;
044    
045        public AbstractMessage() {
046            super();
047        }
048    
049        public AbstractMessage(Message request) {
050            this(request, false);
051        }
052    
053        public AbstractMessage(Message request, boolean keepClientId) {
054            super();
055            this.messageId = UUIDUtil.randomUUID();
056            this.timestamp = System.currentTimeMillis();
057            this.clientId = (keepClientId && request.getClientId() != null ? request.getClientId() : UUIDUtil.randomUUID());
058        }
059    
060        public Object getBody() {
061            return body;
062        }
063    
064        public void setBody(Object body) {
065            this.body = body;
066        }
067    
068        public Object getClientId() {
069            return clientId;
070        }
071    
072        public void setClientId(Object clientId) {
073            this.clientId = clientId;
074        }
075    
076        public String getDestination() {
077            return destination;
078        }
079    
080        public void setDestination(String destination) {
081            this.destination = destination;
082        }
083    
084        public Map<String, Object> getHeaders() {
085            return headers;
086        }
087    
088        public void setHeaders(Map<String, Object> headers) {
089            this.headers = headers;
090        }
091    
092        public Object getHeader(String name) {
093            return (headers != null ? headers.get(name) : null);
094        }
095    
096        public boolean headerExists(String name) {
097            return (headers != null ? headers.containsKey(name) : false);
098        }
099    
100        public void setHeader(String name, Object value) {
101            if (headers == null)
102                headers = new HashMap<String, Object>();
103            headers.put(name, value);
104        }
105    
106        public String getMessageId() {
107            return messageId;
108        }
109    
110        public void setMessageId(String messageId) {
111            this.messageId = messageId;
112        }
113    
114        public long getTimestamp() {
115            return timestamp;
116        }
117    
118        public void setTimestamp(long timestamp) {
119            this.timestamp = timestamp;
120        }
121    
122        public long getTimeToLive() {
123            return timeToLive;
124        }
125    
126        public void setTimeToLive(long timeToLive) {
127            this.timeToLive = timeToLive;
128        }
129    
130        protected void toString(StringBuilder sb, String indent, String bodyAlternative) {
131            sb.append('\n').append(indent).append("  destination = ").append(destination);
132    
133            if (headers != null && headers.containsKey(REMOTE_CREDENTIALS_HEADER)) {
134                Map<String, Object> headersCopy = new HashMap<String, Object>(headers);
135                headersCopy.put(REMOTE_CREDENTIALS_HEADER, HIDDEN_CREDENTIALS);
136                sb.append('\n').append(indent).append("  headers = ").append(headersCopy);
137            }
138            else
139                sb.append('\n').append(indent).append("  headers = ").append(headers);
140    
141            sb.append('\n').append(indent).append("  messageId = ").append(messageId);
142            sb.append('\n').append(indent).append("  timestamp = ").append(timestamp);
143            sb.append('\n').append(indent).append("  clientId = ").append(clientId);
144            sb.append('\n').append(indent).append("  timeToLive = ").append(timeToLive);
145            sb.append('\n').append(indent).append("  body = ").append(printBody(body, bodyAlternative));
146        }
147    
148        private static String printBody(Object body, String bodyAlternative) {
149    
150            body = (bodyAlternative != null ? bodyAlternative : body);
151            if (body == null)
152                return null;
153    
154            if (body.getClass().isArray() || body instanceof Collection<?> || body instanceof Map<?, ?>)
155                return StringUtil.toString(body, 100); // limit to first 100 elements.
156    
157            return body.toString();
158        }
159    }