OfflineMessageStrategy.java 5.13 KB
Newer Older
Matt Tucker's avatar
Matt Tucker committed
1 2 3 4 5
/**
 * $RCSfile$
 * $Revision$
 * $Date$
 *
Matt Tucker's avatar
Matt Tucker committed
6
 * Copyright (C) 2004 Jive Software. All rights reserved.
Matt Tucker's avatar
Matt Tucker committed
7
 *
Matt Tucker's avatar
Matt Tucker committed
8 9
 * This software is published under the terms of the GNU Public License (GPL),
 * a copy of which is included in this distribution.
Matt Tucker's avatar
Matt Tucker committed
10
 */
Matt Tucker's avatar
Matt Tucker committed
11

Matt Tucker's avatar
Matt Tucker committed
12 13 14
package org.jivesoftware.messenger;

import org.jivesoftware.messenger.auth.UnauthorizedException;
Derek DeMoro's avatar
Derek DeMoro committed
15
import org.jivesoftware.messenger.container.BasicModule;
Matt Tucker's avatar
Matt Tucker committed
16
import org.jivesoftware.messenger.user.UserNotFoundException;
Derek DeMoro's avatar
Derek DeMoro committed
17 18
import org.jivesoftware.util.Log;
import org.xmpp.packet.JID;
19
import org.xmpp.packet.Message;
Derek DeMoro's avatar
Derek DeMoro committed
20
import org.xmpp.packet.PacketError;
Matt Tucker's avatar
Matt Tucker committed
21 22

/**
Derek DeMoro's avatar
Derek DeMoro committed
23
 * <p>Implements the strategy as a basic server module.</p>
Matt Tucker's avatar
Matt Tucker committed
24 25 26
 *
 * @author Iain Shigeoka
 */
Derek DeMoro's avatar
Derek DeMoro committed
27
public class OfflineMessageStrategy extends BasicModule {
Matt Tucker's avatar
Matt Tucker committed
28

29 30
    private static int quota = 100*1024; // Default to 100 K.
    private static Type type = Type.store_and_bounce;
Derek DeMoro's avatar
Derek DeMoro committed
31
    private SessionManager sessionManager;
Matt Tucker's avatar
Matt Tucker committed
32

33 34
    private XMPPServer xmppServer;
    private OfflineMessageStore messageStore;
Matt Tucker's avatar
Matt Tucker committed
35

Derek DeMoro's avatar
Derek DeMoro committed
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
    public OfflineMessageStrategy() {
        super("Offline Message Strategy");
    }

    public int getQuota() {
        return quota;
    }

    public void setQuota(int quota) {
        OfflineMessageStrategy.quota = quota;
        JiveGlobals.setProperty("xmpp.offline.quota", Integer.toString(quota));
    }

    public OfflineMessageStrategy.Type getType() {
        return type;
    }

    public void setType(OfflineMessageStrategy.Type type) {
        if (type == null) {
            throw new IllegalArgumentException();
        }
        OfflineMessageStrategy.type = type;
        JiveGlobals.setProperty("xmpp.offline.type", type.toString());
    }

    public void storeOffline(Message message) throws UnauthorizedException, UserNotFoundException {
        if (message != null) {
Matt Tucker's avatar
Matt Tucker committed
63 64 65
            Session senderSession = sessionManager.getSession(message.getFrom());
            if (senderSession == null) {
                return;
Derek DeMoro's avatar
Derek DeMoro committed
66 67 68 69
            }
            JID sender = senderSession.getAddress();

            // server messages and anonymous messages can be silently dropped
Matt Tucker's avatar
Matt Tucker committed
70
            if (sender == null || sender.getNode() == null) {
Derek DeMoro's avatar
Derek DeMoro committed
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
                // silently drop the server message
            }
            else {
                if (type == Type.bounce) {
                    bounce(message);
                }
                else if (type == Type.store) {
                    store(message);
                }
                else if (type == Type.store_and_bounce) {
                    if (underQuota(message)) {
                        store(message);
                    }
                    else {
                        bounce(message);
                    }
                }
                else if (type == Type.store_and_drop) {
                    if (underQuota(message)) {
                        store(message);
                    }
                }
            }
        }
    }

    private boolean underQuota(Message message) {
        return quota > messageStore.getSize(message.getTo().getNode()) + message.toXML().length();
    }

    private void store(Message message) {
        messageStore.addMessage(message);
    }

    private void bounce(Message message) {
        // Generate a rejection response to the sender
        try {
Matt Tucker's avatar
Matt Tucker committed
108
            Message response = new Message();
Derek DeMoro's avatar
Derek DeMoro committed
109 110
            response.setTo(message.getFrom());
            response.setFrom(xmppServer.createJID(null, null));
Matt Tucker's avatar
Matt Tucker committed
111 112
            response.setBody("Message could not be delivered to " + message.getTo() +
                    ". User is offline or unreachable.");
Derek DeMoro's avatar
Derek DeMoro committed
113 114 115 116 117

            Session session = sessionManager.getSession(message.getFrom());
            session.getConnection().deliver(response);

            Message errorResponse = message.createCopy();
Matt Tucker's avatar
Matt Tucker committed
118 119
            errorResponse.setError(new PacketError(PacketError.Condition.item_not_found,
                    PacketError.Type.continue_processing));
Derek DeMoro's avatar
Derek DeMoro committed
120 121 122 123 124 125 126
            session.getConnection().deliver(errorResponse);
        }
        catch (Exception e) {
            Log.error(e);
        }
    }

127 128 129 130 131
    public void initialize(XMPPServer server) {
        super.initialize(server);
        xmppServer = server;
        messageStore = server.getOfflineMessageStore();
        sessionManager = server.getSessionManager();
Derek DeMoro's avatar
Derek DeMoro committed
132

133 134 135 136 137 138 139 140
        String quota = JiveGlobals.getProperty("xmpp.offline.quota");
        if (quota != null && quota.length() > 0) {
            OfflineMessageStrategy.quota = Integer.parseInt(quota);
        }
        String type = JiveGlobals.getProperty("xmpp.offline.type");
        if (type != null && type.length() > 0) {
            OfflineMessageStrategy.type = Type.valueOf(type);
        }
Derek DeMoro's avatar
Derek DeMoro committed
141
    }
Matt Tucker's avatar
Matt Tucker committed
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

    /**
     * Strategy types.
     */
    public enum Type {

        /**
         * All messages are bounced to the sender.
         */
        bounce,

        /**
         * All messages are silently dropped.
         */
        drop,

        /**
         * All messages are stored.
         */
        store,

        /**
         * Messages are stored up to the storage limit, and then bounced.
         */
        store_and_bounce,

        /**
         * Messages are stored up to the storage limit, and then silently dropped.
         */
        store_and_drop;
    }
}