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.mail.Session;
|
---|
17 | import javax.servlet.http.*;
|
---|
18 |
|
---|
19 | import org.apache.velocity.Template;
|
---|
20 | import org.apache.velocity.context.Context;
|
---|
21 | import org.apache.velocity.servlet.VelocityServlet;
|
---|
22 | import org.greenstone.gsdlas.users.UserManager;
|
---|
23 |
|
---|
24 | /**
|
---|
25 | * @author schweer
|
---|
26 | *
|
---|
27 | * TODO To change the template for this generated type comment go to
|
---|
28 | * Window - Preferences - Java - Code Style - Code Templates
|
---|
29 | */
|
---|
30 | public class AlertingService extends VelocityServlet {
|
---|
31 |
|
---|
32 | public static final Set actions = new TreeSet();
|
---|
33 | static {
|
---|
34 | actions.add("createSubscription");
|
---|
35 | actions.add("deleteSubscription");
|
---|
36 | actions.add("editSubscription");
|
---|
37 | actions.add("showFeed");
|
---|
38 | actions.add("listSubscriptions");
|
---|
39 | actions.add("login");
|
---|
40 | actions.add("register");
|
---|
41 | actions.add("logout");
|
---|
42 | }
|
---|
43 |
|
---|
44 |
|
---|
45 | protected Template handleRequest(HttpServletRequest req,
|
---|
46 | HttpServletResponse res, Context context) {
|
---|
47 |
|
---|
48 | String action = req.getParameter("action");
|
---|
49 |
|
---|
50 | if (action == null || !actions.contains(action)) {
|
---|
51 | String title = "Unknown action";
|
---|
52 | String message = "I don't know how to " + action;
|
---|
53 | String details = "The only actions I know are " + actions;
|
---|
54 | return showError(context, message, details);
|
---|
55 | }
|
---|
56 |
|
---|
57 |
|
---|
58 | Map args = req.getParameterMap();
|
---|
59 | // TODO stop this, we need multi-valued stuff
|
---|
60 | args = normalise(args);
|
---|
61 |
|
---|
62 | String templateString = "";
|
---|
63 |
|
---|
64 | try {
|
---|
65 | Method method = AlertingService.class.getDeclaredMethod(action, new Class[] {Map.class, Context.class});
|
---|
66 | templateString = (String) method.invoke(this, new Object[] {args, context});
|
---|
67 | } catch (Exception e) {
|
---|
68 | String message = "An error has occured, I couldn't do what you told me to do.";
|
---|
69 | String details = e.getMessage() + " (" + e.getClass().getName() + "), action is " + action;
|
---|
70 | return showError(context, message, details);
|
---|
71 | }
|
---|
72 |
|
---|
73 | Template template = null;
|
---|
74 | try {
|
---|
75 | template = getTemplate(templateString);
|
---|
76 | } catch (Exception e) {
|
---|
77 | // TODO Auto-generated catch block
|
---|
78 | e.printStackTrace();
|
---|
79 | try {
|
---|
80 | error((HttpServletRequest)context.get(REQUEST),
|
---|
81 | (HttpServletResponse)context.get(RESPONSE),
|
---|
82 | e);
|
---|
83 | } catch (Exception e2) {
|
---|
84 | // TODO Auto-generated catch block
|
---|
85 | e2.printStackTrace();
|
---|
86 | }
|
---|
87 | }
|
---|
88 | return template;
|
---|
89 | }
|
---|
90 |
|
---|
91 | /**
|
---|
92 | * @param arguments
|
---|
93 | * @param context
|
---|
94 | * @return the Velocity template to use
|
---|
95 | * @throws Exception
|
---|
96 | */
|
---|
97 | public String createSubscription(Map arguments, Context context) throws Exception {
|
---|
98 | HttpSession session = ((HttpServletRequest)context.get(REQUEST)).getSession(true);
|
---|
99 | if (!UserManager.getInstance().isLoggedIn(session)) {
|
---|
100 | session.setAttribute("next_action", "createSubscription");
|
---|
101 | return "login.vm";
|
---|
102 | }
|
---|
103 | if (arguments.containsKey("next_page") && arguments.get("next_page").equals("finish")) {
|
---|
104 | ProfileStore.getInstance().createSubscription(arguments);
|
---|
105 | return listSubscriptions(arguments, context);
|
---|
106 | } else {
|
---|
107 | return showSubscriptionWizardPage(arguments, context, true);
|
---|
108 | }
|
---|
109 | }
|
---|
110 |
|
---|
111 | public String deleteSubscription(Map arguments, Context context) {
|
---|
112 | HttpSession session = ((HttpServletRequest)context.get(REQUEST)).getSession(true);
|
---|
113 | if (!UserManager.getInstance().isLoggedIn(session)) {
|
---|
114 | session.setAttribute("next_action", "deleteSubscription");
|
---|
115 | return "login.vm";
|
---|
116 | }
|
---|
117 | String subscriptionID = (String) arguments.get("subscriptionID");
|
---|
118 | ProfileStore.getInstance().deleteSubscription(subscriptionID);
|
---|
119 | return listSubscriptions(arguments, context);
|
---|
120 | }
|
---|
121 |
|
---|
122 | public String editSubscription(Map arguments, Context context) throws Exception {
|
---|
123 | HttpSession session = ((HttpServletRequest)context.get(REQUEST)).getSession(true);
|
---|
124 | if (!UserManager.getInstance().isLoggedIn(session)) {
|
---|
125 | session.setAttribute("next_action", "editSubscription");
|
---|
126 | return "login.vm";
|
---|
127 | }
|
---|
128 | if (arguments.containsKey("next_page") && arguments.get("next_page").equals("finish")) {
|
---|
129 | ProfileStore.getInstance().changeSubscription(arguments, session);
|
---|
130 | return listSubscriptions(arguments, context);
|
---|
131 | } else {
|
---|
132 | return showSubscriptionWizardPage(arguments, context, false);
|
---|
133 | }
|
---|
134 | }
|
---|
135 |
|
---|
136 | public String showFeed(Map arguments, Context context) {
|
---|
137 | context.put("list", ""/* TODO pass subscription list */);
|
---|
138 | return "feed.vm";
|
---|
139 | }
|
---|
140 |
|
---|
141 | public String listSubscriptions(Map arguments, Context context) {
|
---|
142 | context.put("title", "List of Subscriptions");
|
---|
143 | context.put("list", ProfileStore.getInstance().getAllSubscriptions());
|
---|
144 | return "list.vm";
|
---|
145 | }
|
---|
146 |
|
---|
147 | public String login(Map arguments, Context context) throws Exception {
|
---|
148 | HttpSession session = ((HttpServletRequest)context.get(REQUEST)).getSession(true);
|
---|
149 | UserManager.getInstance().loginUser(arguments, session);
|
---|
150 | if (session.getAttribute("next_action") != null) {
|
---|
151 | String nextAction = (String) session.getAttribute("next_action");
|
---|
152 | Method method = AlertingService.class.getDeclaredMethod(nextAction, new Class[] {Map.class, Context.class});
|
---|
153 | return (String) method.invoke(this, new Object[] {arguments, context});
|
---|
154 | }
|
---|
155 | return listSubscriptions(arguments, context);
|
---|
156 | }
|
---|
157 |
|
---|
158 | public String register(Map arguments, Context context) throws Exception {
|
---|
159 | HttpSession session = ((HttpServletRequest)context.get(REQUEST)).getSession(true);
|
---|
160 | UserManager.getInstance().createUser(arguments, session);
|
---|
161 | return login(arguments, context);
|
---|
162 | }
|
---|
163 |
|
---|
164 | public String logout(Map arguments, Context context) {
|
---|
165 | HttpSession session = ((HttpServletRequest)context.get(REQUEST)).getSession(true);
|
---|
166 | Enumeration atts = session.getAttributeNames();
|
---|
167 | while (atts.hasMoreElements()) {
|
---|
168 | session.removeAttribute((String) atts.nextElement());
|
---|
169 | }
|
---|
170 | session.invalidate();
|
---|
171 | return "general.vm";
|
---|
172 | }
|
---|
173 |
|
---|
174 | public void receiveEvent(Map event) {
|
---|
175 | GreenstoneCommunicator gsComm = null;
|
---|
176 | try {
|
---|
177 | gsComm = new GreenstoneCommunicator(new URL((String) event.get("host_url")));
|
---|
178 | } catch (MalformedURLException e) {
|
---|
179 | // TODO Auto-generated catch block
|
---|
180 | e.printStackTrace();
|
---|
181 | } catch (Exception e) {
|
---|
182 | // TODO Auto-generated catch block
|
---|
183 | e.printStackTrace();
|
---|
184 | }
|
---|
185 | Set matchedSubscriptions = ProfileStore.getInstance().filter(event, gsComm);
|
---|
186 | // TODO do something with the matched subscriptions
|
---|
187 | }
|
---|
188 |
|
---|
189 | /**
|
---|
190 | * @param args
|
---|
191 | */
|
---|
192 | private Map normalise(Map args) {
|
---|
193 | Map result = new TreeMap();
|
---|
194 | for (Iterator iter = args.keySet().iterator(); iter.hasNext();) {
|
---|
195 | String key = (String) iter.next();
|
---|
196 | if (!key.equals("action")) {
|
---|
197 | String firstValue = ((String[]) args.get(key))[0];
|
---|
198 | result.put(key, firstValue);
|
---|
199 | }
|
---|
200 | }
|
---|
201 | return result;
|
---|
202 | }
|
---|
203 |
|
---|
204 | /**
|
---|
205 | * @param context
|
---|
206 | * @param message
|
---|
207 | * @param details
|
---|
208 | * @return
|
---|
209 | */
|
---|
210 | private Template showError(Context context, String message, String details) {
|
---|
211 | context.put("title", "Error");
|
---|
212 | context.put("message", message);
|
---|
213 | context.put("details", details);
|
---|
214 | try {
|
---|
215 | return getTemplate("error.vm");
|
---|
216 | } catch (Exception e) {
|
---|
217 | try {
|
---|
218 | super.error((HttpServletRequest)context.get("req"),
|
---|
219 | (HttpServletResponse)context.get("res"),
|
---|
220 | e);
|
---|
221 | } catch (Exception e2) {
|
---|
222 | e2.printStackTrace();
|
---|
223 | }
|
---|
224 | }
|
---|
225 | return null;
|
---|
226 | }
|
---|
227 |
|
---|
228 | /**
|
---|
229 | * @param arguments
|
---|
230 | * @param context
|
---|
231 | * @param create
|
---|
232 | * @return
|
---|
233 | * @throws Exception
|
---|
234 | */
|
---|
235 | private String showSubscriptionWizardPage(Map arguments, Context context, boolean create) throws Exception {
|
---|
236 | if (create) {
|
---|
237 | context.put("action", "createSubscription");
|
---|
238 | } else {
|
---|
239 | context.put("action", "editSubscription");
|
---|
240 | }
|
---|
241 | if (!arguments.containsKey("current_page")) {
|
---|
242 | return "sub_type-details.vm";
|
---|
243 | }
|
---|
244 |
|
---|
245 | HttpSession session = ((HttpServletRequest)context.get(REQUEST)).getSession(true);
|
---|
246 | String currentPage = (String) arguments.get("current_page");
|
---|
247 | String direction = (String) arguments.get("next_page");
|
---|
248 |
|
---|
249 | // save page arguments
|
---|
250 | savePageArgsToSession(currentPage, arguments, session);
|
---|
251 |
|
---|
252 | String nextPage = getNextPage(currentPage, direction);
|
---|
253 |
|
---|
254 | // fill prefill
|
---|
255 | context.put("prefill", getPageArgsFromSession(nextPage, session));
|
---|
256 |
|
---|
257 | // fill preview
|
---|
258 | context.put("preview", getPagePreview(nextPage, session));
|
---|
259 |
|
---|
260 | // get page-specific stuff
|
---|
261 | if (nextPage.equals("host")) {
|
---|
262 |
|
---|
263 | } else if (nextPage.equals("collection")) {
|
---|
264 |
|
---|
265 | }
|
---|
266 |
|
---|
267 | return "sub_" + nextPage + ".vm";
|
---|
268 | }
|
---|
269 |
|
---|
270 | /**
|
---|
271 | * @param nextPage
|
---|
272 | * @param session
|
---|
273 | * @return
|
---|
274 | */
|
---|
275 | private Map getPagePreview(String nextPage, HttpSession session) {
|
---|
276 | Map preview = new TreeMap();
|
---|
277 | if (nextPage.equals("type-details")) {
|
---|
278 | return preview;
|
---|
279 | }
|
---|
280 | preview.putAll(getPageArgsFromSession("type-details", session));
|
---|
281 | if (nextPage.equals("host")) {
|
---|
282 | return preview;
|
---|
283 | }
|
---|
284 | preview.putAll(getPageArgsFromSession("host", session));
|
---|
285 | if (nextPage.equals("collection")) {
|
---|
286 | return preview;
|
---|
287 | }
|
---|
288 | preview.putAll(getPageArgsFromSession("collection", session));
|
---|
289 | return preview;
|
---|
290 | }
|
---|
291 |
|
---|
292 | /**
|
---|
293 | * @param currentPage
|
---|
294 | * @param direction
|
---|
295 | * @return
|
---|
296 | * @throws Exception
|
---|
297 | */
|
---|
298 | private String getNextPage(String currentPage, String direction) throws Exception {
|
---|
299 | String nextPage;
|
---|
300 | if (currentPage.equals("host") && direction.equals("back")) {
|
---|
301 | nextPage = "type-details";
|
---|
302 | } else if (currentPage.equals("type-details") || (currentPage.equals("collection") && direction.equals("back"))) {
|
---|
303 | nextPage = "host";
|
---|
304 | } else if (currentPage.equals("host") || (currentPage.equals("notification") && direction.equals("back"))) {
|
---|
305 | nextPage = "collection";
|
---|
306 | } else if (currentPage.equals("collection")) {
|
---|
307 | nextPage = "notification";
|
---|
308 | } else {
|
---|
309 | throw new Exception("unknown combination of currentPage=" + currentPage + " and nextPage=" + direction);
|
---|
310 | }
|
---|
311 | return nextPage;
|
---|
312 | }
|
---|
313 |
|
---|
314 | /**
|
---|
315 | * @param page
|
---|
316 | * @param session
|
---|
317 | * @return TODO
|
---|
318 | */
|
---|
319 | private Map getPageArgsFromSession(String page, HttpSession session) {
|
---|
320 | Map pageArgs = (Map) session.getAttribute("page_args");
|
---|
321 | if (pageArgs == null || !pageArgs.containsKey(page))
|
---|
322 | return new TreeMap();
|
---|
323 | return (Map) pageArgs.get(page);
|
---|
324 | }
|
---|
325 |
|
---|
326 | /**
|
---|
327 | * @param page
|
---|
328 | * @param arguments
|
---|
329 | * @param session
|
---|
330 | */
|
---|
331 | private void savePageArgsToSession(String page, Map arguments, HttpSession session) {
|
---|
332 | Map pageArgs = (Map) session.getAttribute("page_args");
|
---|
333 | if (pageArgs == null) {
|
---|
334 | pageArgs = new TreeMap();
|
---|
335 | }
|
---|
336 | pageArgs.put(page, arguments);
|
---|
337 | session.setAttribute("page_args", pageArgs);
|
---|
338 | }
|
---|
339 | }
|
---|