001    /**
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.activemq.transport.stomp;
018    
019    import java.util.Arrays;
020    import java.util.Collections;
021    import java.util.HashMap;
022    import java.util.Iterator;
023    import java.util.Map;
024    
025    import org.apache.activemq.command.Command;
026    import org.apache.activemq.command.Endpoint;
027    import org.apache.activemq.command.Response;
028    import org.apache.activemq.state.CommandVisitor;
029    
030    /**
031     * Represents all the data in a STOMP frame.
032     * 
033     * @author <a href="http://hiramchirino.com">chirino</a>
034     */
035    public class StompFrame implements Command {
036    
037        public static final byte[] NO_DATA = new byte[] {};
038    
039        private String action;
040        private Map<String, String> headers = new HashMap<String, String>();
041        private byte[] content = NO_DATA;
042    
043        public StompFrame(String command) {
044            this(command, null, null);
045        }
046        
047        public StompFrame(String command, Map<String, String> headers) {
048            this(command, headers, null);
049        }    
050        
051        public StompFrame(String command, Map<String, String> headers, byte[] data) {
052            this.action = command;
053            if (headers != null)
054                    this.headers = headers;
055            if (data != null)
056                    this.content = data;
057        }
058        
059        public StompFrame() {
060        }
061    
062        public String getAction() {
063            return action;
064        }
065    
066        public void setAction(String command) {
067            this.action = command;
068        }
069    
070        public byte[] getContent() {
071            return content;
072        }
073        
074        public String getBody() {
075            return new String(content);
076        }
077    
078        public void setContent(byte[] data) {
079            this.content = data;
080        }
081    
082        public Map<String, String> getHeaders() {
083            return headers;
084        }
085    
086        public void setHeaders(Map<String, String> headers) {
087            this.headers = headers;
088        }
089    
090        //
091        // Methods in the Command interface
092        //
093        public int getCommandId() {
094            return 0;
095        }
096    
097        public Endpoint getFrom() {
098            return null;
099        }
100    
101        public Endpoint getTo() {
102            return null;
103        }
104    
105        public boolean isBrokerInfo() {
106            return false;
107        }
108    
109        public boolean isMessage() {
110            return false;
111        }
112    
113        public boolean isMessageAck() {
114            return false;
115        }
116    
117        public boolean isMessageDispatch() {
118            return false;
119        }
120    
121        public boolean isMessageDispatchNotification() {
122            return false;
123        }
124    
125        public boolean isResponse() {
126            return false;
127        }
128    
129        public boolean isResponseRequired() {
130            return false;
131        }
132    
133        public boolean isShutdownInfo() {
134            return false;
135        }
136    
137        public boolean isWireFormatInfo() {
138            return false;
139        }
140    
141        public void setCommandId(int value) {
142        }
143    
144        public void setFrom(Endpoint from) {
145        }
146    
147        public void setResponseRequired(boolean responseRequired) {
148        }
149    
150        public void setTo(Endpoint to) {
151        }
152    
153        public Response visit(CommandVisitor visitor) throws Exception {
154            return null;
155        }
156    
157        public byte getDataStructureType() {
158            return 0;
159        }
160    
161        public boolean isMarshallAware() {
162            return false;
163        }
164    
165        public String toString() {
166            StringBuffer buffer = new StringBuffer();
167            buffer.append(getAction());
168            buffer.append("\n");
169            Map headers = getHeaders();
170            for (Iterator iter = headers.entrySet().iterator(); iter.hasNext();) {
171                Map.Entry entry = (Map.Entry)iter.next();
172                buffer.append(entry.getKey());
173                buffer.append(":");
174                buffer.append(entry.getValue());
175                buffer.append("\n");
176            }
177            buffer.append("\n");
178            if (getContent() != null) {
179                try {
180                    buffer.append(new String(getContent()));
181                } catch (Throwable e) {
182                    buffer.append(Arrays.toString(getContent()));
183                }
184            }
185            return buffer.toString();
186        }
187    }