Skip to content

Commit

Permalink
Merge pull request #5 from Kowalski-IO/2.4.0
Browse files Browse the repository at this point in the history
2.4.0
  • Loading branch information
BrandonKowalski authored Jun 26, 2016
2 parents 870fbe2 + bb7fd39 commit c42b835
Show file tree
Hide file tree
Showing 60 changed files with 5,785 additions and 802 deletions.
4 changes: 2 additions & 2 deletions claptrap-api-external/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@
<parent>
<groupId>io.kowalski</groupId>
<artifactId>claptrap</artifactId>
<version>2.2.1</version>
<version>2.4.0</version>
</parent>

<groupId>io.kowalski.claptrap.api</groupId>
<artifactId>claptrap-api-external</artifactId>
<name>claptrap-api-external</name>

<properties>
<okhttp.version>3.2.0</okhttp.version>
<okhttp.version>3.3.1</okhttp.version>
</properties>

<dependencies>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,19 +81,19 @@ public Collection<Email> fetchAll() throws ClaptrapException {
}

/**
* Fetch all of the emails for a particular server stored by the targeted
* Claptrap instance.
* Fetch all of the emails for a particular environment stored by the
* targeted Claptrap instance.
*
* @param serverName for the emails to fetch
* @return all of the emails for a particular server stored in this
* @param environmentName for the emails to fetch
* @return all of the emails for a particular environment stored in this
* instance.
* @throws ClaptrapException if the emails could not be fetched or parsed.
*/
@Override
public Collection<Email> fetchAllForServer(final String serverName) throws ClaptrapException {
public Collection<Email> fetchAllForEnvironment(final String environment) throws ClaptrapException {
try {
final List<Email> result = new ArrayList<Email>();
final Request request = new Request.Builder().url(instanceURL.concat("/api/emails/").concat(serverName))
final Request request = new Request.Builder().url(instanceURL.concat("/api/emails/").concat(environment))
.build();
final Response response = okhttp.newCall(request).execute();
final List<Email> parsedEmails = mapper.readValue(response.body().string(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,12 @@ public void allEmails() throws EmailException, ClaptrapException {
}

@Test
public void allEmailsForServer() throws ClaptrapException, EmailException {
seedForServerEmails();
public void allEmailsForEnvironment() throws ClaptrapException, EmailException {
seedForEnvironmentEmails();

final Collection<Email> fetchedEmails = mailbox.fetchAllForServer("test.claptrap.kowalski.io");
final Collection<Email> fetchedEmails = mailbox.fetchAllForEnvironment("test.claptrap.kowalski.io");

assertEquals("seedForServerEmails sends 2 emails total for server forServer", 2, fetchedEmails.size());
assertEquals("seedForEnvironmentEmails sends 2 emails total for server forEnvironment", 2, fetchedEmails.size());

}

Expand All @@ -80,7 +80,7 @@ public void allEmailsForCriteria() throws EmailException, ClaptrapException {

final List<Email> fetchedEmails = Lists.newArrayList(mailbox.fetchForCriteria(filter));

assertEquals("seedForServerEmails sends 1 emails total to Nugget", 1, fetchedEmails.size());
assertEquals("seedForEnvironmentEmails sends 1 emails total to Nugget", 1, fetchedEmails.size());

assertEquals("[email protected]", fetchedEmails.get(0).getRecipient());
}
Expand All @@ -96,20 +96,20 @@ private void seedAllEmails() throws EmailException {
email.setFrom("[email protected]", "Claptrap");
email.setSubject("Test simple email");

email.setMsg("This is a simple email test to see if Claptrap actually works!");
email.setMsg("This is a simple email test to see if Claptrap actually works! Here is a link to see if linkify is working: https://kowalski.io");

email.send();

}

private void seedForServerEmails() throws EmailException {
private void seedForEnvironmentEmails() throws EmailException {
final SimpleEmail email = new SimpleEmail();
email.setHostName(hostname);
email.setSmtpPort(smtpPort);

email.addTo("[email protected]", "John Johnson");
email.addTo("[email protected]", "Frank Underwood");
email.setFrom("forServer@test.claptrap.kowalski.io", "Claptrap");
email.setFrom("forEnvironment@test.claptrap.kowalski.io", "Claptrap");
email.setSubject("Test simple email");

email.setMsg("This is a simple email test to see if Claptrap actually works!");
Expand Down
8 changes: 6 additions & 2 deletions claptrap-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,16 @@
<parent>
<groupId>io.kowalski</groupId>
<artifactId>claptrap</artifactId>
<version>2.2.1</version>
<version>2.4.0</version>
</parent>

<groupId>io.kowalski.claptrap</groupId>
<artifactId>claptrap-core</artifactId>
<name>claptrap-core</name>

<properties>
<subethasmtp.version>3.1.7</subethasmtp.version>
</properties>

<dependencies>

Expand All @@ -37,7 +41,7 @@
<dependency>
<groupId>org.subethamail</groupId>
<artifactId>subethasmtp</artifactId>
<version>3.1.7</version>
<version>${subethasmtp.version}</version>
</dependency>

</dependencies>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,26 @@

import com.google.inject.AbstractModule;
import com.google.inject.TypeLiteral;
import com.google.inject.name.Names;
import com.hazelcast.config.Config;
import com.hazelcast.config.JoinConfig;
import com.hazelcast.config.NetworkConfig;
import com.hazelcast.core.Hazelcast;
import com.hazelcast.core.HazelcastInstance;

import io.kowalski.claptrap.models.Email;
import io.kowalski.claptrap.models.Log;
import io.kowalski.claptrap.models.SMS;
import io.kowalski.claptrap.storage.BroadcastService;
import io.kowalski.claptrap.storage.email.EmailBroadcastService;
import io.kowalski.claptrap.storage.logs.LogBroadcastService;
import io.kowalski.claptrap.storage.sms.SMSBroadcastService;

public class GuiceModule extends AbstractModule {

private final HazelcastInstance hazelcast;
private final ConcurrentHashMap<String, SseBroadcaster> broadcasterMap;
private final ConcurrentHashMap<String, SseBroadcaster> emailBroadcasterMap;
private final ConcurrentHashMap<String, SseBroadcaster> logBroadcasterMap;

public GuiceModule() {
final Config config = new Config();
Expand All @@ -38,15 +42,19 @@ public GuiceModule() {

hazelcast.getConfig().setProperty("hazelcast.logging.type", "slf4j");

broadcasterMap = new ConcurrentHashMap<String, SseBroadcaster>();
emailBroadcasterMap = new ConcurrentHashMap<String, SseBroadcaster>();
logBroadcasterMap = new ConcurrentHashMap<String, SseBroadcaster>();
}

@Override
protected void configure() {
bind(HazelcastInstance.class).toInstance(hazelcast);
bind(new TypeLiteral<ConcurrentHashMap<String, SseBroadcaster>>() {
}).toInstance(broadcasterMap);
}).annotatedWith(Names.named("emailBroadcastMap")).toInstance(emailBroadcasterMap);
bind(new TypeLiteral<ConcurrentHashMap<String, SseBroadcaster>>() {
}).annotatedWith(Names.named("logBroadcastMap")).toInstance(logBroadcasterMap);
bind(new TypeLiteral<BroadcastService<Email, String>>(){}).to(EmailBroadcastService.class);
bind(new TypeLiteral<BroadcastService<Log, String>>(){}).to(LogBroadcastService.class);
bind(new TypeLiteral<BroadcastService<SMS, String>>(){}).to(SMSBroadcastService.class);
}

Expand All @@ -55,7 +63,7 @@ public final HazelcastInstance getHazelcast() {
}

public final ConcurrentHashMap<String, SseBroadcaster> getBroadcasterMap() {
return broadcasterMap;
return emailBroadcasterMap;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public List<Email> parseMessages() throws Exception {
for (final String recipient : recipients) {
final Email email = new Email();

email.setServerName(retrieveServerName(from));
email.setEnvironment(retrieveServerName(from));
email.setSender(from);
email.setSubject(messageParser.getSubject());

Expand All @@ -108,7 +108,7 @@ public List<Email> parseMessages() throws Exception {
private Email createExceptionEmail(final Exception e) {
final Email email = new Email();

email.setServerName("Claptrap.error");
email.setEnvironment("Claptrap.error");
email.setSender("[email protected]");
email.setSubject("Exception occurred processing email");
email.setPlainBody(e.getMessage());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.util.concurrent.ConcurrentHashMap;

import javax.inject.Inject;
import javax.inject.Named;
import javax.ws.rs.core.MediaType;

import org.glassfish.jersey.media.sse.EventOutput;
Expand All @@ -19,13 +20,13 @@ public class EmailBroadcastService implements BroadcastService<Email, String> {
private final ConcurrentHashMap<String, SseBroadcaster> broadcasterMap;

@Inject
public EmailBroadcastService(final ConcurrentHashMap<String, SseBroadcaster> broadcasterMap) {
public EmailBroadcastService(@Named("emailBroadcastMap") final ConcurrentHashMap<String, SseBroadcaster> broadcasterMap) {
this.broadcasterMap = broadcasterMap;
}

@Override
public void broadcast(final Email email) {
final SseBroadcaster broadcaster = fetchBroadcaster(email.getServerName());
final SseBroadcaster broadcaster = fetchBroadcaster(email.getEnvironment());

final OutboundEvent.Builder eventBuilder = new OutboundEvent.Builder();
final OutboundEvent event = eventBuilder.name("message")
Expand All @@ -37,30 +38,30 @@ public void broadcast(final Email email) {
}

@Override
public void broadcast(final List<Email> broadcastables) {
for (final Email email : broadcastables) {
public void broadcast(final List<Email> emails) {
for (final Email email : emails) {
broadcast(email);
}
}

@Override
public EventOutput generateEventOutput(final String serverName) {
final SseBroadcaster broadcaster = fetchBroadcaster(serverName);
public EventOutput generateEventOutput(final String environmentName) {
final SseBroadcaster broadcaster = fetchBroadcaster(environmentName);
final EventOutput eventOutput = new EventOutput();
broadcaster.add(eventOutput);
return eventOutput;
}

@Override
public SseBroadcaster fetchBroadcaster(final String serverName) {
public SseBroadcaster fetchBroadcaster(final String environmentName) {
final Optional<SseBroadcaster> optionalBroadcaster =
Optional.ofNullable(broadcasterMap.get(serverName));
Optional.ofNullable(broadcasterMap.get(environmentName));

SseBroadcaster broadcaster;

if (!optionalBroadcaster.isPresent()) {
broadcaster = new SseBroadcaster();
broadcasterMap.put(serverName, broadcaster);
broadcasterMap.put(environmentName, broadcaster);
} else {
broadcaster = optionalBroadcaster.get();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public void store(final Email email) {
@Override
public void store(final List<Email> emails) {
for (final Email email : emails) {
fetchMap(MAP_NAME).set(email.getId(), email);
store(email);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package io.kowalski.claptrap.storage.logs;

import java.util.List;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;

import javax.inject.Inject;
import javax.inject.Named;
import javax.ws.rs.core.MediaType;

import org.glassfish.jersey.media.sse.EventOutput;
import org.glassfish.jersey.media.sse.OutboundEvent;
import org.glassfish.jersey.media.sse.SseBroadcaster;

import io.kowalski.claptrap.models.Log;
import io.kowalski.claptrap.storage.BroadcastService;

public class LogBroadcastService implements BroadcastService<Log, String> {

private final ConcurrentHashMap<String, SseBroadcaster> broadcasterMap;

@Inject
public LogBroadcastService(@Named("logBroadcastMap") final ConcurrentHashMap<String, SseBroadcaster> broadcasterMap) {
this.broadcasterMap = broadcasterMap;
}

@Override
public void broadcast(final Log log) {
final SseBroadcaster broadcaster = fetchBroadcaster(log.getEnvironment());

final OutboundEvent.Builder eventBuilder = new OutboundEvent.Builder();
final OutboundEvent event = eventBuilder.name("message").mediaType(MediaType.APPLICATION_JSON_TYPE)
.data(Log.class, log).build();

broadcaster.broadcast(event);
}

@Override
public void broadcast(final List<Log> logs) {
for (final Log log : logs) {
broadcast(log);
}
}

@Override
public EventOutput generateEventOutput(final String environmentName) {
final SseBroadcaster broadcaster = fetchBroadcaster(environmentName);
final EventOutput eventOutput = new EventOutput();
broadcaster.add(eventOutput);
return eventOutput;
}

@Override
public SseBroadcaster fetchBroadcaster(final String environmentName) {
final Optional<SseBroadcaster> optionalBroadcaster =
Optional.ofNullable(broadcasterMap.get(environmentName));

SseBroadcaster broadcaster;

if (!optionalBroadcaster.isPresent()) {
broadcaster = new SseBroadcaster();
broadcasterMap.put(environmentName, broadcaster);
} else {
broadcaster = optionalBroadcaster.get();
}

return broadcaster;
}

}
Loading

0 comments on commit c42b835

Please sign in to comment.