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 package net.sf.hermesftp.console;
26
27 import java.util.List;
28 import java.util.Map;
29 import java.util.Properties;
30
31 import net.sf.hermesftp.common.FtpConstants;
32 import net.sf.hermesftp.usermanager.UserManager;
33 import net.sf.hermesftp.usermanager.model.UserData;
34
35 /***
36 * Provides an overview of system and application properties.
37 *
38 * @author Administrator
39 */
40 public class UserServlet extends AbstractConsoleServlet {
41
42 private static final String NOT_APPLICABLE = "n/a";
43
44 private static final long serialVersionUID = -594524060863329206L;
45
46 private UserManager userManager;
47
48 /***
49 * {@inheritDoc}
50 */
51 protected Properties getContentProperties() {
52 Properties result = new Properties();
53 String s = userManager != null ? createUserTable() : NOT_APPLICABLE;
54 result.put("user.table", s);
55 return result;
56 }
57
58 private String createUserTable() {
59
60 List<UserData> list = null;
61 try {
62 list = getUserManager().getUserDataList();
63 } catch (Exception e) {
64 return "not available";
65 }
66 StringBuffer sb = new StringBuffer();
67 sb.append(ConsoleConstants.TABLE_START_TAG);
68 createColumnHeaders(sb);
69
70 int rowCount = 0;
71 for (UserData userData : list) {
72 rowCount++;
73 fillRow(sb, rowCount, userData);
74 }
75
76 sb.append(ConsoleConstants.TABLE_END_TAG);
77 return sb.toString();
78 }
79
80 private void fillRow(StringBuffer sb, int rowCount, UserData user) {
81 String band = rowCount % 2 == 0 ? ConsoleConstants.TR_START_TAG_CLASS_A
82 : ConsoleConstants.TR_START_TAG_CLASS_B;
83 sb.append(band);
84
85 fillField(sb, user.getUid());
86 fillField(sb, user.getFullName());
87 fillNumField(sb, getUserStat(user.getUid(), FtpConstants.STAT_BYTES_DOWNLOADED));
88 fillNumField(sb, getUserStat(user.getUid(), FtpConstants.STAT_BYTES_UPLOADED));
89 fillNumField(sb, getUserStat(user.getUid(), FtpConstants.STAT_FILES_DOWNLOADED));
90 fillNumField(sb, getUserStat(user.getUid(), FtpConstants.STAT_FILES_UPLOADED));
91 fillNumField(sb, getUserStat(user.getUid(), FtpConstants.STAT_DOWNLOAD_RATE));
92 fillNumField(sb, getUserStat(user.getUid(), FtpConstants.STAT_UPLOAD_RATE));
93
94 sb.append(ConsoleConstants.TR_END_TAG);
95 }
96
97 private void createColumnHeaders(StringBuffer sb) {
98 sb.append(ConsoleConstants.TR_START_TAG);
99 sb.append(ConsoleConstants.TH_START_TAG + "User ID" + ConsoleConstants.TH_END_TAG);
100 sb.append(ConsoleConstants.TH_START_TAG + "Name" + ConsoleConstants.TH_END_TAG);
101 sb.append(ConsoleConstants.TH_START_TAG + "DL Bytes" + ConsoleConstants.TH_END_TAG);
102 sb.append(ConsoleConstants.TH_START_TAG + "UL Bytes" + ConsoleConstants.TH_END_TAG);
103 sb.append(ConsoleConstants.TH_START_TAG + "DL Files" + ConsoleConstants.TH_END_TAG);
104 sb.append(ConsoleConstants.TH_START_TAG + "UL Files" + ConsoleConstants.TH_END_TAG);
105 sb.append(ConsoleConstants.TH_START_TAG + "DL KB/s" + ConsoleConstants.TH_END_TAG);
106 sb.append(ConsoleConstants.TH_START_TAG + "UL KB/s" + ConsoleConstants.TH_END_TAG);
107 sb.append(ConsoleConstants.TR_END_TAG);
108 }
109
110 private String getUserStat(String uid, String key) {
111 String result;
112 Map<String, Long> userStats = getUserManager().getUserStatistics(uid);
113 if (userStats != null) {
114 Long val = userStats.get(key);
115 result = val == null ? "0" : formatNum(val);
116 } else {
117 result = NOT_APPLICABLE;
118 }
119 return result;
120 }
121
122 /***
123 * Getter methode for property <code>userManager</code>.
124 *
125 * @return Property <code>userManager</code>.
126 */
127 public UserManager getUserManager() {
128 return userManager;
129 }
130
131 /***
132 * Setter methode for property <code>userManager</code>.
133 *
134 * @param userManager Value for <code>userManager</code>.
135 */
136 public void setUserManager(UserManager userManager) {
137 this.userManager = userManager;
138 }
139
140 }