You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@clerezza.apache.org by re...@apache.org on 2010/06/04 13:43:47 UTC

svn commit: r951382 - in /incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.platform.mail/src/main/java/org/apache/clerezza/platform/mail: MailMan.java MailManImpl.java MailSessionFactory.java

Author: reto
Date: Fri Jun  4 11:43:47 2010
New Revision: 951382

URL: http://svn.apache.org/viewvc?rev=951382&view=rev
Log:
CLEREZZA-36: extracting SesseionFactory, deprecated methods

Added:
    incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.platform.mail/src/main/java/org/apache/clerezza/platform/mail/MailSessionFactory.java
Modified:
    incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.platform.mail/src/main/java/org/apache/clerezza/platform/mail/MailMan.java
    incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.platform.mail/src/main/java/org/apache/clerezza/platform/mail/MailManImpl.java

Modified: incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.platform.mail/src/main/java/org/apache/clerezza/platform/mail/MailMan.java
URL: http://svn.apache.org/viewvc/incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.platform.mail/src/main/java/org/apache/clerezza/platform/mail/MailMan.java?rev=951382&r1=951381&r2=951382&view=diff
==============================================================================
--- incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.platform.mail/src/main/java/org/apache/clerezza/platform/mail/MailMan.java (original)
+++ incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.platform.mail/src/main/java/org/apache/clerezza/platform/mail/MailMan.java Fri Jun  4 11:43:47 2010
@@ -26,9 +26,9 @@ import org.apache.clerezza.rdf.utils.Gra
 
 
 /**
- * Subclasses of this interface provide methods for sending emails and 
- * especially emails to platform users.
- *
+ * Implementations provide methods for sending emails, in particular to platform
+ * users.
+ * 
  * @author mir
  */
 public interface MailMan {
@@ -121,11 +121,33 @@ public interface MailMan {
 	 * but not visible to other recipients.
 	 * @param subject the subject of the message
 	 * @param message the message to be sent
+	 * @deprecated use javax.mail with the session returned by MailSessionFactory
 	 */
+	@Deprecated
 	public void sendEmail(InternetAddress from, InternetAddress to,
 			InternetAddress[] cc, InternetAddress[] bcc, String subject,
 			String message) throws MessagingException;
-	
+
+	/**
+     * Sends an message with the specified subject to the specified
+     * <code>Adress</code> to and carbon copies to the <code>Adress<code>es
+     * cc and bcc (Not visible to other recipients). Cc and bcc can be null, all
+     * other parameter has to be specified.
+     * @author oliver straesser
+     * @param from sender address
+     * @param to recipient address
+     * @param cc addresess to which copies of the message will be sent to
+     * @param bcc addreses to which copies of the message will be sent to,
+     * but not visible to other recipients.
+     * @param subject the subject of the message
+     * @param message the message to be sent
+	 * @deprecated use javax.mail with the session returned by MailSessionFactory
+     */
+	@Deprecated
+    public void sendEmail(InternetAddress from, InternetAddress to,
+            InternetAddress[] cc, InternetAddress[] bcc, String subject,
+            String message, String mediaType) throws MessagingException;
+
 	/**
 	 * Sends the rendered <code>GraphNode</code> with the specified subject to the specified
 	 * <code>Adress</code> to and carbon copies to the <code>Adress<code>es

Modified: incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.platform.mail/src/main/java/org/apache/clerezza/platform/mail/MailManImpl.java
URL: http://svn.apache.org/viewvc/incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.platform.mail/src/main/java/org/apache/clerezza/platform/mail/MailManImpl.java?rev=951382&r1=951381&r2=951382&view=diff
==============================================================================
--- incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.platform.mail/src/main/java/org/apache/clerezza/platform/mail/MailManImpl.java (original)
+++ incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.platform.mail/src/main/java/org/apache/clerezza/platform/mail/MailManImpl.java Fri Jun  4 11:43:47 2010
@@ -24,19 +24,15 @@ import java.security.PrivilegedException
 import java.util.Collections;
 import java.util.Date;
 import java.util.List;
-import java.util.Properties;
 
-import javax.mail.Authenticator;
 import javax.mail.Message;
 import javax.mail.MessagingException;
-import javax.mail.PasswordAuthentication;
 import javax.mail.Session;
 import javax.mail.Transport;
 import javax.mail.internet.AddressException;
 import javax.mail.internet.InternetAddress;
 import javax.mail.internet.MimeMessage;
 import javax.ws.rs.core.MediaType;
-import org.osgi.service.component.ComponentContext;
 import org.apache.clerezza.platform.typerendering.Renderer;
 import org.apache.clerezza.platform.typerendering.RendererFactory;
 import org.apache.clerezza.rdf.core.MGraph;
@@ -50,82 +46,35 @@ import org.apache.clerezza.rdf.core.spar
 import org.apache.clerezza.rdf.ontologies.FOAF;
 import org.apache.clerezza.rdf.ontologies.PLATFORM;
 import org.apache.clerezza.rdf.utils.GraphNode;
+import org.apache.felix.scr.annotations.Component;
+import org.apache.felix.scr.annotations.Reference;
+import org.apache.felix.scr.annotations.Service;
 
 /**
- * @scr.component
- * @scr.service interface="org.apache.clerezza.platform.mail.MailMan"
+ *
+ * Implementation of <code>MailMan</code> providing  utility methods to send
+ * emails rendering GraphNodes
  *
  * @author mir, daniel
  */
+@Component
+@Service(MailMan.class)
+@Reference
 public class MailManImpl implements MailMan {
 
-	/**
-	 * Service property
-	 *
-	 * @scr.property value="false"
-	 *               description="Specifies if TLS (SSL) encryption is used."
-	 */
-	public static final String MAIL_USE_TLS = "useTLS";
-	/**
-	 * Service property
-	 *
-	 * @scr.property value="smtp.myHost.org"
-	 *               description="Specifies the SMTP host."
-	 */
-	public static final String MAIL_SMTP_HOST = "smtpHost";
-	/**
-	 * Service property
-	 *
-	 * @scr.property value="25" description="Specifies the SMTP port."
-	 */
-	public static final String MAIL_SMTP_PORT = "smtpPort";
-	/**
-	 * Service property
-	 *
-	 * @scr.property value="myPassword" description=
-	 *               "Specifies the authentication password (plain text) for SMTP."
-	 */
-	public static final String MAIL_SMTP_PASSWORD = "smtpPassword";
-	/**
-	 * Service property
-	 *
-	 * @scr.property value="myUserName"
-	 *               description="Specifies the User to authenticate for SMTP."
-	 */
-	public static final String MAIL_SMTP_USER = "smtpUser";
-	private Properties properties;
-	/**
-	 * @scr.reference
-	 */
+	@Reference
 	private TcManager tcManager;
 
-	/**
-	 * @scr.reference
-	 */
+	@Reference
 	RendererFactory rendererFactory;
 
+	@Reference
+	MailSessionFactory mailSessionFactory;
+
 
 	private static String SYSTEM_GRAPH_URI = "http://tpf.localhost/system.graph";
 	private UriRef systemGraphUri = new UriRef(SYSTEM_GRAPH_URI);
 
-	protected void activate(ComponentContext componentContext) {
-		properties = new Properties();
-
-		properties.setProperty("mail.debug", "false");
-
-		if (((String) componentContext.getProperties().get(MAIL_USE_TLS)).equals("true")) {
-			properties.setProperty("mail.smtp.starttls.enable", "true");
-			properties.setProperty("mail.smtp.auth", "true");
-			properties.setProperty("mail.smtp.socketFactory.fallback", "false");
-		}
-
-		properties.setProperty("mail.transport.protocol", "smtp");
-		properties.setProperty("mail.smtp.port", (String) componentContext.getProperties().get(MAIL_SMTP_PORT));
-		properties.setProperty("mail.smtp.host", (String) componentContext.getProperties().get(MAIL_SMTP_HOST));
-		properties.setProperty("smtpUser", (String) componentContext.getProperties().get(MAIL_SMTP_USER));
-		properties.setProperty("smtpPassword", (String) componentContext.getProperties().get(MAIL_SMTP_PASSWORD));
-	}
-
 	@Override
 	public void sendEmailToUser(final String fromUser, final String toUser,
 			final String subject, final String message) throws MessagingException {
@@ -206,7 +155,6 @@ public class MailManImpl implements Mail
 		}
 	}
 
-
 	@Override
 	public void sendEmailToUsers(final String fromUser, final String[] toUsers,
 			final String subject, final GraphNode graphNode,
@@ -256,36 +204,35 @@ public class MailManImpl implements Mail
 		sendEmail(from, to, cc, bcc, subject, message, "text/plain", null, null);
 	}
 
-	public void sendEmail(final InternetAddress from,final InternetAddress to,
-			final InternetAddress[] cc,	final InternetAddress[] bcc,
-			final String subject, final Object content,	final String mediaType,
-			final List<MediaType> acceptableMediaTypes,	final String mode)
+	@Override
+    public void sendEmail(InternetAddress from, InternetAddress to,
+            InternetAddress[] cc, InternetAddress[] bcc, String subject,
+            String message, String mediaType) throws MessagingException {
+        AccessController.checkPermission(
+                new MailManPermission("", MailManPermission.SEND_MAIL));
+        sendEmail(from, to, cc, bcc, subject, message, mediaType, null, null);
+    }
+
+	private void sendEmail(final InternetAddress from, final InternetAddress to,
+			final InternetAddress[] cc, final InternetAddress[] bcc,
+			final String subject, final Object content, final String mediaType,
+			final List<MediaType> acceptableMediaTypes, final String mode)
 			throws MessagingException {
+		final Session session = mailSessionFactory.getSession();
 		try {
 			AccessController.doPrivileged(new PrivilegedExceptionAction<Object>() {
 
 				@Override
 				public Object run() throws MessagingException {
-					Authenticator auth = new Authenticator() {
-
-						@Override
-						public PasswordAuthentication getPasswordAuthentication() {
-							return new PasswordAuthentication(properties.
-									getProperty("smtpUser"),
-									properties.getProperty("smtpPassword"));
-						}
-					};
-					Session session = Session.getInstance(properties, auth);
 					MimeMessage mimeMessage = new MimeMessage(session);
 					Date date = new Date();
 					mimeMessage.setSentDate(date);
 					mimeMessage.setSubject(subject);
 					if (content instanceof GraphNode) {
 						GraphNode graphNode = (GraphNode) content;
-						Renderer renderer = rendererFactory.
-							createRenderer(graphNode, mode, 
-							acceptableMediaTypes == null ? Collections.singletonList(
-							MediaType.WILDCARD_TYPE) : acceptableMediaTypes);
+						Renderer renderer = rendererFactory.createRenderer(graphNode, mode,
+								acceptableMediaTypes == null ? Collections.singletonList(
+								MediaType.WILDCARD_TYPE) : acceptableMediaTypes);
 						if (renderer == null) {
 							throw new MessagingException("No renderer appropriate found");
 						}
@@ -318,19 +265,21 @@ public class MailManImpl implements Mail
 
 	}
 
+
 	private InternetAddress getUserAddress(String user) throws MessagingException {
 		MGraph systemGraph = tcManager.getMGraph(systemGraphUri);
 
-		final String queryString = "SELECT ?email WHERE { " +
-				"?x " + FOAF.mbox + " ?email . " +
-				"?x " + PLATFORM.userName + " \"" + user + "\" . " +
-				"}";
+		final String queryString = "SELECT ?email WHERE { "
+				+ "?x " + FOAF.mbox + " ?email . "
+				+ "?x " + PLATFORM.userName + " \"" + user + "\" . "
+				+ "}";
 		try {
 			SelectQuery selectQuery = (SelectQuery) QueryParser.getInstance().parse(queryString);
 			ResultSet result = tcManager.executeSparqlQuery(selectQuery, systemGraph);
 			if (result.hasNext()) {
 				Resource email = result.next().get("email");
 				String emailString = ((UriRef) email).getUnicodeString();
+				//TODO should add personal name (if available) as well
 				return new InternetAddress(emailString.substring("mailto:".length()));
 			}
 		} catch (ParseException ex) {

Added: incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.platform.mail/src/main/java/org/apache/clerezza/platform/mail/MailSessionFactory.java
URL: http://svn.apache.org/viewvc/incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.platform.mail/src/main/java/org/apache/clerezza/platform/mail/MailSessionFactory.java?rev=951382&view=auto
==============================================================================
--- incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.platform.mail/src/main/java/org/apache/clerezza/platform/mail/MailSessionFactory.java (added)
+++ incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.platform.mail/src/main/java/org/apache/clerezza/platform/mail/MailSessionFactory.java Fri Jun  4 11:43:47 2010
@@ -0,0 +1,103 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.clerezza.platform.mail;
+
+
+import java.util.Properties;
+import javax.mail.Authenticator;
+import javax.mail.PasswordAuthentication;
+import javax.mail.Session;
+import org.apache.felix.scr.annotations.Component;
+import org.apache.felix.scr.annotations.Service;
+import org.osgi.service.component.ComponentContext;
+
+/**
+ * Provides an isntance of javax.mail.Session matching the configuration of this 
+ * service.
+ *
+ * @author reto
+ */
+@Component
+@Service(MailSessionFactory.class)
+public class MailSessionFactory {
+	/**
+	 * Service property
+	 *
+	 * @scr.property value="false"
+	 *               description="Specifies if TLS (SSL) encryption is used."
+	 */
+	public static final String MAIL_USE_TLS = "useTLS";
+	/**
+	 * Service property
+	 *
+	 * @scr.property value="smtp.myHost.org"
+	 *               description="Specifies the SMTP host."
+	 */
+	public static final String MAIL_SMTP_HOST = "smtpHost";
+	/**
+	 * Service property
+	 *
+	 * @scr.property value="25" description="Specifies the SMTP port."
+	 */
+	public static final String MAIL_SMTP_PORT = "smtpPort";
+	/**
+	 * Service property
+	 *
+	 * @scr.property value="myPassword" description=
+	 *               "Specifies the authentication password (plain text) for SMTP."
+	 */
+	public static final String MAIL_SMTP_PASSWORD = "smtpPassword";
+	/**
+	 * Service property
+	 *
+	 * @scr.property value="myUserName"
+	 *               description="Specifies the User to authenticate for SMTP."
+	 */
+	public static final String MAIL_SMTP_USER = "smtpUser";
+
+	private Properties properties;
+
+	protected void activate(ComponentContext componentContext) {
+		properties = new Properties();
+		properties.setProperty("mail.debug", "false");
+		if (((String) componentContext.getProperties().get(MAIL_USE_TLS)).equals("true")) {
+			properties.setProperty("mail.smtp.starttls.enable", "true");
+			properties.setProperty("mail.smtp.auth", "true");
+			properties.setProperty("mail.smtp.socketFactory.fallback", "false");
+		}
+		properties.setProperty("mail.transport.protocol", "smtp");
+		properties.setProperty("mail.smtp.port", (String) componentContext.getProperties().get(MAIL_SMTP_PORT));
+		properties.setProperty("mail.smtp.host", (String) componentContext.getProperties().get(MAIL_SMTP_HOST));
+		properties.setProperty("smtpUser", (String) componentContext.getProperties().get(MAIL_SMTP_USER));
+		properties.setProperty("smtpPassword", (String) componentContext.getProperties().get(MAIL_SMTP_PASSWORD));
+	}
+
+	public Session getSession() {
+		Authenticator auth = new Authenticator() {
+			@Override
+			public PasswordAuthentication getPasswordAuthentication() {
+				return new PasswordAuthentication(properties.
+						getProperty("smtpUser"),
+						properties.getProperty("smtpPassword"));
+			}
+		};
+		return Session.getInstance(properties, auth);
+	}
+}