Skip to content

Commit

Permalink
Send Mail Agent
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaelfsilva committed Dec 27, 2012
1 parent 12f8b6d commit 985fa3a
Show file tree
Hide file tree
Showing 31 changed files with 2,154 additions and 0 deletions.
28 changes: 28 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>fr.insalyon.creatis</groupId>
<artifactId>sma-service</artifactId>
<version>0.1</version>
<packaging>pom</packaging>
<name>SMA</name>
<description>Send-Message-Agent</description>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<modules>
<module>sma-common</module>
<module>sma-client</module>
<module>sma-server</module>
</modules>

<scm>
<connection>scm:svn:svn+ssh://svn.creatis.insa-lyon.fr/svn/vip/SMA/trunk</connection>
<developerConnection>scm:svn:svn+ssh://svn.creatis.insa-lyon.fr/svn/vip/SMA/trunk</developerConnection>
<url>svn+ssh://svn.creatis.insa-lyon.fr/svn/vip/SMA/trunk</url>
</scm>

</project>
51 changes: 51 additions & 0 deletions sma-client/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>fr.insalyon.creatis</groupId>
<artifactId>sma-client</artifactId>
<packaging>jar</packaging>
<version>0.1</version>
<name>SMA-Client</name>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
</plugins>
</build>

<dependencies>

<dependency>
<groupId>fr.insalyon.creatis</groupId>
<artifactId>sma-common</artifactId>
<version>0.1</version>
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
<scope>test</scope>
</dependency>

</dependencies>

<distributionManagement>
<repository>
<id>creatis-releases</id>
<url>http://vip.creatis.insa-lyon.fr:9007/nexus/content/repositories/releases</url>
</repository>
<snapshotRepository>
<id>creatis-snapshots</id>
<name>Internal Snapshots</name>
<url>http://vip.creatis.insa-lyon.fr:9007/nexus/content/repositories/snapshots</url>
</snapshotRepository>
</distributionManagement>
</project>
109 changes: 109 additions & 0 deletions sma-client/src/main/java/fr/insalyon/creatis/sma/client/SMAClient.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
/* Copyright CNRS-CREATIS
*
* Rafael Ferreira da Silva
* [email protected]
* http://www.rafaelsilva.com
*
* This software is a grid-enabled data-driven workflow manager and editor.
*
* This software is governed by the CeCILL license under French law and
* abiding by the rules of distribution of free software. You can use,
* modify and/ or redistribute the software under the terms of the CeCILL
* license as circulated by CEA, CNRS and INRIA at the following URL
* "http://www.cecill.info".
*
* As a counterpart to the access to the source code and rights to copy,
* modify and redistribute granted by the license, users are provided only
* with a limited warranty and the software's author, the holder of the
* economic rights, and the successive licensors have only limited
* liability.
*
* In this respect, the user's attention is drawn to the risks associated
* with loading, using, modifying and/or developing or reproducing the
* software by the user in light of its specific status of free software,
* that may mean that it is complicated to manipulate, and that also
* therefore means that it is reserved for developers and experienced
* professionals having in-depth computer knowledge. Users are therefore
* encouraged to load and test the software's suitability as regards their
* requirements in conditions enabling the security of their systems and/or
* data to be ensured and, more generally, to use and operate it in the
* same conditions as regards security.
*
* The fact that you are presently reading this means that you have had
* knowledge of the CeCILL license and that you accept its terms.
*/
package fr.insalyon.creatis.sma.client;

import fr.insalyon.creatis.sma.common.Communication;
import fr.insalyon.creatis.sma.common.Constants;
import fr.insalyon.creatis.sma.common.ExecutorConstants;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;

/**
*
* @author Rafael Ferreira da Silva
*/
public class SMAClient {

private String host;
private int port;

public SMAClient(String host, int port) {
this.host = host;
this.port = port;
}

/**
*
* @param subject
* @param contents
* @param recipients
* @param direct
* @param username
* @throws SMAClientException
*/
public String sendEmail(String subject, String contents, String[] recipients,
boolean direct, String username) throws SMAClientException {

try {
StringBuilder sb = new StringBuilder();
for (String recipient : recipients) {
if (sb.length() > 0) {
sb.append(Constants.MSG_SEP_2);
}
sb.append(recipient);
}

Communication communication = getCommunication();
communication.sendMessage(
ExecutorConstants.MESSAGEPOOL_ADD_OPERATION + Constants.MSG_SEP_1
+ subject + Constants.MSG_SEP_1
+ contents + Constants.MSG_SEP_1
+ sb.toString() + Constants.MSG_SEP_1
+ direct + Constants.MSG_SEP_1
+ username);
communication.sendEndOfMessage();

String operationID = communication.getMessage();
communication.close();
return operationID;

} catch (IOException ex) {
throw new SMAClientException(ex);
}
}

private Communication getCommunication() throws SMAClientException {

try {
return new Communication(new Socket(host, port));

} catch (UnknownHostException ex) {
throw new SMAClientException(ex);
} catch (IOException ex) {
throw new SMAClientException(ex);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/* Copyright CNRS-CREATIS
*
* Rafael Ferreira da Silva
* [email protected]
* http://www.rafaelsilva.com
*
* This software is a grid-enabled data-driven workflow manager and editor.
*
* This software is governed by the CeCILL license under French law and
* abiding by the rules of distribution of free software. You can use,
* modify and/ or redistribute the software under the terms of the CeCILL
* license as circulated by CEA, CNRS and INRIA at the following URL
* "http://www.cecill.info".
*
* As a counterpart to the access to the source code and rights to copy,
* modify and redistribute granted by the license, users are provided only
* with a limited warranty and the software's author, the holder of the
* economic rights, and the successive licensors have only limited
* liability.
*
* In this respect, the user's attention is drawn to the risks associated
* with loading, using, modifying and/or developing or reproducing the
* software by the user in light of its specific status of free software,
* that may mean that it is complicated to manipulate, and that also
* therefore means that it is reserved for developers and experienced
* professionals having in-depth computer knowledge. Users are therefore
* encouraged to load and test the software's suitability as regards their
* requirements in conditions enabling the security of their systems and/or
* data to be ensured and, more generally, to use and operate it in the
* same conditions as regards security.
*
* The fact that you are presently reading this means that you have had
* knowledge of the CeCILL license and that you accept its terms.
*/
package fr.insalyon.creatis.sma.client;

/**
*
* @author Rafael Ferreira da Silva
*/
public class SMAClientException extends Exception {

public SMAClientException(String string) {
super(string);
}

public SMAClientException(Throwable thrwbl) {
super(thrwbl);
}
}
43 changes: 43 additions & 0 deletions sma-common/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>fr.insalyon.creatis</groupId>
<artifactId>sma-common</artifactId>
<packaging>jar</packaging>
<version>0.1</version>
<name>SMA-Common</name>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
</plugins>
</build>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
<scope>test</scope>
</dependency>
</dependencies>

<distributionManagement>
<repository>
<id>creatis-releases</id>
<url>http://vip.creatis.insa-lyon.fr:9007/nexus/content/repositories/releases</url>
</repository>
<snapshotRepository>
<id>creatis-snapshots</id>
<name>Internal Snapshots</name>
<url>http://vip.creatis.insa-lyon.fr:9007/nexus/content/repositories/snapshots</url>
</snapshotRepository>
</distributionManagement>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/* Copyright CNRS-CREATIS
*
* Rafael Ferreira da Silva
* [email protected]
* http://www.rafaelsilva.com
*
* This software is a grid-enabled data-driven workflow manager and editor.
*
* This software is governed by the CeCILL license under French law and
* abiding by the rules of distribution of free software. You can use,
* modify and/ or redistribute the software under the terms of the CeCILL
* license as circulated by CEA, CNRS and INRIA at the following URL
* "http://www.cecill.info".
*
* As a counterpart to the access to the source code and rights to copy,
* modify and redistribute granted by the license, users are provided only
* with a limited warranty and the software's author, the holder of the
* economic rights, and the successive licensors have only limited
* liability.
*
* In this respect, the user's attention is drawn to the risks associated
* with loading, using, modifying and/or developing or reproducing the
* software by the user in light of its specific status of free software,
* that may mean that it is complicated to manipulate, and that also
* therefore means that it is reserved for developers and experienced
* professionals having in-depth computer knowledge. Users are therefore
* encouraged to load and test the software's suitability as regards their
* requirements in conditions enabling the security of their systems and/or
* data to be ensured and, more generally, to use and operate it in the
* same conditions as regards security.
*
* The fact that you are presently reading this means that you have had
* knowledge of the CeCILL license and that you accept its terms.
*/
package fr.insalyon.creatis.sma.common;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;

/**
*
* @author Rafael Ferreira da Silva
*/
public class Communication {

private BufferedReader in;
private PrintWriter out;

public Communication(Socket socket) throws IOException {

this.in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
this.out = new PrintWriter(socket.getOutputStream(), true);
}

public void sendMessage(String message) {
out.println(message);
out.flush();
}

public void sendErrorMessage(String message) {
sendMessage(Constants.MSG_ERROR + message);
}

public void sendSucessMessage() {
sendMessage(Constants.MSG_SUCCESS);
}

public void sendEndOfMessage() {
sendMessage(Constants.MSG_END);
}

public String getMessage() throws IOException {

StringBuilder messageBuilder = new StringBuilder();

String message = in.readLine();
if (message == null || message.startsWith(Constants.MSG_ERROR)) {
throw new IOException(message);
}

while (!message.equals(Constants.MSG_END)) {
if (!messageBuilder.toString().isEmpty()) {
messageBuilder.append(Constants.MSG_SEP_1);
}
messageBuilder.append(message);
message = in.readLine();
}

return messageBuilder.toString();
}

public void close() throws IOException {

out.close();
}
}
Loading

0 comments on commit 985fa3a

Please sign in to comment.