PacketsNotification.java 5.01 KB
Newer Older
1 2 3 4
/**
 * $Revision: $
 * $Date: $
 *
5
 * Copyright (C) 2005-2008 Jive Software. All rights reserved.
6
 *
7 8 9 10 11 12 13 14 15 16 17
 * 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.
18 19
 */

20
package org.jivesoftware.openfire.commands.admin;
21 22

import org.dom4j.Element;
23 24 25 26
import org.jivesoftware.openfire.commands.AdHocCommand;
import org.jivesoftware.openfire.commands.SessionData;
import org.jivesoftware.openfire.component.InternalComponentManager;
import org.jivesoftware.openfire.interceptor.PacketCopier;
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
import org.xmpp.forms.DataForm;
import org.xmpp.forms.FormField;
import org.xmpp.packet.JID;

import java.util.Arrays;
import java.util.List;

/**
 * Command that allows to retrieve the presence of all active users.
 *
 * @author Gaston Dombiak
 *
 * TODO Use i18n
 * TODO Create command for removing subscriptions. Subscriptions will now be removed when component disconnects.
 */
public class PacketsNotification extends AdHocCommand {

44 45
    @Override
	protected void addStageInformation(SessionData data, Element command) {
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
        DataForm form = new DataForm(DataForm.Type.form);
        form.setTitle("Receiving notification of packets activity");
        form.addInstruction("Fill out this form to configure packets to receive.");

        FormField field = form.addField();
        field.setType(FormField.Type.hidden);
        field.setVariable("FORM_TYPE");
        field.addValue("http://jabber.org/protocol/admin");

        field = form.addField();
        field.setType(FormField.Type.list_multi);
        field.setLabel("Type of packet");
        field.setVariable("packet_type");
        field.addOption("Presence", "presence");
        field.addOption("IQ", "iq");
        field.addOption("Message", "message");
        field.setRequired(true);

        field = form.addField();
        field.setType(FormField.Type.list_single);
        field.setLabel("Direction");
        field.setVariable("direction");
        field.addOption("Incoming", "incoming");
        field.addOption("Outgoing", "outgoing");
        field.setRequired(true);

        field = form.addField();
        field.setType(FormField.Type.list_single);
        field.setLabel("Processing time");
        field.setVariable("processed");
        field.addOption("Before processing", "false");
        field.addOption("After processing", "true");
        field.setRequired(true);

        // Add the form to the command
        command.add(form.getElement());
    }

84 85
    @Override
	public void execute(SessionData data, Element command) {
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
        boolean presenceEnabled = false;
        boolean messageEnabled = false;
        boolean iqEnabled = false;
        for (String packet_type : data.getData().get("packet_type")) {
            if ("presence".equals(packet_type)) {
                presenceEnabled = true;
            }
            else if ("iq".equals(packet_type)) {
                iqEnabled = true;
            }
            else if ("message".equals(packet_type)) {
                messageEnabled = true;
            }
        }

        boolean incoming = "incoming".equals(data.getData().get("direction").get(0));
        boolean processed = "true".equals(data.getData().get("processed").get(0));

        JID componentJID = data.getOwner();
        // Create or update subscription of the component to receive packet notifications
106 107
        PacketCopier.getInstance()
                .addSubscriber(componentJID, iqEnabled, messageEnabled, presenceEnabled, incoming, processed);
108 109 110 111 112 113 114

        // Inform that everything went fine
        Element note = command.addElement("note");
        note.addAttribute("type", "info");
        note.setText("Operation finished successfully");
    }

115 116
    @Override
	public String getCode() {
117 118 119
        return "http://jabber.org/protocol/admin#packets_notification";
    }

120 121
    @Override
	public String getDefaultLabel() {
122 123 124
        return "Get notifications of packet activity";
    }

125 126
    @Override
	protected List<Action> getActions(SessionData data) {
127 128 129
        return Arrays.asList(Action.complete);
    }

130 131
    @Override
	protected Action getExecuteAction(SessionData data) {
132 133 134
        return Action.complete;
    }

135 136
    @Override
	public int getMaxStages(SessionData data) {
137 138 139 140 141 142 143 144 145 146
        return 1;
    }

    /**
     * Returns if the requester can access this command. Only components are allowed to
     * execute this command.
     *
     * @param requester the JID of the user requesting to execute this command.
     * @return true if the requester can access this command.
     */
147 148
    @Override
	public boolean hasPermission(JID requester) {
149
        return InternalComponentManager.getInstance().hasComponent(requester);
150 151
    }
}