summaryrefslogtreecommitdiff
path: root/java/client/src/main/java/org/apache/qpid/client/message/JMSHeaderAdapter.java
blob: e295d4a2a041813a65e1b261c0dc7d357e578db9 (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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
/*
 *
 * 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.
 *
 */
package org.apache.qpid.client.message;

import java.util.Enumeration;

import javax.jms.JMSException;
import javax.jms.MessageFormatException;

import org.apache.mina.common.ByteBuffer;
import org.apache.qpid.AMQPInvalidClassException;
import org.apache.qpid.framing.AMQShortString;
import org.apache.qpid.framing.FieldTable;


public final class JMSHeaderAdapter
{
    private final FieldTable _headers;

    public JMSHeaderAdapter(FieldTable headers)
    {
        _headers = headers;
    }


    public FieldTable getHeaders()
    {
        return _headers;
    }

    public boolean getBoolean(String string) throws JMSException
    {
        checkPropertyName(string);
        Boolean b = getHeaders().getBoolean(string);

        if (b == null)
        {
            if (getHeaders().containsKey(string))
            {
                Object str = getHeaders().getObject(string);

                if (str == null || !(str instanceof String))
                {
                    throw new MessageFormatException("getBoolean can't use " + string + " item.");
                }
                else
                {
                    return Boolean.valueOf((String) str);
                }
            }
            else
            {
                b = Boolean.valueOf(null);
            }
        }

        return b;
    }

    public boolean getBoolean(AMQShortString string) throws JMSException
    {
        checkPropertyName(string);
        Boolean b = getHeaders().getBoolean(string);

        if (b == null)
        {
            if (getHeaders().containsKey(string))
            {
                Object str = getHeaders().getObject(string);

                if (str == null || !(str instanceof String))
                {
                    throw new MessageFormatException("getBoolean can't use " + string + " item.");
                }
                else
                {
                    return Boolean.valueOf((String) str);
                }
            }
            else
            {
                b = Boolean.valueOf(null);
            }
        }

        return b;
    }

    public char getCharacter(String string) throws JMSException
    {
        checkPropertyName(string);
        Character c = getHeaders().getCharacter(string);

        if (c == null)
        {
            if (getHeaders().isNullStringValue(string))
            {
                throw new NullPointerException("Cannot convert null char");
            }
            else
            {
                throw new MessageFormatException("getChar can't use " + string + " item.");
            }
        }
        else
        {
            return (char) c;
        }
    }

    public byte[] getBytes(String string) throws JMSException
    {
        return getBytes(new AMQShortString(string));
    }

    public byte[] getBytes(AMQShortString string) throws JMSException
    {
        checkPropertyName(string);

        byte[] bs = getHeaders().getBytes(string);

        if (bs == null)
        {
            throw new MessageFormatException("getBytes can't use " + string + " item.");
        }
        else
        {
            return bs;
        }
    }

    public byte getByte(String string) throws JMSException
    {
        checkPropertyName(string);
        Byte b = getHeaders().getByte(string);
        if (b == null)
        {
            if (getHeaders().containsKey(string))
            {
                Object str = getHeaders().getObject(string);

                if (str == null || !(str instanceof String))
                {
                    throw new MessageFormatException("getByte can't use " + string + " item.");
                }
                else
                {
                    return Byte.valueOf((String) str);
                }
            }
            else
            {
                b = Byte.valueOf(null);
            }
        }

        return b;
    }

    public short getShort(String string) throws JMSException
    {
        checkPropertyName(string);
        Short s = getHeaders().getShort(string);

        if (s == null)
        {
            s = Short.valueOf(getByte(string));
        }

        return s;
    }

    public int getInteger(String string) throws JMSException
    {
        checkPropertyName(string);
        Integer i = getHeaders().getInteger(string);

        if (i == null)
        {
            i = Integer.valueOf(getShort(string));
        }

        return i;
    }

    public long getLong(String string) throws JMSException
    {
        checkPropertyName(string);
        Long l = getHeaders().getLong(string);

        if (l == null)
        {
            l = Long.valueOf(getInteger(string));
        }

        return l;
    }

    public float getFloat(String string) throws JMSException
    {
        checkPropertyName(string);
        Float f = getHeaders().getFloat(string);

        if (f == null)
        {
            if (getHeaders().containsKey(string))
            {
                Object str = getHeaders().getObject(string);

                if (str == null || !(str instanceof String))
                {
                    throw new MessageFormatException("getFloat can't use " + string + " item.");
                }
                else
                {
                    return Float.valueOf((String) str);
                }
            }
            else
            {
                throw new NullPointerException("No such property: " + string);
            }

        }

        return f;
    }

    public double getDouble(String string) throws JMSException
    {
        checkPropertyName(string);
        Double d = getHeaders().getDouble(string);

        if (d == null)
        {
            d = Double.valueOf(getFloat(string));
        }

        return d;
    }

    public String getString(String string) throws JMSException
    {
        checkPropertyName(string);
        String s = getHeaders().getString(string);

        if (s == null)
        {
            if (getHeaders().containsKey(string))
            {
                Object o = getHeaders().getObject(string);
                if (o instanceof byte[])
                {
                    throw new MessageFormatException("getObject couldn't find " + string + " item.");
                }
                else
                {
                    if (o == null)
                    {
                        return null;
                    }
                    else
                    {
                        s = String.valueOf(o);
                    }
                }
            }//else return s // null; 
        }

        return s;
    }

    public Object getObject(String string) throws JMSException
    {
        checkPropertyName(string);
        return getHeaders().getObject(string);
    }

    public void setBoolean(AMQShortString string, boolean b) throws JMSException
    {
        checkPropertyName(string);
        getHeaders().setBoolean(string, b);
    }

    public void setBoolean(String string, boolean b) throws JMSException
    {
        checkPropertyName(string);
        getHeaders().setBoolean(string, b);
    }

    public void setChar(String string, char c) throws JMSException
    {
        checkPropertyName(string);
        getHeaders().setChar(string, c);
    }

    public Object setBytes(AMQShortString string, byte[] bytes)
    {
        checkPropertyName(string);
        return getHeaders().setBytes(string, bytes);
    }

    public Object setBytes(String string, byte[] bytes)
    {
        checkPropertyName(string);
        return getHeaders().setBytes(string, bytes);
    }

    public Object setBytes(String string, byte[] bytes, int start, int length)
    {
        checkPropertyName(string);
        return getHeaders().setBytes(string, bytes, start, length);
    }

    public void setByte(String string, byte b) throws JMSException
    {
        checkPropertyName(string);
        getHeaders().setByte(string, b);
    }

    public void setByte(AMQShortString string, byte b) throws JMSException
    {
        checkPropertyName(string);
        getHeaders().setByte(string, b);
    }


    public void setShort(String string, short i) throws JMSException
    {
        checkPropertyName(string);
        getHeaders().setShort(string, i);
    }

    public void setInteger(String string, int i) throws JMSException
    {
        checkPropertyName(string);
        getHeaders().setInteger(string, i);
    }

    public void setInteger(AMQShortString string, int i) throws JMSException
    {
        checkPropertyName(string);
        getHeaders().setInteger(string, i);
    }

    public void setLong(String string, long l) throws JMSException
    {
        checkPropertyName(string);
        getHeaders().setLong(string, l);
    }

    public void setFloat(String string, float v) throws JMSException
    {
        checkPropertyName(string);
        getHeaders().setFloat(string, v);
    }

    public void setDouble(String string, double v) throws JMSException
    {
        checkPropertyName(string);
        getHeaders().setDouble(string, v);
    }

    public void setString(String string, String string1) throws JMSException
    {
        checkPropertyName(string);
        getHeaders().setString(string, string1);
    }

    public void setString(AMQShortString string, String string1) throws JMSException
    {
        checkPropertyName(string);
        getHeaders().setString(string, string1);
    }

    public void setObject(String string, Object object) throws JMSException
    {
        checkPropertyName(string);
        try
        {
            getHeaders().setObject(string, object);
        }
        catch (AMQPInvalidClassException aice)
        {
            MessageFormatException mfe = new MessageFormatException(AMQPInvalidClassException.INVALID_OBJECT_MSG + (object == null ? "null" : object.getClass()));
            mfe.setLinkedException(aice);
            mfe.initCause(aice);
            throw mfe;
        }
    }

    public boolean itemExists(String string) throws JMSException
    {
        checkPropertyName(string);
        return getHeaders().containsKey(string);
    }

    public Enumeration getPropertyNames()
    {
        return getHeaders().getPropertyNames();
    }

    public void clear()
    {
        getHeaders().clear();
    }

    public boolean propertyExists(AMQShortString propertyName)
    {
        checkPropertyName(propertyName);
        return getHeaders().propertyExists(propertyName);
    }

    public boolean propertyExists(String propertyName)
    {
        checkPropertyName(propertyName);
        return getHeaders().propertyExists(propertyName);
    }

    public Object put(Object key, Object value)
    {
        checkPropertyName(key.toString());
        return getHeaders().setObject(key.toString(), value);
    }

    public Object remove(AMQShortString propertyName)
    {
        checkPropertyName(propertyName);
        return getHeaders().remove(propertyName);
    }

    public Object remove(String propertyName)
    {
        checkPropertyName(propertyName);
        return getHeaders().remove(propertyName);
    }

    public boolean isEmpty()
    {
        return getHeaders().isEmpty();
    }

    public void writeToBuffer(ByteBuffer data)
    {
        getHeaders().writeToBuffer(data);
    }

    public Enumeration getMapNames()
    {
        return getPropertyNames();
    }

    protected void checkPropertyName(CharSequence propertyName)
    {
        if (propertyName == null)
        {
            throw new IllegalArgumentException("Property name must not be null");
        }
        else if (propertyName.length() == 0)
        {
            throw new IllegalArgumentException("Property name must not be the empty string");
        }

        checkIdentiferFormat(propertyName);
    }

    protected void checkIdentiferFormat(CharSequence propertyName)
    {
//        JMS requirements 3.5.1 Property Names
//        Identifiers:
//        - An identifier is an unlimited-length character sequence that must begin
//          with a Java identifier start character; all following characters must be Java
//          identifier part characters. An identifier start character is any character for
//          which the method Character.isJavaIdentifierStart returns true. This includes
//          '_' and '$'. An identifier part character is any character for which the
//          method Character.isJavaIdentifierPart returns true.
//        - Identifiers cannot be the names NULL, TRUE, or FALSE.
//          Identifiers cannot be NOT, AND, OR, BETWEEN, LIKE, IN, IS, or
//          ESCAPE.
//          Identifiers are either header field references or property references. The
//          type of a property value in a message selector corresponds to the type
//          used to set the property. If a property that does not exist in a message is
//          referenced, its value is NULL. The semantics of evaluating NULL values
//          in a selector are described in Section 3.8.1.2, Null Values.
//          The conversions that apply to the get methods for properties do not
//          apply when a property is used in a message selector expression. For
//          example, suppose you set a property as a string value, as in the
//          following:
//              myMessage.setStringProperty("NumberOfOrders", "2");
//          The following expression in a message selector would evaluate to false,
//          because a string cannot be used in an arithmetic expression:
//          "NumberOfOrders > 1"
//          Identifiers are case sensitive.
//          Message header field references are restricted to JMSDeliveryMode,
//          JMSPriority, JMSMessageID, JMSTimestamp, JMSCorrelationID, and
//          JMSType. JMSMessageID, JMSCorrelationID, and JMSType values may be
//          null and if so are treated as a NULL value.

        if (Boolean.getBoolean("strict-jms"))
        {
            // JMS start character
            if (!(Character.isJavaIdentifierStart(propertyName.charAt(0))))
            {
                throw new IllegalArgumentException("Identifier '" + propertyName + "' does not start with a valid JMS identifier start character");
            }

            // JMS part character
            int length = propertyName.length();
            for (int c = 1; c < length; c++)
            {
                if (!(Character.isJavaIdentifierPart(propertyName.charAt(c))))
                {
                    throw new IllegalArgumentException("Identifier '" + propertyName + "' contains an invalid JMS identifier character");
                }
            }

            // JMS invalid names
            if ((propertyName.equals("NULL")
                 || propertyName.equals("TRUE")
                 || propertyName.equals("FALSE")
                 || propertyName.equals("NOT")
                 || propertyName.equals("AND")
                 || propertyName.equals("OR")
                 || propertyName.equals("BETWEEN")
                 || propertyName.equals("LIKE")
                 || propertyName.equals("IN")
                 || propertyName.equals("IS")
                 || propertyName.equals("ESCAPE")))
            {
                throw new IllegalArgumentException("Identifier '" + propertyName + "' is not allowed in JMS");
            }
        }

    }
}