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