source: trunk/gsdl3/extensions/gsdl-as/src/org/greenstone/gsdlas/AlertingService.java@ 8717

Last change on this file since 8717 was 8717, checked in by schweer, 19 years ago

started to rework control flow, user authentication

  • Property svn:keywords set to Author Date Id Revision
File size: 7.6 KB
Line 
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 */
9package org.greenstone.gsdlas;
10
11import java.lang.reflect.Method;
12import java.net.MalformedURLException;
13import java.net.URL;
14import java.util.*;
15
16import javax.servlet.http.*;
17
18import org.apache.velocity.Template;
19import org.apache.velocity.context.Context;
20import org.apache.velocity.servlet.VelocityServlet;
21import org.greenstone.gsdlas.users.UserManager;
22
23/**
24 * @author schweer
25 *
26 * TODO To change the template for this generated type comment go to
27 * Window - Preferences - Java - Code Style - Code Templates
28 */
29public class AlertingService extends VelocityServlet {
30
31 public static final Set actions = new TreeSet();
32 static {
33 actions.add("createSubscription");
34 actions.add("deleteSubscription");
35 actions.add("editSubscription");
36 actions.add("showFeed");
37 actions.add("listSubscriptions");
38 actions.add("login");
39 actions.add("register");
40 }
41
42
43 protected Template handleRequest(HttpServletRequest req,
44 HttpServletResponse res, Context context) {
45
46 String action = req.getParameter("action");
47
48 if (action == null || !actions.contains(action)) {
49 String title = "Unknown action";
50 String message = "I don't know how to " + action;
51 String details = "The only actions I know are " + actions;
52 return showError(context, message, details);
53 }
54
55
56 Map args = req.getParameterMap();
57 // TODO stop this, we need multi-valued stuff
58 args = normalise(args);
59
60 String templateString = "";
61
62 try {
63 Method method = AlertingService.class.getDeclaredMethod(action, new Class[] {Map.class, Context.class});
64 templateString = (String) method.invoke(this, new Object[] {args, context});
65 } catch (Exception e) {
66 String message = "An error has occured, I couldn't do what you told me to do.";
67 String details = e.getMessage() + " (" + e.getClass().getName() + "), action is " + action;
68 return showError(context, message, details);
69 }
70
71 Template template = null;
72 try {
73 template = getTemplate(templateString);
74 } catch (Exception e) {
75 // TODO Auto-generated catch block
76 e.printStackTrace();
77 try {
78 error((HttpServletRequest)context.get(REQUEST),
79 (HttpServletResponse)context.get(RESPONSE),
80 e);
81 } catch (Exception e2) {
82 // TODO Auto-generated catch block
83 e2.printStackTrace();
84 }
85 }
86 return template;
87 }
88
89 /**
90 * @param arguments
91 * @param context
92 * @return the Velocity template to use
93 * @throws Exception
94 */
95 public String createSubscription(Map arguments, Context context) throws Exception {
96 HttpSession session = ((HttpServletRequest)context.get(REQUEST)).getSession(true);
97 if (!UserManager.getInstance().isLoggedIn(session)) {
98 session.setAttribute("next_action", "createSubscription");
99 return "login.vm";
100 }
101 // TODO handle wizard-style stuff
102 // if "finish"
103 ProfileStore.getInstance().createSubscription(arguments);
104 return listSubscriptions(arguments, context);
105 // else show next page
106 }
107
108 public String deleteSubscription(Map arguments, Context context) {
109 HttpSession session = ((HttpServletRequest)context.get(REQUEST)).getSession(true);
110 if (!UserManager.getInstance().isLoggedIn(session)) {
111 session.setAttribute("next_action", "deleteSubscription");
112 return "login.vm";
113 }
114 String subscriptionID = (String) arguments.get("subscriptionID");
115 ProfileStore.getInstance().deleteSubscription(subscriptionID);
116 return listSubscriptions(arguments, context);
117 }
118
119 public String editSubscription(Map arguments, Context context) {
120 HttpSession session = ((HttpServletRequest)context.get(REQUEST)).getSession(true);
121 if (!UserManager.getInstance().isLoggedIn(session)) {
122 session.setAttribute("next_action", "editSubscription");
123 return "login.vm";
124 }
125 // TODO handle wizard-style stuff
126 // if "finish"
127 ProfileStore.getInstance().changeSubscription(arguments, session);
128 return listSubscriptions(arguments, context);
129 // else show next page
130 }
131
132 public String showFeed(Map arguments, Context context) {
133 context.put("list", ""/* TODO pass subscription list */);
134 return "feed.vm";
135 }
136
137 public String listSubscriptions(Map arguments, Context context) {
138 context.put("title", "List of Subscriptions");
139 context.put("list", ProfileStore.getInstance().getAllSubscriptions());
140 return "list.vm";
141 }
142
143 public String login(Map arguments, Context context) throws Exception {
144 HttpSession session = ((HttpServletRequest)context.get(REQUEST)).getSession(true);
145 UserManager.getInstance().loginUser(arguments, session);
146 if (session.getAttribute("next_action") != null) {
147 String nextAction = (String) session.getAttribute("next_action");
148 Method method = AlertingService.class.getDeclaredMethod(nextAction, new Class[] {Map.class, Context.class});
149 return (String) method.invoke(this, new Object[] {arguments, context});
150 }
151 return listSubscriptions(arguments, context);
152 }
153
154 public String register(Map arguments, Context context) throws Exception {
155 HttpSession session = ((HttpServletRequest)context.get(REQUEST)).getSession(true);
156 UserManager.getInstance().createUser(arguments, session);
157 return login(arguments, context);
158 }
159
160 public void receiveEvent(Map event) {
161 GreenstoneCommunicator gsComm = null;
162 try {
163 gsComm = new GreenstoneCommunicator(new URL((String) event.get("host_url")));
164 } catch (MalformedURLException e) {
165 // TODO Auto-generated catch block
166 e.printStackTrace();
167 } catch (Exception e) {
168 // TODO Auto-generated catch block
169 e.printStackTrace();
170 }
171 Set matchedSubscriptions = ProfileStore.getInstance().filter(event, gsComm);
172 // TODO do something with the matched subscriptions
173 }
174
175 /**
176 * @param args
177 */
178 private Map normalise(Map args) {
179 Map result = new TreeMap();
180 for (Iterator iter = args.keySet().iterator(); iter.hasNext();) {
181 String key = (String) iter.next();
182 if (!key.equals("action")) {
183 String firstValue = ((String[]) args.get(key))[0];
184 result.put(key, firstValue);
185 }
186 }
187 return result;
188 }
189
190 /**
191 * @param context
192 * @param message
193 * @param details
194 * @return
195 */
196 private Template showError(Context context, String message, String details) {
197 context.put("title", "Error");
198 context.put("message", message);
199 context.put("details", details);
200 try {
201 return getTemplate("error.vm");
202 } catch (Exception e) {
203 try {
204 super.error((HttpServletRequest)context.get("req"),
205 (HttpServletResponse)context.get("res"),
206 e);
207 } catch (Exception e2) {
208 e2.printStackTrace();
209 }
210 }
211 return null;
212 }
213}
Note: See TracBrowser for help on using the repository browser.