1/*
2 * Copyright 2009 Mike Cumings
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *   http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.kenai.jbosh;
18
19import java.util.Collections;
20import java.util.Map;
21import java.util.Set;
22
23/**
24 * Class representing a single message to or from the BOSH connection
25 * manager (CM).
26 * <p/>
27 * These messages consist of a single {@code body} element
28 * (qualified within the BOSH namespace:
29 * {@code http://jabber.org/protocol/httpbind}) and contain zero or more
30 * child elements (of any namespace).  These child elements constitute the
31 * message payload.
32 * <p/>
33 * In addition to the message payload, the attributes of the wrapper
34 * {@code body} element may also need to be used as part of the communication
35 * protocol being implemented on top of BOSH, or to define additional
36 * namespaces used by the child "payload" elements.  These attributes are
37 * exposed via accessors.
38 */
39public abstract class AbstractBody {
40
41    ///////////////////////////////////////////////////////////////////////////
42    // Constructor:
43
44    /**
45     * Restrict subclasses to the local package.
46     */
47    AbstractBody() {
48        // Empty
49    }
50
51    ///////////////////////////////////////////////////////////////////////////
52    // Public methods:
53
54    /**
55     * Get a set of all defined attribute names.
56     *
57     * @return set of qualified attribute names
58     */
59    public final Set<BodyQName> getAttributeNames() {
60        Map<BodyQName, String> attrs = getAttributes();
61        return Collections.unmodifiableSet(attrs.keySet());
62    }
63
64    /**
65     * Get the value of the specified attribute.
66     *
67     * @param attr name of the attribute to retriece
68     * @return attribute value, or {@code null} if not defined
69     */
70    public final String getAttribute(final BodyQName attr) {
71        Map<BodyQName, String> attrs = getAttributes();
72        return attrs.get(attr);
73    }
74
75    ///////////////////////////////////////////////////////////////////////////
76    // Abstract methods:
77
78    /**
79     * Get a map of all defined attribute names with their corresponding values.
80     *
81     * @return map of qualified attributes
82     */
83    public abstract Map<BodyQName, String> getAttributes();
84
85    /**
86     * Get an XML String representation of this message.
87     *
88     * @return XML string representing the body message
89     */
90    public abstract String toXML();
91
92    ///////////////////////////////////////////////////////////////////////////
93    // Package-private methods:
94
95    /**
96     * Returns the qualified name of the root/wrapper element.
97     *
98     * @return qualified name
99     */
100    static BodyQName getBodyQName() {
101        return BodyQName.createBOSH("body");
102    }
103
104}
105