source: main/trunk/greenstone3/src/java/org/greenstone/gsdl3/service/FavouriteBasket.java@ 37518

Last change on this file since 37518 was 37518, checked in by kjdon, 14 months ago

simplified Favourites - items now are just collection and id. no metadata involved. getting metadata is done by the action now (a=gdl), and depends on format statements etc. the xml element is now documentNode, not item.

  • Property svn:keywords set to Author Date Id Revision
File size: 16.7 KB
RevLine 
[12016]1/*
[36062]2 * FavouriteBasket.java
[12184]3 * Copyright (C) 2006 New Zealand Digital Library, http://www.nzdl.org
[12016]4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19
20package org.greenstone.gsdl3.service;
21
22import java.sql.Statement;
23import java.util.Hashtable;
24import java.util.HashMap;
25import java.util.ArrayList;
26import java.util.HashSet;
27import java.util.Iterator;
[32308]28import java.util.Set;
[12016]29
[28966]30import org.w3c.dom.Document;
[12016]31import org.w3c.dom.Element;
32import org.w3c.dom.NodeList;
33
[22085]34import org.greenstone.util.GlobalProperties;
[12016]35import org.greenstone.gsdl3.util.GSXML;
36import org.greenstone.gsdl3.util.GSPath;
[24993]37import org.greenstone.gsdl3.util.UserContext;
[28966]38import org.greenstone.gsdl3.util.XMLConverter;
[12016]39
[25635]40import java.io.Serializable;
[12016]41import java.net.InetAddress;
42import java.util.Properties;
43import java.util.Date;
44
45import javax.mail.*;
46import javax.mail.internet.*;
47
48import java.awt.event.ActionEvent;
49import java.awt.event.ActionListener;
50import javax.swing.Timer;
51
[13124]52import org.apache.log4j.*;
53
[36062]54public class FavouriteBasket extends ServiceRack
[24977]55{
[12016]56
[36062]57 static Logger logger = Logger.getLogger(org.greenstone.gsdl3.service.FavouriteBasket.class.getName());
[13124]58
[24977]59 // the services on offer
60 // these strings must match what is found in the properties file
[37518]61 protected static final String ADD_ITEM_SERVICE = "AddFavourite";
62 protected static final String DISPLAY_ITEMS_SERVICE = "GetFavouritesList";
63 protected static final String ITEM_NUM_SERVICE = "GetNumFavourites";
64 protected static final String DELETE_ITEMS_SERVICE = "DeleteFavourites";
65 protected static final String SEND_MAIL_SERVICE = "SendFavouritesMail";
[33072]66 //protected static final String DELETE_ITEM_SERVICE = "DeleteItem";
[12016]67
[37518]68 // for AddFavourite
[24977]69 protected static final String ITEM_PARAM = "item";
[37518]70 // for DeleteFavouritess
[32453]71 protected static final String ITEMS_PARAM = "items";
72 // for SendMail
73 protected static final String ADDRESS_PARAM = "address";
74 protected static final String SUBJECT_PARAM = "subject";
75 protected static final String CONTENT_PARAM = "content";
76 protected static final String CC_PARAM = "cc";
77 protected static final String BCC_PARAM = "bcc";
78
79
[24977]80 protected static final String delimiter = "|";
81 protected static final int delay = 1800000;
[12016]82
[25635]83 protected Hashtable<String, Hashtable<String, Hashtable<String, Item>>> userMap = null;
84 protected Hashtable<String, UserTimer> timerMap = null;
[24977]85 protected String username = "";
86 protected String password = "";
[12016]87
[32308]88 HashSet<String> meta_names = null;
89
90 /** constructor */
[36062]91 public FavouriteBasket()
[24977]92 {
[25635]93 userMap = new Hashtable<String, Hashtable<String, Hashtable<String, Item>>>();
94 timerMap = new Hashtable<String, UserTimer>();
[24977]95 }
[12016]96
[25635]97 private Hashtable<String, Hashtable<String, Item>> updateDocMap(Element request)
[24977]98 {
[12016]99
[33084]100 UserContext userContext = new UserContext(request);
101 String id = null;
102 String user_name =userContext.getUsername();
[33220]103 if (user_name == null || user_name.equals("")) {
[33084]104 id = userContext.getUserID();
105 } else {
106 id = user_name;
107 }
[24977]108 if (userMap.containsKey(id))
109 {
110 if (timerMap.containsKey(id))
111 {
[25635]112 UserTimer timer = timerMap.get(id);
[24977]113 timer.restart();
114 }
[25635]115 return userMap.get(id);
[24977]116 }
117 else
118 {
119 UserTimer timer = new UserTimer(delay, id);
120 timerMap.put(id, timer);
121 timer.start();
[25635]122 Hashtable<String, Hashtable<String, Item>> newDocs = new Hashtable<String, Hashtable<String, Item>>();
[24977]123 userMap.put(id, newDocs);
124 return newDocs;
125 }
126 }
[12016]127
[24977]128 /** configure this service */
129 public boolean configure(Element info, Element extra_info)
130 {
[36062]131 logger.info("Configuring FavouriteBasket...");
[24977]132 this.config_info = info;
[12016]133
[24977]134 // set up short_service_info_ - for now just has name and type
[28966]135 Element add_service = this.desc_doc.createElement(GSXML.SERVICE_ELEM);
[24977]136 add_service.setAttribute(GSXML.TYPE_ATT, "gather"); // what??
137 add_service.setAttribute(GSXML.NAME_ATT, ADD_ITEM_SERVICE);
138 this.short_service_info.appendChild(add_service);
[12016]139
[24977]140 // set up short_service_info_ - for now just has name and type
[28966]141 Element disp_service = this.desc_doc.createElement(GSXML.SERVICE_ELEM);
[24977]142 disp_service.setAttribute(GSXML.TYPE_ATT, "gather"); // what??
143 disp_service.setAttribute(GSXML.NAME_ATT, DISPLAY_ITEMS_SERVICE);
144 this.short_service_info.appendChild(disp_service);
[12016]145
[24977]146 // set up short_service_info_ - for now just has name and type
[28966]147 Element num_service = this.desc_doc.createElement(GSXML.SERVICE_ELEM);
[24977]148 num_service.setAttribute(GSXML.TYPE_ATT, "gather"); // what??
149 num_service.setAttribute(GSXML.NAME_ATT, ITEM_NUM_SERVICE);
150 this.short_service_info.appendChild(num_service);
[12016]151
[24977]152 // set up short_service_info_ - for now just has name and type
[28966]153 Element delete_service = this.desc_doc.createElement(GSXML.SERVICE_ELEM);
[24977]154 delete_service.setAttribute(GSXML.TYPE_ATT, "gather"); // what??
155 delete_service.setAttribute(GSXML.NAME_ATT, DELETE_ITEMS_SERVICE);
156 this.short_service_info.appendChild(delete_service);
[12016]157
[24977]158 // set up short_service_info_ - for now just has name and type
[33072]159 // Element deleteone_service = this.desc_doc.createElement(GSXML.SERVICE_ELEM);
160 // deleteone_service.setAttribute(GSXML.TYPE_ATT, "gather"); // what??
161 // deleteone_service.setAttribute(GSXML.NAME_ATT, DELETE_ITEM_SERVICE);
162 // this.short_service_info.appendChild(deleteone_service);
[12016]163
[24977]164 // set up short_service_info_ - for now just has name and type
[28966]165 Element mail_service = this.desc_doc.createElement(GSXML.SERVICE_ELEM);
[24977]166 mail_service.setAttribute(GSXML.TYPE_ATT, "gather"); // what??
167 mail_service.setAttribute(GSXML.NAME_ATT, SEND_MAIL_SERVICE);
168 this.short_service_info.appendChild(mail_service);
[12016]169
[24977]170 return true;
[12016]171
172 }
173
[24977]174 /** returns a specific service description */
[28966]175 protected Element getServiceDescription(Document doc, String service_id, String lang, String subset)
[24977]176 {
[12016]177
[24977]178 if (service_id.equals(ADD_ITEM_SERVICE))
179 {
[28966]180 Element add_service = doc.createElement(GSXML.SERVICE_ELEM);
[24977]181 add_service.setAttribute(GSXML.TYPE_ATT, "gather"); // what??
182 add_service.setAttribute(GSXML.NAME_ATT, ADD_ITEM_SERVICE);
183 return add_service;
184 }
185 if (service_id.equals(DISPLAY_ITEMS_SERVICE))
186 {
[12016]187
[28966]188 Element disp_service = doc.createElement(GSXML.SERVICE_ELEM);
[24977]189 disp_service.setAttribute(GSXML.TYPE_ATT, "gather"); // what??
190 disp_service.setAttribute(GSXML.NAME_ATT, DISPLAY_ITEMS_SERVICE);
191 return disp_service;
192 }
[12016]193
[24977]194 if (service_id.equals(ITEM_NUM_SERVICE))
195 {
[12016]196
[28966]197 Element num_service = doc.createElement(GSXML.SERVICE_ELEM);
[24977]198 num_service.setAttribute(GSXML.TYPE_ATT, "gather"); // what??
199 num_service.setAttribute(GSXML.NAME_ATT, ITEM_NUM_SERVICE);
200 return num_service;
201 }
[12016]202
[24977]203 if (service_id.equals(DELETE_ITEMS_SERVICE))
204 {
[12016]205
[28966]206 Element del_service = doc.createElement(GSXML.SERVICE_ELEM);
[24977]207 del_service.setAttribute(GSXML.TYPE_ATT, "gather"); // what??
208 del_service.setAttribute(GSXML.NAME_ATT, DELETE_ITEMS_SERVICE);
209 return del_service;
210 }
[12016]211
[24977]212 if (service_id.equals(SEND_MAIL_SERVICE))
213 {
214
[28966]215 Element mail_service = doc.createElement(GSXML.SERVICE_ELEM);
[24977]216 mail_service.setAttribute(GSXML.TYPE_ATT, "gather"); // what??
217 mail_service.setAttribute(GSXML.NAME_ATT, SEND_MAIL_SERVICE);
218 return mail_service;
219 }
220
221 return null;
[12016]222 }
223
[37518]224 protected Element processAddFavourite(Element request)
[24977]225 {
[33084]226
[25635]227 Hashtable<String, Hashtable<String, Item>> docsMap = updateDocMap(request);
[12016]228
[24977]229 // Create a new (empty) result message
[28966]230 Document result_doc = XMLConverter.newDOM();
231 Element result = result_doc.createElement(GSXML.RESPONSE_ELEM);
[12016]232
[24977]233 // Get the parameters of the request
234 Element param_list = (Element) GSXML.getChildByTagName(request, GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER);
235 if (param_list == null)
236 {
[37518]237 logger.error("FavouriteBasket Error: AddFavourite request had no paramList.");
[24977]238 return result; // Return the empty result
239 }
[12016]240
[25635]241 HashMap<String, Serializable> params = GSXML.extractParams(param_list, false);
[24977]242
[32453]243 String item = (String) params.get(ITEM_PARAM);
[24977]244 String collection = "";
245 int pos = item.indexOf(":");
246 if (pos != -1)
247 {
248 collection = item.substring(0, pos);
249 item = item.substring(pos + 1);
250 }
251
252 if (docsMap.containsKey(collection))
253 {
[25635]254 Hashtable<String, Item> items = docsMap.get(collection);
[24977]255 if (!items.containsKey(item))
256 {
[37518]257 Item newItem = new Item(collection, item);
[24977]258 items.put(item, newItem);
[28966]259 result.appendChild(newItem.wrapIntoElement(result_doc));
[24977]260 }
261 }
262 else
263 {
[25635]264 Hashtable<String, Item> items = new Hashtable<String, Item>();
[37518]265 Item newItem = new Item(collection, item);
[24977]266 items.put(item, newItem);
267 docsMap.put(collection, items);
[28966]268 result.appendChild(newItem.wrapIntoElement(result_doc));
[24977]269 }
270
271 return result;
[12016]272 }
273
274
[37518]275 protected Element processDeleteFavouritess(Element request)
[24977]276 {
[33084]277
[25635]278 Hashtable<String, Hashtable<String, Item>> docsMap = updateDocMap(request);
[12016]279
[24977]280 // Create a new (empty) result message
[28966]281 Document result_doc = XMLConverter.newDOM();
282 Element result = result_doc.createElement(GSXML.RESPONSE_ELEM);
[24977]283
284 // Get the parameters of the request
285 Element param_list = (Element) GSXML.getChildByTagName(request, GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER);
286
287 if (param_list == null)
288 {
[36062]289 logger.error("FavouriteBasket Error: DeleteItem request had no paramList.");
[24977]290 return result; // Return the empty result
[12016]291 }
[24977]292
[25635]293 HashMap<String, Serializable> params = GSXML.extractParams(param_list, false);
[24977]294
[32453]295 String param = (String) params.get(ITEMS_PARAM);
[24977]296
297 if (param == null)
298 return result;
299
300 String[] items = param.split("\\|");
301
302 for (int i = 0; i < items.length; i++)
303 {
304 String item = items[i];
305 if (item.trim().length() == 0)
306 continue;
307 String collection = "";
308 int pos = item.indexOf(":");
309 if (pos != -1)
310 {
311 collection = item.substring(0, pos);
312 item = item.substring(pos + 1);
313 }
314
315 if (docsMap.containsKey(collection))
316 {
[25635]317 Hashtable itemMap = docsMap.get(collection);
[24977]318 if (itemMap.containsKey(item))
319 {
320 itemMap.remove(item);
321 }
322 if (itemMap.size() == 0)
323 {
324 docsMap.remove(collection);
325 }
326 }
327
[12016]328 }
329
[24977]330 return result;
[12016]331 }
332
333
[37518]334 protected Element processGetNumFavourites(Element request)
[24977]335 {
[25635]336 Hashtable<String, Hashtable<String, Item>> docsMap = updateDocMap(request);
[12016]337
[24977]338 // Create a new (empty) result message
[28966]339 Document result_doc = XMLConverter.newDOM();
340 Element result = result_doc.createElement(GSXML.RESPONSE_ELEM);
[24977]341
342 int size = 0;
343 String ids = "";
[25635]344 Iterator<String> keys = docsMap.keySet().iterator();
[24977]345
346 while (keys.hasNext())
347 {
[25635]348 Hashtable items = docsMap.get(keys.next());
[24977]349 size += items.size();
350 Iterator values = items.values().iterator();
351 while (values.hasNext())
352 {
353 Item item = (Item) values.next();
[28966]354 result.appendChild(item.wrapIntoElement(result_doc));
[24977]355 }
356 }
357
[28966]358 Element selement = result_doc.createElement("size");
[24977]359 selement.setAttribute("value", size + "");
360 result.appendChild(selement);
361
362 return result;
[12016]363 }
364
365
[37518]366 protected Element processGetFavouritesList(Element request)
[24977]367 {
[25635]368 Hashtable<String, Hashtable<String, Item>> docsMap = updateDocMap(request);
[12016]369
[24977]370 // Create a new (empty) result message
[28966]371 Document result_doc = XMLConverter.newDOM();
372 Element result = result_doc.createElement(GSXML.RESPONSE_ELEM);
[12016]373
[25635]374 Iterator<String> keys = docsMap.keySet().iterator();
[12016]375
[24977]376 while (keys.hasNext())
377 {
[25635]378 String collection = keys.next();
379 Hashtable items = docsMap.get(collection);
[24977]380 Iterator itemItr = items.values().iterator();
[12016]381
[36064]382 Element collectionNode = result_doc.createElement("favouriteList");
[24977]383 collectionNode.setAttribute("name", collection);
384 result.appendChild(collectionNode);
[12016]385
[24977]386 while (itemItr.hasNext())
387 {
388 Item item = (Item) itemItr.next();
[32308]389 collectionNode.appendChild(item.wrapIntoElement(result_doc));
[24977]390 }
391 }
[12016]392
[24977]393 return result;
394 }
395
[32308]396
397
398
[37518]399 public Element processSendFavouritesMail(Element request)
[24977]400 {
[19984]401 // Create a new (empty) result message
[28966]402 Document result_doc = XMLConverter.newDOM();
403 Element result = result_doc.createElement(GSXML.RESPONSE_ELEM);
[24977]404
[19984]405 // Get the parameters of the request
[24977]406 Element param_list = (Element) GSXML.getChildByTagName(request, GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER);
407
408 if (param_list == null)
409 {
[36062]410 logger.error("FavouriteBasket Error: SendMail request had no paramList.");
[24977]411 return result; // Return the empty result
[19984]412 }
[24977]413
[25635]414 HashMap<String, Serializable> params = GSXML.extractParams(param_list, false);
[24977]415
[32453]416 String to = (String) params.get(ADDRESS_PARAM);
417 String subject = (String) params.get(SUBJECT_PARAM);
418 String content = (String) params.get(CONTENT_PARAM);
419 String cc = (String) params.get(CC_PARAM);
420 String bcc = (String) params.get(BCC_PARAM);
[24977]421
[19984]422 String mailhost = GlobalProperties.getProperty("mail.smtp.host");
423 username = GlobalProperties.getProperty("mail.smtp.username");
424 password = GlobalProperties.getProperty("mail.smtp.password");
425 String from = GlobalProperties.getProperty("mail.from");
[31729]426 String port = GlobalProperties.getProperty("mail.smtp.port");
427 String mail_security = GlobalProperties.getProperty("mail.security");
[19984]428 String mailer = "msgsend";
[24977]429
430 try
431 {
432
[19984]433 Properties props = System.getProperties();
[24977]434
[19984]435 //Setup smtp host and from address
436 // XXX - could use Session.getTransport() and Transport.connect()
437 // XXX - assume we're using SMTP
[24977]438 if (mailhost != null && !mailhost.trim().equals(""))
439 {
[19984]440 props.put("mail.smtp.host", mailhost);
[24977]441 }
442 else
443 {
[19984]444 props.put("mail.smtp.host", "localhost");
445 }
[24977]446 if (from != null && !from.trim().equals(""))
447 {
[19984]448 props.put("mail.from", from);
449 }
[24977]450
[31729]451
452 if (port != null && !port.trim().equals("")) {
453 props.put("mail.smtp.port", port);
454 }
455
456 if (mail_security != null) {
457 mail_security = mail_security.trim();
458 if (mail_security.equals("ssl")) {
459 props.put("mail.smtp.ssl.enable", "true");
460 } else if (mail_security.equals("tls")) {
461 props.put("mail.smtp.starttls.enable", "true");
462 }
463 else if (mail_security.equals("")) {
464 logger.error("unknown security protocol "+mail_security +", should be ssl or tls");
465 }
466 }
467
468 // this doesn't seem to matter having this when
469 // username and password are empty
470 props.put("mail.smtp.auth", "true");
471
[19984]472 //setup username and password to the smtp server
[31729]473
[24977]474 if (username == null || username.trim().equals(""))
475 username = "";
476 if (password == null || password.trim().equals(""))
477 password = "";
[31729]478
[24977]479 Authenticator auth = new Authenticator()
480 {
481 protected PasswordAuthentication getPasswordAuthentication()
482 {
483 return new PasswordAuthentication(username, password);
[19984]484 }
485 };
[24977]486
[19984]487 Session session = Session.getInstance(props, auth);
[24977]488
[19984]489 Message msg = new MimeMessage(session);
490 msg.setFrom();
[24977]491 msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to, false));
492 if (cc != null)
493 {
494 msg.setRecipients(Message.RecipientType.CC, InternetAddress.parse(cc, false));
[19984]495 }
[24977]496 if (bcc != null)
497 {
498 msg.setRecipients(Message.RecipientType.BCC, InternetAddress.parse(bcc, false));
[19984]499 }
500 msg.setSubject(subject);
[24977]501 msg.setText(content.replaceAll("-------", "&"));
[19984]502 msg.setHeader("X-Mailer", mailer);
503 msg.setSentDate(new Date());
[24977]504
[19984]505 // send the thing off
506 Transport.send(msg);
[24977]507
[19984]508 logger.info("\nMail was sent successfully.");
[28966]509 result.appendChild(result_doc.createTextNode("Mail was sent successfully."));
[24977]510 }
511 catch (Exception e)
512 {
[31729]513 logger.error("Error sending mail!");
514 e.printStackTrace();
515 result.appendChild(result_doc.createTextNode(e.getMessage()));
[19984]516 }
[24977]517
[19984]518 return result;
[12016]519 }
520
[24977]521 protected class Item
522 {
[19984]523 public String collection;
524 public String docid;
[32308]525
526 public Item(String coll, String id)
[24977]527 {
[19984]528 this.collection = coll;
529 this.docid = id;
530 }
[12016]531
[24977]532 public boolean equals(Object o)
533 {
534 if (!(o instanceof Item))
535 {
536 return false;
537 }
538 Item item = (Item) o;
539 String id = collection + ":" + docid;
540 String idin = item.collection + ":" + item.docid;
541 return id.equals(idin);
[12016]542
[24977]543 }
[12016]544
[28966]545 public Element wrapIntoElement(Document doc)
[24977]546 {
[37518]547 Element itemElement = doc.createElement("documentNode");
548 itemElement.setAttribute("nodeID", docid);
[24977]549 itemElement.setAttribute("collection", collection);
[32308]550
[24977]551 return itemElement;
552 }
[12016]553 }
554
[24977]555 private class UserTimer extends Timer implements ActionListener
556 {
557 String id = "";
[12016]558
[24977]559 public UserTimer(int delay, String id)
560 {
561 super(delay, (ActionListener) null);
562 addActionListener(this);
563 this.id = id;
564 }
[12016]565
[24977]566 public void actionPerformed(ActionEvent e)
567 {
568 userMap.remove(id);
569 timerMap.remove(id);
570 stop();
571 }
572
[12016]573 }
[24977]574
[12016]575}
Note: See TracBrowser for help on using the repository browser.