GetUsersPresence.java 4.7 KB
Newer Older
Gaston Dombiak's avatar
Gaston Dombiak committed
1 2 3 4
/**
 * $Revision: $
 * $Date: $
 *
5
 * Copyright (C) 2005-2008 Jive Software. All rights reserved.
Gaston Dombiak's avatar
Gaston Dombiak committed
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.
Gaston Dombiak's avatar
Gaston Dombiak committed
18 19
 */

20
package org.jivesoftware.openfire.commands.admin;
Gaston Dombiak's avatar
Gaston Dombiak committed
21 22

import org.dom4j.Element;
23 24 25 26 27
import org.jivesoftware.openfire.SessionManager;
import org.jivesoftware.openfire.commands.AdHocCommand;
import org.jivesoftware.openfire.commands.SessionData;
import org.jivesoftware.openfire.component.InternalComponentManager;
import org.jivesoftware.openfire.session.ClientSession;
Gaston Dombiak's avatar
Gaston Dombiak committed
28 29
import org.xmpp.forms.DataForm;
import org.xmpp.forms.FormField;
30
import org.xmpp.packet.JID;
Gaston Dombiak's avatar
Gaston Dombiak committed
31 32

import java.util.Collection;
33
import java.util.Collections;
Gaston Dombiak's avatar
Gaston Dombiak committed
34 35 36 37 38 39 40 41 42 43 44
import java.util.List;

/**
 * Command that allows to retrieve the presence of all active users.
 *
 * @author Gaston Dombiak
 *
 * TODO Use i18n
 */
public class GetUsersPresence extends AdHocCommand {

45 46
    @Override
	protected void addStageInformation(SessionData data, Element command) {
Gaston Dombiak's avatar
Gaston Dombiak committed
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
        DataForm form = new DataForm(DataForm.Type.form);
        form.setTitle("Requesting Presence of Active Users");
        form.addInstruction("Fill out this form to request the active users presence of this service.");

        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_single);
        field.setLabel("Maximum number of items to return");
        field.setVariable("max_items");
        field.addOption("25", "25");
        field.addOption("50", "50");
        field.addOption("75", "75");
        field.addOption("100", "100");
        field.addOption("150", "150");
        field.addOption("200", "200");
        field.addOption("None", "none");

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

72 73
    @Override
	public void execute(SessionData data, Element command) {
Gaston Dombiak's avatar
Gaston Dombiak committed
74 75
        String max_items = data.getData().get("max_items").get(0);
        int maxItems = -1;
Gaston Dombiak's avatar
Gaston Dombiak committed
76
        if (max_items != null && "none".equals(max_items)) {
Gaston Dombiak's avatar
Gaston Dombiak committed
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
            try {
                maxItems = Integer.parseInt(max_items);
            }
            catch (NumberFormatException e) {
                // Do nothing. Assume that all users are being requested
            }
        }

        DataForm form = new DataForm(DataForm.Type.result);

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

        field = form.addField();
        field.setLabel("The presences of active users");
        field.setVariable("activeuserpresences");

        // Get list of users (i.e. bareJIDs) that are connected to the server
        Collection<ClientSession> sessions = SessionManager.getInstance().getSessions();
        int index = 1;
        for (ClientSession session : sessions) {
            if (session.getPresence().isAvailable()) {
                field.addValue(session.getPresence().toXML());
            }
            if (maxItems > 0 && index >= maxItems) {
                break;
            }
        }
        command.add(form.getElement());
    }

110 111
    @Override
	public String getCode() {
Gaston Dombiak's avatar
Gaston Dombiak committed
112 113 114
        return "http://jabber.org/protocol/admin#get-active-presences";
    }

115 116
    @Override
	public String getDefaultLabel() {
Gaston Dombiak's avatar
Gaston Dombiak committed
117 118 119
        return "Get Presence of Active Users";
    }

120 121
    @Override
	protected List<Action> getActions(SessionData data) {
122
        return Collections.singletonList(Action.complete);
Gaston Dombiak's avatar
Gaston Dombiak committed
123 124
    }

125 126
    @Override
	protected Action getExecuteAction(SessionData data) {
Gaston Dombiak's avatar
Gaston Dombiak committed
127 128 129
        return Action.complete;
    }

130 131
    @Override
	public int getMaxStages(SessionData data) {
Gaston Dombiak's avatar
Gaston Dombiak committed
132 133
        return 1;
    }
134 135 136 137 138 139 140 141

    /**
     * Returns if the requester can access this command. Admins and components are allowed to
     * execute this command.
     *
     * @param requester the JID of the entity requesting to execute this command.
     * @return true if the requester can access this command.
     */
142 143
    @Override
	public boolean hasPermission(JID requester) {
144
        return super.hasPermission(requester) || InternalComponentManager.getInstance().hasComponent(requester);
145
    }
Gaston Dombiak's avatar
Gaston Dombiak committed
146
}