PageRenderTime 91ms CodeModel.GetById 40ms app.highlight 16ms RepoModel.GetById 14ms app.codeStats 0ms

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