source: greenstone3/trunk/src/java/org/greenstone/gsdl3/service/BerryBasket.java@ 19819

Last change on this file since 19819 was 13270, checked in by shaoqun, 18 years ago

replace Category class which is deprecated with Logger class

  • Property svn:keywords set to Author Date Id Revision
File size: 19.9 KB
Line 
1/*
2 * BerryBasket.java
3 * Copyright (C) 2006 New Zealand Digital Library, http://www.nzdl.org
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;
28
29import org.w3c.dom.Element;
30import org.w3c.dom.NodeList;
31
32import org.greenstone.gsdl3.util.GSXML;
33import org.greenstone.gsdl3.util.GSPath;
34
35import java.net.InetAddress;
36import java.util.Properties;
37import java.util.Date;
38
39import javax.mail.*;
40import javax.mail.internet.*;
41
42import java.awt.event.ActionEvent;
43import java.awt.event.ActionListener;
44import javax.swing.Timer;
45
46import org.apache.log4j.*;
47
48public class BerryBasket
49 extends ServiceRack {
50
51 static Logger logger = Logger.getLogger(org.greenstone.gsdl3.service.BerryBasket.class.getName());
52
53 // the services on offer
54 // these strings must match what is found in the properties file
55 protected static final String ADD_ITEM_SERVICE = "AddItem";
56 protected static final String DISPLAY_ITEMS_SERVICE = "DisplayList";
57 protected static final String ITEM_NUM_SERVICE = "ItemNum";
58 protected static final String DELETE_ITEMS_SERVICE = "DeleteItems";
59 protected static final String SEND_MAIL_SERVICE = "SendMail";
60 protected static final String DELETE_ITEM_SERVICE = "DeleteItem";
61
62 protected static final String ITEM_PARAM = "item";
63 protected static final String delimiter ="|";
64 protected static final int delay = 1800000;
65
66 protected Hashtable userMap = null;
67 protected Hashtable timerMap = null;
68 protected String username="";
69 protected String password="";
70
71
72 /** constructor */
73 public BerryBasket()
74 {
75 userMap = new Hashtable();
76 timerMap = new Hashtable();
77 }
78
79 private Hashtable updateDocMap(Element request){
80
81 String id = request.getAttribute("uid");
82
83 if (userMap.containsKey(id)){
84 if (timerMap.containsKey(id)){
85 UserTimer timer = (UserTimer)timerMap.get(id);
86 timer.restart();
87 }
88 return (Hashtable) userMap.get(id);
89 }
90 else{
91 UserTimer timer = new UserTimer(delay,id);
92 timerMap.put(id,timer);
93 timer.start();
94 Hashtable newDocs= new Hashtable();
95 userMap.put(id,newDocs);
96 return newDocs;
97 }
98 }
99
100
101 /** configure this service */
102 public boolean configure(Element info, Element extra_info)
103 {
104 logger.info("Configuring BerryBasket...");
105 this.config_info = info;
106
107 // set up short_service_info_ - for now just has name and type
108 Element add_service = this.doc.createElement(GSXML.SERVICE_ELEM);
109 add_service.setAttribute(GSXML.TYPE_ATT, "gather"); // what??
110 add_service.setAttribute(GSXML.NAME_ATT, ADD_ITEM_SERVICE);
111 this.short_service_info.appendChild(add_service);
112
113
114 // set up short_service_info_ - for now just has name and type
115 Element disp_service = this.doc.createElement(GSXML.SERVICE_ELEM);
116 disp_service.setAttribute(GSXML.TYPE_ATT, "gather"); // what??
117 disp_service.setAttribute(GSXML.NAME_ATT, DISPLAY_ITEMS_SERVICE);
118 this.short_service_info.appendChild(disp_service);
119
120 // set up short_service_info_ - for now just has name and type
121 Element num_service = this.doc.createElement(GSXML.SERVICE_ELEM);
122 num_service.setAttribute(GSXML.TYPE_ATT, "gather"); // what??
123 num_service.setAttribute(GSXML.NAME_ATT, ITEM_NUM_SERVICE);
124 this.short_service_info.appendChild(num_service);
125
126 // set up short_service_info_ - for now just has name and type
127 Element delete_service = this.doc.createElement(GSXML.SERVICE_ELEM);
128 delete_service.setAttribute(GSXML.TYPE_ATT, "gather"); // what??
129 delete_service.setAttribute(GSXML.NAME_ATT, DELETE_ITEMS_SERVICE);
130 this.short_service_info.appendChild(delete_service);
131
132 // set up short_service_info_ - for now just has name and type
133 Element deleteone_service = this.doc.createElement(GSXML.SERVICE_ELEM);
134 deleteone_service.setAttribute(GSXML.TYPE_ATT, "gather"); // what??
135 deleteone_service.setAttribute(GSXML.NAME_ATT, DELETE_ITEM_SERVICE);
136 this.short_service_info.appendChild(deleteone_service);
137
138 // set up short_service_info_ - for now just has name and type
139 Element mail_service = this.doc.createElement(GSXML.SERVICE_ELEM);
140 mail_service.setAttribute(GSXML.TYPE_ATT, "gather"); // what??
141 mail_service.setAttribute(GSXML.NAME_ATT, SEND_MAIL_SERVICE);
142 this.short_service_info.appendChild(mail_service);
143
144
145 return true;
146
147 }
148
149 /** returns a specific service description */
150 protected Element getServiceDescription(String service_id, String lang, String subset) {
151
152 if (service_id.equals(ADD_ITEM_SERVICE)) {
153 Element add_service = this.doc.createElement(GSXML.SERVICE_ELEM);
154 add_service.setAttribute(GSXML.TYPE_ATT, "gather"); // what??
155 add_service.setAttribute(GSXML.NAME_ATT, ADD_ITEM_SERVICE);
156 return add_service;
157 }
158 if (service_id.equals(DISPLAY_ITEMS_SERVICE)) {
159
160 Element disp_service = this.doc.createElement(GSXML.SERVICE_ELEM);
161 disp_service.setAttribute(GSXML.TYPE_ATT, "gather"); // what??
162 disp_service.setAttribute(GSXML.NAME_ATT, DISPLAY_ITEMS_SERVICE);
163 return disp_service;
164 }
165
166 if (service_id.equals(ITEM_NUM_SERVICE)) {
167
168 Element num_service = this.doc.createElement(GSXML.SERVICE_ELEM);
169 num_service.setAttribute(GSXML.TYPE_ATT, "gather"); // what??
170 num_service.setAttribute(GSXML.NAME_ATT, ITEM_NUM_SERVICE);
171 return num_service;
172 }
173
174 if (service_id.equals(DELETE_ITEMS_SERVICE)) {
175
176 Element del_service = this.doc.createElement(GSXML.SERVICE_ELEM);
177 del_service.setAttribute(GSXML.TYPE_ATT, "gather"); // what??
178 del_service.setAttribute(GSXML.NAME_ATT, DELETE_ITEMS_SERVICE);
179 return del_service;
180 }
181
182 if (service_id.equals(DELETE_ITEM_SERVICE)) {
183
184 Element delone_service = this.doc.createElement(GSXML.SERVICE_ELEM);
185 delone_service.setAttribute(GSXML.TYPE_ATT, "gather"); // what??
186 delone_service.setAttribute(GSXML.NAME_ATT, DELETE_ITEM_SERVICE);
187 return delone_service;
188 }
189
190 if (service_id.equals(SEND_MAIL_SERVICE)) {
191
192 Element mail_service = this.doc.createElement(GSXML.SERVICE_ELEM);
193 mail_service.setAttribute(GSXML.TYPE_ATT, "gather"); // what??
194 mail_service.setAttribute(GSXML.NAME_ATT, SEND_MAIL_SERVICE);
195 return mail_service;
196 }
197
198 return null;
199 }
200
201 protected Element processAddItem(Element request) {
202 Hashtable docsMap = updateDocMap(request);
203
204 // Create a new (empty) result message
205 Element result = this.doc.createElement(GSXML.RESPONSE_ELEM);
206
207 // Get the parameters of the request
208 Element param_list = (Element) GSXML.getChildByTagName(request, GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
209 if (param_list == null) {
210 logger.error("BerryBasket Error: AddItem request had no paramList.");
211 return result; // Return the empty result
212 }
213
214 HashMap params = GSXML.extractParams(param_list, false);
215
216 String item = (String)params.get("item");
217 String collection = "";
218 int pos = item.indexOf(":");
219 if (pos != -1) {
220 collection = item.substring(0,pos);
221 item = item.substring(pos+1);
222 }
223
224 if (docsMap.containsKey(collection)){
225 Hashtable items = (Hashtable) docsMap.get(collection);
226 if (!items.containsKey(item)){
227 Item newItem = generateItem(collection,item);
228 items.put(item, newItem);
229 result.appendChild(newItem.wrapIntoElement());
230 }
231 }
232 else{
233 Hashtable items = new Hashtable();
234 Item newItem = generateItem(collection,item);
235 items.put(item, newItem);
236 docsMap.put(collection,items);
237 result.appendChild(newItem.wrapIntoElement());
238 }
239
240 return result;
241 }
242
243 private Item generateItem(String collection, String id){
244
245 Item item = new Item(collection, id);
246 String to = GSPath.appendLink(collection, "DocumentMetadataRetrieve");
247 ArrayList tmp = new ArrayList();
248 tmp.add(id);
249 Element response = getDocumentMetadata(to,"en","dumy", tmp.iterator());
250 Element doc_node = (Element)response.getElementsByTagName(GSXML.DOC_NODE_ELEM).item(0);
251
252 String node_id = doc_node.getAttribute(GSXML.NODE_ID_ATT);
253 Element metadata_list = (Element)doc_node.getElementsByTagName(GSXML.METADATA_ELEM+GSXML.LIST_MODIFIER).item(0);
254
255 //assign title metadata if any
256 Element metadata = GSXML.getNamedElement(metadata_list,"metadata","name","Title");
257 if (metadata != null){
258 item.title = GSXML.getNodeText(metadata).trim();
259 }
260 //assign date metadata if any
261 metadata = GSXML.getNamedElement(metadata_list,"metadata","name","Date");
262 if (metadata != null){
263 item.date = GSXML.getNodeText(metadata).trim();
264 }
265
266 //assign root title metadata if any
267 metadata = GSXML.getNamedElement(metadata_list,"metadata","name","root_Title");
268 if (metadata != null){
269 String rootTitle = GSXML.getNodeText(metadata).trim();
270 if (!rootTitle.equals(item.title)){
271 item.rootTitle = rootTitle;
272 }
273
274 }
275
276 return item;
277 }
278
279
280 protected Element processDeleteItems(Element request) {
281 Hashtable docsMap = updateDocMap(request);
282
283 // Create a new (empty) result message
284 Element result = this.doc.createElement(GSXML.RESPONSE_ELEM);
285
286 // Get the parameters of the request
287 Element param_list = (Element) GSXML.getChildByTagName(request, GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
288
289 //GSXML.printXMLNode(param_list);
290
291 if (param_list == null) {
292 logger.error("BerryBasket Error: DeleteItem request had no paramList.");
293 return result; // Return the empty result
294 }
295
296 HashMap params = GSXML.extractParams(param_list, false);
297
298 String param = (String)params.get("items");
299
300 if (param == null) return result;
301
302 String[] items = param.split("\\|");
303
304 for (int i=0; i<items.length; i++){
305 String item = items[i];
306 if (item.trim().length() == 0) continue;
307 String collection = "";
308 int pos = item.indexOf(":");
309 if (pos != -1) {
310 collection = item.substring(0,pos);
311 item = item.substring(pos+1);
312 }
313
314 if (docsMap.containsKey(collection)){
315 Hashtable itemMap = (Hashtable) docsMap.get(collection);
316 if (itemMap.containsKey(item)){
317 itemMap.remove(item);
318 }
319 if (itemMap.size()==0){
320 docsMap.remove(collection);
321 }
322 }
323
324 }
325
326 return result;
327 }
328
329 protected Element processDeleteItem(Element request) {
330 Hashtable docsMap = updateDocMap(request);
331
332 // Create a new (empty) result message
333 Element result = this.doc.createElement(GSXML.RESPONSE_ELEM);
334
335 // Get the parameters of the request
336 Element param_list = (Element) GSXML.getChildByTagName(request, GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
337
338 //GSXML.printXMLNode(param_list);
339
340 if (param_list == null) {
341 logger.error("BerryBasket Error: DeleteItem request had no paramList.");
342 return result; // Return the empty result
343 }
344
345 HashMap params = GSXML.extractParams(param_list, false);
346
347 String param = (String)params.get("item");
348
349 if (param == null) return result;
350
351 String item = param;
352
353 String collection = "";
354 int pos = item.indexOf(":");
355
356 if (pos != -1) {
357 collection = item.substring(0,pos);
358 item = item.substring(pos+1);
359 }
360
361 if (docsMap.containsKey(collection)){
362 Hashtable itemMap = (Hashtable) docsMap.get(collection);
363 if (itemMap.containsKey(item)){
364 itemMap.remove(item);
365 }
366 if (itemMap.size()==0){
367 docsMap.remove(collection);
368 }
369 }
370
371 return result;
372 }
373
374
375 protected Element processItemNum(Element request){
376 // GSXML.printXMLNode(request);
377 Hashtable docsMap = updateDocMap(request);
378
379 // Create a new (empty) result message
380 Element result = this.doc.createElement(GSXML.RESPONSE_ELEM);
381
382 int size = 0;
383 String ids ="";
384 Iterator keys = docsMap.keySet().iterator();
385
386 while(keys.hasNext()){
387 Hashtable items = (Hashtable) docsMap.get((String)keys.next());
388 size+=items.size();
389 Iterator values = items.values().iterator();
390 while(values.hasNext()){
391 Item item = (Item)values.next();
392 result.appendChild(item.wrapIntoElement());
393 }
394 }
395
396 Element selement = this.doc.createElement("size");
397 selement.setAttribute("value",size+"");
398 result.appendChild(selement);
399
400 return result;
401 }
402
403
404 private Element getDocumentMetadata(String to, String lang, String uid, Iterator ids){
405
406 // Build a request to obtain some document metadata
407 Element dm_message = this.doc.createElement(GSXML.MESSAGE_ELEM);
408 Element dm_request = GSXML.createBasicRequest(this.doc, GSXML.REQUEST_TYPE_PROCESS, to, lang, uid);
409 dm_message.appendChild(dm_request);
410
411 // Create a parameter list to specify the required metadata information
412 HashSet meta_names = new HashSet();
413 meta_names.add("Title"); // the default
414 meta_names.add("root_Title");
415 meta_names.add("Date");
416
417
418 Element param_list = this.doc.createElement(GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
419
420 Element param = null;
421 Iterator i = meta_names.iterator();
422 while (i.hasNext()) {
423 String name = (String)i.next();
424 param = this.doc.createElement(GSXML.PARAM_ELEM);
425 param_list.appendChild(param);
426 param.setAttribute(GSXML.NAME_ATT, "metadata");
427 param.setAttribute(GSXML.VALUE_ATT, name);
428
429 }
430
431 dm_request.appendChild(param_list);
432
433 // create the doc node list for the metadata request
434 Element dm_doc_list = this.doc.createElement(GSXML.DOC_NODE_ELEM+GSXML.LIST_MODIFIER);
435 dm_request.appendChild(dm_doc_list);
436
437 while (ids.hasNext()){
438 // Add the documentNode to the list
439 Element dm_doc_node = this.doc.createElement(GSXML.DOC_NODE_ELEM);
440 dm_doc_list.appendChild(dm_doc_node);
441 dm_doc_node.setAttribute(GSXML.NODE_ID_ATT,(String)ids.next());
442 }
443
444 return (Element) this.router.process(dm_message);
445
446 }
447
448
449 protected Element processDisplayList(Element request) {
450 // Create a new (empty) result message
451 Hashtable docsMap = updateDocMap(request);
452
453 // Create a new (empty) result message
454 Element result = this.doc.createElement(GSXML.RESPONSE_ELEM);
455
456 Iterator keys = docsMap.keySet().iterator();
457
458 while(keys.hasNext()){
459 String collection = (String)keys.next();
460 Hashtable items = (Hashtable)docsMap.get(collection);
461 Iterator itemItr = items.values().iterator();
462
463 Element collectionNode = this.doc.createElement("collection");
464 collectionNode.setAttribute("name",collection);
465 result.appendChild(collectionNode);
466
467 while(itemItr.hasNext()){
468 Item item = (Item)itemItr.next();
469 Element itemElement = this.doc.createElement("item");
470
471 collectionNode.appendChild(itemElement);
472 itemElement.setAttribute("name", item.docid);
473 itemElement.setAttribute("collection", item.collection);
474 itemElement.setAttribute("title",item.title);
475 itemElement.setAttribute("date",item.date);
476 itemElement.setAttribute("root_title",item.rootTitle);
477 }
478 }
479
480 return result;
481
482 }
483
484
485 public Element processSendMail(Element request){
486 // Create a new (empty) result message
487 Element result = this.doc.createElement(GSXML.RESPONSE_ELEM);
488
489 // Get the parameters of the request
490 Element param_list = (Element) GSXML.getChildByTagName(request, GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
491
492 GSXML.printXMLNode(param_list);
493
494 if (param_list == null) {
495 logger.error("BerryBasket Error: SendMail request had no paramList.");
496 return result; // Return the empty result
497 }
498
499 HashMap params = GSXML.extractParams(param_list, false);
500
501 String to = (String)params.get("address");
502 String subject = (String)params.get("subject");
503 String mailhost = (String)params.get("host");
504 String content = (String)params.get("content");
505 String cc = (String)params.get("cc");
506 String bcc = (String)params.get("bcc");
507 username = (String)params.get("user");
508 password = (String)params.get("password");
509 String mailer = "msgsend";
510
511 try {
512
513 Properties props = System.getProperties();
514 // XXX - could use Session.getTransport() and Transport.connect()
515 // XXX - assume we're using SMTP
516 // TODO: need to get Mail server info from the configuration file
517 if (mailhost != null && mailhost.trim().equals(""))
518 props.put("mail.smtp.host", mailhost);
519 else{
520 props.put("mail.smtp.host", "webmail.cs.waikato.ac.nz");
521 }
522
523 // TODO: need to get account info from the configuration file
524 Authenticator auth = new Authenticator(){
525 protected PasswordAuthentication getPasswordAuthentication(){
526 if (username == null || username.equals("")){
527 username = "xxxxx";
528 password = "xxxxx";
529 }
530 return new PasswordAuthentication(username,password);
531 }
532 };
533
534 Session session = Session.getInstance(props, auth);
535
536 Message msg = new MimeMessage(session);
537 msg.setFrom();
538
539 msg.setRecipients(Message.RecipientType.TO,
540 InternetAddress.parse(to, false));
541 if (cc != null)
542 msg.setRecipients(Message.RecipientType.CC,
543 InternetAddress.parse(cc, false));
544 if (bcc != null)
545 msg.setRecipients(Message.RecipientType.BCC,
546 InternetAddress.parse(bcc, false));
547
548 msg.setSubject(subject);
549
550 msg.setText(content.replaceAll("-------","&"));
551 msg.setHeader("X-Mailer", mailer);
552 msg.setSentDate(new Date());
553
554 // send the thing off
555 Transport.send(msg);
556
557 logger.info("\nMail was sent successfully.");
558 result.appendChild(this.doc.createTextNode("Mail was sent successfully."));
559 } catch (Exception e) {
560 e.printStackTrace();
561 result.appendChild(this.doc.createTextNode(e.getMessage()));
562 }
563
564 return result;
565 }
566
567 protected class Item {
568
569 public String collection;
570 public String docid;
571 public String title="";
572 public String query="";
573 public String date="";
574 public String rootTitle="";
575
576 public Item(String coll, String id) {
577 this.collection = coll;
578 this.docid = id;
579 }
580
581 public boolean equals(Object o){
582 if (! (o instanceof Item)){
583 return false;
584 }
585 Item item = (Item)o;
586 String id = collection+":"+docid;
587 String idin = item.collection+":"+item.docid;
588 return id.equals(idin);
589
590 }
591
592 public String toString(){
593
594 return collection+":"+docid+":"+"["+((!rootTitle.equals(""))?(rootTitle+":"):"")+title+"]";
595 }
596
597 public Element wrapIntoElement(){
598 Element itemElement = doc.createElement("item");
599 itemElement.setAttribute("name", docid);
600 itemElement.setAttribute("collection", collection);
601 itemElement.setAttribute("title",title);
602 itemElement.setAttribute("date",date);
603 itemElement.setAttribute("root_title",rootTitle);
604 return itemElement;
605 }
606 }
607 private class UserTimer extends Timer implements ActionListener{
608 String id="";
609
610 public UserTimer(int delay, String id){
611 super(delay,(ActionListener)null);
612 addActionListener(this);
613 this.id=id;
614 }
615
616
617 public void actionPerformed(ActionEvent e){
618 userMap.remove(id);
619 timerMap.remove(id);
620 stop();
621 }
622
623 }
624
625}
Note: See TracBrowser for help on using the repository browser.