/src/main/java/com/google/ie/web/controller/EmailController.java

http://thoughtsite.googlecode.com/ · Java · 220 lines · 126 code · 22 blank · 72 comment · 9 complexity · 548b114fea3c967140ea82f8c3900427 MD5 · raw file

  1. /* Copyright 2010 Google Inc.
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS.
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License
  14. */
  15. package com.google.ie.web.controller;
  16. import com.google.ie.common.constants.IdeaExchangeConstants;
  17. import com.google.ie.common.exception.IdeasExchangeException;
  18. import org.apache.commons.lang.StringUtils;
  19. import org.apache.log4j.Logger;
  20. import org.springframework.beans.factory.annotation.Autowired;
  21. import org.springframework.beans.factory.annotation.Value;
  22. import org.springframework.context.MessageSource;
  23. import org.springframework.stereotype.Controller;
  24. import org.springframework.web.bind.annotation.RequestMapping;
  25. import org.springframework.web.bind.annotation.RequestMethod;
  26. import org.springframework.web.bind.annotation.RequestParam;
  27. import java.util.Locale;
  28. import java.util.Properties;
  29. import javax.mail.Message;
  30. import javax.mail.MessagingException;
  31. import javax.mail.Session;
  32. import javax.mail.Transport;
  33. import javax.mail.internet.AddressException;
  34. import javax.mail.internet.InternetAddress;
  35. import javax.mail.internet.MimeMessage;
  36. import javax.mail.internet.MimeMessage.RecipientType;
  37. import javax.servlet.http.HttpServletRequest;
  38. /**
  39. * Handle request to send mails.
  40. *
  41. * @author asirohi
  42. *
  43. */
  44. @Controller
  45. @RequestMapping("/mail")
  46. public class EmailController {
  47. private static Logger log = Logger.getLogger(EmailController.class);
  48. private static final String PROJECT_INVITE_MAIL_SUBJECT = "Invite to join a Project";
  49. private static final String COMMA = ",";
  50. private static final String SEMICOLON = ";";
  51. private static final String MAIL_PROJECT_INVITE_KEY = "mail.project.invite";
  52. private static final int ZERO = 0;
  53. private static final int ONE = 1;
  54. private static final int TWO = 2;
  55. private static final int FOUR = 4;
  56. @Autowired
  57. private MessageSource messageSource;
  58. @Value("${adminMailId}")
  59. private String adminMailId;
  60. /**
  61. * Handle request for sending mails when a user create a project
  62. * and invite friends to become members.
  63. *
  64. * @throws IdeasExchangeException
  65. * @throws MessagingException
  66. * @throws AddressException
  67. *
  68. */
  69. @RequestMapping(value = "/joinProject", method = RequestMethod.POST)
  70. public String inviteToJoinProject(@RequestParam(required = false) String recepientEmailIds,
  71. @RequestParam(required = false) String otherInfoString, Locale locale,
  72. HttpServletRequest req)
  73. throws IdeasExchangeException, AddressException, MessagingException {
  74. String serverName = req.getServerName();
  75. String projectKey;
  76. String ownerName;
  77. String projectName;
  78. if (otherInfoString != null) {
  79. /* getting data which are required for sending mail. */
  80. String infoData[] = otherInfoString.split(COMMA);
  81. if (infoData.length > TWO) {
  82. ownerName = infoData[ZERO];
  83. projectName = infoData[ONE];
  84. projectKey = infoData[TWO];
  85. /* Iterating through all mail ids and sending messages. */
  86. for (String emailIdAndName : recepientEmailIds.split(COMMA)) {
  87. String info[] = emailIdAndName.split(SEMICOLON);
  88. if (info.length > TWO) {
  89. String displayName = info[ONE];
  90. String emailId = info[ZERO];
  91. String developerKey = info[TWO];
  92. String[] message = getMessageToSend(projectKey, ownerName, projectName,
  93. displayName,
  94. developerKey, emailId, serverName);
  95. String emailText = messageSource.getMessage(MAIL_PROJECT_INVITE_KEY,
  96. message, locale);
  97. log.info("Sending Mail to : " + emailId + " Text: " + emailText);
  98. sendMail(emailId, emailText, PROJECT_INVITE_MAIL_SUBJECT);
  99. }
  100. }
  101. }
  102. }
  103. return "queue/queue";
  104. }
  105. /**
  106. * @param displayName
  107. * @param projectName
  108. * @param ownerName
  109. * @param projectKey
  110. * @return
  111. */
  112. private String[] getMessageToSend(String projectKey, String ownerName, String projectName,
  113. String displayName, String developerKey, String emailId, String serverName) {
  114. StringBuilder messages = new StringBuilder();
  115. messages.append(ownerName);
  116. messages.append("," + projectName);
  117. messages.append("," + serverName + "/projects/joinProject"
  118. + IdeaExchangeConstants.BACKSLASH
  119. + projectKey
  120. + IdeaExchangeConstants.BACKSLASH + developerKey
  121. + IdeaExchangeConstants.BACKSLASH + emailId);
  122. return messages.toString().split(COMMA);
  123. }
  124. /**
  125. * Send mail to the given email id with the provided text and subject.
  126. *
  127. * @param recepientEmailId email id of the recepient
  128. * @param emailText text of the mail
  129. * @param subject subject of the mail
  130. * @throws IdeasExchangeException
  131. * @throws MessagingException
  132. * @throws AddressException
  133. */
  134. protected void sendMail(String recepientEmailId, String emailText,
  135. String subject) throws IdeasExchangeException, AddressException,
  136. MessagingException {
  137. Properties prop = new Properties();
  138. Session session = Session.getDefaultInstance(prop, null);
  139. Message message = new MimeMessage(session);
  140. message.setRecipient(RecipientType.TO, new InternetAddress(recepientEmailId));
  141. message.setFrom(new InternetAddress(getAdminMailId()));
  142. message.setText(emailText);
  143. message.setSubject(subject);
  144. Transport.send(message);
  145. log.info("Mail sent successfully to : " + recepientEmailId + " for " + subject);
  146. }
  147. /**
  148. * Handle request for sending mails to only one email id with the given ","
  149. * separated information string.
  150. *
  151. * @param recepientEmailId Email id of the recepient
  152. * @param otherInfoString "'" separated string containing the message key
  153. * and other message parameters.
  154. * @param locale
  155. * @return
  156. * @throws IdeasExchangeException
  157. * @throws MessagingException
  158. * @throws AddressException
  159. */
  160. @RequestMapping(value = "/singleMail", method = RequestMethod.POST)
  161. public String singleMail(@RequestParam(required = false) String recepientEmailIds,
  162. @RequestParam(required = false) String otherInfoString, Locale locale)
  163. throws IdeasExchangeException, AddressException, MessagingException {
  164. if (!StringUtils.isBlank(otherInfoString)) {
  165. String infoData[] = otherInfoString.split(COMMA);
  166. if (!StringUtils.isBlank(recepientEmailIds)) {
  167. String emailText = messageSource.getMessage(infoData[FOUR],
  168. infoData, locale);
  169. log.info("Sending Mail to : " + recepientEmailIds + " Text: " + emailText);
  170. sendMail(recepientEmailIds, emailText, infoData[ZERO] + " is " + infoData[TWO]);
  171. }
  172. }
  173. return "queue/queue";
  174. }
  175. /**
  176. * @return the messageSource
  177. */
  178. public MessageSource getMessageSource() {
  179. return messageSource;
  180. }
  181. /**
  182. * @param messageSource the messageSource to set
  183. */
  184. public void setMessageSource(MessageSource messageSource) {
  185. this.messageSource = messageSource;
  186. }
  187. /**
  188. * This method return the admin mail id which will be used for from mailid.
  189. *
  190. * @return String
  191. */
  192. public String getAdminMailId() {
  193. if (adminMailId != null) {
  194. return adminMailId.trim();
  195. }
  196. return adminMailId;
  197. }
  198. }