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.EventObject;
20
21/**
22 * Event representing a message sent to or from a BOSH connection manager.
23 * <p/>
24 * This class is immutable and thread-safe.
25 */
26public final class BOSHMessageEvent extends EventObject {
27
28    /**
29     * Serialized version.
30     */
31    private static final long serialVersionUID = 1L;
32
33    /**
34     * Message which was sent or received.
35     */
36    private final AbstractBody body;
37
38    /**
39     * Creates a new message event instance.
40     *
41     * @param source event source
42     * @param cBody message body
43     */
44    private BOSHMessageEvent(
45            final Object source,
46            final AbstractBody cBody) {
47        super(source);
48        if (cBody == null) {
49            throw(new IllegalArgumentException(
50                    "message body may not be null"));
51        }
52        body = cBody;
53    }
54
55    /**
56     * Creates a new message event for clients sending events to the
57     * connection manager.
58     *
59     * @param source sender of the message
60     * @param body message body
61     * @return event instance
62     */
63    static BOSHMessageEvent createRequestSentEvent(
64            final BOSHClient source,
65            final AbstractBody body) {
66        return new BOSHMessageEvent(source, body);
67    }
68
69    /**
70     * Creates a new message event for clients receiving new messages
71     * from the connection manager.
72     *
73     * @param source receiver of the message
74     * @param body message body
75     * @return event instance
76     */
77    static BOSHMessageEvent createResponseReceivedEvent(
78            final BOSHClient source,
79            final AbstractBody body) {
80        return new BOSHMessageEvent(source, body);
81    }
82
83    /**
84     * Gets the message body which was sent or received.
85     *
86     * @return message body
87     */
88    public AbstractBody getBody() {
89        return body;
90    }
91
92}
93