Skip to content
This repository has been archived by the owner on Nov 23, 2021. It is now read-only.

Commit

Permalink
Browse files Browse the repository at this point in the history
Added support for multiple mailboxes
  • Loading branch information
piotr-wilczynski committed Apr 24, 2017
1 parent eb59287 commit a5259ea
Show file tree
Hide file tree
Showing 22 changed files with 359 additions and 122 deletions.
4 changes: 4 additions & 0 deletions bb-email/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@
<groupId>com.google.inject</groupId>
<artifactId>guice</artifactId>
</dependency>
<dependency>
<groupId>com.google.inject.extensions</groupId>
<artifactId>guice-assistedinject</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
Expand Down
19 changes: 12 additions & 7 deletions bb-email/src/main/java/com/cognifide/qa/bb/email/EmailClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@
import org.slf4j.LoggerFactory;

import com.cognifide.qa.bb.email.connector.Connector;
import com.google.inject.Inject;
import com.cognifide.qa.bb.email.connector.ConnectorFactory;
import com.google.inject.assistedinject.Assisted;
import com.google.inject.assistedinject.AssistedInject;

/**
* This class represents email client.
Expand All @@ -49,13 +51,16 @@ public class EmailClient {
private Folder folder;

/**
* @param connection MailBox protocol connection.
* @param emailDataFactory Email data factory
* @param id Identification of configuration.
* @param connectionFactory Email config factory
* @param emailConfigFactory Email config factory
*/
@Inject
public EmailClient(Connector connection, EmailDataFactory emailDataFactory) {
this.connection = connection;
this.emailDataFactory = emailDataFactory;
@AssistedInject
public EmailClient(@Assisted String id, ConnectorFactory connectionFactory,
EmailConfigFactory emailConfigFactory) {
EmailConfig emailConfig = emailConfigFactory.create(id);
this.connection = connectionFactory.create(id).get();
this.emailDataFactory = new EmailDataFactory(emailConfig);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*-
* #%L
* Bobcat
* %%
* Copyright (C) 2016 Cognifide Ltd.
* %%
* Licensed 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.
* #L%
*/
package com.cognifide.qa.bb.email;

public interface EmailClientFactory {

EmailClient create(String id);
}
53 changes: 53 additions & 0 deletions bb-email/src/main/java/com/cognifide/qa/bb/email/EmailConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*-
* #%L
* Bobcat
* %%
* Copyright (C) 2016 Cognifide Ltd.
* %%
* Licensed 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.
* #L%
*/
package com.cognifide.qa.bb.email;

import java.util.Collections;
import java.util.Map;

import org.apache.commons.lang3.text.StrSubstitutor;

import com.cognifide.qa.bb.email.constants.EmailConfigKeys;
import com.google.inject.Injector;
import com.google.inject.Key;
import com.google.inject.assistedinject.Assisted;
import com.google.inject.assistedinject.AssistedInject;
import com.google.inject.name.Names;

public class EmailConfig {

private final Map<String, String> map;
private final Injector injector;

@AssistedInject
public EmailConfig(@Assisted String id, Injector injector) {
this.map = Collections.singletonMap(EmailConfigKeys.ID, id);
this.injector = injector;
}

public String getParameter(String name) {
return getParameter(String.class, name);
}

public <T> T getParameter(Class<T> type, String name) {
String realId = StrSubstitutor.replace(name, map);
return injector.getInstance(Key.get(type, Names.named(realId)));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*-
* #%L
* Bobcat
* %%
* Copyright (C) 2016 Cognifide Ltd.
* %%
* Licensed 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.
* #L%
*/
package com.cognifide.qa.bb.email;

public interface EmailConfigFactory {

EmailConfig create(String id);
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
import com.cognifide.qa.bb.email.connector.EmailException;
import com.cognifide.qa.bb.email.constants.EmailConfigKeys;
import com.google.inject.Inject;
import com.google.inject.name.Named;

/**
* This class creates EmailData.
Expand All @@ -45,11 +44,11 @@ public final class EmailDataFactory {

private static final Logger LOGGER = LoggerFactory.getLogger(EmailDataFactory.class);

private final Pattern addressPattern;
private Pattern addressPattern;

@Inject
public EmailDataFactory(
@Named(EmailConfigKeys.EMAIL_ADDRESS_PATTERN) String addressPatternString) {
public EmailDataFactory(EmailConfig emailConfig) {
String addressPatternString = emailConfig.getParameter(EmailConfigKeys.EMAIL_ADDRESS_PATTERN);
this.addressPattern = Pattern.compile(addressPatternString);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@
*/
package com.cognifide.qa.bb.email;

import com.cognifide.qa.bb.email.connector.Connector;
import com.cognifide.qa.bb.email.connector.ConnectorProvider;
import com.cognifide.qa.bb.email.connector.ConnectorFactory;
import com.google.inject.AbstractModule;
import com.google.inject.assistedinject.FactoryModuleBuilder;

/**
* Install this module to enable email service. After installation, following email protocols will
Expand All @@ -36,6 +36,9 @@ public class EmailModule extends AbstractModule {

@Override
protected void configure() {
bind(Connector.class).toProvider(ConnectorProvider.class);
install(new FactoryModuleBuilder().build(ConnectorFactory.class));
install(new FactoryModuleBuilder().build(EmailConfigFactory.class));
install(new FactoryModuleBuilder().build(EmailClientFactory.class));
install(new FactoryModuleBuilder().build(EmailSenderFactory.class));
}
}
24 changes: 12 additions & 12 deletions bb-email/src/main/java/com/cognifide/qa/bb/email/EmailSender.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,36 +25,36 @@

import com.cognifide.qa.bb.email.connector.EmailException;
import com.cognifide.qa.bb.email.constants.EmailConfigKeys;
import com.google.inject.Inject;
import com.google.inject.Singleton;
import com.google.inject.name.Named;
import com.google.inject.assistedinject.Assisted;
import com.google.inject.assistedinject.AssistedInject;

/**
* Simple class for sending emails. It uses org.apache.commons.mail library.
*/
@Singleton
public class EmailSender {

@Inject
@Named(EmailConfigKeys.EMAIL_USERNAME)
private String username;

@Inject
@Named(EmailConfigKeys.EMAIL_PASSWORD)
private String password;

@Inject
@Named(EmailConfigKeys.SMTP_SERVER_ADDRESS)
private String smtpServer;

@Inject
@Named(EmailConfigKeys.SMTP_SERVER_PORT)
private int smtpPort;

@Inject
@Named(EmailConfigKeys.SMTP_SERVER_SECURE)
private boolean secure;

@AssistedInject
public EmailSender(@Assisted String id, EmailConfigFactory factory) {
EmailConfig emailConfig = factory.create(id);
this.username = emailConfig.getParameter(EmailConfigKeys.EMAIL_USERNAME);
this.password = emailConfig.getParameter(EmailConfigKeys.EMAIL_PASSWORD);
this.smtpServer = emailConfig.getParameter(EmailConfigKeys.SMTP_SERVER_ADDRESS);
this.smtpPort = emailConfig.getParameter(Integer.class, EmailConfigKeys.SMTP_SERVER_PORT);
this.secure = emailConfig.getParameter(Boolean.class, EmailConfigKeys.SMTP_SERVER_SECURE);
}

public void sendEmail(final EmailData emailData) {
try {
Email email = new SimpleEmail();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*-
* #%L
* Bobcat
* %%
* Copyright (C) 2016 Cognifide Ltd.
* %%
* Licensed 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.
* #L%
*/
package com.cognifide.qa.bb.email;

public interface EmailSenderFactory {

EmailSender create(String id);
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,37 +19,32 @@
*/
package com.cognifide.qa.bb.email.connector;

import com.cognifide.qa.bb.email.EmailConfig;
import com.cognifide.qa.bb.email.constants.EmailConfigKeys;
import com.google.inject.Inject;
import com.google.inject.Singleton;
import com.google.inject.name.Named;

/**
* This class configures email connector using data from property file.
*/
@Singleton
public class ConnectorConfig {

@Inject
@Named(EmailConfigKeys.EMAIL_USERNAME)
private String username;

@Inject
@Named(EmailConfigKeys.EMAIL_PASSWORD)
private String password;

@Inject
@Named(EmailConfigKeys.EMAIL_SERVER_PORT)
private int port;

@Inject
@Named(EmailConfigKeys.EMAIL_FOLDER_NAME)
private String folderName;

@Inject
@Named(EmailConfigKeys.EMAIL_SERVER_ADDRESS)
private String server;

public ConnectorConfig(EmailConfig emailConfig) {
this.username = emailConfig.getParameter(EmailConfigKeys.EMAIL_USERNAME);
this.password = emailConfig.getParameter(EmailConfigKeys.EMAIL_PASSWORD);
this.port = emailConfig.getParameter(Integer.class, EmailConfigKeys.EMAIL_SERVER_PORT);
this.folderName = emailConfig.getParameter(EmailConfigKeys.EMAIL_FOLDER_NAME);
this.server = emailConfig.getParameter(EmailConfigKeys.EMAIL_SERVER_ADDRESS);
}

/**
* @return Name of inbox folder.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*-
* #%L
* Bobcat
* %%
* Copyright (C) 2016 Cognifide Ltd.
* %%
* Licensed 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.
* #L%
*/
package com.cognifide.qa.bb.email.connector;

public interface ConnectorFactory {

ConnectorProvider create(String id);
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,28 @@
*/
package com.cognifide.qa.bb.email.connector;

import com.cognifide.qa.bb.email.EmailConfig;
import com.cognifide.qa.bb.email.EmailConfigFactory;
import com.cognifide.qa.bb.email.constants.EmailConfigKeys;
import com.google.inject.Inject;
import com.google.inject.Provider;
import com.google.inject.name.Named;
import com.google.inject.assistedinject.Assisted;
import com.google.inject.assistedinject.AssistedInject;

/**
* This class is a provider of mailbox connector.
*/
public class ConnectorProvider implements Provider<Connector> {
public class ConnectorProvider {

@Inject
private ConnectorConfig configuration;

@Inject
@Named(EmailConfigKeys.EMAIL_SERVER_PROTOCOL)
private String protocol;

@Override
@AssistedInject
public ConnectorProvider(@Assisted String id, EmailConfigFactory emailConfigFactory) {
EmailConfig emailConfig = emailConfigFactory.create(id);
this.configuration = new ConnectorConfig(emailConfig);
this.protocol = emailConfig.getParameter(EmailConfigKeys.EMAIL_SERVER_PROTOCOL);
}

public Connector get() {
ConnectorType type = ConnectorType.valueOf(protocol.trim().toUpperCase());
return type.getInstance(configuration);
Expand Down
Loading

0 comments on commit a5259ea

Please sign in to comment.