1 | /*
|
---|
2 | * Created on Nov 22, 2004
|
---|
3 | * Copyright (C) Andrea Schweer, 2004
|
---|
4 | *
|
---|
5 | * This file is part of the Greenstone Alerting Service.
|
---|
6 | * Refer to the COPYING file in the base directory of this package
|
---|
7 | * for licensing information.
|
---|
8 | */
|
---|
9 | package org.greenstone.gsdlas;
|
---|
10 |
|
---|
11 | import java.lang.reflect.Method;
|
---|
12 | import java.net.MalformedURLException;
|
---|
13 | import java.net.URL;
|
---|
14 | import java.util.*;
|
---|
15 |
|
---|
16 | import javax.servlet.http.*;
|
---|
17 |
|
---|
18 | import org.apache.velocity.Template;
|
---|
19 | import org.apache.velocity.context.Context;
|
---|
20 | import org.apache.velocity.servlet.VelocityServlet;
|
---|
21 | import org.greenstone.gsdlas.users.UserManagementException;
|
---|
22 | import org.greenstone.gsdlas.users.UserManager;
|
---|
23 | import org.greenstone.gsdlas.util.ArrayHelper;
|
---|
24 |
|
---|
25 | /**
|
---|
26 | * @author schweer
|
---|
27 | *
|
---|
28 | * TODO To change the template for this generated type comment go to
|
---|
29 | * Window - Preferences - Java - Code Style - Code Templates
|
---|
30 | */
|
---|
31 | public class AlertingService extends VelocityServlet {
|
---|
32 |
|
---|
33 | public static final String[] actions = new String[] {
|
---|
34 | "createSubscription",
|
---|
35 | "deleteSubscription",
|
---|
36 | "editSubscription",
|
---|
37 | "showFeed",
|
---|
38 | "listSubscriptions",
|
---|
39 | "login",
|
---|
40 | "register",
|
---|
41 | "logout",
|
---|
42 | "showLoginForm",
|
---|
43 | "showRegistrationForm"
|
---|
44 | };
|
---|
45 |
|
---|
46 |
|
---|
47 | protected Template handleRequest(HttpServletRequest req,
|
---|
48 | HttpServletResponse res, Context context) {
|
---|
49 |
|
---|
50 | String action = req.getParameter("action");
|
---|
51 |
|
---|
52 | Map args = req.getParameterMap();
|
---|
53 | // TODO stop this, we need multi-valued stuff
|
---|
54 | args = normalise(args);
|
---|
55 |
|
---|
56 | if (action != null && action.equals("receiveEvent")) {
|
---|
57 | receiveEvent(args);
|
---|
58 | return null;
|
---|
59 | }
|
---|
60 |
|
---|
61 | if (action == null || !ArrayHelper.contains(actions, action)) {
|
---|
62 | String title = "Unknown action";
|
---|
63 | String message = "I don't know how to " + action;
|
---|
64 | String details = "The only actions I know are " + Arrays.toString(actions);
|
---|
65 | return showError(context, message, details);
|
---|
66 | }
|
---|
67 |
|
---|
68 | String templateString = "";
|
---|
69 |
|
---|
70 | try {
|
---|
71 | Method method = AlertingService.class.getDeclaredMethod(action, new Class[] {Map.class, Context.class});
|
---|
72 | templateString = (String) method.invoke(this, new Object[] {args, context});
|
---|
73 | } catch (Exception e) {
|
---|
74 | String message = "An error has occured, I couldn't do what you told me to do.";
|
---|
75 | String details = e.getMessage() + " (" + e.getClass().getName() + "); "
|
---|
76 | + e.getCause()
|
---|
77 | + " ; action is " + action;
|
---|
78 | return showError(context, message, details);
|
---|
79 | }
|
---|
80 |
|
---|
81 | Template template = null;
|
---|
82 | try {
|
---|
83 | template = getTemplate(templateString);
|
---|
84 | } catch (Exception e) {
|
---|
85 | // TODO Auto-generated catch block
|
---|
86 | e.printStackTrace();
|
---|
87 | try {
|
---|
88 | error((HttpServletRequest)context.get(REQUEST),
|
---|
89 | (HttpServletResponse)context.get(RESPONSE),
|
---|
90 | e);
|
---|
91 | } catch (Exception e2) {
|
---|
92 | // TODO Auto-generated catch block
|
---|
93 | e2.printStackTrace();
|
---|
94 | }
|
---|
95 | }
|
---|
96 | return template;
|
---|
97 | }
|
---|
98 |
|
---|
99 | /**
|
---|
100 | * @param arguments
|
---|
101 | * @param context
|
---|
102 | * @return the Velocity template to use
|
---|
103 | * @throws Exception
|
---|
104 | */
|
---|
105 | public String createSubscription(Map arguments, Context context) throws Exception {
|
---|
106 | HttpSession session = ((HttpServletRequest)context.get(REQUEST)).getSession(true);
|
---|
107 | if (!UserManager.getInstance().isLoggedIn(session)) {
|
---|
108 | session.setAttribute("next_action", "createSubscription");
|
---|
109 | return showLoginForm(arguments, context);
|
---|
110 | }
|
---|
111 | if (arguments.containsKey("next_page") && arguments.get("next_page").equals("finish")) {
|
---|
112 | arguments.putAll(getPageArgsFromSession(session));
|
---|
113 | ProfileStore.getInstance().createSubscription(arguments);
|
---|
114 | return listSubscriptions(arguments, context);
|
---|
115 | } else {
|
---|
116 | return showSubscriptionWizardPage(arguments, context, true);
|
---|
117 | }
|
---|
118 | }
|
---|
119 |
|
---|
120 | public String deleteSubscription(Map arguments, Context context) {
|
---|
121 | HttpSession session = ((HttpServletRequest)context.get(REQUEST)).getSession(true);
|
---|
122 | if (!UserManager.getInstance().isLoggedIn(session)) {
|
---|
123 | session.setAttribute("next_action", "deleteSubscription");
|
---|
124 | return showLoginForm(arguments, context);
|
---|
125 | }
|
---|
126 | String subscriptionID = (String) arguments.get("subscriptionID");
|
---|
127 | ProfileStore.getInstance().deleteSubscription(subscriptionID);
|
---|
128 | return listSubscriptions(arguments, context);
|
---|
129 | }
|
---|
130 |
|
---|
131 | public String editSubscription(Map arguments, Context context) throws Exception {
|
---|
132 | HttpSession session = ((HttpServletRequest)context.get(REQUEST)).getSession(true);
|
---|
133 | if (!UserManager.getInstance().isLoggedIn(session)) {
|
---|
134 | session.setAttribute("next_action", "editSubscription");
|
---|
135 | return showLoginForm(arguments, context);
|
---|
136 | }
|
---|
137 | if (arguments.containsKey("next_page") && arguments.get("next_page").equals("finish")) {
|
---|
138 | ProfileStore.getInstance().changeSubscription(arguments, session);
|
---|
139 | return listSubscriptions(arguments, context);
|
---|
140 | } else {
|
---|
141 | return showSubscriptionWizardPage(arguments, context, false);
|
---|
142 | }
|
---|
143 | }
|
---|
144 |
|
---|
145 | public String showFeed(Map arguments, Context context) {
|
---|
146 | context.put("list", ""/* TODO pass subscription list */);
|
---|
147 | return "feed.vm";
|
---|
148 | }
|
---|
149 |
|
---|
150 | public String showLoginForm(Map arguments, Context context) {
|
---|
151 | return "login.vm";
|
---|
152 | }
|
---|
153 |
|
---|
154 | public String showRegistrationForm(Map arguments, Context context) {
|
---|
155 | return "register.vm";
|
---|
156 | }
|
---|
157 |
|
---|
158 | public String listSubscriptions(Map arguments, Context context) {
|
---|
159 | HttpSession session = ((HttpServletRequest)context.get(REQUEST)).getSession(true);
|
---|
160 | if (!UserManager.getInstance().isLoggedIn(session)) {
|
---|
161 | session.setAttribute("next_action", "listSubscriptions");
|
---|
162 | return showLoginForm(arguments, context);
|
---|
163 | }
|
---|
164 | String username = (String) session.getAttribute("username");
|
---|
165 | context.put("title", "List of Subscriptions for " + username);
|
---|
166 | context.put("list", ProfileStore.getInstance().getAllSubscriptionsFor(username));
|
---|
167 | return "list.vm";
|
---|
168 | }
|
---|
169 |
|
---|
170 | public String login(Map arguments, Context context) throws Exception {
|
---|
171 | HttpSession session = ((HttpServletRequest)context.get(REQUEST)).getSession(true);
|
---|
172 | try {
|
---|
173 | UserManager.getInstance().loginUser(arguments, session);
|
---|
174 | } catch (UserManagementException e) {
|
---|
175 | context.put("error", Boolean.TRUE);
|
---|
176 | context.put("errormessage", e.getMessage());
|
---|
177 | return showLoginForm(arguments, context);
|
---|
178 | }
|
---|
179 | if (session.getAttribute("next_action") != null) {
|
---|
180 | String nextAction = (String) session.getAttribute("next_action");
|
---|
181 | Method method = AlertingService.class.getDeclaredMethod(nextAction, new Class[] {Map.class, Context.class});
|
---|
182 | return (String) method.invoke(this, new Object[] {arguments, context});
|
---|
183 | }
|
---|
184 | return listSubscriptions(arguments, context);
|
---|
185 | }
|
---|
186 |
|
---|
187 | public String register(Map arguments, Context context) throws Exception {
|
---|
188 | HttpSession session = ((HttpServletRequest)context.get(REQUEST)).getSession(true);
|
---|
189 | try {
|
---|
190 | UserManager.getInstance().createUser(arguments, session);
|
---|
191 | } catch (UserManagementException e) {
|
---|
192 | context.put("error", Boolean.TRUE);
|
---|
193 | context.put("errormessage", e.getMessage());
|
---|
194 | return showRegistrationForm(arguments, context);
|
---|
195 | }
|
---|
196 | return login(arguments, context);
|
---|
197 | }
|
---|
198 |
|
---|
199 | public String logout(Map arguments, Context context) {
|
---|
200 | HttpSession session = ((HttpServletRequest)context.get(REQUEST)).getSession(true);
|
---|
201 | Enumeration atts = session.getAttributeNames();
|
---|
202 | while (atts.hasMoreElements()) {
|
---|
203 | session.removeAttribute((String) atts.nextElement());
|
---|
204 | }
|
---|
205 | session.invalidate();
|
---|
206 | return "general.vm";
|
---|
207 | }
|
---|
208 |
|
---|
209 | public void receiveEvent(Map event) {
|
---|
210 | GreenstoneCommunicator gsComm = null;
|
---|
211 | try {
|
---|
212 | gsComm = new GreenstoneCommunicator(new URL((String) event.get("host_url")));
|
---|
213 | } catch (MalformedURLException e) {
|
---|
214 | // TODO Auto-generated catch block
|
---|
215 | e.printStackTrace();
|
---|
216 | } catch (Exception e) {
|
---|
217 | // TODO Auto-generated catch block
|
---|
218 | e.printStackTrace();
|
---|
219 | }
|
---|
220 | Set matchedSubscriptions = ProfileStore.getInstance().filter(event, gsComm);
|
---|
221 | // TODO do something with the matched subscriptions
|
---|
222 | }
|
---|
223 |
|
---|
224 | /**
|
---|
225 | * @param args
|
---|
226 | */
|
---|
227 | private Map normalise(Map args) {
|
---|
228 | Map result = new TreeMap();
|
---|
229 | for (Iterator iter = args.keySet().iterator(); iter.hasNext();) {
|
---|
230 | String key = (String) iter.next();
|
---|
231 | if (key.equals("type") || key.equals("host")
|
---|
232 | || key.equals("collection") || key.equals("way")) {
|
---|
233 | // multi-valued attributes
|
---|
234 | String[] values = ((String[]) args.get(key));
|
---|
235 | result.put(key, Arrays.asList(values));
|
---|
236 | } else {
|
---|
237 | String firstValue = ((String[])args.get(key))[0];
|
---|
238 | result.put(key, firstValue);
|
---|
239 | }
|
---|
240 | }
|
---|
241 | return result;
|
---|
242 | }
|
---|
243 |
|
---|
244 | /**
|
---|
245 | * @param context
|
---|
246 | * @param message
|
---|
247 | * @param details
|
---|
248 | * @return
|
---|
249 | */
|
---|
250 | private Template showError(Context context, String message, String details) {
|
---|
251 | context.put("title", "Error");
|
---|
252 | context.put("message", message);
|
---|
253 | context.put("details", details);
|
---|
254 | try {
|
---|
255 | return getTemplate("error.vm");
|
---|
256 | } catch (Exception e) {
|
---|
257 | try {
|
---|
258 | super.error((HttpServletRequest)context.get("req"),
|
---|
259 | (HttpServletResponse)context.get("res"),
|
---|
260 | e);
|
---|
261 | } catch (Exception e2) {
|
---|
262 | e2.printStackTrace();
|
---|
263 | }
|
---|
264 | }
|
---|
265 | return null;
|
---|
266 | }
|
---|
267 |
|
---|
268 | /**
|
---|
269 | * @param arguments
|
---|
270 | * @param context
|
---|
271 | * @param create
|
---|
272 | * @return
|
---|
273 | * @throws Exception
|
---|
274 | */
|
---|
275 | private String showSubscriptionWizardPage(Map arguments, Context context, boolean create) throws Exception {
|
---|
276 | if (create) {
|
---|
277 | context.put("action", "createSubscription");
|
---|
278 | } else {
|
---|
279 | context.put("action", "editSubscription");
|
---|
280 | }
|
---|
281 | if (!arguments.containsKey("current_page")) {
|
---|
282 | return "sub_type-details.vm";
|
---|
283 | }
|
---|
284 |
|
---|
285 | HttpSession session = ((HttpServletRequest)context.get(REQUEST)).getSession(true);
|
---|
286 | String currentPage = (String) arguments.get("current_page");
|
---|
287 | String direction = (String) arguments.get("next_page");
|
---|
288 |
|
---|
289 | // save page arguments
|
---|
290 | savePageArgsToSession(currentPage, arguments, session);
|
---|
291 |
|
---|
292 | String nextPage = getNextPage(currentPage, direction);
|
---|
293 |
|
---|
294 | // fill prefill
|
---|
295 | context.put("prefill", getPageArgsFromSession(nextPage, session));
|
---|
296 |
|
---|
297 | // fill preview
|
---|
298 | context.put("preview", getPagePreview(nextPage, session));
|
---|
299 |
|
---|
300 | // get page-specific stuff
|
---|
301 | if (nextPage.equals("host")) {
|
---|
302 | GreenstoneCommunicator gsComm = null;
|
---|
303 | String[] hostNames;
|
---|
304 | try {
|
---|
305 | gsComm = new GreenstoneCommunicator();
|
---|
306 | hostNames = gsComm.getHostNames();
|
---|
307 | } catch (Exception e) {
|
---|
308 | hostNames = new String[] { "localhost" };
|
---|
309 | }
|
---|
310 | context.put("hostnames", hostNames);
|
---|
311 | } else if (nextPage.equals("collection")) {
|
---|
312 | // TODO might be query instead of just name
|
---|
313 | List hostNames = (List) arguments.get("host");
|
---|
314 | GreenstoneCommunicator gsComm = null;
|
---|
315 | Map collNames = new TreeMap();
|
---|
316 | for (Iterator iter = hostNames.iterator(); iter.hasNext();) {
|
---|
317 | String host = (String) iter.next();
|
---|
318 | Set collNamesForHost = new TreeSet();
|
---|
319 | try {
|
---|
320 | gsComm = new GreenstoneCommunicator(new URL("http://" + host + ":8080/soap/servlet/rpcrouter"));
|
---|
321 | collNamesForHost.addAll(Arrays.asList(gsComm.getCollectionNames()));
|
---|
322 | } catch (Exception e) {
|
---|
323 | // TODO Auto-generated catch block
|
---|
324 | e.printStackTrace();
|
---|
325 | }
|
---|
326 | collNames.put(host, collNamesForHost);
|
---|
327 | }
|
---|
328 | context.put("collectionnames", collNames);
|
---|
329 | context.put("hostnames", hostNames);
|
---|
330 | }
|
---|
331 |
|
---|
332 | return "sub_" + nextPage + ".vm";
|
---|
333 | }
|
---|
334 |
|
---|
335 | /**
|
---|
336 | * @param nextPage
|
---|
337 | * @param session
|
---|
338 | * @return
|
---|
339 | */
|
---|
340 | private Map getPagePreview(String nextPage, HttpSession session) {
|
---|
341 | Map preview = new TreeMap();
|
---|
342 | if (nextPage.equals("type-details")) {
|
---|
343 | return preview;
|
---|
344 | }
|
---|
345 | preview.putAll(getPageArgsFromSession("type-details", session));
|
---|
346 | if (nextPage.equals("host")) {
|
---|
347 | return preview;
|
---|
348 | }
|
---|
349 | preview.putAll(getPageArgsFromSession("host", session));
|
---|
350 | if (nextPage.equals("collection")) {
|
---|
351 | return preview;
|
---|
352 | }
|
---|
353 | preview.putAll(getPageArgsFromSession("collection", session));
|
---|
354 | return preview;
|
---|
355 | }
|
---|
356 |
|
---|
357 | /**
|
---|
358 | * @param currentPage
|
---|
359 | * @param direction
|
---|
360 | * @return
|
---|
361 | * @throws Exception
|
---|
362 | */
|
---|
363 | private String getNextPage(String currentPage, String direction) throws Exception {
|
---|
364 | String nextPage;
|
---|
365 | if (currentPage.equals("host") && direction.equals("back")) {
|
---|
366 | nextPage = "type-details";
|
---|
367 | } else if (currentPage.equals("type-details") || (currentPage.equals("collection") && direction.equals("back"))) {
|
---|
368 | nextPage = "host";
|
---|
369 | } else if (currentPage.equals("host") || (currentPage.equals("notification") && direction.equals("back"))) {
|
---|
370 | nextPage = "collection";
|
---|
371 | } else if (currentPage.equals("collection")) {
|
---|
372 | nextPage = "notification";
|
---|
373 | } else {
|
---|
374 | throw new Exception("unknown combination of currentPage=" + currentPage + " and nextPage=" + direction);
|
---|
375 | }
|
---|
376 | return nextPage;
|
---|
377 | }
|
---|
378 |
|
---|
379 | /**
|
---|
380 | * @param page
|
---|
381 | * @param session
|
---|
382 | * @return
|
---|
383 | */
|
---|
384 | private Map getPageArgsFromSession(String page, HttpSession session) {
|
---|
385 | Map pageArgs = (Map) session.getAttribute("page_args");
|
---|
386 | if (pageArgs == null || !pageArgs.containsKey(page))
|
---|
387 | return new TreeMap();
|
---|
388 | return (Map) pageArgs.get(page);
|
---|
389 | }
|
---|
390 |
|
---|
391 | private Map getPageArgsFromSession(HttpSession session) {
|
---|
392 | Map result = new TreeMap();
|
---|
393 | Map pageArgs = (Map) session.getAttribute("page_args");
|
---|
394 | if (pageArgs != null) {
|
---|
395 | for (Iterator iter = pageArgs.values().iterator(); iter.hasNext();) {
|
---|
396 | Map args = (Map) iter.next();
|
---|
397 | result.putAll(args);
|
---|
398 | }
|
---|
399 | }
|
---|
400 | return result;
|
---|
401 | }
|
---|
402 |
|
---|
403 | /**
|
---|
404 | * @param page
|
---|
405 | * @param arguments
|
---|
406 | * @param session
|
---|
407 | */
|
---|
408 | private void savePageArgsToSession(String page, Map arguments, HttpSession session) {
|
---|
409 | Map pageArgs = (Map) session.getAttribute("page_args");
|
---|
410 | if (pageArgs == null) {
|
---|
411 | pageArgs = new TreeMap();
|
---|
412 | }
|
---|
413 | pageArgs.put(page, arguments);
|
---|
414 | session.setAttribute("page_args", pageArgs);
|
---|
415 | }
|
---|
416 | }
|
---|