Changeset 8738
- Timestamp:
- 2004-12-06T11:44:03+13:00 (19 years ago)
- Location:
- trunk
- Files:
-
- 15 added
- 51 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/greenstone3-extensions/gsdl-as/build.xml
r8644 r8738 217 217 <!-- Include all JAR files that will be included in /WEB-INF/lib --> 218 218 <!-- *** CUSTOMIZE HERE AS REQUIRED BY YOUR APPLICATION *** --> 219 <!-- 220 <pathelement location="${foo.jar}"/>221 -->219 <fileset dir="lib"> 220 <include name="*.jar"/> 221 </fileset> 222 222 223 223 <!-- Include all elements that Tomcat exposes to applications --> -
trunk/greenstone3-extensions/gsdl-as/lib/templates/sub_collection.vm
r8724 r8738 37 37 <ol> 38 38 <li>You can choose one or more collections from the list below. Only events from the collections(s) you have chosen will be sent to you<br/> 39 <select multiple="multiple" name="host" size="7" style="margin:5px 0px;"> 40 <optgroup label="localhost"> 41 <option>greenstone 2 mg demo</option> 42 <option>greenstone 2 mgpp demo</option> 43 <option>greenstone 3 mg demo</option> 44 <option selected="selected">any collection on host localhost</option> 45 </optgroup> 46 <optgroup label="nzdl.org"> 47 <option>acronym extraction demo</option> 48 <option>music library</option> 49 <option>youth oral history</option> 50 <option>kiwi aircraft images</option> 51 <option selected="selected">any collection on host nzdl.org</option> 52 </optgroup> 39 <select multiple="multiple" name="collection" size="7" style="margin:5px 0px;"> 40 #foreach($host in $hostnames) 41 <optgroup label="$host"> 42 #foreach($collection in $collectionnames.get($host)) 43 <option>$collection</option> 44 #end 45 <option selected="selected">any collection on host $host</option> 46 </optgroup> 47 #end 53 48 </select></li> 54 49 <li>You can enter a query in the field below. Only events coming from a collection whose name matches this query will be sent to you.<br/> … … 59 54 </td> 60 55 <td> 61 <input type="text" name=" document_title" style="width:80%;"/>56 <input type="text" name="collection_query" style="width:80%;" value="$!prefill.collection_query"/> 62 57 </td> 63 58 </tr> -
trunk/greenstone3-extensions/gsdl-as/lib/templates/sub_host.vm
r8724 r8738 38 38 <li>You can choose one or more hosts from the list below. Only events from the host(s) you have chosen will be sent to you.<br/> 39 39 <select multiple="multiple" name="host" size="3" style="margin:5px 0px;"> 40 <option selected="selected">localhost</option> 41 <option>www.greenstone.org</option> 42 <option>www.nzdl.org</option> 43 <option>any host</option> 40 #set($firstoption = true); 41 #foreach($host in $hostnames) 42 <option #if($firstoption) selected="selected" #set($firstoption = false) #end>$host</option> 43 #end 44 <option value="any">any host</option> 44 45 </select></li> 45 46 <li>You can enter a query in the field below. Only events coming from a host whose name matches this query will be sent to you.<br/> … … 50 51 </td> 51 52 <td> 52 <input type="text" name=" document_title" style="width:80%;"/>53 <input type="text" name="host_query" style="width:80%;" value="$!prefill.host_query"/> 53 54 </td> 54 55 </tr> -
trunk/greenstone3-extensions/gsdl-as/lib/templates/sub_notification.vm
r8724 r8738 37 37 You will be able to see the subscription in the list of all your subscriptions after logging in to the alerting service. <br/> 38 38 You can assign a name to your subscription that makes it easier for you to recognise it: 39 <input name="subscription_name" type="text" />39 <input name="subscription_name" type="text" value="$!prefill.subscription_name"/> 40 40 </div> 41 41 <br/> … … 52 52 </td> 53 53 <td> 54 <input type="text" name="email" />54 <input type="text" name="email" value="$!prefill.email"/> 55 55 </td> 56 56 </tr> -
trunk/greenstone3-extensions/gsdl-as/src/org/greenstone/gsdlas/AlertingService.java
r8724 r8738 14 14 import java.util.*; 15 15 16 import javax.mail.Session;17 16 import javax.servlet.http.*; 18 17 … … 20 19 import org.apache.velocity.context.Context; 21 20 import org.apache.velocity.servlet.VelocityServlet; 21 import org.greenstone.gsdlas.users.UserManagementException; 22 22 import org.greenstone.gsdlas.users.UserManager; 23 23 … … 30 30 public class AlertingService extends VelocityServlet { 31 31 32 public static final Set actions = new TreeSet();32 public static final Set actions; 33 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"); 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); 42 47 } 43 48 … … 67 72 } catch (Exception e) { 68 73 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; 74 String details = e.getMessage() + " (" + e.getClass().getName() + "); " 75 + e.getCause() 76 + " ; action is " + action; 70 77 return showError(context, message, details); 71 78 } … … 99 106 if (!UserManager.getInstance().isLoggedIn(session)) { 100 107 session.setAttribute("next_action", "createSubscription"); 101 return "login.vm";108 return showLoginForm(arguments, context); 102 109 } 103 110 if (arguments.containsKey("next_page") && arguments.get("next_page").equals("finish")) { 111 arguments.putAll(getPageArgsFromSession(session)); 104 112 ProfileStore.getInstance().createSubscription(arguments); 105 113 return listSubscriptions(arguments, context); … … 113 121 if (!UserManager.getInstance().isLoggedIn(session)) { 114 122 session.setAttribute("next_action", "deleteSubscription"); 115 return "login.vm";123 return showLoginForm(arguments, context); 116 124 } 117 125 String subscriptionID = (String) arguments.get("subscriptionID"); … … 124 132 if (!UserManager.getInstance().isLoggedIn(session)) { 125 133 session.setAttribute("next_action", "editSubscription"); 126 return "login.vm";134 return showLoginForm(arguments, context); 127 135 } 128 136 if (arguments.containsKey("next_page") && arguments.get("next_page").equals("finish")) { … … 138 146 return "feed.vm"; 139 147 } 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 } 140 156 141 157 public String listSubscriptions(Map arguments, Context context) { 142 context.put("title", "List of Subscriptions"); 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); 143 165 context.put("list", ProfileStore.getInstance().getAllSubscriptions()); 144 166 return "list.vm"; … … 147 169 public String login(Map arguments, Context context) throws Exception { 148 170 HttpSession session = ((HttpServletRequest)context.get(REQUEST)).getSession(true); 149 UserManager.getInstance().loginUser(arguments, session); 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 } 150 178 if (session.getAttribute("next_action") != null) { 151 179 String nextAction = (String) session.getAttribute("next_action"); … … 158 186 public String register(Map arguments, Context context) throws Exception { 159 187 HttpSession session = ((HttpServletRequest)context.get(REQUEST)).getSession(true); 160 UserManager.getInstance().createUser(arguments, session); 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 } 161 195 return login(arguments, context); 162 196 } … … 194 228 for (Iterator iter = args.keySet().iterator(); iter.hasNext();) { 195 229 String key = (String) iter.next(); 196 if (!key.equals("action")) { 197 String firstValue = ((String[]) args.get(key))[0]; 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]; 198 237 result.put(key, firstValue); 199 238 } … … 260 299 // get page-specific stuff 261 300 if (nextPage.equals("host")) { 262 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); 263 310 } else if (nextPage.equals("collection")) { 264 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); 265 329 } 266 330 … … 324 388 } 325 389 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 326 402 /** 327 403 * @param page -
trunk/greenstone3-extensions/gsdl-as/src/org/greenstone/gsdlas/GreenstoneCommunicator.java
r8720 r8738 9 9 package org.greenstone.gsdlas; 10 10 11 import java.io.OutputStreamWriter;12 11 import java.net.URL; 13 12 import java.util.*; … … 18 17 import org.apache.soap.Fault; 19 18 import org.apache.soap.rpc.*; 20 import org.apache.soap.util.xml.DOM2Writer;21 19 import org.w3c.dom.*; 22 20 … … 44 42 } 45 43 44 /** 45 * 46 */ 47 public GreenstoneCommunicator() { 48 49 // TODO Auto-generated constructor stub 50 } 51 52 public String[] getHostNames() { 53 return new String[] {"localhost"}; 54 } 55 46 56 public String[] getCollectionNames() throws Exception { 47 57 Document document = createSubsetDescriptionRequest("", "collectionList"); … … 56 66 result[i] = name.getNodeValue(); 57 67 } 68 69 System.out.println(Arrays.toString(result)); 58 70 59 71 return result; … … 139 151 private Element sendToGreenstone(Element message) throws Exception { 140 152 Call call = new Call(); 141 call.setTargetObjectURI(" teevee.localsite");153 call.setTargetObjectURI("localsite"); 142 154 call.setMethodName("process"); 143 155 // set Encoding Style to use literal XML -
trunk/greenstone3-extensions/gsdl-as/src/org/greenstone/gsdlas/NotificationStore.java
r8720 r8738 9 9 package org.greenstone.gsdlas; 10 10 11 import java.io.UnsupportedEncodingException;12 import java.net.URLEncoder;13 11 import java.util.*; 14 import java.util.Map;15 import java.util.Set;16 12 17 13 import org.greenstone.gsdlas.profiles.Subscription; -
trunk/greenstone3-extensions/gsdl-as/src/org/greenstone/gsdlas/ProfileStore.java
r8718 r8738 14 14 15 15 import org.greenstone.gsdlas.profiles.*; 16 import org.greenstone.gsdlas.profiles.IdEqualsPredicate;17 import org.greenstone.gsdlas.profiles.Subscription;18 16 19 17 /** … … 33 31 34 32 private ProfileStore() { 35 36 33 } 37 34 … … 206 203 for (Iterator iter = matchedPredicates.iterator(); iter.hasNext();) { 207 204 Predicate pred = (Predicate) iter.next(); 208 for (Iterator iterator = pred.getSubscription s().iterator(); iterator205 for (Iterator iterator = pred.getSubscriptionIDs().iterator(); iterator 209 206 .hasNext();) { 210 207 Subscription sub = (Subscription) iterator.next(); … … 273 270 /** 274 271 * @param arguments 275 * @throws ParseException 276 */ 277 public void createSubscription(Map arguments) throws ParseException { 278 // TODO pass user information to profile store 272 * @throws Exception 273 */ 274 public void createSubscription(Map arguments) throws Exception { 279 275 Subscription sub = new Subscription(arguments); 280 // TODO this should probably be somewhere else281 for (Iterator iter = sub.getPredicates().iterator(); iter.hasNext();) {282 Predicate predicate = (Predicate) iter.next();283 if (predicate != null) {284 predicate.addSubscription(sub);285 }286 }287 276 addSubscription(sub); 288 277 } -
trunk/greenstone3-extensions/gsdl-as/src/org/greenstone/gsdlas/database/DatabaseException.java
r8717 r8738 20 20 super(message, cause); 21 21 } 22 23 /** 24 * @param string 25 */ 26 public DatabaseException(String message) { 27 super(message); 28 } 22 29 } -
trunk/greenstone3-extensions/gsdl-as/src/org/greenstone/gsdlas/database/DatabaseManager.java
r8717 r8738 37 37 public Connection getDatabaseConnection() throws DatabaseException { 38 38 if(database == null) { 39 String connectionString = "jdbc:mysql://localhost/alerting ";39 String connectionString = "jdbc:mysql://localhost/alerting?user=gsdl3admin"; 40 40 try { 41 41 Class.forName("com.mysql.jdbc.Driver").newInstance(); 42 Connection connection= DriverManager.getConnection(connectionString);42 database = DriverManager.getConnection(connectionString); 43 43 } catch (Exception e) { 44 44 throw new DatabaseException("Could not establish connection to database (" + connectionString + ")", e); -
trunk/greenstone3-extensions/gsdl-as/src/org/greenstone/gsdlas/profiles/Predicate.java
r8609 r8738 9 9 package org.greenstone.gsdlas.profiles; 10 10 11 import java.sql.*; 12 import java.sql.SQLException; 13 import java.sql.Statement; 11 14 import java.util.*; 12 15 import java.util.Set; 13 16 import java.util.TreeSet; 17 18 import org.greenstone.gsdlas.database.DatabaseException; 19 import org.greenstone.gsdlas.database.DatabaseManager; 20 import org.greenstone.gsdlas.util.ArrayHelper; 14 21 15 22 /** … … 26 33 protected int id; 27 34 28 public void addSubscription(Subscription sub) { 29 subscriptions.add(sub); 35 36 public static final String[] singleValueFields = new String[] { 37 "document_title", 38 "document_content", 39 "metadata_has_field", 40 "host_query", 41 "collection_query" }; 42 public static final String[] multiValueFields = new String[] { 43 "type", 44 "host", 45 "collection" }; 46 47 public void addSubscription(int subscriptionID) { 48 subscriptions.add(new Integer(subscriptionID)); 49 // TODO save to db 30 50 } 31 51 … … 33 53 * @see org.greenstone.gsdlas.profiles.Predicate#getSubscriptions() 34 54 */ 35 public Set getSubscription s() {55 public Set getSubscriptionIDs() { 36 56 return Collections.unmodifiableSet(subscriptions); 37 57 } … … 74 94 75 95 public abstract boolean isSatisfied(Map event); 96 97 /** 98 * @param key 99 * @return 100 */ 101 public static boolean isSingleValued(String key) { 102 return ArrayHelper.contains(singleValueFields, key); 103 } 104 105 /** 106 * @param key 107 * @return 108 */ 109 public static boolean isMultiValued(String key) { 110 return ArrayHelper.contains(singleValueFields, key); 111 } 112 113 /** 114 * @return 115 * @throws DatabaseException 116 * @throws SQLException 117 */ 118 int saveToDatabase() throws SQLException, DatabaseException { 119 // TODO handle multi-valued predicate (in subclass?) 120 String query = "SELECT id FROM predicates " + 121 "WHERE type='single' AND field = '" + field + 122 "' AND singleValue = '" + value + "';"; 123 Statement statement = DatabaseManager.getInstance().getDatabaseConnection().createStatement(); 124 System.out.println(query); 125 ResultSet result = statement.executeQuery(query); 126 if (result.next()) { // predicate already exists in database 127 System.out.println("predicate " + id + " already exists"); 128 return result.getInt("id"); 129 } 130 // predicate has been newly created 131 String insert = "INSERT INTO predicates (type, field, singleValue) " + 132 "VALUES ('single', '" + field + "', '" + value + "');"; 133 System.out.println(insert); 134 statement.execute(insert); 135 result = statement.executeQuery(query); 136 if (result.next()) { 137 return result.getInt("id"); 138 } else { 139 throw new DatabaseException("could not save predicate"); 140 } 141 } 76 142 77 143 } -
trunk/greenstone3-extensions/gsdl-as/src/org/greenstone/gsdlas/profiles/PredicateFactory.java
r8720 r8738 9 9 package org.greenstone.gsdlas.profiles; 10 10 11 import java.sql.SQLException; 11 12 import java.util.*; 12 13 import java.util.Map; 13 14 import java.util.TreeMap; 15 16 import org.greenstone.gsdlas.database.DatabaseException; 14 17 15 18 … … 42 45 * @return 43 46 * @throws ParseException 47 * @throws DatabaseException 48 * @throws SQLException 44 49 */ 45 50 public static Predicate createPredicate(String key, String value) 46 throws ParseException { 51 throws SQLException, DatabaseException { 52 System.out.println("creating predicate for key " + key + " and value " + value); 53 if (!Predicate.isSingleValued(key)) { 54 return null; 55 } 47 56 Predicate result = null; 48 57 if (value == null || value.length() == 0) { … … 59 68 result = createSubstringMatchPredicate(key, value); 60 69 } else { 61 throw new ParseException("value " + value + " for field " + key 62 + " does not lead to a parseable predicate"); 70 return null; 63 71 } 64 result.setID( id++);72 result.setID(result.saveToDatabase()); 65 73 return result; 74 } 75 76 public static Predicate createPredicate(String key, List values) throws SQLException, DatabaseException { 77 if (!Predicate.isMultiValued(key)) { 78 return null; 79 } 80 // TODO properly handle multi-valued predicates 81 return createPredicate(key, (String) values.get(0)); 66 82 } 67 83 … … 108 124 return predicate; 109 125 } 110 126 111 127 /** 112 128 * @param field -
trunk/greenstone3-extensions/gsdl-as/src/org/greenstone/gsdlas/profiles/Subscription.java
r8719 r8738 9 9 package org.greenstone.gsdlas.profiles; 10 10 11 import java.sql.*; 12 import java.sql.Connection; 13 import java.sql.Statement; 11 14 import java.util.*; 12 15 import java.util.Map; 13 16 import java.util.TreeMap; 17 18 import org.greenstone.gsdlas.database.DatabaseException; 19 import org.greenstone.gsdlas.database.DatabaseManager; 14 20 15 21 /** … … 21 27 public class Subscription implements Comparable { 22 28 private Map map; 29 23 30 private int id; 31 private String username; 32 private String name; 33 private String email; 34 private boolean rssNotification; 35 private boolean eventsSincePageNotification; 36 24 37 private int numOfNonEqualsPredicates; 25 38 26 private static int highestId = 0; 27 28 public Subscription(Map valueMap) throws ParseException { 39 public Subscription(Map valueMap) throws DatabaseException, SQLException { 29 40 map = new TreeMap(); 30 41 for (Iterator iter = valueMap.keySet().iterator(); iter.hasNext();) { 31 42 String key = (String) iter.next(); 32 String value = (String) valueMap.get(key); 33 Predicate predicate = PredicateFactory.createPredicate(key, value); 34 map.put(key, predicate); 43 Object value = valueMap.get(key); 44 45 Predicate predicate = null; 46 if (value instanceof String) { 47 predicate = PredicateFactory.createPredicate(key, (String) value); 48 map.put(key, predicate); 49 } else if (value instanceof List) { 50 List values = (List)value; 51 predicate = PredicateFactory.createPredicate(key, values); 52 map.put(key, predicate); 53 } 35 54 36 55 if (predicate != null && !(predicate instanceof IdEqualsPredicate)) { … … 38 57 } 39 58 } 40 id = highestId++; 59 60 System.out.println("finished creating predicates"); 61 62 username = (String) valueMap.get("username"); 63 name = (String) valueMap.get("subscription_name"); 64 email = (String) valueMap.get("email"); 65 rssNotification = valueMap.containsKey("way") && ((List)valueMap.get("way")).contains("rss"); 66 eventsSincePageNotification = valueMap.containsKey("way") && ((List)valueMap.get("way")).contains("page"); 67 id = saveToDatabase(true); 68 69 for (Iterator iter = getPredicates().iterator(); iter.hasNext();) { 70 Predicate predicate = (Predicate) iter.next(); 71 if (predicate != null) { 72 predicate.addSubscription(id); 73 } 74 } 41 75 } 42 76 … … 95 129 return new Integer(id).compareTo(new Integer(other.id)); 96 130 } 131 132 private int saveToDatabase(boolean initial) throws DatabaseException, SQLException { 133 Connection conn = DatabaseManager.getInstance().getDatabaseConnection(); 134 Statement statement = conn.createStatement(); 135 String sqlString; 136 if (initial) { 137 sqlString = "INSERT INTO subscriptions (name, email, rss, page, user) " + 138 "VALUES ('" + name + "','" + email + "'," + (rssNotification ? 1 : 0) + 139 "," + (eventsSincePageNotification ? 1: 0)+ ", '" + username + "');"; 140 } else { 141 sqlString = "UPDATE subscriptions SET name='" + name + "', email='" + 142 email + "', rss=" + (rssNotification ? 1 : 0) + ",page=" + 143 (eventsSincePageNotification ? 1 : 0)+ "WHERE id=" + id + ";"; 144 // cannot change user 145 } 146 System.out.println(sqlString); 147 statement.executeUpdate(sqlString); 148 149 sqlString = "SELECT id FROM subscriptions WHERE name like '" + name + 150 "' AND email like '" + email + "' AND rss=" + (rssNotification ? 1 : 0) 151 + " AND page=" + (eventsSincePageNotification ? 1 : 0) + " AND user like '" + 152 username + "';"; 153 System.out.println(sqlString); 154 ResultSet result = statement.executeQuery(sqlString); 155 int id; 156 if (result.next()) { 157 id = result.getInt("id"); 158 } else { 159 throw new DatabaseException("Couldn't save subscription"); 160 } 161 162 if (initial) { 163 for (Iterator iter = getPredicates().iterator(); iter.hasNext();) { 164 Predicate predicate = (Predicate) iter.next(); 165 sqlString = "INSERT INTO subs_to_predicates (subscription, predicate) " + 166 "VALUES (" + id + "," + predicate.getID() + ");"; 167 statement.executeUpdate(sqlString); 168 } 169 } 170 return id; 171 } 97 172 } -
trunk/greenstone3-extensions/gsdl-as/src/org/greenstone/gsdlas/users/UserManager.java
r8717 r8738 9 9 package org.greenstone.gsdlas.users; 10 10 11 import java.sql.*; 11 12 import java.util.Map; 12 13 13 14 import javax.servlet.http.HttpSession; 15 16 import org.greenstone.gsdlas.database.DatabaseManager; 14 17 15 18 /** … … 40 43 if (session.getCreationTime() - session.getLastAccessedTime() > session.getMaxInactiveInterval()) 41 44 return false; 42 43 // TODO Auto-generated method stub 44 return true; 45 return session.getAttribute("username") != null; 45 46 } 46 47 … … 48 49 * @param arguments 49 50 * @param session 51 * @throws PasswordMismatchException 52 * @throws UserManagementException 50 53 */ 51 public void createUser(Map arguments, HttpSession session) { 52 // TODO Auto-generated method stub 54 public void createUser(Map arguments, HttpSession session) throws PasswordMismatchException, UserManagementException { 55 if (!arguments.get("password").equals(arguments.get("password2"))) { 56 throw new PasswordMismatchException("The passwords don't match"); 57 } 58 String username = (String) arguments.get("username"); 59 60 byte[] password = ((String) arguments.get("password")).getBytes(); 61 62 byte[] pwdHash = password; 63 // try { 64 // pwdHash = MessageDigest.getInstance("MD5").digest(password); 65 // } catch (NoSuchAlgorithmException e) { 66 // e.printStackTrace(); 67 // throw new UserManagementException("could not create user", e); 68 // } 69 70 try { 71 Connection conn = DatabaseManager.getInstance().getDatabaseConnection(); 72 Statement statement = conn.createStatement(); 73 statement.executeUpdate("INSERT INTO users (username, password) " + 74 "VALUES ('" + username + "','" + new String(pwdHash) + "')"); 75 } catch (Exception e) { 76 e.printStackTrace(); 77 throw new UserManagementException("could not create user", e); 78 } 53 79 54 80 } … … 57 83 * @param arguments 58 84 * @param session 85 * @throws UserManagementException 59 86 */ 60 public void loginUser(Map arguments, HttpSession session) {61 // TODO Auto-generated method stub87 public void loginUser(Map arguments, HttpSession session) throws UserManagementException { 88 String username = (String) arguments.get("username"); 62 89 90 if (isLoggedIn(session) && session.getAttribute("username").equals(username)) { 91 return; // already logged in 92 } 93 94 byte[] password = ((String) arguments.get("password")).getBytes(); 95 96 byte[] pwdHash = password; 97 try { 98 // pwdHash = MessageDigest.getInstance("MD5").digest(password); 99 100 Connection conn = DatabaseManager.getInstance().getDatabaseConnection(); 101 Statement statement = conn.createStatement(); 102 ResultSet results = statement.executeQuery("SELECT password " + 103 "FROM users WHERE username like '" + username + "';"); 104 String pwdFromDB = ""; 105 if(results.next()) { 106 pwdFromDB = results.getString("password"); 107 } 108 if (!pwdFromDB.equals(new String(pwdHash))) { 109 throw new PasswordMismatchException("user " + username 110 + " is unknown, or the passwords don't match"); 111 } 112 session.setAttribute("username", username); 113 } catch (Exception e) { 114 e.printStackTrace(); 115 throw new UserManagementException("could not login user", e); 116 } 117 63 118 } 64 119 -
trunk/greenstone3-extensions/gsdl-as/test-src/org/greenstone/gsdlas/ProfileStoreTest.java
r8717 r8738 13 13 14 14 import junit.framework.TestCase; 15 16 import org.greenstone.gsdlas.profiles.*;17 15 18 16 /** … … 31 29 try { 32 30 addSubscriptions(); 33 } catch ( ParseException e) {31 } catch (Exception e) { 34 32 e.printStackTrace(); 35 33 fail(e.getMessage()); … … 60 58 } 61 59 62 private void addSubscriptions() throws ParseException {60 private void addSubscriptions() throws Exception { 63 61 Map valueMap = new TreeMap(); 64 62 … … 83 81 } 84 82 85 private void addSubscription(Map valueMap) throws ParseException {83 private void addSubscription(Map valueMap) throws Exception { 86 84 ProfileStore.getInstance().createSubscription(valueMap); 87 85 } -
trunk/greenstone3-extensions/gsdl-as/web/form.css
r8724 r8738 40 40 color: #555; 41 41 border-top: 10px solid #CCC; 42 font-family: "Trebuchet MS", Arial, helvetica, sans-serif; 42 43 } 43 44 … … 49 50 #progress td.current { 50 51 background-color: #F0E68C; 51 font-style: italic;52 52 color: #000; 53 53 } -
trunk/greenstone3-extensions/gsdl-as/web/index.html
r8720 r8738 17 17 <ul> 18 18 <li> 19 <a href="/alerting/service?action=showLoginForm">Log in</a></li> 20 </li> 21 <li> 19 22 <a href="subscribe.html">Create</a> a subscription.</li> 20 23 <li> 21 <a href=" service?action=listSubscriptions">View and edit</a> your existing subscriptions.</li>24 <a href="/alerting/service?action=listSubscriptions">View and edit</a> your existing subscriptions.</li> 22 25 <li> 23 26 <a href="about.html">Read more</a> about the alerting service.</li> 27 <li> 28 <a href="/alerting/service?action=logout">Log out</a></li? 24 29 </ul> 25 30 </div> -
trunk/gsdl3/extensions/gsdl-as/build.xml
r8644 r8738 217 217 <!-- Include all JAR files that will be included in /WEB-INF/lib --> 218 218 <!-- *** CUSTOMIZE HERE AS REQUIRED BY YOUR APPLICATION *** --> 219 <!-- 220 <pathelement location="${foo.jar}"/>221 -->219 <fileset dir="lib"> 220 <include name="*.jar"/> 221 </fileset> 222 222 223 223 <!-- Include all elements that Tomcat exposes to applications --> -
trunk/gsdl3/extensions/gsdl-as/lib/templates/sub_collection.vm
r8724 r8738 37 37 <ol> 38 38 <li>You can choose one or more collections from the list below. Only events from the collections(s) you have chosen will be sent to you<br/> 39 <select multiple="multiple" name="host" size="7" style="margin:5px 0px;"> 40 <optgroup label="localhost"> 41 <option>greenstone 2 mg demo</option> 42 <option>greenstone 2 mgpp demo</option> 43 <option>greenstone 3 mg demo</option> 44 <option selected="selected">any collection on host localhost</option> 45 </optgroup> 46 <optgroup label="nzdl.org"> 47 <option>acronym extraction demo</option> 48 <option>music library</option> 49 <option>youth oral history</option> 50 <option>kiwi aircraft images</option> 51 <option selected="selected">any collection on host nzdl.org</option> 52 </optgroup> 39 <select multiple="multiple" name="collection" size="7" style="margin:5px 0px;"> 40 #foreach($host in $hostnames) 41 <optgroup label="$host"> 42 #foreach($collection in $collectionnames.get($host)) 43 <option>$collection</option> 44 #end 45 <option selected="selected">any collection on host $host</option> 46 </optgroup> 47 #end 53 48 </select></li> 54 49 <li>You can enter a query in the field below. Only events coming from a collection whose name matches this query will be sent to you.<br/> … … 59 54 </td> 60 55 <td> 61 <input type="text" name=" document_title" style="width:80%;"/>56 <input type="text" name="collection_query" style="width:80%;" value="$!prefill.collection_query"/> 62 57 </td> 63 58 </tr> -
trunk/gsdl3/extensions/gsdl-as/lib/templates/sub_host.vm
r8724 r8738 38 38 <li>You can choose one or more hosts from the list below. Only events from the host(s) you have chosen will be sent to you.<br/> 39 39 <select multiple="multiple" name="host" size="3" style="margin:5px 0px;"> 40 <option selected="selected">localhost</option> 41 <option>www.greenstone.org</option> 42 <option>www.nzdl.org</option> 43 <option>any host</option> 40 #set($firstoption = true); 41 #foreach($host in $hostnames) 42 <option #if($firstoption) selected="selected" #set($firstoption = false) #end>$host</option> 43 #end 44 <option value="any">any host</option> 44 45 </select></li> 45 46 <li>You can enter a query in the field below. Only events coming from a host whose name matches this query will be sent to you.<br/> … … 50 51 </td> 51 52 <td> 52 <input type="text" name=" document_title" style="width:80%;"/>53 <input type="text" name="host_query" style="width:80%;" value="$!prefill.host_query"/> 53 54 </td> 54 55 </tr> -
trunk/gsdl3/extensions/gsdl-as/lib/templates/sub_notification.vm
r8724 r8738 37 37 You will be able to see the subscription in the list of all your subscriptions after logging in to the alerting service. <br/> 38 38 You can assign a name to your subscription that makes it easier for you to recognise it: 39 <input name="subscription_name" type="text" />39 <input name="subscription_name" type="text" value="$!prefill.subscription_name"/> 40 40 </div> 41 41 <br/> … … 52 52 </td> 53 53 <td> 54 <input type="text" name="email" />54 <input type="text" name="email" value="$!prefill.email"/> 55 55 </td> 56 56 </tr> -
trunk/gsdl3/extensions/gsdl-as/src/org/greenstone/gsdlas/AlertingService.java
r8724 r8738 14 14 import java.util.*; 15 15 16 import javax.mail.Session;17 16 import javax.servlet.http.*; 18 17 … … 20 19 import org.apache.velocity.context.Context; 21 20 import org.apache.velocity.servlet.VelocityServlet; 21 import org.greenstone.gsdlas.users.UserManagementException; 22 22 import org.greenstone.gsdlas.users.UserManager; 23 23 … … 30 30 public class AlertingService extends VelocityServlet { 31 31 32 public static final Set actions = new TreeSet();32 public static final Set actions; 33 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"); 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); 42 47 } 43 48 … … 67 72 } catch (Exception e) { 68 73 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; 74 String details = e.getMessage() + " (" + e.getClass().getName() + "); " 75 + e.getCause() 76 + " ; action is " + action; 70 77 return showError(context, message, details); 71 78 } … … 99 106 if (!UserManager.getInstance().isLoggedIn(session)) { 100 107 session.setAttribute("next_action", "createSubscription"); 101 return "login.vm";108 return showLoginForm(arguments, context); 102 109 } 103 110 if (arguments.containsKey("next_page") && arguments.get("next_page").equals("finish")) { 111 arguments.putAll(getPageArgsFromSession(session)); 104 112 ProfileStore.getInstance().createSubscription(arguments); 105 113 return listSubscriptions(arguments, context); … … 113 121 if (!UserManager.getInstance().isLoggedIn(session)) { 114 122 session.setAttribute("next_action", "deleteSubscription"); 115 return "login.vm";123 return showLoginForm(arguments, context); 116 124 } 117 125 String subscriptionID = (String) arguments.get("subscriptionID"); … … 124 132 if (!UserManager.getInstance().isLoggedIn(session)) { 125 133 session.setAttribute("next_action", "editSubscription"); 126 return "login.vm";134 return showLoginForm(arguments, context); 127 135 } 128 136 if (arguments.containsKey("next_page") && arguments.get("next_page").equals("finish")) { … … 138 146 return "feed.vm"; 139 147 } 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 } 140 156 141 157 public String listSubscriptions(Map arguments, Context context) { 142 context.put("title", "List of Subscriptions"); 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); 143 165 context.put("list", ProfileStore.getInstance().getAllSubscriptions()); 144 166 return "list.vm"; … … 147 169 public String login(Map arguments, Context context) throws Exception { 148 170 HttpSession session = ((HttpServletRequest)context.get(REQUEST)).getSession(true); 149 UserManager.getInstance().loginUser(arguments, session); 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 } 150 178 if (session.getAttribute("next_action") != null) { 151 179 String nextAction = (String) session.getAttribute("next_action"); … … 158 186 public String register(Map arguments, Context context) throws Exception { 159 187 HttpSession session = ((HttpServletRequest)context.get(REQUEST)).getSession(true); 160 UserManager.getInstance().createUser(arguments, session); 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 } 161 195 return login(arguments, context); 162 196 } … … 194 228 for (Iterator iter = args.keySet().iterator(); iter.hasNext();) { 195 229 String key = (String) iter.next(); 196 if (!key.equals("action")) { 197 String firstValue = ((String[]) args.get(key))[0]; 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]; 198 237 result.put(key, firstValue); 199 238 } … … 260 299 // get page-specific stuff 261 300 if (nextPage.equals("host")) { 262 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); 263 310 } else if (nextPage.equals("collection")) { 264 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); 265 329 } 266 330 … … 324 388 } 325 389 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 326 402 /** 327 403 * @param page -
trunk/gsdl3/extensions/gsdl-as/src/org/greenstone/gsdlas/GreenstoneCommunicator.java
r8720 r8738 9 9 package org.greenstone.gsdlas; 10 10 11 import java.io.OutputStreamWriter;12 11 import java.net.URL; 13 12 import java.util.*; … … 18 17 import org.apache.soap.Fault; 19 18 import org.apache.soap.rpc.*; 20 import org.apache.soap.util.xml.DOM2Writer;21 19 import org.w3c.dom.*; 22 20 … … 44 42 } 45 43 44 /** 45 * 46 */ 47 public GreenstoneCommunicator() { 48 49 // TODO Auto-generated constructor stub 50 } 51 52 public String[] getHostNames() { 53 return new String[] {"localhost"}; 54 } 55 46 56 public String[] getCollectionNames() throws Exception { 47 57 Document document = createSubsetDescriptionRequest("", "collectionList"); … … 56 66 result[i] = name.getNodeValue(); 57 67 } 68 69 System.out.println(Arrays.toString(result)); 58 70 59 71 return result; … … 139 151 private Element sendToGreenstone(Element message) throws Exception { 140 152 Call call = new Call(); 141 call.setTargetObjectURI(" teevee.localsite");153 call.setTargetObjectURI("localsite"); 142 154 call.setMethodName("process"); 143 155 // set Encoding Style to use literal XML -
trunk/gsdl3/extensions/gsdl-as/src/org/greenstone/gsdlas/NotificationStore.java
r8720 r8738 9 9 package org.greenstone.gsdlas; 10 10 11 import java.io.UnsupportedEncodingException;12 import java.net.URLEncoder;13 11 import java.util.*; 14 import java.util.Map;15 import java.util.Set;16 12 17 13 import org.greenstone.gsdlas.profiles.Subscription; -
trunk/gsdl3/extensions/gsdl-as/src/org/greenstone/gsdlas/ProfileStore.java
r8718 r8738 14 14 15 15 import org.greenstone.gsdlas.profiles.*; 16 import org.greenstone.gsdlas.profiles.IdEqualsPredicate;17 import org.greenstone.gsdlas.profiles.Subscription;18 16 19 17 /** … … 33 31 34 32 private ProfileStore() { 35 36 33 } 37 34 … … 206 203 for (Iterator iter = matchedPredicates.iterator(); iter.hasNext();) { 207 204 Predicate pred = (Predicate) iter.next(); 208 for (Iterator iterator = pred.getSubscription s().iterator(); iterator205 for (Iterator iterator = pred.getSubscriptionIDs().iterator(); iterator 209 206 .hasNext();) { 210 207 Subscription sub = (Subscription) iterator.next(); … … 273 270 /** 274 271 * @param arguments 275 * @throws ParseException 276 */ 277 public void createSubscription(Map arguments) throws ParseException { 278 // TODO pass user information to profile store 272 * @throws Exception 273 */ 274 public void createSubscription(Map arguments) throws Exception { 279 275 Subscription sub = new Subscription(arguments); 280 // TODO this should probably be somewhere else281 for (Iterator iter = sub.getPredicates().iterator(); iter.hasNext();) {282 Predicate predicate = (Predicate) iter.next();283 if (predicate != null) {284 predicate.addSubscription(sub);285 }286 }287 276 addSubscription(sub); 288 277 } -
trunk/gsdl3/extensions/gsdl-as/src/org/greenstone/gsdlas/database/DatabaseException.java
r8717 r8738 20 20 super(message, cause); 21 21 } 22 23 /** 24 * @param string 25 */ 26 public DatabaseException(String message) { 27 super(message); 28 } 22 29 } -
trunk/gsdl3/extensions/gsdl-as/src/org/greenstone/gsdlas/database/DatabaseManager.java
r8717 r8738 37 37 public Connection getDatabaseConnection() throws DatabaseException { 38 38 if(database == null) { 39 String connectionString = "jdbc:mysql://localhost/alerting ";39 String connectionString = "jdbc:mysql://localhost/alerting?user=gsdl3admin"; 40 40 try { 41 41 Class.forName("com.mysql.jdbc.Driver").newInstance(); 42 Connection connection= DriverManager.getConnection(connectionString);42 database = DriverManager.getConnection(connectionString); 43 43 } catch (Exception e) { 44 44 throw new DatabaseException("Could not establish connection to database (" + connectionString + ")", e); -
trunk/gsdl3/extensions/gsdl-as/src/org/greenstone/gsdlas/profiles/Predicate.java
r8609 r8738 9 9 package org.greenstone.gsdlas.profiles; 10 10 11 import java.sql.*; 12 import java.sql.SQLException; 13 import java.sql.Statement; 11 14 import java.util.*; 12 15 import java.util.Set; 13 16 import java.util.TreeSet; 17 18 import org.greenstone.gsdlas.database.DatabaseException; 19 import org.greenstone.gsdlas.database.DatabaseManager; 20 import org.greenstone.gsdlas.util.ArrayHelper; 14 21 15 22 /** … … 26 33 protected int id; 27 34 28 public void addSubscription(Subscription sub) { 29 subscriptions.add(sub); 35 36 public static final String[] singleValueFields = new String[] { 37 "document_title", 38 "document_content", 39 "metadata_has_field", 40 "host_query", 41 "collection_query" }; 42 public static final String[] multiValueFields = new String[] { 43 "type", 44 "host", 45 "collection" }; 46 47 public void addSubscription(int subscriptionID) { 48 subscriptions.add(new Integer(subscriptionID)); 49 // TODO save to db 30 50 } 31 51 … … 33 53 * @see org.greenstone.gsdlas.profiles.Predicate#getSubscriptions() 34 54 */ 35 public Set getSubscription s() {55 public Set getSubscriptionIDs() { 36 56 return Collections.unmodifiableSet(subscriptions); 37 57 } … … 74 94 75 95 public abstract boolean isSatisfied(Map event); 96 97 /** 98 * @param key 99 * @return 100 */ 101 public static boolean isSingleValued(String key) { 102 return ArrayHelper.contains(singleValueFields, key); 103 } 104 105 /** 106 * @param key 107 * @return 108 */ 109 public static boolean isMultiValued(String key) { 110 return ArrayHelper.contains(singleValueFields, key); 111 } 112 113 /** 114 * @return 115 * @throws DatabaseException 116 * @throws SQLException 117 */ 118 int saveToDatabase() throws SQLException, DatabaseException { 119 // TODO handle multi-valued predicate (in subclass?) 120 String query = "SELECT id FROM predicates " + 121 "WHERE type='single' AND field = '" + field + 122 "' AND singleValue = '" + value + "';"; 123 Statement statement = DatabaseManager.getInstance().getDatabaseConnection().createStatement(); 124 System.out.println(query); 125 ResultSet result = statement.executeQuery(query); 126 if (result.next()) { // predicate already exists in database 127 System.out.println("predicate " + id + " already exists"); 128 return result.getInt("id"); 129 } 130 // predicate has been newly created 131 String insert = "INSERT INTO predicates (type, field, singleValue) " + 132 "VALUES ('single', '" + field + "', '" + value + "');"; 133 System.out.println(insert); 134 statement.execute(insert); 135 result = statement.executeQuery(query); 136 if (result.next()) { 137 return result.getInt("id"); 138 } else { 139 throw new DatabaseException("could not save predicate"); 140 } 141 } 76 142 77 143 } -
trunk/gsdl3/extensions/gsdl-as/src/org/greenstone/gsdlas/profiles/PredicateFactory.java
r8720 r8738 9 9 package org.greenstone.gsdlas.profiles; 10 10 11 import java.sql.SQLException; 11 12 import java.util.*; 12 13 import java.util.Map; 13 14 import java.util.TreeMap; 15 16 import org.greenstone.gsdlas.database.DatabaseException; 14 17 15 18 … … 42 45 * @return 43 46 * @throws ParseException 47 * @throws DatabaseException 48 * @throws SQLException 44 49 */ 45 50 public static Predicate createPredicate(String key, String value) 46 throws ParseException { 51 throws SQLException, DatabaseException { 52 System.out.println("creating predicate for key " + key + " and value " + value); 53 if (!Predicate.isSingleValued(key)) { 54 return null; 55 } 47 56 Predicate result = null; 48 57 if (value == null || value.length() == 0) { … … 59 68 result = createSubstringMatchPredicate(key, value); 60 69 } else { 61 throw new ParseException("value " + value + " for field " + key 62 + " does not lead to a parseable predicate"); 70 return null; 63 71 } 64 result.setID( id++);72 result.setID(result.saveToDatabase()); 65 73 return result; 74 } 75 76 public static Predicate createPredicate(String key, List values) throws SQLException, DatabaseException { 77 if (!Predicate.isMultiValued(key)) { 78 return null; 79 } 80 // TODO properly handle multi-valued predicates 81 return createPredicate(key, (String) values.get(0)); 66 82 } 67 83 … … 108 124 return predicate; 109 125 } 110 126 111 127 /** 112 128 * @param field -
trunk/gsdl3/extensions/gsdl-as/src/org/greenstone/gsdlas/profiles/Subscription.java
r8719 r8738 9 9 package org.greenstone.gsdlas.profiles; 10 10 11 import java.sql.*; 12 import java.sql.Connection; 13 import java.sql.Statement; 11 14 import java.util.*; 12 15 import java.util.Map; 13 16 import java.util.TreeMap; 17 18 import org.greenstone.gsdlas.database.DatabaseException; 19 import org.greenstone.gsdlas.database.DatabaseManager; 14 20 15 21 /** … … 21 27 public class Subscription implements Comparable { 22 28 private Map map; 29 23 30 private int id; 31 private String username; 32 private String name; 33 private String email; 34 private boolean rssNotification; 35 private boolean eventsSincePageNotification; 36 24 37 private int numOfNonEqualsPredicates; 25 38 26 private static int highestId = 0; 27 28 public Subscription(Map valueMap) throws ParseException { 39 public Subscription(Map valueMap) throws DatabaseException, SQLException { 29 40 map = new TreeMap(); 30 41 for (Iterator iter = valueMap.keySet().iterator(); iter.hasNext();) { 31 42 String key = (String) iter.next(); 32 String value = (String) valueMap.get(key); 33 Predicate predicate = PredicateFactory.createPredicate(key, value); 34 map.put(key, predicate); 43 Object value = valueMap.get(key); 44 45 Predicate predicate = null; 46 if (value instanceof String) { 47 predicate = PredicateFactory.createPredicate(key, (String) value); 48 map.put(key, predicate); 49 } else if (value instanceof List) { 50 List values = (List)value; 51 predicate = PredicateFactory.createPredicate(key, values); 52 map.put(key, predicate); 53 } 35 54 36 55 if (predicate != null && !(predicate instanceof IdEqualsPredicate)) { … … 38 57 } 39 58 } 40 id = highestId++; 59 60 System.out.println("finished creating predicates"); 61 62 username = (String) valueMap.get("username"); 63 name = (String) valueMap.get("subscription_name"); 64 email = (String) valueMap.get("email"); 65 rssNotification = valueMap.containsKey("way") && ((List)valueMap.get("way")).contains("rss"); 66 eventsSincePageNotification = valueMap.containsKey("way") && ((List)valueMap.get("way")).contains("page"); 67 id = saveToDatabase(true); 68 69 for (Iterator iter = getPredicates().iterator(); iter.hasNext();) { 70 Predicate predicate = (Predicate) iter.next(); 71 if (predicate != null) { 72 predicate.addSubscription(id); 73 } 74 } 41 75 } 42 76 … … 95 129 return new Integer(id).compareTo(new Integer(other.id)); 96 130 } 131 132 private int saveToDatabase(boolean initial) throws DatabaseException, SQLException { 133 Connection conn = DatabaseManager.getInstance().getDatabaseConnection(); 134 Statement statement = conn.createStatement(); 135 String sqlString; 136 if (initial) { 137 sqlString = "INSERT INTO subscriptions (name, email, rss, page, user) " + 138 "VALUES ('" + name + "','" + email + "'," + (rssNotification ? 1 : 0) + 139 "," + (eventsSincePageNotification ? 1: 0)+ ", '" + username + "');"; 140 } else { 141 sqlString = "UPDATE subscriptions SET name='" + name + "', email='" + 142 email + "', rss=" + (rssNotification ? 1 : 0) + ",page=" + 143 (eventsSincePageNotification ? 1 : 0)+ "WHERE id=" + id + ";"; 144 // cannot change user 145 } 146 System.out.println(sqlString); 147 statement.executeUpdate(sqlString); 148 149 sqlString = "SELECT id FROM subscriptions WHERE name like '" + name + 150 "' AND email like '" + email + "' AND rss=" + (rssNotification ? 1 : 0) 151 + " AND page=" + (eventsSincePageNotification ? 1 : 0) + " AND user like '" + 152 username + "';"; 153 System.out.println(sqlString); 154 ResultSet result = statement.executeQuery(sqlString); 155 int id; 156 if (result.next()) { 157 id = result.getInt("id"); 158 } else { 159 throw new DatabaseException("Couldn't save subscription"); 160 } 161 162 if (initial) { 163 for (Iterator iter = getPredicates().iterator(); iter.hasNext();) { 164 Predicate predicate = (Predicate) iter.next(); 165 sqlString = "INSERT INTO subs_to_predicates (subscription, predicate) " + 166 "VALUES (" + id + "," + predicate.getID() + ");"; 167 statement.executeUpdate(sqlString); 168 } 169 } 170 return id; 171 } 97 172 } -
trunk/gsdl3/extensions/gsdl-as/src/org/greenstone/gsdlas/users/UserManager.java
r8717 r8738 9 9 package org.greenstone.gsdlas.users; 10 10 11 import java.sql.*; 11 12 import java.util.Map; 12 13 13 14 import javax.servlet.http.HttpSession; 15 16 import org.greenstone.gsdlas.database.DatabaseManager; 14 17 15 18 /** … … 40 43 if (session.getCreationTime() - session.getLastAccessedTime() > session.getMaxInactiveInterval()) 41 44 return false; 42 43 // TODO Auto-generated method stub 44 return true; 45 return session.getAttribute("username") != null; 45 46 } 46 47 … … 48 49 * @param arguments 49 50 * @param session 51 * @throws PasswordMismatchException 52 * @throws UserManagementException 50 53 */ 51 public void createUser(Map arguments, HttpSession session) { 52 // TODO Auto-generated method stub 54 public void createUser(Map arguments, HttpSession session) throws PasswordMismatchException, UserManagementException { 55 if (!arguments.get("password").equals(arguments.get("password2"))) { 56 throw new PasswordMismatchException("The passwords don't match"); 57 } 58 String username = (String) arguments.get("username"); 59 60 byte[] password = ((String) arguments.get("password")).getBytes(); 61 62 byte[] pwdHash = password; 63 // try { 64 // pwdHash = MessageDigest.getInstance("MD5").digest(password); 65 // } catch (NoSuchAlgorithmException e) { 66 // e.printStackTrace(); 67 // throw new UserManagementException("could not create user", e); 68 // } 69 70 try { 71 Connection conn = DatabaseManager.getInstance().getDatabaseConnection(); 72 Statement statement = conn.createStatement(); 73 statement.executeUpdate("INSERT INTO users (username, password) " + 74 "VALUES ('" + username + "','" + new String(pwdHash) + "')"); 75 } catch (Exception e) { 76 e.printStackTrace(); 77 throw new UserManagementException("could not create user", e); 78 } 53 79 54 80 } … … 57 83 * @param arguments 58 84 * @param session 85 * @throws UserManagementException 59 86 */ 60 public void loginUser(Map arguments, HttpSession session) {61 // TODO Auto-generated method stub87 public void loginUser(Map arguments, HttpSession session) throws UserManagementException { 88 String username = (String) arguments.get("username"); 62 89 90 if (isLoggedIn(session) && session.getAttribute("username").equals(username)) { 91 return; // already logged in 92 } 93 94 byte[] password = ((String) arguments.get("password")).getBytes(); 95 96 byte[] pwdHash = password; 97 try { 98 // pwdHash = MessageDigest.getInstance("MD5").digest(password); 99 100 Connection conn = DatabaseManager.getInstance().getDatabaseConnection(); 101 Statement statement = conn.createStatement(); 102 ResultSet results = statement.executeQuery("SELECT password " + 103 "FROM users WHERE username like '" + username + "';"); 104 String pwdFromDB = ""; 105 if(results.next()) { 106 pwdFromDB = results.getString("password"); 107 } 108 if (!pwdFromDB.equals(new String(pwdHash))) { 109 throw new PasswordMismatchException("user " + username 110 + " is unknown, or the passwords don't match"); 111 } 112 session.setAttribute("username", username); 113 } catch (Exception e) { 114 e.printStackTrace(); 115 throw new UserManagementException("could not login user", e); 116 } 117 63 118 } 64 119 -
trunk/gsdl3/extensions/gsdl-as/test-src/org/greenstone/gsdlas/ProfileStoreTest.java
r8717 r8738 13 13 14 14 import junit.framework.TestCase; 15 16 import org.greenstone.gsdlas.profiles.*;17 15 18 16 /** … … 31 29 try { 32 30 addSubscriptions(); 33 } catch ( ParseException e) {31 } catch (Exception e) { 34 32 e.printStackTrace(); 35 33 fail(e.getMessage()); … … 60 58 } 61 59 62 private void addSubscriptions() throws ParseException {60 private void addSubscriptions() throws Exception { 63 61 Map valueMap = new TreeMap(); 64 62 … … 83 81 } 84 82 85 private void addSubscription(Map valueMap) throws ParseException {83 private void addSubscription(Map valueMap) throws Exception { 86 84 ProfileStore.getInstance().createSubscription(valueMap); 87 85 } -
trunk/gsdl3/extensions/gsdl-as/web/form.css
r8724 r8738 40 40 color: #555; 41 41 border-top: 10px solid #CCC; 42 font-family: "Trebuchet MS", Arial, helvetica, sans-serif; 42 43 } 43 44 … … 49 50 #progress td.current { 50 51 background-color: #F0E68C; 51 font-style: italic;52 52 color: #000; 53 53 } -
trunk/gsdl3/extensions/gsdl-as/web/index.html
r8720 r8738 17 17 <ul> 18 18 <li> 19 <a href="/alerting/service?action=showLoginForm">Log in</a></li> 20 </li> 21 <li> 19 22 <a href="subscribe.html">Create</a> a subscription.</li> 20 23 <li> 21 <a href=" service?action=listSubscriptions">View and edit</a> your existing subscriptions.</li>24 <a href="/alerting/service?action=listSubscriptions">View and edit</a> your existing subscriptions.</li> 22 25 <li> 23 26 <a href="about.html">Read more</a> about the alerting service.</li> 27 <li> 28 <a href="/alerting/service?action=logout">Log out</a></li? 24 29 </ul> 25 30 </div> -
trunk/gsdl3/packages/gsdl-as/build.xml
r8644 r8738 217 217 <!-- Include all JAR files that will be included in /WEB-INF/lib --> 218 218 <!-- *** CUSTOMIZE HERE AS REQUIRED BY YOUR APPLICATION *** --> 219 <!-- 220 <pathelement location="${foo.jar}"/>221 -->219 <fileset dir="lib"> 220 <include name="*.jar"/> 221 </fileset> 222 222 223 223 <!-- Include all elements that Tomcat exposes to applications --> -
trunk/gsdl3/packages/gsdl-as/lib/templates/sub_collection.vm
r8724 r8738 37 37 <ol> 38 38 <li>You can choose one or more collections from the list below. Only events from the collections(s) you have chosen will be sent to you<br/> 39 <select multiple="multiple" name="host" size="7" style="margin:5px 0px;"> 40 <optgroup label="localhost"> 41 <option>greenstone 2 mg demo</option> 42 <option>greenstone 2 mgpp demo</option> 43 <option>greenstone 3 mg demo</option> 44 <option selected="selected">any collection on host localhost</option> 45 </optgroup> 46 <optgroup label="nzdl.org"> 47 <option>acronym extraction demo</option> 48 <option>music library</option> 49 <option>youth oral history</option> 50 <option>kiwi aircraft images</option> 51 <option selected="selected">any collection on host nzdl.org</option> 52 </optgroup> 39 <select multiple="multiple" name="collection" size="7" style="margin:5px 0px;"> 40 #foreach($host in $hostnames) 41 <optgroup label="$host"> 42 #foreach($collection in $collectionnames.get($host)) 43 <option>$collection</option> 44 #end 45 <option selected="selected">any collection on host $host</option> 46 </optgroup> 47 #end 53 48 </select></li> 54 49 <li>You can enter a query in the field below. Only events coming from a collection whose name matches this query will be sent to you.<br/> … … 59 54 </td> 60 55 <td> 61 <input type="text" name=" document_title" style="width:80%;"/>56 <input type="text" name="collection_query" style="width:80%;" value="$!prefill.collection_query"/> 62 57 </td> 63 58 </tr> -
trunk/gsdl3/packages/gsdl-as/lib/templates/sub_host.vm
r8724 r8738 38 38 <li>You can choose one or more hosts from the list below. Only events from the host(s) you have chosen will be sent to you.<br/> 39 39 <select multiple="multiple" name="host" size="3" style="margin:5px 0px;"> 40 <option selected="selected">localhost</option> 41 <option>www.greenstone.org</option> 42 <option>www.nzdl.org</option> 43 <option>any host</option> 40 #set($firstoption = true); 41 #foreach($host in $hostnames) 42 <option #if($firstoption) selected="selected" #set($firstoption = false) #end>$host</option> 43 #end 44 <option value="any">any host</option> 44 45 </select></li> 45 46 <li>You can enter a query in the field below. Only events coming from a host whose name matches this query will be sent to you.<br/> … … 50 51 </td> 51 52 <td> 52 <input type="text" name=" document_title" style="width:80%;"/>53 <input type="text" name="host_query" style="width:80%;" value="$!prefill.host_query"/> 53 54 </td> 54 55 </tr> -
trunk/gsdl3/packages/gsdl-as/lib/templates/sub_notification.vm
r8724 r8738 37 37 You will be able to see the subscription in the list of all your subscriptions after logging in to the alerting service. <br/> 38 38 You can assign a name to your subscription that makes it easier for you to recognise it: 39 <input name="subscription_name" type="text" />39 <input name="subscription_name" type="text" value="$!prefill.subscription_name"/> 40 40 </div> 41 41 <br/> … … 52 52 </td> 53 53 <td> 54 <input type="text" name="email" />54 <input type="text" name="email" value="$!prefill.email"/> 55 55 </td> 56 56 </tr> -
trunk/gsdl3/packages/gsdl-as/src/org/greenstone/gsdlas/AlertingService.java
r8724 r8738 14 14 import java.util.*; 15 15 16 import javax.mail.Session;17 16 import javax.servlet.http.*; 18 17 … … 20 19 import org.apache.velocity.context.Context; 21 20 import org.apache.velocity.servlet.VelocityServlet; 21 import org.greenstone.gsdlas.users.UserManagementException; 22 22 import org.greenstone.gsdlas.users.UserManager; 23 23 … … 30 30 public class AlertingService extends VelocityServlet { 31 31 32 public static final Set actions = new TreeSet();32 public static final Set actions; 33 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"); 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); 42 47 } 43 48 … … 67 72 } catch (Exception e) { 68 73 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; 74 String details = e.getMessage() + " (" + e.getClass().getName() + "); " 75 + e.getCause() 76 + " ; action is " + action; 70 77 return showError(context, message, details); 71 78 } … … 99 106 if (!UserManager.getInstance().isLoggedIn(session)) { 100 107 session.setAttribute("next_action", "createSubscription"); 101 return "login.vm";108 return showLoginForm(arguments, context); 102 109 } 103 110 if (arguments.containsKey("next_page") && arguments.get("next_page").equals("finish")) { 111 arguments.putAll(getPageArgsFromSession(session)); 104 112 ProfileStore.getInstance().createSubscription(arguments); 105 113 return listSubscriptions(arguments, context); … … 113 121 if (!UserManager.getInstance().isLoggedIn(session)) { 114 122 session.setAttribute("next_action", "deleteSubscription"); 115 return "login.vm";123 return showLoginForm(arguments, context); 116 124 } 117 125 String subscriptionID = (String) arguments.get("subscriptionID"); … … 124 132 if (!UserManager.getInstance().isLoggedIn(session)) { 125 133 session.setAttribute("next_action", "editSubscription"); 126 return "login.vm";134 return showLoginForm(arguments, context); 127 135 } 128 136 if (arguments.containsKey("next_page") && arguments.get("next_page").equals("finish")) { … … 138 146 return "feed.vm"; 139 147 } 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 } 140 156 141 157 public String listSubscriptions(Map arguments, Context context) { 142 context.put("title", "List of Subscriptions"); 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); 143 165 context.put("list", ProfileStore.getInstance().getAllSubscriptions()); 144 166 return "list.vm"; … … 147 169 public String login(Map arguments, Context context) throws Exception { 148 170 HttpSession session = ((HttpServletRequest)context.get(REQUEST)).getSession(true); 149 UserManager.getInstance().loginUser(arguments, session); 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 } 150 178 if (session.getAttribute("next_action") != null) { 151 179 String nextAction = (String) session.getAttribute("next_action"); … … 158 186 public String register(Map arguments, Context context) throws Exception { 159 187 HttpSession session = ((HttpServletRequest)context.get(REQUEST)).getSession(true); 160 UserManager.getInstance().createUser(arguments, session); 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 } 161 195 return login(arguments, context); 162 196 } … … 194 228 for (Iterator iter = args.keySet().iterator(); iter.hasNext();) { 195 229 String key = (String) iter.next(); 196 if (!key.equals("action")) { 197 String firstValue = ((String[]) args.get(key))[0]; 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]; 198 237 result.put(key, firstValue); 199 238 } … … 260 299 // get page-specific stuff 261 300 if (nextPage.equals("host")) { 262 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); 263 310 } else if (nextPage.equals("collection")) { 264 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); 265 329 } 266 330 … … 324 388 } 325 389 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 326 402 /** 327 403 * @param page -
trunk/gsdl3/packages/gsdl-as/src/org/greenstone/gsdlas/GreenstoneCommunicator.java
r8720 r8738 9 9 package org.greenstone.gsdlas; 10 10 11 import java.io.OutputStreamWriter;12 11 import java.net.URL; 13 12 import java.util.*; … … 18 17 import org.apache.soap.Fault; 19 18 import org.apache.soap.rpc.*; 20 import org.apache.soap.util.xml.DOM2Writer;21 19 import org.w3c.dom.*; 22 20 … … 44 42 } 45 43 44 /** 45 * 46 */ 47 public GreenstoneCommunicator() { 48 49 // TODO Auto-generated constructor stub 50 } 51 52 public String[] getHostNames() { 53 return new String[] {"localhost"}; 54 } 55 46 56 public String[] getCollectionNames() throws Exception { 47 57 Document document = createSubsetDescriptionRequest("", "collectionList"); … … 56 66 result[i] = name.getNodeValue(); 57 67 } 68 69 System.out.println(Arrays.toString(result)); 58 70 59 71 return result; … … 139 151 private Element sendToGreenstone(Element message) throws Exception { 140 152 Call call = new Call(); 141 call.setTargetObjectURI(" teevee.localsite");153 call.setTargetObjectURI("localsite"); 142 154 call.setMethodName("process"); 143 155 // set Encoding Style to use literal XML -
trunk/gsdl3/packages/gsdl-as/src/org/greenstone/gsdlas/NotificationStore.java
r8720 r8738 9 9 package org.greenstone.gsdlas; 10 10 11 import java.io.UnsupportedEncodingException;12 import java.net.URLEncoder;13 11 import java.util.*; 14 import java.util.Map;15 import java.util.Set;16 12 17 13 import org.greenstone.gsdlas.profiles.Subscription; -
trunk/gsdl3/packages/gsdl-as/src/org/greenstone/gsdlas/ProfileStore.java
r8718 r8738 14 14 15 15 import org.greenstone.gsdlas.profiles.*; 16 import org.greenstone.gsdlas.profiles.IdEqualsPredicate;17 import org.greenstone.gsdlas.profiles.Subscription;18 16 19 17 /** … … 33 31 34 32 private ProfileStore() { 35 36 33 } 37 34 … … 206 203 for (Iterator iter = matchedPredicates.iterator(); iter.hasNext();) { 207 204 Predicate pred = (Predicate) iter.next(); 208 for (Iterator iterator = pred.getSubscription s().iterator(); iterator205 for (Iterator iterator = pred.getSubscriptionIDs().iterator(); iterator 209 206 .hasNext();) { 210 207 Subscription sub = (Subscription) iterator.next(); … … 273 270 /** 274 271 * @param arguments 275 * @throws ParseException 276 */ 277 public void createSubscription(Map arguments) throws ParseException { 278 // TODO pass user information to profile store 272 * @throws Exception 273 */ 274 public void createSubscription(Map arguments) throws Exception { 279 275 Subscription sub = new Subscription(arguments); 280 // TODO this should probably be somewhere else281 for (Iterator iter = sub.getPredicates().iterator(); iter.hasNext();) {282 Predicate predicate = (Predicate) iter.next();283 if (predicate != null) {284 predicate.addSubscription(sub);285 }286 }287 276 addSubscription(sub); 288 277 } -
trunk/gsdl3/packages/gsdl-as/src/org/greenstone/gsdlas/database/DatabaseException.java
r8717 r8738 20 20 super(message, cause); 21 21 } 22 23 /** 24 * @param string 25 */ 26 public DatabaseException(String message) { 27 super(message); 28 } 22 29 } -
trunk/gsdl3/packages/gsdl-as/src/org/greenstone/gsdlas/database/DatabaseManager.java
r8717 r8738 37 37 public Connection getDatabaseConnection() throws DatabaseException { 38 38 if(database == null) { 39 String connectionString = "jdbc:mysql://localhost/alerting ";39 String connectionString = "jdbc:mysql://localhost/alerting?user=gsdl3admin"; 40 40 try { 41 41 Class.forName("com.mysql.jdbc.Driver").newInstance(); 42 Connection connection= DriverManager.getConnection(connectionString);42 database = DriverManager.getConnection(connectionString); 43 43 } catch (Exception e) { 44 44 throw new DatabaseException("Could not establish connection to database (" + connectionString + ")", e); -
trunk/gsdl3/packages/gsdl-as/src/org/greenstone/gsdlas/profiles/Predicate.java
r8609 r8738 9 9 package org.greenstone.gsdlas.profiles; 10 10 11 import java.sql.*; 12 import java.sql.SQLException; 13 import java.sql.Statement; 11 14 import java.util.*; 12 15 import java.util.Set; 13 16 import java.util.TreeSet; 17 18 import org.greenstone.gsdlas.database.DatabaseException; 19 import org.greenstone.gsdlas.database.DatabaseManager; 20 import org.greenstone.gsdlas.util.ArrayHelper; 14 21 15 22 /** … … 26 33 protected int id; 27 34 28 public void addSubscription(Subscription sub) { 29 subscriptions.add(sub); 35 36 public static final String[] singleValueFields = new String[] { 37 "document_title", 38 "document_content", 39 "metadata_has_field", 40 "host_query", 41 "collection_query" }; 42 public static final String[] multiValueFields = new String[] { 43 "type", 44 "host", 45 "collection" }; 46 47 public void addSubscription(int subscriptionID) { 48 subscriptions.add(new Integer(subscriptionID)); 49 // TODO save to db 30 50 } 31 51 … … 33 53 * @see org.greenstone.gsdlas.profiles.Predicate#getSubscriptions() 34 54 */ 35 public Set getSubscription s() {55 public Set getSubscriptionIDs() { 36 56 return Collections.unmodifiableSet(subscriptions); 37 57 } … … 74 94 75 95 public abstract boolean isSatisfied(Map event); 96 97 /** 98 * @param key 99 * @return 100 */ 101 public static boolean isSingleValued(String key) { 102 return ArrayHelper.contains(singleValueFields, key); 103 } 104 105 /** 106 * @param key 107 * @return 108 */ 109 public static boolean isMultiValued(String key) { 110 return ArrayHelper.contains(singleValueFields, key); 111 } 112 113 /** 114 * @return 115 * @throws DatabaseException 116 * @throws SQLException 117 */ 118 int saveToDatabase() throws SQLException, DatabaseException { 119 // TODO handle multi-valued predicate (in subclass?) 120 String query = "SELECT id FROM predicates " + 121 "WHERE type='single' AND field = '" + field + 122 "' AND singleValue = '" + value + "';"; 123 Statement statement = DatabaseManager.getInstance().getDatabaseConnection().createStatement(); 124 System.out.println(query); 125 ResultSet result = statement.executeQuery(query); 126 if (result.next()) { // predicate already exists in database 127 System.out.println("predicate " + id + " already exists"); 128 return result.getInt("id"); 129 } 130 // predicate has been newly created 131 String insert = "INSERT INTO predicates (type, field, singleValue) " + 132 "VALUES ('single', '" + field + "', '" + value + "');"; 133 System.out.println(insert); 134 statement.execute(insert); 135 result = statement.executeQuery(query); 136 if (result.next()) { 137 return result.getInt("id"); 138 } else { 139 throw new DatabaseException("could not save predicate"); 140 } 141 } 76 142 77 143 } -
trunk/gsdl3/packages/gsdl-as/src/org/greenstone/gsdlas/profiles/PredicateFactory.java
r8720 r8738 9 9 package org.greenstone.gsdlas.profiles; 10 10 11 import java.sql.SQLException; 11 12 import java.util.*; 12 13 import java.util.Map; 13 14 import java.util.TreeMap; 15 16 import org.greenstone.gsdlas.database.DatabaseException; 14 17 15 18 … … 42 45 * @return 43 46 * @throws ParseException 47 * @throws DatabaseException 48 * @throws SQLException 44 49 */ 45 50 public static Predicate createPredicate(String key, String value) 46 throws ParseException { 51 throws SQLException, DatabaseException { 52 System.out.println("creating predicate for key " + key + " and value " + value); 53 if (!Predicate.isSingleValued(key)) { 54 return null; 55 } 47 56 Predicate result = null; 48 57 if (value == null || value.length() == 0) { … … 59 68 result = createSubstringMatchPredicate(key, value); 60 69 } else { 61 throw new ParseException("value " + value + " for field " + key 62 + " does not lead to a parseable predicate"); 70 return null; 63 71 } 64 result.setID( id++);72 result.setID(result.saveToDatabase()); 65 73 return result; 74 } 75 76 public static Predicate createPredicate(String key, List values) throws SQLException, DatabaseException { 77 if (!Predicate.isMultiValued(key)) { 78 return null; 79 } 80 // TODO properly handle multi-valued predicates 81 return createPredicate(key, (String) values.get(0)); 66 82 } 67 83 … … 108 124 return predicate; 109 125 } 110 126 111 127 /** 112 128 * @param field -
trunk/gsdl3/packages/gsdl-as/src/org/greenstone/gsdlas/profiles/Subscription.java
r8719 r8738 9 9 package org.greenstone.gsdlas.profiles; 10 10 11 import java.sql.*; 12 import java.sql.Connection; 13 import java.sql.Statement; 11 14 import java.util.*; 12 15 import java.util.Map; 13 16 import java.util.TreeMap; 17 18 import org.greenstone.gsdlas.database.DatabaseException; 19 import org.greenstone.gsdlas.database.DatabaseManager; 14 20 15 21 /** … … 21 27 public class Subscription implements Comparable { 22 28 private Map map; 29 23 30 private int id; 31 private String username; 32 private String name; 33 private String email; 34 private boolean rssNotification; 35 private boolean eventsSincePageNotification; 36 24 37 private int numOfNonEqualsPredicates; 25 38 26 private static int highestId = 0; 27 28 public Subscription(Map valueMap) throws ParseException { 39 public Subscription(Map valueMap) throws DatabaseException, SQLException { 29 40 map = new TreeMap(); 30 41 for (Iterator iter = valueMap.keySet().iterator(); iter.hasNext();) { 31 42 String key = (String) iter.next(); 32 String value = (String) valueMap.get(key); 33 Predicate predicate = PredicateFactory.createPredicate(key, value); 34 map.put(key, predicate); 43 Object value = valueMap.get(key); 44 45 Predicate predicate = null; 46 if (value instanceof String) { 47 predicate = PredicateFactory.createPredicate(key, (String) value); 48 map.put(key, predicate); 49 } else if (value instanceof List) { 50 List values = (List)value; 51 predicate = PredicateFactory.createPredicate(key, values); 52 map.put(key, predicate); 53 } 35 54 36 55 if (predicate != null && !(predicate instanceof IdEqualsPredicate)) { … … 38 57 } 39 58 } 40 id = highestId++; 59 60 System.out.println("finished creating predicates"); 61 62 username = (String) valueMap.get("username"); 63 name = (String) valueMap.get("subscription_name"); 64 email = (String) valueMap.get("email"); 65 rssNotification = valueMap.containsKey("way") && ((List)valueMap.get("way")).contains("rss"); 66 eventsSincePageNotification = valueMap.containsKey("way") && ((List)valueMap.get("way")).contains("page"); 67 id = saveToDatabase(true); 68 69 for (Iterator iter = getPredicates().iterator(); iter.hasNext();) { 70 Predicate predicate = (Predicate) iter.next(); 71 if (predicate != null) { 72 predicate.addSubscription(id); 73 } 74 } 41 75 } 42 76 … … 95 129 return new Integer(id).compareTo(new Integer(other.id)); 96 130 } 131 132 private int saveToDatabase(boolean initial) throws DatabaseException, SQLException { 133 Connection conn = DatabaseManager.getInstance().getDatabaseConnection(); 134 Statement statement = conn.createStatement(); 135 String sqlString; 136 if (initial) { 137 sqlString = "INSERT INTO subscriptions (name, email, rss, page, user) " + 138 "VALUES ('" + name + "','" + email + "'," + (rssNotification ? 1 : 0) + 139 "," + (eventsSincePageNotification ? 1: 0)+ ", '" + username + "');"; 140 } else { 141 sqlString = "UPDATE subscriptions SET name='" + name + "', email='" + 142 email + "', rss=" + (rssNotification ? 1 : 0) + ",page=" + 143 (eventsSincePageNotification ? 1 : 0)+ "WHERE id=" + id + ";"; 144 // cannot change user 145 } 146 System.out.println(sqlString); 147 statement.executeUpdate(sqlString); 148 149 sqlString = "SELECT id FROM subscriptions WHERE name like '" + name + 150 "' AND email like '" + email + "' AND rss=" + (rssNotification ? 1 : 0) 151 + " AND page=" + (eventsSincePageNotification ? 1 : 0) + " AND user like '" + 152 username + "';"; 153 System.out.println(sqlString); 154 ResultSet result = statement.executeQuery(sqlString); 155 int id; 156 if (result.next()) { 157 id = result.getInt("id"); 158 } else { 159 throw new DatabaseException("Couldn't save subscription"); 160 } 161 162 if (initial) { 163 for (Iterator iter = getPredicates().iterator(); iter.hasNext();) { 164 Predicate predicate = (Predicate) iter.next(); 165 sqlString = "INSERT INTO subs_to_predicates (subscription, predicate) " + 166 "VALUES (" + id + "," + predicate.getID() + ");"; 167 statement.executeUpdate(sqlString); 168 } 169 } 170 return id; 171 } 97 172 } -
trunk/gsdl3/packages/gsdl-as/src/org/greenstone/gsdlas/users/UserManager.java
r8717 r8738 9 9 package org.greenstone.gsdlas.users; 10 10 11 import java.sql.*; 11 12 import java.util.Map; 12 13 13 14 import javax.servlet.http.HttpSession; 15 16 import org.greenstone.gsdlas.database.DatabaseManager; 14 17 15 18 /** … … 40 43 if (session.getCreationTime() - session.getLastAccessedTime() > session.getMaxInactiveInterval()) 41 44 return false; 42 43 // TODO Auto-generated method stub 44 return true; 45 return session.getAttribute("username") != null; 45 46 } 46 47 … … 48 49 * @param arguments 49 50 * @param session 51 * @throws PasswordMismatchException 52 * @throws UserManagementException 50 53 */ 51 public void createUser(Map arguments, HttpSession session) { 52 // TODO Auto-generated method stub 54 public void createUser(Map arguments, HttpSession session) throws PasswordMismatchException, UserManagementException { 55 if (!arguments.get("password").equals(arguments.get("password2"))) { 56 throw new PasswordMismatchException("The passwords don't match"); 57 } 58 String username = (String) arguments.get("username"); 59 60 byte[] password = ((String) arguments.get("password")).getBytes(); 61 62 byte[] pwdHash = password; 63 // try { 64 // pwdHash = MessageDigest.getInstance("MD5").digest(password); 65 // } catch (NoSuchAlgorithmException e) { 66 // e.printStackTrace(); 67 // throw new UserManagementException("could not create user", e); 68 // } 69 70 try { 71 Connection conn = DatabaseManager.getInstance().getDatabaseConnection(); 72 Statement statement = conn.createStatement(); 73 statement.executeUpdate("INSERT INTO users (username, password) " + 74 "VALUES ('" + username + "','" + new String(pwdHash) + "')"); 75 } catch (Exception e) { 76 e.printStackTrace(); 77 throw new UserManagementException("could not create user", e); 78 } 53 79 54 80 } … … 57 83 * @param arguments 58 84 * @param session 85 * @throws UserManagementException 59 86 */ 60 public void loginUser(Map arguments, HttpSession session) {61 // TODO Auto-generated method stub87 public void loginUser(Map arguments, HttpSession session) throws UserManagementException { 88 String username = (String) arguments.get("username"); 62 89 90 if (isLoggedIn(session) && session.getAttribute("username").equals(username)) { 91 return; // already logged in 92 } 93 94 byte[] password = ((String) arguments.get("password")).getBytes(); 95 96 byte[] pwdHash = password; 97 try { 98 // pwdHash = MessageDigest.getInstance("MD5").digest(password); 99 100 Connection conn = DatabaseManager.getInstance().getDatabaseConnection(); 101 Statement statement = conn.createStatement(); 102 ResultSet results = statement.executeQuery("SELECT password " + 103 "FROM users WHERE username like '" + username + "';"); 104 String pwdFromDB = ""; 105 if(results.next()) { 106 pwdFromDB = results.getString("password"); 107 } 108 if (!pwdFromDB.equals(new String(pwdHash))) { 109 throw new PasswordMismatchException("user " + username 110 + " is unknown, or the passwords don't match"); 111 } 112 session.setAttribute("username", username); 113 } catch (Exception e) { 114 e.printStackTrace(); 115 throw new UserManagementException("could not login user", e); 116 } 117 63 118 } 64 119 -
trunk/gsdl3/packages/gsdl-as/test-src/org/greenstone/gsdlas/ProfileStoreTest.java
r8717 r8738 13 13 14 14 import junit.framework.TestCase; 15 16 import org.greenstone.gsdlas.profiles.*;17 15 18 16 /** … … 31 29 try { 32 30 addSubscriptions(); 33 } catch ( ParseException e) {31 } catch (Exception e) { 34 32 e.printStackTrace(); 35 33 fail(e.getMessage()); … … 60 58 } 61 59 62 private void addSubscriptions() throws ParseException {60 private void addSubscriptions() throws Exception { 63 61 Map valueMap = new TreeMap(); 64 62 … … 83 81 } 84 82 85 private void addSubscription(Map valueMap) throws ParseException {83 private void addSubscription(Map valueMap) throws Exception { 86 84 ProfileStore.getInstance().createSubscription(valueMap); 87 85 } -
trunk/gsdl3/packages/gsdl-as/web/form.css
r8724 r8738 40 40 color: #555; 41 41 border-top: 10px solid #CCC; 42 font-family: "Trebuchet MS", Arial, helvetica, sans-serif; 42 43 } 43 44 … … 49 50 #progress td.current { 50 51 background-color: #F0E68C; 51 font-style: italic;52 52 color: #000; 53 53 } -
trunk/gsdl3/packages/gsdl-as/web/index.html
r8720 r8738 17 17 <ul> 18 18 <li> 19 <a href="/alerting/service?action=showLoginForm">Log in</a></li> 20 </li> 21 <li> 19 22 <a href="subscribe.html">Create</a> a subscription.</li> 20 23 <li> 21 <a href=" service?action=listSubscriptions">View and edit</a> your existing subscriptions.</li>24 <a href="/alerting/service?action=listSubscriptions">View and edit</a> your existing subscriptions.</li> 22 25 <li> 23 26 <a href="about.html">Read more</a> about the alerting service.</li> 27 <li> 28 <a href="/alerting/service?action=logout">Log out</a></li? 24 29 </ul> 25 30 </div>
Note:
See TracChangeset
for help on using the changeset viewer.