summaryrefslogtreecommitdiff
path: root/qpid/java/client-api/src/main/java/org/apache/qpid/messaging/util/MessageFactory_AMQP_0_10.java
blob: 1b6a2cdc53dd4aeb3fbd59b447cad7b301f5b6af (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
package org.apache.qpid.messaging.util;

import java.nio.ByteBuffer;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.qpid.messaging.Message;
import org.apache.qpid.messaging.MessageEncodingException;
import org.apache.qpid.messaging.MessageFactory;
import org.apache.qpid.messaging.MessagingException;
import org.apache.qpid.transport.DeliveryProperties;
import org.apache.qpid.transport.MessageDeliveryMode;
import org.apache.qpid.transport.MessageDeliveryPriority;
import org.apache.qpid.transport.MessageProperties;
import org.apache.qpid.transport.codec.BBDecoder;
import org.apache.qpid.transport.codec.BBEncoder;
import org.apache.qpid.util.UUIDGen;
import org.apache.qpid.util.UUIDs;

/**
 * A generic message factory that is based on the AMQP 0-10 encoding.
 *
 */
public class MessageFactory_AMQP_0_10 extends AbstractMessageFactory
{
    protected Class<? extends MessageFactory> getFactoryClass()
    {
        return MessageFactory_AMQP_0_10.class;
    }

    protected Map<String,Object> decodeAsMap(ByteBuffer buf) throws MessageEncodingException
    {
        try
        {
            BBDecoder decorder = new BBDecoder();
            decorder.init(buf);
            return decorder.readMap();
        }
        catch (Exception e)
        {
            throw new MessageEncodingException("Error decoding content as Map",e);
        }
    }

    protected ByteBuffer encodeMap(Map<String,Object> map) throws MessageEncodingException
    {
        try
        {
            //need to investigate the capacity here.
            BBEncoder encoder = new BBEncoder(1024);
            encoder.writeMap(map);
            return (ByteBuffer)encoder.buffer().flip();
        }
        catch (Exception e)
        {
            throw new MessageEncodingException("Cannot encode Map" ,e);
        }
    }

    protected List<Object> decodeAsList(ByteBuffer buf) throws MessageEncodingException
    {
        try
        {
            BBDecoder decorder = new BBDecoder();
            decorder.init(buf);
            return decorder.readList();
        }
        catch (Exception e)
        {
            throw new MessageEncodingException("Error decoding content as List",e);
        }
    }

    protected ByteBuffer encodeList(List<Object> list) throws MessageEncodingException
    {
        try
        {
            //need to investigate the capacity here.
            BBEncoder encoder = new BBEncoder(1024);
            encoder.writeList(list);
            return (ByteBuffer)encoder.buffer().flip();
        }
        catch (Exception e)
        {
            throw new MessageEncodingException("Cannot encode List" ,e);
        }
    }

    @Override
    protected Message createFactorySpecificMessageDelegate()
    {
        return new Mesage_AMQP_0_10_Delegate();
    }

    class Mesage_AMQP_0_10_Delegate implements Message
    {
        private MessageProperties _messageProps;
        private DeliveryProperties _deliveryProps;

        private UUIDGen _ssnNameGenerator = UUIDs.newGenerator();

        // creating a new message for sending
        protected Mesage_AMQP_0_10_Delegate()
        {
            this(new MessageProperties(),new DeliveryProperties());
        }

        // Message received with data.
        protected Mesage_AMQP_0_10_Delegate(MessageProperties messageProps,
                                            DeliveryProperties deliveryProps)
        {
            _messageProps = messageProps;
            _deliveryProps = deliveryProps;
        }

        @Override
        public String getMessageId() throws MessagingException
        {
            return _messageProps.getMessageId().toString();
        }

        @Override
        public void setMessageId(String messageId) throws MessagingException
        {
            // Temp hack for the time being
            _messageProps.setMessageId(_ssnNameGenerator.generate());
        }

        @Override
        public String getSubject() throws MessagingException
        {
            Map<String,Object> props = _messageProps.getApplicationHeaders();
            return props == null ? null : (String)props.get(QPID_SUBJECT);
        }

        @Override
        public void setSubject(String subject) throws MessagingException
        {
            Map<String,Object> props = _messageProps.getApplicationHeaders();
            if (props == null)
            {
                props = new HashMap<String,Object>();
                _messageProps.setApplicationHeaders(props);
            }
            props.put(QPID_SUBJECT, subject);
        }

        @Override
        public String getContentType() throws MessagingException
        {
            return _messageProps.getContentType();
        }

        @Override
        public void setContentType(String contentType)
        throws MessagingException
        {
            _messageProps.setContentType(contentType);
        }

        @Override
        public String getCorrelationId() throws MessagingException
        {
            return new String(_messageProps.getCorrelationId());
        }

        @Override
        public void setCorrelationId(String correlationId)
        throws MessagingException
        {
            _messageProps.setCorrelationId(correlationId.getBytes());
        }

        @Override
        public String getReplyTo() throws MessagingException
        {
            return addressFrom0_10_ReplyTo(_deliveryProps.getExchange(),
                    _deliveryProps.getRoutingKey());
        }

        @Override
        public void setReplyTo(String replyTo) throws MessagingException
        {
            // TODO
        }

        @Override
        public String getUserId() throws MessagingException
        {
            return new String(_messageProps.getUserId());
        }

        @Override
        public void setUserId(String userId) throws MessagingException
        {
            _messageProps.setUserId(userId.getBytes());
        }

        @Override
        public boolean isDurable() throws MessagingException
        {
            return _deliveryProps.getDeliveryMode() == MessageDeliveryMode.PERSISTENT;
        }

        @Override
        public void setDurable(boolean durable) throws MessagingException
        {
            _deliveryProps.setDeliveryMode(durable ?
                    MessageDeliveryMode.PERSISTENT: MessageDeliveryMode.NON_PERSISTENT);
        }

        @Override
        public boolean isRedelivered() throws MessagingException
        {
            return _deliveryProps.getRedelivered();
        }

        @Override
        public void setRedelivered(boolean redelivered)
        throws MessagingException
        {

            _deliveryProps.setRedelivered(redelivered);
        }

        @Override
        public int getPriority() throws MessagingException
        {
            return _deliveryProps.getPriority().getValue();
        }

        @Override
        public void setPriority(int priority) throws MessagingException
        {
            _deliveryProps.setPriority(MessageDeliveryPriority.get((short)priority));

        }

        @Override
        public long getTtl() throws MessagingException
        {
            return _deliveryProps.getTtl();
        }

        @Override
        public void setTtl(long ttl) throws MessagingException
        {
            _deliveryProps.setTtl(ttl);
        }

        @Override
        public long getTimestamp() throws MessagingException
        {
            return _deliveryProps.getTimestamp();
        }

        @Override
        public void setTimestamp(long timestamp) throws MessagingException
        {
            _deliveryProps.setTimestamp(timestamp);
        }

        @Override
        public Map<String, Object> getProperties() throws MessagingException
        {
            return _messageProps.getApplicationHeaders();
        }

        @Override
        public void setProperty(String key, Object value)
        throws MessagingException
        {
            Map<String,Object> props = _messageProps.getApplicationHeaders();
            if (props == null)
            {
                props = new HashMap<String,Object>();
                _messageProps.setApplicationHeaders(props);
            }
            props.put(key, value);
        }

        private String addressFrom0_10_ReplyTo(String exchange, String routingKey)
        {
            if ("".equals(exchange)) // type Queue
            {
                return routingKey;
            }
            else
            {
                return exchange + "/" + routingKey;
            }
        }

        // noop, this is for the headers only.
        @Override
        public ByteBuffer getContent() throws MessagingException
        {
            throw new MessagingException("Empty!");
        }
    }
}