summaryrefslogtreecommitdiff
path: root/qpid/cpp/src/qpid/broker/amqp/Message.cpp
blob: 572eea38819414c8f3285734301ddc0e57b2986f (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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
/*
 *
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 *
 */
#include "Message.h"
#include "qpid/amqp/Decoder.h"
#include "qpid/amqp/descriptors.h"
#include "qpid/amqp/ListBuilder.h"
#include "qpid/amqp/MapBuilder.h"
#include "qpid/amqp/MapHandler.h"
#include "qpid/amqp/MessageEncoder.h"
#include "qpid/amqp/Reader.h"
#include "qpid/amqp/typecodes.h"
#include "qpid/types/encodings.h"
#include "qpid/log/Statement.h"
#include "qpid/framing/Buffer.h"
#include <string.h>

namespace qpid {
namespace broker {
namespace amqp {

using qpid::amqp::CharSequence;
using qpid::amqp::Constructor;
using qpid::amqp::Descriptor;
using qpid::amqp::MapHandler;
using qpid::amqp::Reader;

namespace {
std::string empty;
}

std::string Message::getRoutingKey() const
{
    std::string v;
    v.assign(subject.data, subject.size);
    return v;
}
std::string Message::getUserId() const
{
    std::string v;
    v.assign(userId.data, userId.size);
    return v;
}

bool Message::isPersistent() const
{
    return durable && durable.get();
}
bool Message::getTtl(uint64_t& t) const
{
    if (!ttl) {
        return false;
    } else {
        t = ttl.get();
        return true;
    }
}

uint8_t Message::getPriority() const
{
    if (!priority) return 4;
    else return priority.get();
}

std::string Message::getPropertyAsString(const std::string& /*key*/) const { return empty; }
std::string Message::getAnnotationAsString(const std::string& /*key*/) const { return empty; }

namespace {
    class PropertyAdapter : public Reader {
        MapHandler& handler;
        CharSequence key;
        enum {
            KEY,
            VALUE
        } state;

        void checkValue() {
            if ( state==VALUE ) state = KEY;
            else {
                // TODO: Would throwing an exception make more sense here?
                QPID_LOG(error, "Received non string property key");
                key = CharSequence();
                state = KEY;
            }
        }

        void onNull(const Descriptor*) {checkValue(); handler.handleVoid(key);}
        void onBoolean(bool b, const Descriptor*) {checkValue(); handler.handleBool(key, b);}
        void onUByte(uint8_t i, const Descriptor*) {checkValue(); handler.handleUint8(key, i);}
        void onUShort(uint16_t i, const Descriptor*) {checkValue(); handler.handleUint16(key, i);}
        void onUInt(uint32_t i, const Descriptor*) {checkValue(); handler.handleUint32(key, i);}
        void onULong(uint64_t i, const Descriptor*) {checkValue(); handler.handleUint64(key, i);}
        void onByte(int8_t i, const Descriptor*) {checkValue(); handler.handleInt8(key, i);}
        void onShort(int16_t i, const Descriptor*) {checkValue(); handler.handleInt16(key, i);}
        void onInt(int32_t i, const Descriptor*) {checkValue(); handler.handleInt32(key, i);}
        void onLong(int64_t i, const Descriptor*) {checkValue(); handler.handleInt64(key, i);}
        void onFloat(float x, const Descriptor*) {checkValue(); handler.handleFloat(key, x);}
        void onDouble(double x, const Descriptor*) {checkValue(); handler.handleDouble(key, x);}
        void onTimestamp(int64_t i, const Descriptor*) {checkValue(); handler.handleInt64(key, i);}

        void onString(const CharSequence& s, const Descriptor*) {
            if ( state==KEY ) {
                state = VALUE;
                key = s;
            } else {
                state = KEY;
                handler.handleString(key, s, CharSequence());
            }
        }

        bool onStartList(uint32_t, const CharSequence&, const CharSequence&, const Descriptor*) { return false; }
        bool onStartMap(uint32_t, const CharSequence&, const CharSequence&, const Descriptor*) { return false; }
        bool onStartArray(uint32_t, const CharSequence&, const Constructor&, const Descriptor*) { return false; }

    public:
        PropertyAdapter(MapHandler& mh) :
            handler(mh),
            state(KEY)
         {}
    };
}

void Message::processProperties(MapHandler& mh) const {
    qpid::amqp::Decoder d(applicationProperties.data, applicationProperties.size);
    PropertyAdapter mha(mh);
    d.read(mha);
}

//getContentSize() is primarily used in stats about the number of
//bytes enqueued/dequeued etc, not sure whether this is the right name
//and whether it should indeed only be the content that is thus
//measured
uint64_t Message::getContentSize() const { return data.size(); }
//getContent() is used primarily for decoding qmf messages in
//management and ha, but also by the xml exchange
std::string Message::getContent() const
{
    return std::string(body.data, body.size);
}

Message::Message(size_t size) : data(size)
{
    deliveryAnnotations.init();
    messageAnnotations.init();
    bareMessage.init();

    userId.init();
    to.init();
    subject.init();
    replyTo.init();
    contentType.init();
    contentEncoding.init();

    applicationProperties.init();
    body.init();
    footer.init();
}
char* Message::getData() { return &data[0]; }
const char* Message::getData() const { return &data[0]; }
size_t Message::getSize() const { return data.size(); }

qpid::amqp::MessageId Message::getMessageId() const
{
    return messageId;
}
qpid::amqp::CharSequence Message::getReplyTo() const
{
    return replyTo;
}
qpid::amqp::MessageId Message::getCorrelationId() const
{
    return correlationId;
}
qpid::amqp::CharSequence Message::getContentType() const
{
    return contentType;
}
qpid::amqp::CharSequence Message::getContentEncoding() const
{
    return contentEncoding;
}

qpid::amqp::CharSequence Message::getDeliveryAnnotations() const
{
    return deliveryAnnotations;
}
qpid::amqp::CharSequence Message::getMessageAnnotations() const
{
    return messageAnnotations;
}
qpid::amqp::CharSequence Message::getApplicationProperties() const
{
    return applicationProperties;
}
qpid::amqp::CharSequence Message::getBareMessage() const
{
    return bareMessage;
}
qpid::amqp::CharSequence Message::getBody() const
{
    return body;
}
qpid::amqp::CharSequence Message::getFooter() const
{
    return footer;
}

void Message::scan()
{
    qpid::amqp::Decoder decoder(getData(), getSize());
    decoder.read(*this);
    bareMessage = qpid::amqp::MessageReader::getBareMessage();
    if (bareMessage.data && !bareMessage.size) {
        bareMessage.size = getSize() - (bareMessage.data - getData());
    }
}

const Message& Message::get(const qpid::broker::Message& message)
{
    const Message* m = dynamic_cast<const Message*>(&message.getEncoding());
    if (!m) throw qpid::Exception("Translation not yet implemented!!");
    return *m;
}

void Message::onDurable(bool b) { durable = b; }
void Message::onPriority(uint8_t i) { priority = i; }
void Message::onTtl(uint32_t i) { ttl = i; }
void Message::onFirstAcquirer(bool b) { firstAcquirer = b; }
void Message::onDeliveryCount(uint32_t i) { deliveryCount = i; }

void Message::onMessageId(uint64_t v) { messageId.set(v); }
void Message::onMessageId(const qpid::amqp::CharSequence& v, qpid::types::VariantType t) { messageId.set(v, t); }
void Message::onUserId(const qpid::amqp::CharSequence& v) { userId = v; }
void Message::onTo(const qpid::amqp::CharSequence& v) { to = v; }
void Message::onSubject(const qpid::amqp::CharSequence& v) { subject = v; }
void Message::onReplyTo(const qpid::amqp::CharSequence& v) { replyTo = v; }
void Message::onCorrelationId(uint64_t v) { correlationId.set(v); }
void Message::onCorrelationId(const qpid::amqp::CharSequence& v, qpid::types::VariantType t) { correlationId.set(v, t);}
void Message::onContentType(const qpid::amqp::CharSequence& v) { contentType = v; }
void Message::onContentEncoding(const qpid::amqp::CharSequence& v) { contentEncoding = v; }
void Message::onAbsoluteExpiryTime(int64_t) {}
void Message::onCreationTime(int64_t) {}
void Message::onGroupId(const qpid::amqp::CharSequence&) {}
void Message::onGroupSequence(uint32_t) {}
void Message::onReplyToGroupId(const qpid::amqp::CharSequence&) {}

void Message::onApplicationProperties(const qpid::amqp::CharSequence& v, const qpid::amqp::CharSequence&) { applicationProperties = v; }
void Message::onDeliveryAnnotations(const qpid::amqp::CharSequence&, const qpid::amqp::CharSequence& v) { deliveryAnnotations = v; }
void Message::onMessageAnnotations(const qpid::amqp::CharSequence&, const qpid::amqp::CharSequence& v) { messageAnnotations = v; }

void Message::onData(const qpid::amqp::CharSequence& v) { body = v; }
void Message::onAmqpSequence(const qpid::amqp::CharSequence& v) { body = v; bodyType = qpid::amqp::typecodes::LIST_NAME; }
void Message::onAmqpValue(const qpid::amqp::CharSequence& v, const std::string& t)
{
    body = v;
    if (t == qpid::amqp::typecodes::STRING_NAME) {
        bodyType = qpid::types::encodings::UTF8;
    } else if (t == qpid::amqp::typecodes::SYMBOL_NAME) {
        bodyType = qpid::types::encodings::ASCII;
    } else if (t == qpid::amqp::typecodes::BINARY_NAME) {
        bodyType = qpid::types::encodings::BINARY;
    } else {
        bodyType = t;
    }
}
void Message::onAmqpValue(const qpid::types::Variant& v) { typedBody = v; }

void Message::onFooter(const qpid::amqp::CharSequence&, const qpid::amqp::CharSequence& v) { footer = v; }

bool Message::isTypedBody() const
{
    return !typedBody.isVoid() || !bodyType.empty();
}

qpid::types::Variant Message::getTypedBody() const
{
    if (bodyType == qpid::amqp::typecodes::LIST_NAME) {
        qpid::amqp::ListBuilder builder;
        qpid::amqp::Decoder decoder(body.data, body.size);
        decoder.read(builder);
        return builder.getList();
    } else if (bodyType == qpid::amqp::typecodes::MAP_NAME) {
        qpid::amqp::MapBuilder builder;
        qpid::amqp::Decoder decoder(body.data, body.size);
        decoder.read(builder);
        return builder.getMap();
    } else if (!bodyType.empty()) {
        qpid::types::Variant value(std::string(body.data, body.size));
        value.setEncoding(bodyType);
        return value;
    } else {
        return typedBody;
    }
}


//PersistableMessage interface:
void Message::encode(framing::Buffer& buffer) const
{
    buffer.putLong(0);//4-byte format indicator
    buffer.putRawData((const uint8_t*) getData(), getSize());
    QPID_LOG(debug, "Encoded 1.0 message of " << getSize() << " bytes, including " << bareMessage.size << " bytes of 'bare message'");
}
uint32_t Message::encodedSize() const
{
    return 4/*format indicator*/ + data.size();
}
//in 1.0 the binary header/content makes less sense and in any case
//the functionality that split originally supported (i.e. lazy-loaded
//messages) is no longer in use; for 1.0 we therefore treat the whole
//content as 'header' and load it in the first stage.
uint32_t Message::encodedHeaderSize() const
{
    return encodedSize();
}
void Message::decodeHeader(framing::Buffer& buffer)
{
    if (buffer.available() != getSize()) {
        QPID_LOG(warning, "1.0 Message buffer was " << data.size() << " bytes, but " << buffer.available() << " bytes are available. Resizing.");
        data.resize(buffer.available());
    }
    buffer.getRawData((uint8_t*) getData(), getSize());
    scan();
    QPID_LOG(debug, "Decoded 1.0 message of " << getSize() << " bytes, including " << bareMessage.size << " bytes of 'bare message'");
}
void Message::decodeContent(framing::Buffer& /*buffer*/) {}

boost::intrusive_ptr<PersistableMessage> Message::merge(const std::map<std::string, qpid::types::Variant>& added) const
{
    //message- or delivery- annotations? would have to determine that from the name, for now assume always message-annotations
    std::map<std::string, qpid::types::Variant> combined;
    const std::map<std::string, qpid::types::Variant>* annotations(0);
    if (messageAnnotations) {
        //combine existing and added annotations (TODO: this could be
        //optimised by avoiding the decode and simply 'editing' the
        //size and count in the raw data, then appending the new
        //elements).
        qpid::amqp::MapBuilder builder;
        qpid::amqp::Decoder decoder(messageAnnotations.data, messageAnnotations.size);
        decoder.read(builder);
        combined = builder.getMap();
        for (std::map<std::string, qpid::types::Variant>::const_iterator i = added.begin(); i != added.end(); ++i) {
            combined[i->first] = i->second;
        }
        annotations = &combined;
    } else {
        //additions form a whole new section
        annotations = &added;
    }
    size_t annotationsSize = qpid::amqp::MessageEncoder::getEncodedSize(*annotations, true) + 3/*descriptor*/;

    boost::intrusive_ptr<Message> copy(new Message(bareMessage.size+footer.size+deliveryAnnotations.size+annotationsSize));
    size_t position(0);
    if (deliveryAnnotations.size) {
        ::memcpy(&copy->data[position], deliveryAnnotations.data, deliveryAnnotations.size);
        position += deliveryAnnotations.size;
    }

    qpid::amqp::Encoder encoder(&copy->data[position], annotationsSize);
    encoder.writeMap(*annotations, &qpid::amqp::message::MESSAGE_ANNOTATIONS, true);
    position += encoder.getPosition();

    if (bareMessage) {
        ::memcpy(&copy->data[position], bareMessage.data, bareMessage.size);
        position += bareMessage.size;
    }
    if (footer) {
        ::memcpy(&copy->data[position], footer.data, footer.size);
        position += footer.size;
    }
    copy->data.resize(position);//annotationsSize may be slightly bigger than needed if optimisations are used (e.g. smallint)
    copy->scan();
    {
        qpid::amqp::MapBuilder builder;
        qpid::amqp::Decoder decoder(copy->messageAnnotations.data, copy->messageAnnotations.size);
        decoder.read(builder);
        QPID_LOG(notice, "Merged annotations are now: " << builder.getMap() << " raw=" << std::hex << std::string(copy->messageAnnotations.data, copy->messageAnnotations.size) << " " << copy->messageAnnotations.size << " bytes");
    }
    assert(copy->messageAnnotations);
    assert(copy->bareMessage.size == bareMessage.size);
    assert(copy->footer.size == footer.size);
    assert(copy->deliveryAnnotations.size == deliveryAnnotations.size);
    return copy;
}

}}} // namespace qpid::broker::amqp