1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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
63
64
65
66
67
68
69
70
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
/**
* $RCSfile$
* $Revision$
* $Date$
*
* Copyright (C) 2004 Jive Software. All rights reserved.
*
* This software is published under the terms of the GNU Public License (GPL),
* a copy of which is included in this distribution.
*/
package org.jivesoftware.util;
import org.jivesoftware.wildfire.*;
import org.jivesoftware.wildfire.auth.AuthToken;
import org.jivesoftware.wildfire.group.GroupManager;
import org.jivesoftware.wildfire.muc.MultiUserChatServer;
import org.jivesoftware.wildfire.roster.RosterManager;
import org.jivesoftware.wildfire.user.User;
import org.jivesoftware.wildfire.user.UserManager;
import java.io.*;
import java.net.URL;
import java.util.*;
/**
* A utility bean for Wildfire admin console pages.
*/
public class WebManager extends WebBean {
private int start = 0;
private int range = 15;
public WebManager() {
}
/**
* Returns the auth token redirects to the login page if an auth token is not found.
*/
public AuthToken getAuthToken() {
return (AuthToken)session.getAttribute("jive.admin.authToken");
}
/**
* Returns <tt>true</tt> if the Wildfire container is in setup mode, <tt>false</tt> otherwise.
*/
public boolean isSetupMode() {
return getXMPPServer().isSetupMode();
}
/**
* Returns the XMPP server object -- can get many config items from here.
*/
public XMPPServer getXMPPServer() {
final XMPPServer xmppServer = XMPPServer.getInstance();
if (xmppServer == null) {
// Show that the server is down
showServerDown();
return null;
}
return xmppServer;
}
public UserManager getUserManager() {
return getXMPPServer().getUserManager();
}
public GroupManager getGroupManager() {
return GroupManager.getInstance();
}
public RosterManager getRosterManager() {
return getXMPPServer().getRosterManager();
}
public PrivateStorage getPrivateStore() {
return getXMPPServer().getPrivateStorage();
}
public PresenceManager getPresenceManager() {
return getXMPPServer().getPresenceManager();
}
public SessionManager getSessionManager() {
return getXMPPServer().getSessionManager();
}
public MultiUserChatServer getMultiUserChatServer() {
return getXMPPServer().getMultiUserChatServer();
}
public XMPPServerInfo getServerInfo() {
return getXMPPServer().getServerInfo();
}
/**
* Returns the page user or <tt>null</tt> if one is not found.
*/
public User getUser() {
User pageUser = null;
try {
pageUser = getUserManager().getUser(getAuthToken().getUsername());
}
catch (Exception ignored) {
// Ignore.
}
return pageUser;
}
/**
* Returns <tt>true</tt> if the server is in embedded mode, <tt>false</tt> otherwise.
*/
public boolean isEmbedded() {
try {
ClassUtils.forName("org.jivesoftware.wildfire.starter.ServerStarter");
return true;
}
catch (Exception ignored) {
return false;
}
}
/**
* Restarts the server then sleeps for 3 seconds.
*/
public void restart() {
try {
getXMPPServer().restart();
}
catch (Exception e) {
Log.error(e);
}
sleep();
}
/**
* Stops the server then sleeps for 3 seconds.
*/
public void stop() {
try {
getXMPPServer().stop();
}
catch (Exception e) {
Log.error(e);
}
sleep();
}
public WebManager getManager() {
return this;
}
public void validateService() {
if (getPresenceManager() == null ||
getXMPPServer() == null) {
showServerDown();
}
}
public boolean isServerRunning() {
return !(getPresenceManager() == null || getXMPPServer() == null);
}
public void setStart(int start) {
this.start = start;
}
public int getStart() {
return start;
}
public void setRange(int range) {
this.range = range;
}
public int getRange() {
return range;
}
public int getCurrentPage() {
return (start / range) + 1;
}
private void sleep() {
// Sleep for a minute:
try {
Thread.sleep(3000L);
}
catch (Exception ignored) {
// Ignore.
}
}
protected void showServerDown() {
try {
response.sendRedirect("error-serverdown.jsp");
}
catch (Exception ex) {
ex.printStackTrace();
}
}
/**
* Copies the contents at <CODE>src</CODE> to <CODE>dst</CODE>.
*/
public static void copy(URL src, File dst) throws IOException {
InputStream in = null;
OutputStream out = null;
try {
in = src.openStream();
out = new FileOutputStream(dst);
dst.mkdirs();
copy(in, out);
}
finally {
try {
if (in != null) {
in.close();
}
}
catch (IOException e) {
// Ignore.
}
try {
if (out != null) {
out.close();
}
}
catch (IOException e) {
// Ignore.
}
}
}
/**
* Common code for copy routines. By convention, the streams are
* closed in the same method in which they were opened. Thus,
* this method does not close the streams when the copying is done.
*/
private static void copy(InputStream in, OutputStream out) throws IOException {
byte[] buffer = new byte[4096];
while (true) {
int bytesRead = in.read(buffer);
if (bytesRead < 0) {
break;
}
out.write(buffer, 0, bytesRead);
}
}
/**
* Returns the number of rows per page for the specified page for the current logged user.
* The rows per page value is stored as a user property. The same property is being used for
* different pages. The encoding format is the following "pageName1=value,pageName2=value".
*
* @param pageName the name of the page to look up its stored value.
* @param defaultValue the default value to return if no user value was found.
* @return the number of rows per page for the specified page for the current logged user.
*/
public int getRowsPerPage(String pageName, int defaultValue) {
return getPageProperty(pageName, "console.rows_per_page", defaultValue);
}
/**
* Sets the new number of rows per page for the specified page for the current logged user.
* The rows per page value is stored as a user property. The same property is being used for
* different pages. The encoding format is the following "pageName1=value,pageName2=value".
*
* @param pageName the name of the page to stored its new value.
* @param newValue the new rows per page value.
*/
public void setRowsPerPage(String pageName, int newValue) {
setPageProperty(pageName, "console.rows_per_page", newValue);
}
/**
* Returns the number of seconds between each page refresh for the specified page for the
* current logged user. The value is stored as a user property. The same property is being
* used for different pages. The encoding format is the following
* "pageName1=value,pageName2=value".
*
* @param pageName the name of the page to look up its stored value.
* @param defaultValue the default value to return if no user value was found.
* @return the number of seconds between each page refresh for the specified page for
* the current logged user.
*/
public int getRefreshValue(String pageName, int defaultValue) {
return getPageProperty(pageName, "console.refresh", defaultValue);
}
/**
* Sets the number of seconds between each page refresh for the specified page for the
* current logged user. The value is stored as a user property. The same property is being
* used for different pages. The encoding format is the following
* "pageName1=value,pageName2=value".
*
* @param pageName the name of the page to stored its new value.
* @param newValue the new number of seconds between each page refresh.
*/
public void setRefreshValue(String pageName, int newValue) {
setPageProperty(pageName, "console.refresh", newValue);
}
public int getPageProperty(String pageName, String property, int defaultValue) {
User user = getUser();
if (user != null) {
String values = user.getProperties().get(property);
if (values != null) {
StringTokenizer tokens = new StringTokenizer(values, ",=");
while (tokens.hasMoreTokens()) {
String page = tokens.nextToken().trim();
String rows = tokens.nextToken().trim();
if (pageName.equals(page)) {
try {
return Integer.parseInt(rows);
}
catch (NumberFormatException e) {
return defaultValue;
}
}
}
}
}
return defaultValue;
}
public void setPageProperty(String pageName, String property, int newValue) {
String toStore = pageName + "=" + newValue;
User user = getUser();
if (user != null) {
String values = user.getProperties().get(property);
if (values != null) {
if (values.contains(toStore)) {
// The new value for the page was already stored so do nothing
return;
}
else {
if (values.contains(pageName)) {
// Replace an old value for the page with the new value
int oldValue = getPageProperty(pageName, property, -1);
String toRemove = pageName + "=" + oldValue;
user.getProperties().put(property, values.replace(toRemove, toStore));
}
else {
// Append the new page-value
user.getProperties().put(property, values + "," + toStore);
}
}
}
else {
// Store the new page-value as a new user property
user.getProperties().put(property, toStore);
}
}
}
public Cache[] getCaches() {
List<Cache> caches = new ArrayList<Cache>(CacheManager.getCaches());
Collections.sort(caches, new Comparator() {
public int compare(Object o1, Object o2) {
Cache cache1 = (Cache)o1;
Cache cache2 = (Cache)o2;
return cache1.getName().toLowerCase().compareTo(cache2.getName().toLowerCase());
}
});
return caches.toArray(new Cache[]{});
}
}