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.io.IOException;
|
---|
12 | import java.lang.reflect.Method;
|
---|
13 | import java.net.MalformedURLException;
|
---|
14 | import java.net.URL;
|
---|
15 | import java.util.*;
|
---|
16 |
|
---|
17 | import javax.servlet.ServletException;
|
---|
18 | import javax.servlet.http.HttpServletRequest;
|
---|
19 | import javax.servlet.http.HttpServletResponse;
|
---|
20 |
|
---|
21 | import org.apache.velocity.Template;
|
---|
22 | import org.apache.velocity.context.Context;
|
---|
23 | import org.apache.velocity.exception.ParseErrorException;
|
---|
24 | import org.apache.velocity.exception.ResourceNotFoundException;
|
---|
25 | import org.apache.velocity.servlet.VelocityServlet;
|
---|
26 | import org.greenstone.gsdlas.profiles.*;
|
---|
27 |
|
---|
28 | /**
|
---|
29 | * @author schweer
|
---|
30 | *
|
---|
31 | * TODO To change the template for this generated type comment go to
|
---|
32 | * Window - Preferences - Java - Code Style - Code Templates
|
---|
33 | */
|
---|
34 | public class AlertingService extends VelocityServlet {
|
---|
35 |
|
---|
36 | public static final Set actions = new TreeSet();
|
---|
37 | static {
|
---|
38 | actions.add("subscribe");
|
---|
39 | actions.add("showFeed");
|
---|
40 | actions.add("listSubscriptions"); // TODO remove (just for debugging)
|
---|
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 | context.put("title", "Unknown action");
|
---|
50 | context.put("message", "I don't know how to " + action);
|
---|
51 | context.put("details", "The only actions I know are " + actions);
|
---|
52 | try {
|
---|
53 | return getTemplate("error.vm");
|
---|
54 | } catch (ResourceNotFoundException e1) {
|
---|
55 | // TODO Auto-generated catch block
|
---|
56 | e1.printStackTrace();
|
---|
57 | } catch (ParseErrorException e1) {
|
---|
58 | // TODO Auto-generated catch block
|
---|
59 | e1.printStackTrace();
|
---|
60 | } catch (Exception e1) {
|
---|
61 | // TODO Auto-generated catch block
|
---|
62 | e1.printStackTrace();
|
---|
63 | }
|
---|
64 | return null;
|
---|
65 | }
|
---|
66 |
|
---|
67 |
|
---|
68 | Map args = req.getParameterMap();
|
---|
69 | args = normalise(args);
|
---|
70 |
|
---|
71 | String templateString = "";
|
---|
72 |
|
---|
73 | try {
|
---|
74 | Method method = AlertingService.class.getDeclaredMethod(action, new Class[] {Map.class, Context.class});
|
---|
75 | templateString = (String) method.invoke(this, new Object[] {args, context});
|
---|
76 | } catch (Exception e) {
|
---|
77 | templateString = "error.vm";
|
---|
78 | context.put("title", "Error");
|
---|
79 | context.put("message", "A general error has occured, I couldn't do what you told me to do.");
|
---|
80 | context.put("details", e.getMessage() + " (" + e.getClass().getName() + "), action is " + action);
|
---|
81 | }
|
---|
82 |
|
---|
83 | Template template = null;
|
---|
84 | try {
|
---|
85 | template = getTemplate(templateString);
|
---|
86 | } catch (Exception e) {
|
---|
87 | // TODO Auto-generated catch block
|
---|
88 | e.printStackTrace();
|
---|
89 | try {
|
---|
90 | error(req, res, e);
|
---|
91 | } catch (ServletException e2) {
|
---|
92 | // TODO Auto-generated catch block
|
---|
93 | e2.printStackTrace();
|
---|
94 | } catch (IOException e2) {
|
---|
95 | // TODO Auto-generated catch block
|
---|
96 | e2.printStackTrace();
|
---|
97 | }
|
---|
98 | }
|
---|
99 | return template;
|
---|
100 | }
|
---|
101 |
|
---|
102 | /**
|
---|
103 | * @param arguments
|
---|
104 | * @param context
|
---|
105 | * @return the Velocity template to use
|
---|
106 | */
|
---|
107 | public String subscribe(Map arguments, Context context) {
|
---|
108 | context.put("title", "Adding Subscription");
|
---|
109 | if (arguments.size() == 0) {
|
---|
110 | context.put("message", "Empty subscription, not added");
|
---|
111 | context.put("details", "A subscription has to define at least one predicate.");
|
---|
112 | return "error.vm";
|
---|
113 | }
|
---|
114 | try {
|
---|
115 | // TODO pass user information to profile store
|
---|
116 | Subscription sub = new Subscription(arguments);
|
---|
117 | // TODO this should probably be somewhere else
|
---|
118 | for (Iterator iter = sub.getPredicates().iterator(); iter.hasNext();) {
|
---|
119 | Predicate predicate = (Predicate) iter.next();
|
---|
120 | if (predicate != null) {
|
---|
121 | predicate.addSubscription(sub);
|
---|
122 | }
|
---|
123 | }
|
---|
124 | ProfileStore.getInstance().addSubscription(sub);
|
---|
125 | } catch (ParseException e) {
|
---|
126 | context.put("message", "Eror in subscription creation, not added");
|
---|
127 | context.put("details", e.getMessage());
|
---|
128 | return "error.vm";
|
---|
129 | }
|
---|
130 | context.put("message", "Subscription added successfully");
|
---|
131 | return "general.vm";
|
---|
132 | }
|
---|
133 |
|
---|
134 | public String showFeed(Map arguments, Context context) {
|
---|
135 | context.put("list", ""/* TODO pass subscription list */);
|
---|
136 | return "feed.vm";
|
---|
137 | }
|
---|
138 |
|
---|
139 | public String listSubscriptions(Map arguments, Context context) {
|
---|
140 | context.put("title", "List of Subscriptions");
|
---|
141 | context.put("list", ProfileStore.getInstance().getAllSubscriptions());
|
---|
142 | return "list.vm";
|
---|
143 | }
|
---|
144 |
|
---|
145 | public void receive(Map event) {
|
---|
146 | GreenstoneCommunicator gsComm = null;
|
---|
147 | try {
|
---|
148 | gsComm = new GreenstoneCommunicator(new URL((String) event.get("host_url")));
|
---|
149 | } catch (MalformedURLException e) {
|
---|
150 | // TODO Auto-generated catch block
|
---|
151 | e.printStackTrace();
|
---|
152 | } catch (Exception e) {
|
---|
153 | // TODO Auto-generated catch block
|
---|
154 | e.printStackTrace();
|
---|
155 | }
|
---|
156 | Set matchedSubscriptions = ProfileStore.getInstance().filter(event, gsComm);
|
---|
157 | // TODO do something with the matched subscriptions
|
---|
158 | }
|
---|
159 |
|
---|
160 | /**
|
---|
161 | * @param args
|
---|
162 | */
|
---|
163 | private Map normalise(Map args) {
|
---|
164 | Map result = new TreeMap();
|
---|
165 | for (Iterator iter = args.keySet().iterator(); iter.hasNext();) {
|
---|
166 | String key = (String) iter.next();
|
---|
167 | if (!key.equals("action")) {
|
---|
168 | String firstValue = ((String[]) args.get(key))[0];
|
---|
169 | result.put(key, firstValue);
|
---|
170 | }
|
---|
171 | }
|
---|
172 | return result;
|
---|
173 | }
|
---|
174 | }
|
---|