This repository has been archived by the owner on Mar 27, 2024. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #15 from osbornjd/jay/MarkIIINotifications
Notifications
- Loading branch information
Showing
10 changed files
with
649 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
149 changes: 149 additions & 0 deletions
149
org.eclipse.ice.commands/src/main/java/org/eclipse/ice/commands/EmailUpdateHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2019- UT-Battelle, LLC. | ||
* All rights reserved. This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Public License v1.0 | ||
* which accompanies this distribution, and is available at | ||
* http://www.eclipse.org/legal/epl-v10.html | ||
* | ||
* Contributors: | ||
* Initial API and implementation and/or initial documentation - | ||
* Jay Jay Billings, Joe Osborn | ||
*******************************************************************************/ | ||
package org.eclipse.ice.commands; | ||
|
||
import java.io.IOException; | ||
import java.util.Properties; | ||
|
||
import javax.mail.Message; | ||
import javax.mail.MessagingException; | ||
import javax.mail.PasswordAuthentication; | ||
import javax.mail.Session; | ||
import javax.mail.Transport; | ||
import javax.mail.internet.InternetAddress; | ||
import javax.mail.internet.MimeMessage; | ||
|
||
/** | ||
* This class provides email updates for Command job statuses. It takes a | ||
* provided email address, password, etc. and just sends an email to the same | ||
* address | ||
* | ||
* @author Joe Osborn | ||
* | ||
*/ | ||
public class EmailUpdateHandler implements ICommandUpdateHandler { | ||
|
||
// The email address to send the message to | ||
private String emailAddress = ""; | ||
|
||
// The text that the message should contain | ||
private String emailText = ""; | ||
|
||
// The subject of the message | ||
private String emailSubject = "Commands API Message"; | ||
|
||
// The host smtp server for the email address | ||
private String emailHost = ""; | ||
|
||
// The password for the provided email to be able to send to itself | ||
private String emailPassword = ""; | ||
|
||
/** | ||
* Default constructor | ||
*/ | ||
public EmailUpdateHandler() { | ||
} | ||
|
||
/** | ||
* See {@link org.eclipse.ice.commands.ICommandUpdateHandler#postUpdate()} | ||
*/ | ||
@Override | ||
public void postUpdate() throws IOException { | ||
// Create some properties and setup the default gmail | ||
// server properties | ||
Properties properties = System.getProperties(); | ||
properties.put("mail.smtp.port", "25"); | ||
properties.put("mail.smtp.auth", "true"); | ||
properties.put("mail.smtp.starttls.enable", "true"); // TLS | ||
// Setup mail server | ||
properties.setProperty("mail.smtp.host", emailHost); | ||
|
||
// Get the default Session object. | ||
Session session = Session.getInstance(properties, new javax.mail.Authenticator() { | ||
@Override | ||
protected PasswordAuthentication getPasswordAuthentication() { | ||
return new PasswordAuthentication(emailAddress, emailPassword); | ||
} | ||
}); | ||
// Set session to debug just to explicit | ||
session.setDebug(true); | ||
|
||
try { | ||
// Create a default MimeMessage object | ||
MimeMessage message = new MimeMessage(session); | ||
|
||
// Set the sender and recipient of the email | ||
message.setFrom(new InternetAddress(emailAddress)); | ||
message.addRecipient(Message.RecipientType.TO, new InternetAddress(emailAddress)); | ||
|
||
// Give the email a subject and message content | ||
message.setSubject(emailSubject); | ||
message.setText(emailText); | ||
|
||
Transport.send(message); | ||
logger.info("Email sent successfully"); | ||
} catch (MessagingException mex) { | ||
logger.error("Couldn't send email.", mex); | ||
throw new IOException(); | ||
} | ||
|
||
} | ||
|
||
/** | ||
* Setter for the email subject, see | ||
* {@link org.eclipse.ice.commands.notification.EmailUpdateHandler#emailSubject} | ||
* | ||
* @param emailSubject | ||
*/ | ||
public void setSubject(String emailSubject) { | ||
this.emailSubject = emailSubject; | ||
} | ||
|
||
/** | ||
* Setter for the email address, see | ||
* {@link org.eclipse.ice.commands.notification.EmailUpdateHandler#emailAddress} | ||
* | ||
* @param option | ||
*/ | ||
public void setEmailAddress(String emailAddress) { | ||
this.emailAddress = emailAddress; | ||
} | ||
|
||
/** | ||
* Setter for the email host for email authentication, see | ||
* {@link org.eclipse.ice.commands.notification.EmailUpdateHandler#emailHost} | ||
* | ||
* @param emailPassword | ||
*/ | ||
public void setSmtpHost(String emailHost) { | ||
this.emailHost = emailHost; | ||
} | ||
|
||
/** | ||
* Setter for the email password for email authentication, see | ||
* {@link org.eclipse.ice.commands.notification.EmailUpdateHandler#emailPassword} | ||
* | ||
* @param emailPassword | ||
*/ | ||
public void setPassword(String emailPassword) { | ||
this.emailPassword = emailPassword; | ||
} | ||
|
||
/** | ||
* See {@link org.eclipse.ice.commands.ICommandUpdateHandler#setMessage(String)} | ||
*/ | ||
@Override | ||
public void setMessage(String message) { | ||
this.emailText = message; | ||
} | ||
|
||
} |
87 changes: 87 additions & 0 deletions
87
...eclipse.ice.commands/src/main/java/org/eclipse/ice/commands/HTTPCommandUpdateHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2019- UT-Battelle, LLC. | ||
* All rights reserved. This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Public License v1.0 | ||
* which accompanies this distribution, and is available at | ||
* http://www.eclipse.org/legal/epl-v10.html | ||
* | ||
* Contributors: | ||
* Initial API and implementation and/or initial documentation - | ||
* Jay Jay Billings, Joe Osborn | ||
*******************************************************************************/ | ||
package org.eclipse.ice.commands; | ||
|
||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import org.apache.http.HttpResponse; | ||
import org.apache.http.NameValuePair; | ||
import org.apache.http.client.HttpClient; | ||
import org.apache.http.client.entity.UrlEncodedFormEntity; | ||
import org.apache.http.client.methods.HttpPost; | ||
import org.apache.http.impl.client.HttpClients; | ||
import org.apache.http.message.BasicNameValuePair; | ||
|
||
/** | ||
* This class posts Command job updates to an HTTP link via POST | ||
* | ||
* @author Joe Osborn | ||
* | ||
*/ | ||
public class HTTPCommandUpdateHandler implements ICommandUpdateHandler { | ||
|
||
// HTTP address to post the update to | ||
private String HTTPAddress = ""; | ||
|
||
// Message to be posted | ||
String message = ""; | ||
|
||
/** | ||
* Default constructor | ||
*/ | ||
public HTTPCommandUpdateHandler() { | ||
} | ||
|
||
/** | ||
* Setter for http address to post to | ||
* | ||
* @param HTTPAddress | ||
*/ | ||
public void setHTTPAddress(String HTTPAddress) { | ||
this.HTTPAddress = HTTPAddress; | ||
} | ||
|
||
/** | ||
* See {@link org.eclipse.ice.commands.ICommandUpdateHandler#postUpdate()} | ||
*/ | ||
@Override | ||
public void postUpdate() throws IOException { | ||
|
||
// Get a default httpClient to use to send the post | ||
HttpClient httpClient = HttpClients.createDefault(); | ||
HttpPost httpPost = new HttpPost(HTTPAddress); | ||
|
||
// Setup the parameters to be passed in the post | ||
List<NameValuePair> params = new ArrayList<NameValuePair>(1); | ||
params.add(new BasicNameValuePair("body", message)); | ||
httpPost.setEntity(new UrlEncodedFormEntity(params, "UTF-8")); | ||
|
||
// Execute and get the response | ||
try { | ||
HttpResponse response = httpClient.execute(httpPost); | ||
} catch (Exception e) { | ||
logger.info("HTTP Post was not successful.", e); | ||
throw new IOException(); | ||
} | ||
} | ||
|
||
/** | ||
* See {@link org.eclipse.ice.commands.ICommandUpdateHandler#setMessage(String)} | ||
*/ | ||
@Override | ||
public void setMessage(String message) { | ||
this.message = message; | ||
} | ||
|
||
} |
45 changes: 45 additions & 0 deletions
45
org.eclipse.ice.commands/src/main/java/org/eclipse/ice/commands/ICommandUpdateHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2019- UT-Battelle, LLC. | ||
* All rights reserved. This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Public License v1.0 | ||
* which accompanies this distribution, and is available at | ||
* http://www.eclipse.org/legal/epl-v10.html | ||
* | ||
* Contributors: | ||
* Initial API and implementation and/or initial documentation - | ||
* Jay Jay Billings, Joe Osborn | ||
*******************************************************************************/ | ||
package org.eclipse.ice.commands; | ||
|
||
import java.io.IOException; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
/** | ||
* This interface lays out the format for any Command update class. Command | ||
* update classes notify users via their implemented methods about the status of | ||
* jobs, e.g. if they are completed or not. | ||
* | ||
* @author Joe Osborn | ||
* | ||
*/ | ||
public interface ICommandUpdateHandler { | ||
|
||
/** | ||
* Logger for handling event messages and other information. | ||
*/ | ||
static final Logger logger = LoggerFactory.getLogger(ICommandUpdateHandler.class); | ||
|
||
/* | ||
* This function processes the logic required to post an update about the job | ||
* for the class | ||
*/ | ||
abstract void postUpdate() throws IOException; | ||
|
||
/** | ||
* Function to set the message that will be updated on | ||
* @param message | ||
*/ | ||
abstract void setMessage(String message); | ||
} |
Oops, something went wrong.