1 /*
2  * Copyright (C) 2005-2008 Jive Software. All rights reserved.
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 
17 package org.jivesoftware.openfire.commands.admin;
18 
19 import org.dom4j.Element;
20 import org.jivesoftware.openfire.SessionManager;
21 import org.jivesoftware.openfire.commands.AdHocCommand;
22 import org.jivesoftware.openfire.commands.SessionData;
23 import org.jivesoftware.openfire.session.ClientSession;
24 import org.xmpp.forms.DataForm;
25 import org.xmpp.forms.FormField;
26 
27 import java.util.Collection;
28 import java.util.HashSet;
29 import java.util.List;
30 import java.util.Set;
31 
32 /**
33  * Command that allows to retrieve the number of online users who are active at any one moment.
34  * Active users are those users that have sent an available presence.
35  *
36  * @author Gaston Dombiak
37  */
38 public class GetNumberActiveUsers extends AdHocCommand {
39 
40     @Override
addStageInformation(SessionData data, Element command)41     protected void addStageInformation(SessionData data, Element command) {
42         //Do nothing since there are no stages
43     }
44 
45     @Override
execute(SessionData data, Element command)46     public void execute(SessionData data, Element command) {
47         DataForm form = new DataForm(DataForm.Type.result);
48 
49         FormField field = form.addField();
50         field.setType(FormField.Type.hidden);
51         field.setVariable("FORM_TYPE");
52         field.addValue("http://jabber.org/protocol/admin");
53 
54         field = form.addField();
55         field.setType(FormField.Type.text_single);
56         field.setLabel(getLabel());
57         field.setVariable("activeusersnum");
58         // Make sure that we are only counting based on bareJIDs and not fullJIDs
59         Collection<ClientSession> sessions = SessionManager.getInstance().getSessions();
60         Set<String> users = new HashSet<>(sessions.size());
61         for (ClientSession session : sessions) {
62             if (session.getPresence().isAvailable()) {
63                 users.add(session.getAddress().toBareJID());
64             }
65         }
66         field.addValue(users.size());
67 
68         command.add(form.getElement());
69     }
70 
71     @Override
getActions(SessionData data)72     protected List<Action> getActions(SessionData data) {
73         //Do nothing since there are no stages
74         return null;
75     }
76 
77     @Override
getCode()78     public String getCode() {
79         return "http://jabber.org/protocol/admin#get-active-users-num";
80     }
81 
82     @Override
getDefaultLabel()83     public String getDefaultLabel() {
84         // TODO Use i18n
85         return "Number of Active Users";
86     }
87 
88     @Override
getExecuteAction(SessionData data)89     protected Action getExecuteAction(SessionData data) {
90         //Do nothing since there are no stages
91         return null;
92     }
93 
94     @Override
getMaxStages(SessionData data)95     public int getMaxStages(SessionData data) {
96         return 0;
97     }
98 }
99