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