0001: /**
0002: * LibreSource
0003: * Copyright (C) 2004-2008 Artenum SARL / INRIA
0004: * http://www.libresource.org - contact@artenum.com
0005: *
0006: * This file is part of the LibreSource software,
0007: * which can be used and distributed under license conditions.
0008: * The license conditions are provided in the LICENSE.TXT file
0009: * at the root path of the packaging that enclose this file.
0010: * More information can be found at
0011: * - http://dev.libresource.org/home/license
0012: *
0013: * Initial authors :
0014: *
0015: * Guillaume Bort / INRIA
0016: * Francois Charoy / Universite Nancy 2
0017: * Julien Forest / Artenum
0018: * Claude Godart / Universite Henry Poincare
0019: * Florent Jouille / INRIA
0020: * Sebastien Jourdain / INRIA / Artenum
0021: * Yves Lerumeur / Artenum
0022: * Pascal Molli / Universite Henry Poincare
0023: * Gerald Oster / INRIA
0024: * Mariarosa Penzi / Artenum
0025: * Gerard Sookahet / Artenum
0026: * Raphael Tani / INRIA
0027: *
0028: * Contributors :
0029: *
0030: * Stephane Bagnier / Artenum
0031: * Amadou Dia / Artenum-IUP Blois
0032: * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
0033: */package org.libresource.mailing.ejb;
0034:
0035: import org.libresource.Libresource;
0036: import org.libresource.LibresourceEvent;
0037: import org.libresource.LibresourceException;
0038: import org.libresource.LibresourceResourceIdentifier;
0039: import org.libresource.LibresourceResourceValue;
0040: import org.libresource.LibresourceServiceBase;
0041:
0042: import org.libresource.core.FileData;
0043: import org.libresource.core.interfaces.LibresourceCoreService;
0044:
0045: import org.libresource.files.util.FileResourceUtil;
0046:
0047: import org.libresource.forum.ForumConstants;
0048: import org.libresource.forum.LibresourceForumException;
0049: import org.libresource.forum.ejb.model.MessageResourceValue;
0050: import org.libresource.forum.interfaces.LibresourceForumService;
0051:
0052: import org.libresource.kernel.KernelConstants;
0053: import org.libresource.kernel.LibresourceSecurityException;
0054: import org.libresource.kernel.URINotExistException;
0055: import org.libresource.kernel.interfaces.KernelService;
0056:
0057: import org.libresource.mailing.LibresourceMailingAlreadyExistException;
0058: import org.libresource.mailing.LibresourceMailingException;
0059: import org.libresource.mailing.MailingListConstant;
0060: import org.libresource.mailing.MailingListExportHandler;
0061: import org.libresource.mailing.MailingListImportHandler;
0062: import org.libresource.mailing.ejb.model.MailingListResourceValue;
0063: import org.libresource.mailing.interfaces.MailingListResourceLocal;
0064: import org.libresource.mailing.util.MailingListResourceUtil;
0065:
0066: import org.libresource.membership.MembershipConstants;
0067: import org.libresource.membership.ejb.model.GroupResourceValue;
0068: import org.libresource.membership.ejb.model.ProfileResourceValue;
0069: import org.libresource.membership.interfaces.MembershipService;
0070:
0071: import org.libresource.search.LibresourceIndexableContent;
0072:
0073: import org.libresource.xml.LibresourceExportHandler;
0074: import org.libresource.xml.LibresourceImportHandler;
0075:
0076: import java.io.File;
0077:
0078: import java.net.URI;
0079:
0080: import java.sql.Connection;
0081: import java.sql.ResultSet;
0082: import java.sql.Statement;
0083:
0084: import java.text.DateFormat;
0085:
0086: import java.util.Collection;
0087: import java.util.Date;
0088: import java.util.Iterator;
0089: import java.util.Locale;
0090: import java.util.StringTokenizer;
0091: import java.util.Vector;
0092:
0093: import javax.activation.DataHandler;
0094: import javax.activation.DataSource;
0095: import javax.activation.FileDataSource;
0096: import javax.activation.MimetypesFileTypeMap;
0097:
0098: import javax.mail.BodyPart;
0099: import javax.mail.Message;
0100: import javax.mail.MessagingException;
0101: import javax.mail.Multipart;
0102: import javax.mail.Session;
0103: import javax.mail.Transport;
0104: import javax.mail.internet.InternetAddress;
0105: import javax.mail.internet.MimeBodyPart;
0106: import javax.mail.internet.MimeMessage;
0107: import javax.mail.internet.MimeMultipart;
0108:
0109: import javax.naming.InitialContext;
0110:
0111: import javax.rmi.PortableRemoteObject;
0112:
0113: /**
0114: * The Libresource Mailing service
0115: *
0116: * @libresource.service name="LibresourceMailing" depends="Kernel,Membership"
0117: *
0118: * @ejb.env-entry name="mail-suffix" value="libresource.loria.fr"
0119: */
0120: public abstract class LibresourceMailingServiceBean extends
0121: LibresourceServiceBase {
0122: private transient String mailSuffix;
0123: private transient File fileSystemBase;
0124: private transient javax.sql.DataSource datasource;
0125: private transient Session session;
0126:
0127: /**
0128: * @ejb.interface-method
0129: * @ejb.transaction type="Supports"
0130: */
0131: public String getMailSuffix() throws LibresourceMailingException {
0132: try {
0133: if (mailSuffix == null) {
0134: mailSuffix = (String) new InitialContext()
0135: .lookup("java:comp/env/mail-suffix");
0136: }
0137:
0138: if (mailSuffix == null) {
0139: mailSuffix = (String) new InitialContext()
0140: .lookup("java:comp/env/uri-host");
0141: }
0142:
0143: return mailSuffix;
0144: } catch (Exception e) {
0145: throw new LibresourceMailingException(
0146: "Error in getMailSuffix : " + e.getMessage(), e);
0147: }
0148: }
0149:
0150: // standart methods for resources management
0151:
0152: /**
0153: * @ejb.interface-method
0154: * @ejb.transaction type="Required"
0155: */
0156: public void createMailingList(URI uri, String mail,
0157: String description, String subscribers)
0158: throws LibresourceMailingException,
0159: LibresourceMailingAlreadyExistException,
0160: LibresourceSecurityException {
0161: try {
0162: if (mailingListAlreadyExists(mail, uri)) {
0163: throw new LibresourceMailingAlreadyExistException(
0164: "Mailing list name is already use");
0165: }
0166:
0167: KernelService kernelService = (KernelService) Libresource
0168: .getService(KernelConstants.SERVICE);
0169:
0170: if (!kernelService.checkSecurity(uri,
0171: KernelConstants.SECURITY_CREATE)) {
0172: throw new LibresourceSecurityException(uri,
0173: KernelConstants.SECURITY_CREATE);
0174: }
0175:
0176: MailingListResourceLocal mailingListResourceLocal = MailingListResourceUtil
0177: .getLocalHome().create(mail, description,
0178: subscribers);
0179: kernelService.bind(mailingListResourceLocal
0180: .getLibresourceResourceIdentifier(), uri, mail);
0181:
0182: // event
0183: LibresourceEvent event = new LibresourceEvent(uri,
0184: mailingListResourceLocal
0185: .getLibresourceResourceIdentifier(),
0186: kernelService.getConnectedResource(),
0187: MailingListConstant.EVENT_MAILINGLIST_CREATE);
0188: Libresource.throwEvent(event);
0189: } catch (LibresourceSecurityException se) {
0190: ctx.setRollbackOnly();
0191: throw se;
0192: } catch (LibresourceMailingAlreadyExistException e) {
0193: ctx.setRollbackOnly();
0194: throw e;
0195: } catch (Exception e) {
0196: ctx.setRollbackOnly();
0197: throw new LibresourceMailingException(
0198: "Error in createMailingList : " + e.getMessage(), e);
0199: }
0200: }
0201:
0202: /**
0203: * @ejb.interface-method
0204: * @ejb.transaction type="Required"
0205: */
0206: public void editMailingList(URI uri, String mail, String description)
0207: throws LibresourceMailingException,
0208: LibresourceMailingAlreadyExistException,
0209: LibresourceSecurityException, URINotExistException {
0210: try {
0211: if (mailingListAlreadyExists(mail, uri)) {
0212: throw new LibresourceMailingAlreadyExistException(
0213: "Mailing list name is already use");
0214: }
0215:
0216: KernelService kernelService = (KernelService) Libresource
0217: .getService(KernelConstants.SERVICE);
0218: LibresourceResourceIdentifier resourceIdentifier = kernelService
0219: .lookup(uri);
0220: MailingListResourceLocal mailingListResourceLocal = (MailingListResourceLocal) Libresource
0221: .findResource(resourceIdentifier,
0222: MailingListResourceLocal.class);
0223:
0224: if (!kernelService.checkSecurity(uri,
0225: KernelConstants.SECURITY_UPDATE)) {
0226: throw new LibresourceSecurityException(uri,
0227: KernelConstants.SECURITY_UPDATE);
0228: }
0229:
0230: mailingListResourceLocal.setDescription(description);
0231: mailingListResourceLocal.setMail(mail);
0232:
0233: // update node
0234: kernelService.setShortName(uri, mail);
0235: kernelService.setUpdateDate(uri, new Date());
0236:
0237: // event
0238: LibresourceEvent event = new LibresourceEvent(uri,
0239: resourceIdentifier, kernelService
0240: .getConnectedResource(),
0241: MailingListConstant.EVENT_MAILINGLIST_EDIT);
0242: Libresource.throwEvent(event);
0243:
0244: // indexation
0245: Libresource.index(uri);
0246: } catch (LibresourceSecurityException se) {
0247: ctx.setRollbackOnly();
0248: throw se;
0249: } catch (URINotExistException se) {
0250: ctx.setRollbackOnly();
0251: throw se;
0252: } catch (Exception e) {
0253: ctx.setRollbackOnly();
0254: throw new LibresourceMailingException(
0255: "Error in editMailingList : " + e.getMessage(), e);
0256: }
0257: }
0258:
0259: /**
0260: * @ejb.interface-method
0261: * @ejb.transaction type="Supports"
0262: */
0263: public MailingListResourceValue getMailingList(URI uri)
0264: throws LibresourceMailingException,
0265: LibresourceSecurityException, URINotExistException {
0266: try {
0267: KernelService kernelService = (KernelService) Libresource
0268: .getService(KernelConstants.SERVICE);
0269: LibresourceResourceIdentifier resourceIdentifier = kernelService
0270: .lookup(uri);
0271: MailingListResourceLocal mailingListResourceLocal = (MailingListResourceLocal) Libresource
0272: .findResource(resourceIdentifier,
0273: MailingListResourceLocal.class);
0274: MailingListResourceValue mailingListResourceValue = mailingListResourceLocal
0275: .getMailingListResourceValue();
0276: mailingListResourceValue.setUri(kernelService
0277: .normalizeURI(uri));
0278:
0279: return mailingListResourceValue;
0280: } catch (LibresourceSecurityException se) {
0281: throw se;
0282: } catch (URINotExistException se) {
0283: throw se;
0284: } catch (Exception e) {
0285: throw new LibresourceMailingException(
0286: "Error in getMailingList : " + e.getMessage(), e);
0287: }
0288: }
0289:
0290: /**
0291: * @ejb.interface-method
0292: * @ejb.transaction type="Supports"
0293: */
0294: public MailingListResourceValue systemGetMailingList(URI uri)
0295: throws LibresourceMailingException,
0296: LibresourceSecurityException, URINotExistException {
0297: try {
0298: KernelService kernelService = (KernelService) Libresource
0299: .getService(KernelConstants.SERVICE);
0300: LibresourceResourceIdentifier resourceIdentifier = kernelService
0301: .systemLookup(uri);
0302: MailingListResourceLocal mailingListResourceLocal = (MailingListResourceLocal) Libresource
0303: .findResource(resourceIdentifier,
0304: MailingListResourceLocal.class);
0305: MailingListResourceValue mailingListResourceValue = mailingListResourceLocal
0306: .getMailingListResourceValue();
0307: mailingListResourceValue.setUri(kernelService
0308: .normalizeURI(uri));
0309:
0310: return mailingListResourceValue;
0311: } catch (LibresourceSecurityException se) {
0312: throw se;
0313: } catch (URINotExistException se) {
0314: throw se;
0315: } catch (Exception e) {
0316: throw new LibresourceMailingException(
0317: "Error in systemGetMailingList : " + e.getMessage(),
0318: e);
0319: }
0320: }
0321:
0322: /**
0323: * @ejb.interface-method
0324: * @ejb.transaction type="Supports"
0325: */
0326: public MailingListResourceValue[] listMailingListsAt(URI uri)
0327: throws LibresourceMailingException,
0328: LibresourceSecurityException, URINotExistException {
0329: try {
0330: KernelService kernelService = (KernelService) Libresource
0331: .getService(KernelConstants.SERVICE);
0332: LibresourceResourceValue[] pages = kernelService
0333: .listResourcesAt(
0334: uri,
0335: MailingListResourceLocal.LIBRESOURCE_RESOURCE_SERVICE,
0336: MailingListResourceLocal.LIBRESOURCE_RESOURCE_TYPE);
0337: MailingListResourceValue[] mailingListResourceValues = new MailingListResourceValue[pages.length];
0338:
0339: for (int i = 0; i < mailingListResourceValues.length; i++) {
0340: LibresourceResourceIdentifier resourceIdentifier = pages[i]
0341: .getLibresourceResourceIdentifier();
0342: mailingListResourceValues[i] = ((MailingListResourceLocal) Libresource
0343: .findResource(resourceIdentifier))
0344: .getMailingListResourceValue();
0345: mailingListResourceValues[i].setUri(kernelService
0346: .normalizeURI(pages[i].getUri()));
0347: }
0348:
0349: return mailingListResourceValues;
0350: } catch (LibresourceSecurityException se) {
0351: throw se;
0352: } catch (URINotExistException se) {
0353: throw se;
0354: } catch (Exception e) {
0355: throw new LibresourceMailingException(
0356: "Error in listMailingListsAt : " + e.getMessage(),
0357: e);
0358: }
0359: }
0360:
0361: // mailing list
0362:
0363: /**
0364: * @ejb.interface-method
0365: * @ejb.transaction type="Required"
0366: */
0367: public void subscribeToMailingList(URI mailingListUri)
0368: throws LibresourceMailingException,
0369: LibresourceSecurityException, URINotExistException {
0370: try {
0371: MembershipService membershipService = (MembershipService) Libresource
0372: .getService(MembershipConstants.SERVICE);
0373: KernelService kernelService = (KernelService) Libresource
0374: .getService(KernelConstants.SERVICE);
0375:
0376: if (!kernelService.checkSecurity(mailingListUri,
0377: MailingListConstant.SECURITY_SUBSCRIBE)) {
0378: throw new LibresourceSecurityException(mailingListUri,
0379: MailingListConstant.SECURITY_SUBSCRIBE);
0380: }
0381:
0382: LibresourceResourceIdentifier resourceIdentifier = kernelService
0383: .lookup(mailingListUri);
0384: MailingListResourceLocal mailingListResourceLocal = (MailingListResourceLocal) Libresource
0385: .findResource(resourceIdentifier,
0386: MailingListResourceLocal.class);
0387:
0388: if (kernelService.isAuthentified()) {
0389: String user = membershipService.getProfile().getUri()
0390: .getPath();
0391:
0392: if (!mailingListResourceLocal.isSubscriber(user)) {
0393: mailingListResourceLocal.addSubscriber("User:"
0394: + user);
0395: }
0396: }
0397:
0398: // update node
0399: kernelService.setUpdateDate(mailingListUri, new Date());
0400:
0401: // event
0402: LibresourceEvent event = new LibresourceEvent(
0403: mailingListUri, resourceIdentifier, kernelService
0404: .getConnectedResource(),
0405: MailingListConstant.EVENT_MAILINGLIST_SUBSCRIBE,
0406: membershipService.getProfile().getFullName());
0407: Libresource.throwEvent(event);
0408: } catch (LibresourceSecurityException se) {
0409: ctx.setRollbackOnly();
0410: throw se;
0411: } catch (URINotExistException se) {
0412: ctx.setRollbackOnly();
0413: throw se;
0414: } catch (Exception e) {
0415: ctx.setRollbackOnly();
0416: throw new LibresourceMailingException(
0417: "Error in subscribeToMailingList : "
0418: + e.getMessage(), e);
0419: }
0420: }
0421:
0422: /**
0423: * @ejb.interface-method
0424: * @ejb.transaction type="Required"
0425: */
0426: public void subscribeToMailingList(URI mailingListUri,
0427: URI subscriberUri) throws LibresourceMailingException,
0428: LibresourceSecurityException, URINotExistException {
0429: try {
0430: MembershipService membershipService = (MembershipService) Libresource
0431: .getService(MembershipConstants.SERVICE);
0432: KernelService kernelService = (KernelService) Libresource
0433: .getService(KernelConstants.SERVICE);
0434:
0435: if (!kernelService.checkSecurity(mailingListUri,
0436: KernelConstants.SECURITY_UPDATE)) {
0437: throw new LibresourceSecurityException(mailingListUri,
0438: KernelConstants.SECURITY_UPDATE);
0439: }
0440:
0441: LibresourceResourceIdentifier resourceIdentifier = kernelService
0442: .lookup(mailingListUri);
0443: MailingListResourceLocal mailingListResourceLocal = (MailingListResourceLocal) Libresource
0444: .findResource(resourceIdentifier,
0445: MailingListResourceLocal.class);
0446:
0447: if (kernelService.exist(subscriberUri)) {
0448: if (!mailingListResourceLocal
0449: .isSubscriber(subscriberUri.getPath())) {
0450: try {
0451: // TODO check if user exist
0452: membershipService.getProfile(subscriberUri);
0453: mailingListResourceLocal.addSubscriber("User:"
0454: + subscriberUri.getPath());
0455: } catch (Exception e) {
0456: //
0457: }
0458:
0459: try {
0460: membershipService.getGroup(subscriberUri);
0461: mailingListResourceLocal.addSubscriber("Group:"
0462: + subscriberUri.getPath());
0463: } catch (Exception e) {
0464: //
0465: }
0466: }
0467: }
0468:
0469: // update node
0470: kernelService.setUpdateDate(mailingListUri, new Date());
0471:
0472: // event
0473: LibresourceEvent event = new LibresourceEvent(
0474: mailingListUri, resourceIdentifier, kernelService
0475: .getConnectedResource(),
0476: MailingListConstant.EVENT_MAILINGLIST_SUBSCRIBE,
0477: kernelService.getResource(subscriberUri)
0478: .getShortResourceName());
0479: Libresource.throwEvent(event);
0480: } catch (LibresourceSecurityException se) {
0481: ctx.setRollbackOnly();
0482: throw se;
0483: } catch (URINotExistException se) {
0484: ctx.setRollbackOnly();
0485: throw se;
0486: } catch (Exception e) {
0487: ctx.setRollbackOnly();
0488: throw new LibresourceMailingException(
0489: "Error in subscribeToMailingList : "
0490: + e.getMessage(), e);
0491: }
0492: }
0493:
0494: /**
0495: * @ejb.interface-method
0496: * @ejb.transaction type="Required"
0497: */
0498: public void unsubscribeFromMailingList(URI mailingListUri)
0499: throws LibresourceMailingException,
0500: LibresourceSecurityException, URINotExistException {
0501: try {
0502: MembershipService membershipService = (MembershipService) Libresource
0503: .getService(MembershipConstants.SERVICE);
0504: KernelService kernelService = (KernelService) Libresource
0505: .getService(KernelConstants.SERVICE);
0506: LibresourceResourceIdentifier resourceIdentifier = kernelService
0507: .lookup(mailingListUri);
0508: MailingListResourceLocal mailingListResourceLocal = (MailingListResourceLocal) Libresource
0509: .findResource(resourceIdentifier,
0510: MailingListResourceLocal.class);
0511:
0512: if (kernelService.isAuthentified()) {
0513: String user = membershipService.getProfile().getUri()
0514: .getPath();
0515:
0516: if (mailingListResourceLocal.isSubscriber(user)) {
0517: mailingListResourceLocal.removeSubscriber("User:"
0518: + user);
0519: }
0520: }
0521:
0522: // update node
0523: kernelService.setUpdateDate(mailingListUri, new Date());
0524:
0525: // event
0526: LibresourceEvent event = new LibresourceEvent(
0527: mailingListUri, resourceIdentifier, kernelService
0528: .getConnectedResource(),
0529: MailingListConstant.EVENT_MAILINGLIST_UNSUBSCRIBE,
0530: membershipService.getProfile().getFullName());
0531: Libresource.throwEvent(event);
0532: } catch (LibresourceSecurityException se) {
0533: ctx.setRollbackOnly();
0534: throw se;
0535: } catch (URINotExistException se) {
0536: ctx.setRollbackOnly();
0537: throw se;
0538: } catch (Exception e) {
0539: ctx.setRollbackOnly();
0540: throw new LibresourceMailingException(
0541: "Error in unsubscribeFromMailingList : "
0542: + e.getMessage(), e);
0543: }
0544: }
0545:
0546: /**
0547: * @ejb.interface-method
0548: * @ejb.transaction type="Required"
0549: */
0550: public void unsubscribeFromMailingList(URI mailingListUri,
0551: URI subscriberUri) throws LibresourceMailingException,
0552: LibresourceSecurityException, URINotExistException {
0553: try {
0554: MembershipService membershipService = (MembershipService) Libresource
0555: .getService(MembershipConstants.SERVICE);
0556: KernelService kernelService = (KernelService) Libresource
0557: .getService(KernelConstants.SERVICE);
0558: LibresourceResourceIdentifier resourceIdentifier = kernelService
0559: .lookup(mailingListUri);
0560: MailingListResourceLocal mailingListResourceLocal = (MailingListResourceLocal) Libresource
0561: .findResource(resourceIdentifier,
0562: MailingListResourceLocal.class);
0563:
0564: if (!kernelService.checkSecurity(mailingListUri,
0565: KernelConstants.SECURITY_UPDATE)) {
0566: throw new LibresourceSecurityException(mailingListUri,
0567: KernelConstants.SECURITY_UPDATE);
0568: }
0569:
0570: try {
0571: membershipService.getProfile(subscriberUri);
0572: mailingListResourceLocal.removeSubscriber("User:"
0573: + subscriberUri.getPath());
0574: } catch (Exception e) {
0575: //
0576: }
0577:
0578: try {
0579: membershipService.getGroup(subscriberUri);
0580: mailingListResourceLocal.removeSubscriber("Group:"
0581: + subscriberUri.getPath());
0582: } catch (Exception e) {
0583: //
0584: }
0585:
0586: // update node
0587: kernelService.setUpdateDate(mailingListUri, new Date());
0588:
0589: // event
0590: LibresourceEvent event = new LibresourceEvent(
0591: mailingListUri, resourceIdentifier, kernelService
0592: .getConnectedResource(),
0593: MailingListConstant.EVENT_MAILINGLIST_UNSUBSCRIBE,
0594: kernelService.getResource(subscriberUri)
0595: .getShortResourceName());
0596: Libresource.throwEvent(event);
0597: } catch (LibresourceSecurityException se) {
0598: ctx.setRollbackOnly();
0599: throw se;
0600: } catch (URINotExistException se) {
0601: ctx.setRollbackOnly();
0602: throw se;
0603: } catch (Exception e) {
0604: ctx.setRollbackOnly();
0605: throw new LibresourceMailingException(
0606: "Error in unsubscribeFromMailingList : "
0607: + e.getMessage(), e);
0608: }
0609: }
0610:
0611: /**
0612: * @ejb.interface-method
0613: * @ejb.transaction type="Supports"
0614: */
0615: public boolean isSubscriber(URI mailingListUri)
0616: throws LibresourceMailingException,
0617: LibresourceSecurityException, URINotExistException {
0618: try {
0619: MembershipService membershipService = (MembershipService) Libresource
0620: .getService(MembershipConstants.SERVICE);
0621:
0622: Vector mails = getSubscribersMails(mailingListUri);
0623:
0624: return mails.contains(membershipService.getProfile()
0625: .getEmail());
0626: } catch (LibresourceSecurityException se) {
0627: throw se;
0628: } catch (URINotExistException se) {
0629: throw se;
0630: } catch (Exception e) {
0631: throw new LibresourceMailingException(
0632: "Error in isSubscriber : " + e.getMessage(), e);
0633: }
0634: }
0635:
0636: /**
0637: * @ejb.interface-method
0638: * @ejb.transaction type="Supports"
0639: */
0640: public boolean isSubscriber(URI mailingListUri, String sender)
0641: throws LibresourceMailingException,
0642: LibresourceSecurityException, URINotExistException {
0643: try {
0644: MembershipService membershipService = (MembershipService) Libresource
0645: .getService(MembershipConstants.SERVICE);
0646: KernelService kernelService = (KernelService) Libresource
0647: .getService(KernelConstants.SERVICE);
0648: LibresourceResourceIdentifier resourceIdentifier = kernelService
0649: .systemLookup(mailingListUri);
0650: MailingListResourceLocal mailingListResourceLocal = (MailingListResourceLocal) Libresource
0651: .findResource(resourceIdentifier,
0652: MailingListResourceLocal.class);
0653:
0654: try {
0655: membershipService.getGroup(new URI(sender));
0656:
0657: return mailingListResourceLocal.isSubscriber(sender);
0658: } catch (Exception e) {
0659: //
0660: }
0661:
0662: Vector mails = getSubscribersMails(mailingListUri);
0663:
0664: return mails.contains(sender);
0665: } catch (LibresourceSecurityException se) {
0666: throw se;
0667: } catch (URINotExistException se) {
0668: throw se;
0669: } catch (Exception e) {
0670: throw new LibresourceMailingException(
0671: "Error in isSubscriber : " + e.getMessage(), e);
0672: }
0673: }
0674:
0675: /**
0676: * @ejb.interface-method
0677: * @ejb.transaction type="Supports"
0678: */
0679: public Vector getSubscribers(URI mailingListUri)
0680: throws LibresourceMailingException,
0681: LibresourceSecurityException, URINotExistException {
0682: try {
0683: KernelService kernelService = (KernelService) Libresource
0684: .getService(KernelConstants.SERVICE);
0685: MembershipService membershipService = (MembershipService) Libresource
0686: .getService(MembershipConstants.SERVICE);
0687: LibresourceResourceIdentifier resourceIdentifier = kernelService
0688: .lookup(mailingListUri);
0689: MailingListResourceLocal mailingListResourceLocal = (MailingListResourceLocal) Libresource
0690: .findResource(resourceIdentifier,
0691: MailingListResourceLocal.class);
0692:
0693: Vector result = new Vector();
0694: String[] subscribers = mailingListResourceLocal
0695: .getSubscribers();
0696:
0697: for (int i = 0; i < subscribers.length; i++) {
0698: if (subscribers[i].startsWith("User:")) {
0699: String userUri = subscribers[i].substring(5);
0700:
0701: try {
0702: ProfileResourceValue user = membershipService
0703: .getProfile(new URI(userUri));
0704: result.add(user);
0705: } catch (Exception e) {
0706: //
0707: }
0708: } else {
0709: String groupUri = subscribers[i].substring(6);
0710:
0711: try {
0712: GroupResourceValue group = membershipService
0713: .getGroup(new URI(groupUri));
0714: result.add(group);
0715: } catch (Exception e) {
0716: //
0717: }
0718: }
0719: }
0720:
0721: return result;
0722: } catch (LibresourceSecurityException se) {
0723: throw se;
0724: } catch (URINotExistException se) {
0725: throw se;
0726: } catch (Exception e) {
0727: throw new LibresourceMailingException(
0728: "Error in getSubscribers : " + e.getMessage(), e);
0729: }
0730: }
0731:
0732: /**
0733: * @ejb.interface-method
0734: * @ejb.transaction type="Supports"
0735: */
0736: public Vector getSubscribersMails(URI mailingListUri)
0737: throws LibresourceMailingException, URINotExistException {
0738: try {
0739: KernelService kernelService = (KernelService) Libresource
0740: .getService(KernelConstants.SERVICE);
0741: MembershipService membershipService = (MembershipService) Libresource
0742: .getService(MembershipConstants.SERVICE);
0743: LibresourceResourceIdentifier resourceIdentifier = kernelService
0744: .systemLookup(mailingListUri);
0745: MailingListResourceLocal mailingListResourceLocal = (MailingListResourceLocal) Libresource
0746: .findResource(resourceIdentifier,
0747: MailingListResourceLocal.class);
0748:
0749: Vector result = new Vector();
0750: String[] subscribers = mailingListResourceLocal
0751: .getSubscribers();
0752:
0753: for (int i = 0; i < subscribers.length; i++) {
0754: if (subscribers[i].startsWith("User:")) {
0755: String userUri = subscribers[i].substring(5);
0756:
0757: try {
0758: String mail = membershipService.getProfile(
0759: new URI(userUri)).getEmail();
0760:
0761: if (!result.contains(mail) && (mail != null)) {
0762: result.add(mail);
0763: }
0764: } catch (Exception e) {
0765: //
0766: }
0767: } else {
0768: String groupUri = subscribers[i].substring(6);
0769:
0770: try {
0771: result = getMembers(membershipService
0772: .systemGetGroup(new URI(groupUri)),
0773: result, new Vector());
0774: } catch (Exception e) {
0775: //
0776: }
0777: }
0778: }
0779:
0780: return result;
0781: } catch (URINotExistException se) {
0782: throw se;
0783: } catch (Exception e) {
0784: throw new LibresourceMailingException(
0785: "Error in getSubscribersMails : " + e.getMessage(),
0786: e);
0787: }
0788: }
0789:
0790: /**
0791: * @ejb.interface-method
0792: * @ejb.transaction type="Required"
0793: */
0794: public void addMessageInMailingList(URI mailingListUri, String id,
0795: String title, String body, String sender, String reply)
0796: throws LibresourceMailingException,
0797: LibresourceForumException, LibresourceSecurityException,
0798: URINotExistException {
0799: try {
0800: KernelService kernelService = (KernelService) Libresource
0801: .getService(KernelConstants.SERVICE);
0802: MembershipService membershipService = (MembershipService) Libresource
0803: .getService(MembershipConstants.SERVICE);
0804: URI parentUri;
0805:
0806: if ((reply == null) || (reply.trim().length() == 0)) {
0807: parentUri = mailingListUri;
0808: } else {
0809: try {
0810: reply = reply.substring(1, reply.length() - 1);
0811:
0812: LibresourceResourceIdentifier identifier = new LibresourceResourceIdentifier(
0813: ForumConstants.SERVICE, "Message", reply);
0814: parentUri = kernelService.getMyURI(identifier);
0815: } catch (Exception e) {
0816: parentUri = mailingListUri;
0817: }
0818: }
0819:
0820: URI msgUri = new URI(kernelService
0821: .normalizeAbsoluteURIPath(parentUri)
0822: + "/" + id);
0823: MailingListResourceValue mailingList = (MailingListResourceValue) systemGetMailingList(mailingListUri);
0824: LibresourceForumService libresourceForumService = (LibresourceForumService) Libresource
0825: .getService(ForumConstants.SERVICE);
0826:
0827: if ((title == null) || (title.trim().length() == 0)) {
0828: title = "(no subject)";
0829: }
0830:
0831: String author = membershipService.getProfile(
0832: new URI(getSenderUri(sender))).getFullName();
0833: libresourceForumService.systemCreateMessage(msgUri, id,
0834: title, body, author);
0835:
0836: kernelService.systemResetAcls(msgUri);
0837: kernelService.systemCopyAcl(mailingListUri, msgUri);
0838: kernelService.systemChown(msgUri, kernelService
0839: .getOwner(mailingListUri), false);
0840:
0841: // update node
0842: kernelService.setUpdateDate(mailingListUri, new Date());
0843: } catch (LibresourceSecurityException se) {
0844: ctx.setRollbackOnly();
0845: throw se;
0846: } catch (URINotExistException se) {
0847: ctx.setRollbackOnly();
0848: throw se;
0849: } catch (LibresourceForumException se) {
0850: ctx.setRollbackOnly();
0851: throw se;
0852: } catch (Exception e) {
0853: ctx.setRollbackOnly();
0854: throw new LibresourceMailingException(
0855: "Error in addMessageInMailingList : "
0856: + e.getMessage(), e);
0857: }
0858: }
0859:
0860: /**
0861: * @ejb.interface-method
0862: * @ejb.transaction type="Supports"
0863: */
0864: public String getMessagesTree(URI uri)
0865: throws LibresourceMailingException,
0866: LibresourceForumException, LibresourceSecurityException,
0867: URINotExistException {
0868: try {
0869: LibresourceForumService forumService = (LibresourceForumService) Libresource
0870: .getService(ForumConstants.SERVICE);
0871: StringBuffer buffer = new StringBuffer();
0872:
0873: MessageResourceValue[] messageValues = forumService
0874: .listMessagesAt(new URI(uri.getPath()));
0875:
0876: for (int i = 0; i < messageValues.length; i++) {
0877: listMessagesRecursively(new URI(uri.getPath()
0878: + "?action=viewMail&mail="), messageValues[i]
0879: .getUri(), buffer);
0880: }
0881:
0882: return buffer.toString();
0883: } catch (LibresourceSecurityException se) {
0884: throw se;
0885: } catch (URINotExistException se) {
0886: throw se;
0887: } catch (LibresourceForumException se) {
0888: throw se;
0889: } catch (Exception e) {
0890: throw new LibresourceMailingException(
0891: "Error in getMessagesTree : " + e.getMessage(), e);
0892: }
0893: }
0894:
0895: /**
0896: * @ejb.interface-method
0897: * @ejb.transaction type="Supports"
0898: */
0899: public String getMessagesTreeUnderMessage(URI mailingListUri,
0900: String msgPath) throws LibresourceMailingException,
0901: LibresourceForumException, LibresourceSecurityException,
0902: URINotExistException {
0903: try {
0904: LibresourceForumService forumService = (LibresourceForumService) Libresource
0905: .getService(ForumConstants.SERVICE);
0906: StringBuffer buffer = new StringBuffer();
0907:
0908: MessageResourceValue[] messageValues = forumService
0909: .listMessagesAt(new URI(mailingListUri.getPath()
0910: + "/" + msgPath));
0911:
0912: for (int i = 0; i < messageValues.length; i++) {
0913: listMessagesRecursivelyUnderMessage(new URI(
0914: mailingListUri.getPath()
0915: + "?action=viewMail&mail=" + msgPath
0916: + "/"), messageValues[i].getUri(),
0917: buffer);
0918: }
0919:
0920: return buffer.toString();
0921: } catch (LibresourceSecurityException se) {
0922: throw se;
0923: } catch (URINotExistException se) {
0924: throw se;
0925: } catch (LibresourceForumException se) {
0926: throw se;
0927: } catch (Exception e) {
0928: throw new LibresourceMailingException(
0929: "Error in getMessagesTree : " + e.getMessage(), e);
0930: }
0931: }
0932:
0933: /**
0934: * @ejb.interface-method
0935: * @ejb.transaction type="Supports"
0936: */
0937: public void sendMessage(URI mailingListUri, String subject,
0938: String content, String attachments, String reply)
0939: throws LibresourceMailingException,
0940: LibresourceSecurityException, URINotExistException {
0941: try {
0942: Session session = getSession();
0943: MimeMessage message = new MimeMessage(session);
0944: MembershipService membershipService = (MembershipService) Libresource
0945: .getService(MembershipConstants.SERVICE);
0946: KernelService kernelService = (KernelService) Libresource
0947: .getService(KernelConstants.SERVICE);
0948: message.setFrom(new InternetAddress(membershipService
0949: .getProfile().getEmail()));
0950:
0951: MailingListResourceValue mailingList = (MailingListResourceValue) getMailingList(mailingListUri);
0952:
0953: if ((reply != null) && (reply.trim().length() != 0)) {
0954: message.setHeader("In-Reply-To", "<" + reply + ">");
0955: }
0956:
0957: message.setRecipient(Message.RecipientType.TO,
0958: new InternetAddress(mailingList.getMail() + "@"
0959: + getMailSuffix()));
0960: message.setSubject(subject);
0961:
0962: Multipart multipart = new MimeMultipart();
0963:
0964: // content of the message
0965: BodyPart messageBodyPart = new MimeBodyPart();
0966: messageBodyPart.setText(content);
0967: multipart.addBodyPart(messageBodyPart);
0968:
0969: // for each of the attachments
0970: String fileId = null;
0971: StringTokenizer st = new StringTokenizer(attachments, ",",
0972: false);
0973: FileData fileData = null;
0974: messageBodyPart = null;
0975:
0976: String contentType = null;
0977: String fileName = null;
0978:
0979: //ByteArrayOutputStream buffer = new ByteArrayOutputStream();
0980: while (st.hasMoreTokens()) {
0981: messageBodyPart = null;
0982: fileId = st.nextToken();
0983: fileName = st.nextToken();
0984:
0985: //fileData = loadAttachment(fileId);
0986: String filePath = loadAttachment(fileId);
0987: deleteAttachment(fileId);
0988:
0989: //buffer.reset();
0990: /*InputStream in = fileData.getInputStream();
0991: int byteCount = 0;
0992: byte[] data = new byte[100024];
0993: while ((byteCount = in.read(data, 0, 100024)) > -1) {
0994: buffer.write(data, 0, byteCount);
0995: }
0996: in.close();
0997: buffer.flush();
0998: buffer.close();*/
0999: messageBodyPart = new MimeBodyPart();
1000:
1001: // File temp = createTempFile(fileId);
1002: contentType = new MimetypesFileTypeMap()
1003: .getContentType(fileName);
1004:
1005: //DataSource ds = new ByteArrayDataSource(buffer.toByteArray(), contentType);
1006: //messageBodyPart.setDataHandler(new DataHandler(ds));
1007: // TODO get the path of the temp file and use this :
1008: DataSource source = new FileDataSource(filePath);
1009: messageBodyPart.setDataHandler(new DataHandler(source));
1010:
1011: messageBodyPart
1012: .setDisposition("attachment; filename=\""
1013: + fileName + "\"");
1014: messageBodyPart.setFileName(fileName);
1015: multipart.addBodyPart(messageBodyPart);
1016: }
1017:
1018: // set multipart content of the message
1019: message.setContent(multipart);
1020:
1021: // send the message
1022: Transport.send(message);
1023:
1024: // update node
1025: kernelService.setUpdateDate(mailingListUri, new Date());
1026:
1027: // event
1028: LibresourceEvent event = new LibresourceEvent(
1029: mailingListUri, kernelService
1030: .lookup(mailingListUri), kernelService
1031: .getConnectedResource(),
1032: MailingListConstant.EVENT_MAILINGLIST_SEND_MESSAGE,
1033: subject);
1034: Libresource.throwEvent(event);
1035: } catch (MessagingException e) {
1036: throw new LibresourceMailingException(
1037: "Error in sendMessage : " + e.getMessage());
1038: } catch (Exception e) {
1039: throw new LibresourceMailingException(
1040: "Error in sendMessage : " + e.getMessage(), e);
1041: }
1042: }
1043:
1044: /**
1045: * @ejb.interface-method
1046: * @ejb.transaction type="Supports"
1047: */
1048: public Vector getAllMailingLists()
1049: throws LibresourceMailingException,
1050: LibresourceSecurityException, URINotExistException {
1051: try {
1052: Collection mailingLists = MailingListResourceUtil
1053: .getLocalHome().findAll();
1054: KernelService kernelService = (KernelService) Libresource
1055: .getService(KernelConstants.SERVICE);
1056: Vector res = new Vector();
1057: MailingListResourceLocal mailingListLocal;
1058: MailingListResourceValue mailingList;
1059:
1060: for (Iterator i = mailingLists.iterator(); i.hasNext();) {
1061: mailingListLocal = (MailingListResourceLocal) i.next();
1062: mailingList = mailingListLocal
1063: .getMailingListResourceValue();
1064:
1065: try {
1066: mailingList
1067: .setUri(kernelService
1068: .getMyURI(new LibresourceResourceIdentifier(
1069: MailingListConstant.RESOURCE,
1070: MailingListConstant.RESOURCE,
1071: mailingListLocal.getId())));
1072: res.add(mailingList);
1073: } catch (Exception e) {
1074: // mailingListLocal.remove();
1075: }
1076: }
1077:
1078: return res;
1079: } catch (Exception e) {
1080: throw new LibresourceMailingException(
1081: "Error in getAllMailingLists : " + e.getMessage(),
1082: e);
1083: }
1084: }
1085:
1086: /**
1087: * @ejb.interface-method
1088: * @ejb.transaction type="Required"
1089: */
1090: public String storeAttachment(FileData fileData)
1091: throws LibresourceMailingException,
1092: LibresourceSecurityException, URINotExistException {
1093: try {
1094: LibresourceCoreService coreService = (LibresourceCoreService) Libresource
1095: .getService("LibresourceCore");
1096:
1097: String fileId = FileResourceUtil.generateGUID(fileData
1098: .getName());
1099: coreService.putFileData("files_content_", fileId, fileData);
1100:
1101: return fileId;
1102: } catch (Exception e) {
1103: ctx.setRollbackOnly();
1104: throw new LibresourceMailingException(
1105: "Error in storeAttachments : " + e.getMessage(), e);
1106: }
1107: }
1108:
1109: /**
1110: * @ejb.interface-method
1111: * @ejb.transaction type="Supports"
1112: */
1113: public String loadAttachment(String fileId)
1114: throws LibresourceMailingException,
1115: LibresourceSecurityException, URINotExistException {
1116: try {
1117: LibresourceCoreService coreService = (LibresourceCoreService) Libresource
1118: .getService("LibresourceCore");
1119:
1120: return coreService
1121: .getTempFilePath("files_content_", fileId);
1122: } catch (Exception e) {
1123: throw new LibresourceMailingException(
1124: "Error in loadAttachment : " + e.getMessage(), e);
1125: }
1126: }
1127:
1128: /**
1129: * @ejb.interface-method
1130: * @ejb.transaction type="Required"
1131: */
1132: public void deleteAttachment(String fileId)
1133: throws LibresourceMailingException,
1134: LibresourceSecurityException, URINotExistException {
1135: try {
1136: LibresourceCoreService coreService = (LibresourceCoreService) Libresource
1137: .getService("LibresourceCore");
1138: coreService.deleteFileData("files_content_", fileId);
1139: } catch (Exception e) {
1140: ctx.setRollbackOnly();
1141: throw new LibresourceMailingException(
1142: "Error in deleteAttachment : " + e.getMessage(), e);
1143: }
1144: }
1145:
1146: /**
1147: * @ejb.interface-method
1148: * @ejb.transaction type="Supports"
1149: */
1150: public boolean mailingListAlreadyExists(String name, URI uri)
1151: throws LibresourceMailingException,
1152: LibresourceSecurityException, URINotExistException {
1153: try {
1154: KernelService kernelService = (KernelService) Libresource
1155: .getService(KernelConstants.SERVICE);
1156: Collection mailingLists = MailingListResourceUtil
1157: .getLocalHome().findAll();
1158: MailingListResourceLocal mailingListLocal;
1159:
1160: for (Iterator i = mailingLists.iterator(); i.hasNext();) {
1161: mailingListLocal = (MailingListResourceLocal) i.next();
1162:
1163: try {
1164: URI mailingListUri = kernelService
1165: .getMyURI(new LibresourceResourceIdentifier(
1166: MailingListConstant.RESOURCE,
1167: MailingListConstant.RESOURCE,
1168: mailingListLocal.getId()));
1169:
1170: if ((mailingListLocal.getMail()
1171: .compareToIgnoreCase(name) == 0)
1172: && (mailingListUri.getPath().compareTo(
1173: uri.getPath()) != 0)) {
1174: return true;
1175: }
1176: } catch (Exception e) {
1177: // mailingListLocal.remove();
1178: }
1179: }
1180:
1181: return false;
1182: } catch (Exception e) {
1183: throw new LibresourceMailingException(
1184: "Error in mailingListAlreadyExists : "
1185: + e.getMessage(), e);
1186: }
1187: }
1188:
1189: /**
1190: * @ejb.interface-method
1191: * @ejb.transaction type="Supports"
1192: */
1193: public byte[] getArchiveContent(URI uri, String mailId)
1194: throws LibresourceMailingException,
1195: LibresourceSecurityException, URINotExistException {
1196: Connection connection = null;
1197: Statement stmt = null;
1198: ResultSet rs = null;
1199:
1200: try {
1201: javax.sql.DataSource datasource = Libresource
1202: .getDatasource();
1203: connection = datasource.getConnection();
1204:
1205: String query = "SELECT message_body FROM mailinglists_archive_ WHERE message_name ='"
1206: + mailId + "'";
1207: stmt = connection.createStatement();
1208: rs = stmt.executeQuery(query);
1209:
1210: byte[] body = null;
1211:
1212: if (rs.next()) {
1213: body = rs.getBytes("message_body");
1214: } else {
1215: throw new LibresourceMailingException(
1216: "No mail found with this id : " + mailId);
1217: }
1218:
1219: return body;
1220: } catch (Exception e) {
1221: throw new LibresourceMailingException(
1222: "Error in getArchiveContent : " + e.getMessage(), e);
1223: } finally {
1224: try {
1225: if (rs != null) {
1226: rs.close();
1227: }
1228:
1229: if (stmt != null) {
1230: stmt.close();
1231: }
1232:
1233: if (connection != null) {
1234: connection.close();
1235: }
1236: } catch (Exception e) {
1237: }
1238: }
1239: }
1240:
1241: /**
1242: * @ejb.interface-method
1243: * @ejb.transaction type="Supports"
1244: */
1245: public LibresourceIndexableContent getIndexableContent(
1246: LibresourceResourceIdentifier resourceIdentifier)
1247: throws LibresourceException {
1248: try {
1249: try {
1250: Libresource.checkType(resourceIdentifier,
1251: MailingListResourceLocal.class);
1252:
1253: LibresourceIndexableContent content = new LibresourceIndexableContent();
1254: MailingListResourceLocal mailingListResourceLocal = (MailingListResourceLocal) Libresource
1255: .findResource(resourceIdentifier);
1256: content.addContentPart(mailingListResourceLocal
1257: .getMail());
1258:
1259: if (mailingListResourceLocal.getDescription() != null) {
1260: content.addContentPart(mailingListResourceLocal
1261: .getDescription()
1262: .replaceAll("\\{.*\\}", ""));
1263: }
1264:
1265: return content;
1266: } catch (LibresourceException e) { //
1267: }
1268:
1269: return null;
1270: } catch (Exception e) {
1271: throw new LibresourceException(
1272: "error in LibresourceMailingList.getIndexableContent("
1273: + resourceIdentifier + ")", e);
1274: }
1275: }
1276:
1277: /**
1278: * @ejb.interface-method
1279: * @ejb.transaction type="Supports"
1280: */
1281: public String[] listAvailablesPermissions(
1282: LibresourceResourceIdentifier resourceIdentifier)
1283: throws LibresourceException {
1284: if (resourceIdentifier.getService().equals(
1285: MailingListConstant.RESOURCE)) {
1286: if (resourceIdentifier.getResourceType().equals(
1287: MailingListConstant.RESOURCE)) {
1288: return new String[] { MailingListConstant.SECURITY_SUBSCRIBE };
1289: }
1290: }
1291:
1292: return super .listAvailablesPermissions(resourceIdentifier);
1293: }
1294:
1295: /**
1296: * @ejb.interface-method
1297: * @ejb.transaction type="Supports"
1298: */
1299: public String[] listAvailablesEvents(
1300: LibresourceResourceIdentifier resourceIdentifier)
1301: throws LibresourceException {
1302: if (resourceIdentifier.getService().equals(
1303: MailingListConstant.RESOURCE)) {
1304: if (resourceIdentifier.getResourceType().equals(
1305: MailingListConstant.RESOURCE)) {
1306: return new String[] {
1307: MailingListConstant.EVENT_MAILINGLIST_CREATE,
1308: MailingListConstant.EVENT_MAILINGLIST_EDIT,
1309: MailingListConstant.EVENT_MAILINGLIST_SUBSCRIBE,
1310: MailingListConstant.EVENT_MAILINGLIST_UNSUBSCRIBE,
1311: MailingListConstant.EVENT_MAILINGLIST_SEND_MESSAGE };
1312: }
1313: }
1314:
1315: return super .listAvailablesPermissions(resourceIdentifier);
1316: }
1317:
1318: /**
1319: * @ejb.interface-method
1320: * @ejb.transaction type="Supports"
1321: */
1322: public LibresourceExportHandler getXmlExportHandlerForResource(
1323: URI uri) throws LibresourceException {
1324: try {
1325: KernelService kernelService = (KernelService) Libresource
1326: .getService(KernelConstants.SERVICE);
1327: LibresourceResourceIdentifier resourceIdentifier = kernelService
1328: .lookup(uri);
1329:
1330: try {
1331: Libresource.checkType(resourceIdentifier,
1332: MailingListResourceLocal.class);
1333:
1334: return new MailingListExportHandler(uri);
1335: } catch (Exception e) { //
1336: }
1337:
1338: return super .getXmlExportHandlerForResource(uri);
1339: } catch (Exception e) {
1340: throw new LibresourceException(
1341: "Can't obtain exportHandler for uri " + uri, e);
1342: }
1343: }
1344:
1345: /**
1346: * @ejb.interface-method
1347: * @ejb.transaction type="Supports"
1348: */
1349: public LibresourceImportHandler getXmlImportHandler(String type)
1350: throws LibresourceException {
1351: if (type.equals(MailingListConstant.RESOURCE)) {
1352: return new MailingListImportHandler();
1353: }
1354:
1355: return super .getXmlImportHandler(type);
1356: }
1357:
1358: /**
1359: * @ejb.interface-method
1360: * @ejb.transaction type="Supports"
1361: */
1362: public String getSenderUri(String sender)
1363: throws LibresourceMailingException,
1364: LibresourceSecurityException, URINotExistException {
1365: try {
1366: MembershipService membershipService = (MembershipService) Libresource
1367: .getService(MembershipConstants.SERVICE);
1368: String result = ""; //membershipService.getUserName(sender);
1369:
1370: if (result.trim().length() != 0) {
1371: result = membershipService.getUsersRootURI().getPath()
1372: + "/" + result;
1373: }
1374:
1375: return result;
1376: } catch (LibresourceSecurityException se) {
1377: throw se;
1378:
1379: /*} catch (URINotExistException se) {
1380: throw se;*/
1381: } catch (Exception e) {
1382: throw new LibresourceMailingException(
1383: "Error in getSenderUri : " + e.getMessage(), e);
1384: }
1385: }
1386:
1387: // mailfactory session getter
1388: private Session getSession() throws Exception {
1389: if (session == null) {
1390: session = (Session) PortableRemoteObject.narrow(
1391: new InitialContext()
1392: .lookup("libresourceMailLocalhost"),
1393: Session.class);
1394: }
1395:
1396: return session;
1397: }
1398:
1399: private Vector getMembers(GroupResourceValue group, Vector result,
1400: Vector cycleDetection) throws Exception {
1401: cycleDetection.add(group.getId());
1402:
1403: KernelService kernelService = (KernelService) Libresource
1404: .getService(KernelConstants.SERVICE);
1405: MembershipService membershipService = (MembershipService) Libresource
1406: .getService(MembershipConstants.SERVICE);
1407:
1408: Vector subGroups = new Vector();
1409: String[] members = group.getMembers();
1410:
1411: for (int i = 0; i < members.length; i++) {
1412: if (members[i].startsWith("User:")) {
1413: URI userUri = kernelService.normalizeURI(new URI(
1414: membershipService.getUsersRootURI().getPath()
1415: + "/" + members[i].substring(5)));
1416: String mail = membershipService.getProfile(userUri)
1417: .getEmail();
1418:
1419: if (!result.contains(mail) && (mail != null)) {
1420: result.add(mail);
1421: }
1422: } else {
1423: subGroups.add(kernelService
1424: .getMyURI(new LibresourceResourceIdentifier(
1425: MembershipConstants.SERVICE, "Group",
1426: members[i].substring(6))));
1427: }
1428: }
1429:
1430: for (Iterator j = subGroups.iterator(); j.hasNext();) {
1431: URI groupUri = (URI) j.next();
1432:
1433: try {
1434: GroupResourceValue g = membershipService
1435: .systemGetGroup(groupUri);
1436:
1437: if (!cycleDetection.contains(g.getId())) {
1438: result = getMembers(g, result, cycleDetection);
1439: }
1440: } catch (Exception e) {
1441: //
1442: }
1443: }
1444:
1445: return result;
1446: }
1447:
1448: private void listMessagesRecursively(URI base, URI node,
1449: StringBuffer buffer) throws Exception {
1450: LibresourceForumService forumService = (LibresourceForumService) Libresource
1451: .getService(ForumConstants.SERVICE);
1452: MessageResourceValue messageValue = forumService
1453: .getMessage(node);
1454: MessageResourceValue[] messageValues = forumService
1455: .listMessagesAt(node);
1456: buffer.append("<ul>");
1457: buffer
1458: .append("<li><b><a href=\""
1459: + base.getPath().substring(1));
1460:
1461: String query = new String("");
1462:
1463: if (base.getQuery() != null) {
1464: query = "?" + base.getQuery();
1465: }
1466:
1467: buffer.append(query);
1468: buffer.append(messageValue.getId()
1469: + "\" title=\""
1470: + messageValue.getUri()
1471: + "\">"
1472: + messageValue.getTitle()
1473: + "</a> , "
1474: + messageValue.getAuthorName()
1475: + "</b> ["
1476: + DateFormat.getDateInstance(DateFormat.LONG,
1477: Locale.ENGLISH).format(messageValue.getDate())
1478: + "]</li>");
1479:
1480: for (int i = 0; i < messageValues.length; i++) {
1481: listMessagesRecursively(new URI(base.getPath() + query
1482: + messageValue.getId() + "/"), messageValues[i]
1483: .getUri(), buffer);
1484: }
1485:
1486: buffer.append("</ul>");
1487: }
1488:
1489: private void listMessagesRecursivelyUnderMessage(URI base,
1490: URI node, StringBuffer buffer) throws Exception {
1491: LibresourceForumService forumService = (LibresourceForumService) Libresource
1492: .getService(ForumConstants.SERVICE);
1493: MessageResourceValue messageValue = forumService
1494: .getMessage(node);
1495: MessageResourceValue[] messageValues = forumService
1496: .listMessagesAt(node);
1497: buffer.append("<ul>");
1498: buffer.append("<li><b><a href=\""
1499: + base.getPath().substring(1)
1500: + "?"
1501: + base.getQuery()
1502: + messageValue.getId()
1503: + "\" title=\""
1504: + messageValue.getUri()
1505: + "\">"
1506: + messageValue.getTitle()
1507: + "</a> , "
1508: + messageValue.getAuthorName()
1509: + "</b> ["
1510: + DateFormat.getDateInstance(DateFormat.LONG,
1511: Locale.ENGLISH).format(messageValue.getDate())
1512: + "]</li>");
1513:
1514: for (int i = 0; i < messageValues.length; i++) {
1515: listMessagesRecursively(new URI(base.getPath() + "?"
1516: + base.getQuery() + messageValue.getId() + "/"),
1517: messageValues[i].getUri(), buffer);
1518: }
1519:
1520: buffer.append("</ul>");
1521: }
1522:
1523: private String resolveMailNotifierTargetUri(URI uri, String pattern)
1524: throws Exception {
1525: if (pattern.startsWith("/")) {
1526: return pattern;
1527: }
1528:
1529: String path = uri.getPath();
1530:
1531: if (!path.endsWith("/")) {
1532: path = path + "/";
1533: }
1534:
1535: return new URI(path).resolve(pattern).getPath();
1536: }
1537: }
|