CMSessionParams.java revision d7955ce24d294fb2014c59d11fca184471056f44
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
19/**
20 * A BOSH connection manager session instance.  This consolidates the
21 * configuration knowledge related to the CM session and provides a
22 * mechanism by which
23 */
24final class CMSessionParams {
25
26    private final AttrSessionID sid;
27
28    private final AttrWait wait;
29
30    private final AttrVersion ver;
31
32    private final AttrPolling polling;
33
34    private final AttrInactivity inactivity;
35
36    private final AttrRequests requests;
37
38    private final AttrHold hold;
39
40    private final AttrAccept accept;
41
42    private final AttrMaxPause maxPause;
43
44    private final AttrAck ack;
45
46    private final AttrCharsets charsets;
47
48    private final boolean ackingRequests;
49
50    /**
51     * Prevent direct construction.
52     */
53    private CMSessionParams(
54            final AttrSessionID aSid,
55            final AttrWait aWait,
56            final AttrVersion aVer,
57            final AttrPolling aPolling,
58            final AttrInactivity aInactivity,
59            final AttrRequests aRequests,
60            final AttrHold aHold,
61            final AttrAccept aAccept,
62            final AttrMaxPause aMaxPause,
63            final AttrAck aAck,
64            final AttrCharsets aCharsets,
65            final boolean amAckingRequests) {
66        sid = aSid;
67        wait = aWait;
68        ver = aVer;
69        polling = aPolling;
70        inactivity = aInactivity;
71        requests = aRequests;
72        hold = aHold;
73        accept = aAccept;
74        maxPause = aMaxPause;
75        ack = aAck;
76        charsets = aCharsets;
77        ackingRequests = amAckingRequests;
78    }
79
80    static CMSessionParams fromSessionInit(
81            final AbstractBody req,
82            final AbstractBody resp)
83    throws BOSHException {
84        AttrAck aAck = AttrAck.createFromString(
85                resp.getAttribute(Attributes.ACK));
86        String rid = req.getAttribute(Attributes.RID);
87        boolean acking = (aAck != null && aAck.getValue().equals(rid));
88
89        return new CMSessionParams(
90            AttrSessionID.createFromString(
91                getRequiredAttribute(resp, Attributes.SID)),
92            AttrWait.createFromString(
93                getRequiredAttribute(resp, Attributes.WAIT)),
94            AttrVersion.createFromString(
95                resp.getAttribute(Attributes.VER)),
96            AttrPolling.createFromString(
97                resp.getAttribute(Attributes.POLLING)),
98            AttrInactivity.createFromString(
99                resp.getAttribute(Attributes.INACTIVITY)),
100            AttrRequests.createFromString(
101                resp.getAttribute(Attributes.REQUESTS)),
102            AttrHold.createFromString(
103                resp.getAttribute(Attributes.HOLD)),
104            AttrAccept.createFromString(
105                resp.getAttribute(Attributes.ACCEPT)),
106            AttrMaxPause.createFromString(
107                resp.getAttribute(Attributes.MAXPAUSE)),
108            aAck,
109            AttrCharsets.createFromString(
110                resp.getAttribute(Attributes.CHARSETS)),
111            acking
112            );
113    }
114
115    private static String getRequiredAttribute(
116            final AbstractBody body,
117            final BodyQName name)
118    throws BOSHException {
119        String attrStr = body.getAttribute(name);
120        if (attrStr == null) {
121            throw(new BOSHException(
122                    "Connection Manager session creation response did not "
123                    + "include required '" + name.getLocalPart()
124                    + "' attribute"));
125        }
126        return attrStr;
127    }
128
129    AttrSessionID getSessionID() {
130        return sid;
131    }
132
133    AttrWait getWait() {
134        return wait;
135    }
136
137    AttrVersion getVersion() {
138        return ver;
139    }
140
141    AttrPolling getPollingInterval() {
142        return polling;
143    }
144
145    AttrInactivity getInactivityPeriod() {
146        return inactivity;
147    }
148
149    AttrRequests getRequests() {
150        return requests;
151    }
152
153    AttrHold getHold() {
154        return hold;
155    }
156
157    AttrAccept getAccept() {
158        return accept;
159    }
160
161    AttrMaxPause getMaxPause() {
162        return maxPause;
163    }
164
165    AttrAck getAck() {
166        return ack;
167    }
168
169    AttrCharsets getCharsets() {
170        return charsets;
171    }
172
173    boolean isAckingRequests() {
174        return ackingRequests;
175    }
176
177}
178