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.io.IOException;
28 import java.io.PrintWriter;
29 import java.text.DateFormat;
30 import java.text.DecimalFormat;
31 import java.text.NumberFormat;
32 import java.text.SimpleDateFormat;
33 import java.util.Date;
34 import java.util.Properties;
35
36 import javax.servlet.ServletException;
37 import javax.servlet.http.HttpServlet;
38 import javax.servlet.http.HttpServletRequest;
39 import javax.servlet.http.HttpServletResponse;
40
41 import net.sf.hermesftp.utils.IOUtils;
42 import net.sf.hermesftp.utils.VarMerger;
43
44 /***
45 * Abstract super class of the servlets of the FTP console.
46 *
47 * @author Administrator
48 */
49 public abstract class AbstractConsoleServlet extends HttpServlet {
50
51 private static final long serialVersionUID = 1L;
52
53 private static final int DEFAULT_REFRESH_SECONDS = 10;
54
55 private static final DateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
56
57 private static final NumberFormat NUM_FORMAT = new DecimalFormat("###,###,###,###,##0");
58
59 private static final Date APP_START = new Date();
60
61 private static final String TEXT_ENCODING = "UTF-8";
62
63 private String headerResourceName = "/console-header.html";
64
65 private String footerResourceName = "/console-footer.html";
66
67 private String contentResourceName;
68
69 private int refreshInterval = DEFAULT_REFRESH_SECONDS;
70
71 /***
72 * {@inheritDoc}
73 */
74 protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException,
75 IOException {
76 String page = readPage();
77 VarMerger vm = new VarMerger(page);
78 Properties p = new Properties();
79 p.putAll(System.getProperties());
80 p.putAll(IOUtils.getAppProperties());
81 p.putAll(getContentProperties());
82 p.put("current.date", "" + formatDate(new Date()));
83 p.put("startup.date", "" + formatDate(APP_START));
84 p.put("refresh.interval", "" + getRefreshInterval());
85 vm.merge(p);
86 page = vm.getText();
87 resp.setContentType("text/html");
88 PrintWriter out = resp.getWriter();
89 out.write(page);
90
91 }
92
93 private String readPage() throws IOException {
94 StringBuffer responseBuffer = new StringBuffer();
95 String part;
96 part = IOUtils.loadTextResource(getHeaderResourceName(), TEXT_ENCODING);
97 responseBuffer.append(part);
98 part = IOUtils.loadTextResource(getContentResourceName(), TEXT_ENCODING);
99 responseBuffer.append(part);
100 part = IOUtils.loadTextResource(getFooterResourceName(), TEXT_ENCODING);
101 responseBuffer.append(part);
102 return responseBuffer.toString();
103 }
104
105 /***
106 * Creates an HTML table fied (TD element).
107 *
108 * @param sb The stringbuffer to write to.
109 * @param value The value to fill.
110 */
111 protected void fillField(StringBuffer sb, String value) {
112 sb.append(ConsoleConstants.TD_START_TAG);
113 sb.append(value);
114 sb.append(ConsoleConstants.TD_END_TAG);
115 }
116
117 /***
118 * Creates an HTML table fied (TD element).
119 *
120 * @param sb The stringbuffer to write to.
121 * @param value The numeric value to fill.
122 */
123 protected void fillNumField(StringBuffer sb, String value) {
124 sb.append(ConsoleConstants.TD_START_TAG_NUM);
125 sb.append(value);
126 sb.append(ConsoleConstants.TD_END_TAG);
127 }
128
129 /***
130 * Formats a date value.
131 *
132 * @param date The date to format.
133 * @return The formatted date.
134 */
135 protected String formatDate(Date date) {
136 return DATE_FORMAT.format(date);
137 }
138
139 /***
140 * Formats a numeric value.
141 *
142 * @param num The number to format.
143 * @return The formatted number.
144 */
145 protected String formatNum(Object num) {
146 return NUM_FORMAT.format(num);
147 }
148
149 /***
150 * Formats a long value.
151 *
152 * @param num The number to format.
153 * @return The formatted number.
154 */
155 protected String formatNum(long num) {
156 return NUM_FORMAT.format(num);
157 }
158
159 /***
160 * The content properties object. The properties object represents a mapping of placeholder keys
161 * and their corresponding values.
162 *
163 * @return The properties.
164 */
165 protected abstract Properties getContentProperties();
166
167 /***
168 * Getter methode for property <code>contentResourceName</code>.
169 *
170 * @return Property <code>contentResourceName</code>.
171 */
172 public String getContentResourceName() {
173 return contentResourceName;
174 }
175
176 /***
177 * Setter methode for property <code>contentResourceName</code>.
178 *
179 * @param contentResourceName Value for content resource name.
180 */
181 public void setContentResourceName(String contentResourceName) {
182 this.contentResourceName = contentResourceName;
183 }
184
185 /***
186 * Getter methode for property <code>footerResourceName</code>.
187 *
188 * @return Property <code>footerResourceName</code>.
189 */
190 public String getFooterResourceName() {
191 return footerResourceName;
192 }
193
194 /***
195 * Setter methode for property <code>footerResourceName</code>.
196 *
197 * @param footerResourceName Value for <code>footerResourceName</code>.
198 */
199 public void setFooterResourceName(String footerResourceName) {
200 this.footerResourceName = footerResourceName;
201 }
202
203 /***
204 * Getter methode for property <code>headerResourceName</code>.
205 *
206 * @return Property <code>headerResourceName</code>.
207 */
208 public String getHeaderResourceName() {
209 return headerResourceName;
210 }
211
212 /***
213 * Setter methode for property <code>headerResourceName</code>.
214 *
215 * @param headerResourceName Value for <code>headerResourceName</code>.
216 */
217 public void setHeaderResourceName(String headerResourceName) {
218 this.headerResourceName = headerResourceName;
219 }
220
221 /***
222 * Getter methode for property <code>refreshInterval</code>.
223 *
224 * @return Property <code>refreshInterval</code>.
225 */
226 public int getRefreshInterval() {
227 return refreshInterval;
228 }
229
230 /***
231 * Setter methode for property <code>refreshInterval</code>.
232 *
233 * @param refreshInterval Value for <code>refreshInterval</code>.
234 */
235 public void setRefreshInterval(int refreshInterval) {
236 this.refreshInterval = refreshInterval;
237 }
238
239 }