Source code: org/activemq/transport/stomp/FrameBuilder.java
1 /*
2 * Copyright (c) 2005 Your Corporation. All Rights Reserved.
3 */
4 package org.activemq.transport.stomp;
5
6 import org.activemq.message.ActiveMQMessage;
7
8 import java.io.ByteArrayOutputStream;
9 import java.io.IOException;
10 import java.util.Iterator;
11 import java.util.Properties;
12
13 class FrameBuilder
14 {
15 private String command;
16 private Properties headers = new Properties();
17 private byte[] body = new byte[0];
18
19 public FrameBuilder(String command)
20 {
21 this.command = command;
22 }
23
24 public FrameBuilder addHeader(String key, String value)
25 {
26 if (value != null)
27 {
28 this.headers.setProperty(key, value);
29 }
30 return this;
31 }
32
33 public FrameBuilder addHeader(String key, long value)
34 {
35 this.headers.put(key, new Long(value));
36 return this;
37 }
38
39 public FrameBuilder addHeaders(ActiveMQMessage message)
40 {
41 addHeader(Stomp.Headers.Message.DESTINATION,
42 DestinationNamer.convert(message.getJMSActiveMQDestination()));
43 addHeader(Stomp.Headers.Message.MESSAGE_ID, message.getJMSMessageID());
44 addHeader(Stomp.Headers.Message.CORRELATION_ID, message.getJMSCorrelationID());
45 addHeader(Stomp.Headers.Message.EXPIRATION_TIME, message.getJMSExpiration());
46 if (message.getJMSRedelivered())
47 {
48 addHeader(Stomp.Headers.Message.REDELIVERED, "true");
49 }
50 addHeader(Stomp.Headers.Message.PRORITY, message.getJMSPriority());
51 addHeader(Stomp.Headers.Message.REPLY_TO, DestinationNamer.convert(message.getJMSReplyTo()));
52 addHeader(Stomp.Headers.Message.TIMESTAMP, message.getJMSTimestamp());
53 addHeader(Stomp.Headers.Message.TYPE, message.getJMSType());
54
55 // now lets add all the message headers
56 headers.putAll(message.getProperties());
57 return this;
58 }
59
60
61 public FrameBuilder setBody(byte[] body)
62 {
63 this.body = body;
64 return this;
65 }
66
67 public String toString()
68 {
69 StringBuffer buffer = new StringBuffer();
70 buffer.append(command);
71 buffer.append(Stomp.NEWLINE);
72 for (Iterator iterator = headers.keySet().iterator(); iterator.hasNext();)
73 {
74 String key = (String) iterator.next();
75 String property = headers.getProperty(key);
76 if (property != null)
77 {
78 buffer.append(key)
79 .append(Stomp.Headers.SEPERATOR)
80 .append(property)
81 .append(Stomp.NEWLINE);
82 }
83 }
84 buffer.append(Stomp.NEWLINE);
85 buffer.append(body);
86 buffer.append(Stomp.NULL);
87 buffer.append(Stomp.NEWLINE);
88 return buffer.toString();
89 }
90
91 byte[] toFrame()
92 {
93 ByteArrayOutputStream bout = new ByteArrayOutputStream();
94 try
95 {
96 bout.write(command.getBytes());
97 bout.write(Stomp.NEWLINE.getBytes());
98 for (Iterator iterator = headers.keySet().iterator(); iterator.hasNext();)
99 {
100 String key = (String) iterator.next();
101 String property = headers.getProperty(key);
102 if (property != null)
103 {
104 bout.write(key.getBytes());
105 bout.write(Stomp.Headers.SEPERATOR.getBytes());
106 bout.write(property.getBytes());
107 bout.write(Stomp.NEWLINE.getBytes());
108 }
109 }
110 bout.write(Stomp.NEWLINE.getBytes());
111 bout.write(body);
112 bout.write(Stomp.NULL.getBytes());
113 bout.write(Stomp.NEWLINE.getBytes());
114 }
115 catch (IOException e)
116 {
117 throw new RuntimeException("World is caving in, we just got io error writing to" +
118 "a byte array output stream we instantiated!");
119 }
120 return bout.toByteArray();
121 }
122 }