Skip to content

Instantly share code, notes, and snippets.

@ramrrr
Created November 6, 2014 09:23
Show Gist options
  • Select an option

  • Save ramrrr/2e5a9e4ac896c1b64523 to your computer and use it in GitHub Desktop.

Select an option

Save ramrrr/2e5a9e4ac896c1b64523 to your computer and use it in GitHub Desktop.
Test case fix
package heimdall.channel.email;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.context.support.ReloadableResourceBundleMessageSource;
import org.springframework.core.convert.converter.Converter;
import backbone.sending.SendingChannel;
import backbone.sending.SendingProperty;
import com.amazonaws.AmazonServiceException;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.services.s3.AmazonS3Client;
import com.google.common.base.Optional;
import com.typesafe.config.Config;
import com.typesafe.config.ConfigValue;
/**
* @author Ståle Undheim <staale@staale.org>
*/
public class EmailChannelToChannelSpecifierConverter implements
Converter<EmailChannel, SendingChannel> {
private Config config;
private MessageSource messageSource;
/**
* Initializing constructor.
*
* @param config
* conversion config to use.
* @param reloadableResourceBundleMessageSource
* for messageSource
*/
/**
* Default constructor.
*/
public EmailChannelToChannelSpecifierConverter() {
}
/**
* Initializing constructor.
*
* @param config
* conversion config to use.
*/
public EmailChannelToChannelSpecifierConverter(final Config config) {
this.config = config;
}
public EmailChannelToChannelSpecifierConverter(
final ReloadableResourceBundleMessageSource reloadableResourceBundleMessageSource,
final Config config) {
this.config = config;
this.messageSource = reloadableResourceBundleMessageSource;
}
public MessageSource getMessageSource() {
return messageSource;
}
@Override
public SendingChannel convert(final EmailChannel source) {
final SendingChannel channelSpecifier = new SendingChannel();
channelSpecifier.setChannelId("email");
channelSpecifier.setProviderId("ums");
String smtpServerName = source.getChannelConfig().getSmtpServer();
Map<String, Object> smtpMap = getSMTPDetails(smtpServerName);
Object smtpAuth = smtpMap.get("auth");
Object replyTo = smtpMap.get("replyTo");
Object hostName = smtpMap.get("hostname");
Object port = smtpMap.get("port");
List<SendingProperty> sendingProperty = new ArrayList<>();
sendingProperty.add(new SendingProperty("from", source
.getChannelConfig().getEmailId()));
sendingProperty.add(new SendingProperty("host",
hostName != null ? hostName.toString() : null));
sendingProperty.add(new SendingProperty("port", port != null ? port
.toString() : null));
sendingProperty.add(new SendingProperty("auth",
smtpAuth != null ? smtpAuth.toString() : "false"));
sendingProperty.add(new SendingProperty("fromName", source
.getChannelConfig().getSenderName()));
sendingProperty.add(new SendingProperty("replyTo",
replyTo != null ? replyTo.toString() : null));
if (smtpAuth != null && smtpAuth.equals("true")) {
sendingProperty.add(new SendingProperty("username", String
.valueOf(smtpMap.get("username"))));
sendingProperty.add(new SendingProperty("password", String
.valueOf(smtpMap.get("password"))));
}
channelSpecifier.setChannelProperties(sendingProperty);
final String header = smtpMap.get("emailHeader") == null ? ""
: smtpMap.get("emailHeader").toString();
String attachmentString = "";
String attach = "";
for (EmailAttachment e : source.getMessage().getAttachmentFiles()) {
attachmentString = generateURL(e.getAttachmentKey());
attach = attach
+ messageSource.getMessage("email.attach.div", null,
LocaleContextHolder.getLocale())
+ attachmentString
+ messageSource.getMessage("email.attach.end", null,
LocaleContextHolder.getLocale())
+ e.getAttachmentFileName()
+ messageSource.getMessage("ending.tag", null,
LocaleContextHolder.getLocale());
}
String paperClipImage = messageSource.getMessage("div.email.icon.attachments", null,
LocaleContextHolder.getLocale());
final String fullBody = header + source.getMessage().getBody() + paperClipImage
+ attach;
channelSpecifier.setMessage(Arrays.asList(new SendingProperty(
"subject", source.getMessage().getSubject()),
new SendingProperty("body", fullBody)));
return channelSpecifier;
}
/**
*
* @param key
* URl of the file from Amazon
* @return String
*/
public String generateURL(final String key) {
try {
final Map<String, String> configValue = new HashMap<String, String>();
final Set<Map.Entry<String, ConfigValue>> settings = config
.getConfig("email").entrySet();
for (Map.Entry<String, ConfigValue> db : settings) {
configValue.put(db.getKey(),
String.valueOf(db.getValue().unwrapped()));
}
final AmazonS3Client s3client = new AmazonS3Client(
new BasicAWSCredentials(
configValue.get("storage.awsAccessKey"),
configValue.get("storage.awsSecretKey")));
final Date expiration = new Date(System.currentTimeMillis()
+ TimeUnit.DAYS.toMillis(365));
s3client.getObject(configValue.get("storage.bucket"), key);
return s3client.generatePresignedUrl(
configValue.get("storage.bucket"), key, expiration)
.toString();
} catch (AmazonServiceException e) {
if (e.getErrorCode().equals("NoSuchKey")) {
return Optional.absent().toString();
} else {
throw new IllegalStateException(e);
}
}
}
@SuppressWarnings("unchecked")
private Map<String, Object> getSMTPDetails(final String smtpServerName) {
Map<String, Object> smtpResult = null;
Set<Map.Entry<String, ConfigValue>> smtpSet = ((Map<String, ConfigValue>) config
.getValue("smtp").unwrapped()).entrySet();
Iterator<Map.Entry<String, ConfigValue>> smtps = smtpSet.iterator();
while (smtps.hasNext()) {
Map.Entry<String, ConfigValue> smtp = smtps.next();
Map<String, Object> smtpDetail = (Map<String, Object>) smtp
.getValue();
if (smtpDetail.get("friendlyname").equals(smtpServerName)) {
smtpResult = smtpDetail;
break;
}
}
return smtpResult;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment