aboutsummaryrefslogtreecommitdiff
path: root/src/com/kenai/jbosh/TerminalBindingCondition.java
blob: 0aecfd826a95289a26d0bf509ffbb789e77c43a4 (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
/*
 * Copyright 2009 Mike Cumings
 *
 * Licensed 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 com.kenai.jbosh;

import java.util.HashMap;
import java.util.Map;

/**
 * Terminal binding conditions and their associated messages.
 */
final class TerminalBindingCondition {

    /**
     * Map of condition names to condition instances.
     */
    private static final Map<String, TerminalBindingCondition>
            COND_TO_INSTANCE = new HashMap<String, TerminalBindingCondition>();

    /**
     * Map of HTTP response codes to condition instances.
     */
    private static final Map<Integer, TerminalBindingCondition>
            CODE_TO_INSTANCE = new HashMap<Integer, TerminalBindingCondition>();

    static final TerminalBindingCondition BAD_REQUEST =
            createWithCode("bad-request", "The format of an HTTP header or "
            + "binding element received from the client is unacceptable "
            + "(e.g., syntax error).", Integer.valueOf(400));

    static final TerminalBindingCondition HOST_GONE =
            create("host-gone", "The target domain specified in the 'to' "
            + "attribute or the target host or port specified in the 'route' "
            + "attribute is no longer serviced by the connection manager.");

    static final TerminalBindingCondition HOST_UNKNOWN =
            create("host-unknown", "The target domain specified in the 'to' "
            + "attribute or the target host or port specified in the 'route' "
            + "attribute is unknown to the connection manager.");

    static final TerminalBindingCondition IMPROPER_ADDRESSING =
            create("improper-addressing", "The initialization element lacks a "
            + "'to' or 'route' attribute (or the attribute has no value) but "
            + "the connection manager requires one.");

    static final TerminalBindingCondition INTERNAL_SERVER_ERROR =
            create("internal-server-error", "The connection manager has "
            + "experienced an internal error that prevents it from servicing "
            + "the request.");

    static final TerminalBindingCondition ITEM_NOT_FOUND =
            createWithCode("item-not-found", "(1) 'sid' is not valid, (2) "
            + "'stream' is not valid, (3) 'rid' is larger than the upper limit "
            + "of the expected window, (4) connection manager is unable to "
            + "resend response, (5) 'key' sequence is invalid.",
            Integer.valueOf(404));

    static final TerminalBindingCondition OTHER_REQUEST =
            create("other-request", "Another request being processed at the "
            + "same time as this request caused the session to terminate.");

    static final TerminalBindingCondition POLICY_VIOLATION =
            createWithCode("policy-violation", "The client has broken the "
            + "session rules (polling too frequently, requesting too "
            + "frequently, sending too many simultaneous requests).",
            Integer.valueOf(403));

    static final TerminalBindingCondition REMOTE_CONNECTION_FAILED =
            create("remote-connection-failed", "The connection manager was "
            + "unable to connect to, or unable to connect securely to, or has "
            + "lost its connection to, the server.");

    static final TerminalBindingCondition REMOTE_STREAM_ERROR =
            create("remote-stream-error", "Encapsulated transport protocol "
            + "error.");

    static final TerminalBindingCondition SEE_OTHER_URI =
            create("see-other-uri", "The connection manager does not operate "
            + "at this URI (e.g., the connection manager accepts only SSL or "
            + "TLS connections at some https: URI rather than the http: URI "
            + "requested by the client).");

    static final TerminalBindingCondition SYSTEM_SHUTDOWN =
            create("system-shutdown", "The connection manager is being shut "
            + "down. All active HTTP sessions are being terminated. No new "
            + "sessions can be created.");

    static final TerminalBindingCondition UNDEFINED_CONDITION =
            create("undefined-condition", "Unknown or undefined error "
            + "condition.");

    /**
     * Condition name.
     */
    private final String cond;

    /**
     * Descriptive message.
     */
    private final String msg;

    /**
     * Private constructor to pre
     */
    private TerminalBindingCondition(
            final String condition,
            final String message) {
        cond = condition;
        msg = message;
    }
    
    /**
     * Helper method to call the helper method to add entries.
     */
    private static TerminalBindingCondition create(
            final String condition,
            final String message) {
        return createWithCode(condition, message, null);
    }
    
    /**
     * Helper method to add entries.
     */
    private static TerminalBindingCondition createWithCode(
            final String condition,
            final String message,
            final Integer code) {
        if (condition == null) {
            throw(new IllegalArgumentException(
                    "condition may not be null"));
        }
        if (message == null) {
            throw(new IllegalArgumentException(
                    "message may not be null"));
        }
        if (COND_TO_INSTANCE.get(condition) != null) {
            throw(new IllegalStateException(
                    "Multiple definitions of condition: " + condition));
        }
        TerminalBindingCondition result =
                new TerminalBindingCondition(condition, message);
        COND_TO_INSTANCE.put(condition, result);
        if (code != null) {
            if (CODE_TO_INSTANCE.get(code) != null) {
                throw(new IllegalStateException(
                        "Multiple definitions of code: " + code));
            }
            CODE_TO_INSTANCE.put(code, result);
        }
        return result;
    }

    /**
     * Lookup the terminal binding condition instance with the condition
     * name specified.
     *
     * @param condStr condition name
     * @return terminal binding condition instance, or {@code null} if no
     *  instance is known by the name specified
     */
    static TerminalBindingCondition forString(final String condStr) {
        return COND_TO_INSTANCE.get(condStr);
    }

    /**
     * Lookup the terminal binding condition instance associated with the
     * HTTP response code specified.
     *
     * @param httpRespCode HTTP response code
     * @return terminal binding condition instance, or {@code null} if no
     *  instance is known by the response code specified
     */
    static TerminalBindingCondition forHTTPResponseCode(final int httpRespCode) {
        return CODE_TO_INSTANCE.get(Integer.valueOf(httpRespCode));
    }

    /**
     * Get the name of the condition.
     *
     * @return condition name
     */
    String getCondition() {
        return cond;
    }

    /**
     * Get the human readable error message associated with this condition.
     *
     * @return error message
     */
    String getMessage() {
        return msg;
    }

}