Dave Jarvis' Repositories

git clone https://repo.autonoma.ca/repo/sales.git
.gitignore
dist
target
+lib
nb-configuration.xml
-->
<netbeans.hint.license>mit</netbeans.hint.license>
+ <org-netbeans-modules-javascript2-requirejs.enabled>true</org-netbeans-modules-javascript2-requirejs.enabled>
</properties>
</project-shared-configuration>
pom.xml
</dependency>
<dependency>
- <groupId>javax.mail</groupId>
- <artifactId>javax.mail-api</artifactId>
- <version>1.5.5</version>
- </dependency>
- <dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.atlassian.commonmark</groupId>
<artifactId>commonmark</artifactId>
<version>0.5.1</version>
+ </dependency>
+ <dependency>
+ <groupId>com.sun.mail</groupId>
+ <artifactId>javax.mail</artifactId>
+ <version>1.5.5</version>
</dependency>
</dependencies>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
- <maven.compiler.source>1.7</maven.compiler.source>
- <maven.compiler.target>1.7</maven.compiler.target>
+ <maven.compiler.source>1.8</maven.compiler.source>
+ <maven.compiler.target>1.8</maven.compiler.target>
</properties>
</project>
src/main/java/com/whitemagicsoftware/notify/ElasticEmailTransport.java
import java.net.MalformedURLException;
import java.net.URL;
+import java.util.Arrays;
+import java.util.stream.Collectors;
import javax.mail.Address;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.URLName;
import javax.mail.internet.InternetAddress;
/**
+ * Uses the ElasticEmailTransport to send a message.
*
* @author White Magic Software, Ltd.
}
+ /**
+ * Constructs a service-specific HttpTransport.
+ *
+ * @param session Not used directly by this class.
+ * @param urlName Ignored.
+ */
public ElasticEmailTransport( Session session, URLName urlName ) {
super( session, urlName );
+ setURLName( getURLName() );
}
public void sendMessage( Message message, Address[] addresses )
throws MessagingException {
-
+
String userName = getUsername();
String apiKey = getPassword();
String textBody = body[ 0 ];
String htmlBody = body[ 1 ];
-
- StringBuilder recipient = new StringBuilder( 2048 );
- for( Address address : addresses ) {
- recipient.append( address );
- recipient.append( ";" );
- }
+ String recipient = Arrays.asList( addresses ).stream()
+ .map( address -> address.toString() )
+ .collect( Collectors.joining( ";" ) );
String data = "";
- data += "userName=" + encode( userName );
+ data += "username=" + encode( userName );
data += "&api_key=" + encode( apiKey );
data += "&from=" + encode( fromAddr );
data += "&from_name=" + encode( fromName );
+ data += "&to=" + encode( recipient );
data += "&subject=" + encode( subject );
- data += "&body_html=" + encode( htmlBody );
data += "&body_text=" + encode( textBody );
- data += "&to=" + encode( recipient );
+ data += "&body_html=" + encode( htmlBody );
data += "&charset=" + encode( getDefaultCharset().displayName() );
data += "&encodingtype=0";
*/
@Override
- public URLName getURLName() {
+ public final URLName getURLName() {
return URL_NAME;
}
src/main/java/com/whitemagicsoftware/notify/HttpTransport.java
/**
- * Provides functionality for sending messages via HTTP(S).
- *
- * @author White Magic Software, Ltd.
- */
-public abstract class HttpTransport extends Transport {
-
- public final static String MIME_TYPE_PLAIN = "text/plain";
- public final static String MIME_TYPE_MULTIPART = "multipart/*";
- public final static String MIME_TYPE_HTML = "text/html";
-
- /**
- * Constructs a new HttpTransport capable of sending notifications via a
- * RESTFUL API.
- *
- * @param session Session state.
- * @param urlname Set to null (parameter is overridden by subclasses).
- */
- public HttpTransport( Session session, URLName urlname ) {
- // This should set the protected instance variables of "this.session"
- // and "this.urlname".
- super( session, urlname );
- }
-
- /**
- * Convenience method to help create a message with text and HTML body parts.
- *
- * @param text The message's text body part.
- * @param html The message's HTML body part.
- *
- * @return A Multipart instance that wraps the text and HTML.
- *
- * @throws MessagingException
- */
- public Multipart multipart( String text, String html )
- throws MessagingException {
- Multipart multipart = new MimeMultipart();
-
- multipart.addBodyPart( textBodyPart( text ) );
- multipart.addBodyPart( htmlBodyPart( html ) );
-
- return multipart;
- }
-
- /**
- * Helper method to create a new text body part.
- *
- * @param text The text body part to create.
- *
- * @return The text wrapped in a body part.
- *
- * @throws MessagingException
- */
- protected BodyPart textBodyPart( String text )
- throws MessagingException {
- return bodyPart( text, MIME_TYPE_PLAIN );
- }
-
- /**
- * Helper method to create a new HTML body part.
- *
- * @param html The HTML body part to create.
- *
- * @return The HTML wrapped in a body part.
- *
- * @throws MessagingException
- */
- protected BodyPart htmlBodyPart( String html )
- throws MessagingException {
- return bodyPart( html, MIME_TYPE_HTML );
- }
-
- /**
- * Create a new body part with the given content and MIME type.
- *
- * @param content The content to wrap in a MimeBodyPart.
- * @param mimeType The content's MIME type (text or HTML).
- *
- * @return A body part that wraps the content.
- *
- * @throws MessagingException
- */
- protected BodyPart bodyPart( String content, String mimeType )
- throws MessagingException {
- BodyPart part = new MimeBodyPart();
- part.setContent( content, mimeType );
- return part;
- }
-
- /**
- * Returns the default encoding to use for converting plain text into safe URL
- * parameters.
- *
- * @return StandardCharsets.UTF_8 by default.
- */
- protected Charset getDefaultCharset() {
- return StandardCharsets.UTF_8;
- }
-
- /**
- * Returns the given text string encoded for passing as a URL parameter.
- *
- * @param text The text to encode.
- *
- * @return The encoded text.
- *
- * @throws MessagingException The text could not be encoded.
- */
- protected String encode( final String text ) throws MessagingException {
- try {
- return URLEncoder.encode( text, getDefaultCharset().displayName() );
- } catch( UnsupportedEncodingException e ) {
- throw new MessagingException( "Invalid default encoding: '"
- + getDefaultCharset().displayName() + "'", e );
- }
- }
-
- /**
- * Helper method to encode a StringBuilder.
- *
- * @param text Text to encode.
- *
- * @return The StringBuilder's text, encoded to be URL-friendly.
- *
- * @throws MessagingException
- */
- protected String encode( final StringBuilder text )
- throws MessagingException {
- return encode( text.toString() );
- }
-
- /**
- * Returns the first sender on the list of senders for a given message. This
- * will ignore all other senders (if set).
- *
- * @param message The message to extract the first sender.
- *
- * @return "" if there were no senders, otherwise the first on the list.
- *
- * @throws MessagingException
- */
- protected String getSender( final Message message ) throws MessagingException {
- return getFirst( message.getFrom() );
- }
-
- /**
- * Returns the first non-empty, non-null element in the given list.
- *
- * @param addresses
- *
- * @return The first element in the given list, or the empty string if none
- * found.
- */
- private String getFirst( final Address addresses[] ) {
- String result = "";
- int i = 0;
-
- while( i < addresses.length && empty( result ) ) {
- result = addresses[ i++ ].toString();
- }
-
- return result;
- }
-
- /**
- * Answers whether the string contains any characters.
- *
- * @param string Can be null or empty.
- *
- * @return true The string is empty or zero length or contains only
- * whitespace.
- */
- private boolean empty( final String string ) {
- return string == null || string.trim().length() == 0;
- }
-
- /**
- *
- * @param message Message that contains content to extract.
- *
- * @return The text and HTML portions of the message in array index 0 and 1,
- * respectively.
- *
- * @throws javax.mail.MessagingException Message body not extracted.
- */
- protected String[] extractMessageContents( Message message )
- throws MessagingException {
- String result[] = new String[]{ "", "" };
-
- try {
- Object content = message.getContent();
-
- if( message.isMimeType( MIME_TYPE_PLAIN ) ) {
- result[ 0 ] = content.toString();
- } else if( message.isMimeType( MIME_TYPE_MULTIPART ) ) {
- Multipart multipart = (Multipart)content;
-
- result[ 0 ] = extractMultipart( multipart, MIME_TYPE_PLAIN );
- result[ 1 ] = extractMultipart( multipart, MIME_TYPE_HTML );
- }
- } catch( IOException e ) {
- throw new MessagingException( "Could not extract message content.", e );
- }
-
- return result;
- }
-
- /**
- * Extracts the content from the multipart instance that matches the given
- * mimeType.
- *
- * @param multipart The multipart instance with contents to extract.
- * @param mimeType The mimeType to match against the multipart.
- *
- * @return The contents from the multipart that match the mimeType.
- *
- * @throws MessagingException Failed to extract the content.
- */
- private String extractMultipart( Multipart multipart, String mimeType )
- throws MessagingException {
- String result = "";
- int count = multipart.getCount();
-
- for( int i = 0; i < count; i++ ) {
- BodyPart bodyPart = multipart.getBodyPart( i );
- Object content = exhumeBodyPart( bodyPart );
-
- if( content instanceof Multipart ) {
- result += extractMultipart( (Multipart)content, mimeType );
- } else if( bodyPart.isMimeType( mimeType ) ) {
- result += "\n" + content.toString();
-
- // Avoid doubling-up text.
- break;
- }
- }
-
- return result;
- }
-
- /**
- * Returns the body part content.
- *
- * @param bodyPart The body part containing content to retrieve.
- *
- * @return The body part's contents.
- *
- * @throws MessagingException Error extracting the body part contents.
- */
- private Object exhumeBodyPart( BodyPart bodyPart )
- throws MessagingException {
- try {
- return bodyPart.getContent();
- } catch( IOException e ) {
- throw new MessagingException( "Error opening body part contents.", e );
- }
- }
-
- /**
- * Sends the given data over the wire.
- *
- * @param data The URL-encoded parameters to send.
- *
- * @throws javax.mail.MessagingException
- */
- protected void send( String data ) throws MessagingException {
- HttpURLConnection connection = open();
-
- write( data, connection );
- String response = read( connection );
- }
-
- /**
- * Writes the given URL-encoded parameters to the given connection.
- *
- * @param urlParameters The URL-encoded parameters to write.
- * @param connection The connection that can be written to.
- *
- * @throws MessagingException Error writing the parameters to the given
- * connection.
- */
- protected void write( String urlParameters, HttpURLConnection connection )
- throws MessagingException {
- try {
- write( urlParameters, connection.getOutputStream() );
-
- int response = connection.getResponseCode();
-
- // Ensure a 200 response code is returned.
- if( response != HttpURLConnection.HTTP_OK ) {
- throw new MessagingException( "Unexpected response code: " + response );
- }
- } catch( IOException e ) {
- throw new MessagingException( "Could not stream output connection.", e );
- }
- }
-
- /**
- * Writes a set of encoded URL parameters to the given stream.
- *
- * @param urlParameters The parameters to send.
- * @param outputStream The stream to receive the parameters.
- *
- * @throws MessagingException Message could not be sent.
- */
- protected void write( String urlParameters, OutputStream outputStream )
- throws MessagingException {
- try( OutputStreamWriter writer = new OutputStreamWriter( outputStream ); ) {
- writer.write( urlParameters );
- writer.flush();
- } catch( IOException e ) {
- throw new MessagingException( "Could not send message.", e );
- }
- }
-
- /**
- * Reads the response code returned from a previous call to write. This must
- * be called after write, not before.
- *
- * @param connection The connection that had data written to it.
- *
- * @return A non-null, possibly empty string.
- *
- * @throws MessagingException Response could not be read.
- */
- protected String read( URLConnection connection ) throws MessagingException {
- try {
- return read( new InputStreamReader( connection.getInputStream() ) );
- } catch( IOException e ) {
- throw new MessagingException( "Could not stream input connection.", e );
- }
- }
-
- /**
- * Helper method.
- *
- * @param in The stream from a connection that had data written to it.
- *
- * @return A non-null, possibly empty string.
- *
- * @throws MessagingException Response could not be read.
- */
- protected String read( InputStreamReader in ) throws MessagingException {
- try( BufferedReader rd = new BufferedReader( in ) ) {
- return rd.readLine();
- } catch( IOException e ) {
- throw new MessagingException( "Could not read status.", e );
- }
- }
-
- /**
- * Connects to the REST API over HTTP.
- *
- * @return An opened URLConnection instance.
- *
- * @throws javax.mail.MessagingException The REST API is unavailable.
- */
- protected HttpURLConnection open() throws MessagingException {
- HttpURLConnection connection = openConnection();
- connection.setDoOutput( true );
-
- return connection;
- }
-
- /**
- * Helper method to return an opened URL connection.
- *
- * @return An opened URLConnection instance.
- *
- * @throws javax.mail.MessagingException Error connecting to the provider.
- */
- public HttpURLConnection openConnection() throws MessagingException {
- try {
- return (HttpURLConnection)getURL().openConnection();
- } catch( NoSuchProviderException | IOException e ) {
- throw new MessagingException( "Could not connect to provider.", e );
- }
- }
-
- /**
- * Helper method to return the REST API URL for sending messages.
- *
- * @return An HTTP(S) URL, unopened.
- *
- * @return @throws javax.mail.MessagingException
- */
- private URL getURL() throws MessagingException {
- try {
- return getURLName().getURL();
- } catch( Exception e ) {
- throw new MessagingException( "Provider URL not set.", e );
- }
- }
-
- /**
- * Returns the wrapper for the user name and password.
- *
- * @return The session's credentials wrapper.
- */
- private PasswordAuthentication getPasswordAuthentication() {
- return getSession().getPasswordAuthentication( getURLName() );
+ * Responsible for sending messages over HTTP. Subclasses are responsible for
+ * providing the sendMessage method implementation.
+ *
+ * @author White Magic Software, Ltd.
+ */
+public abstract class HttpTransport extends Transport {
+
+ public final static String MIME_TYPE_PLAIN = "text/plain";
+ public final static String MIME_TYPE_MULTIPART = "multipart/*";
+ public final static String MIME_TYPE_HTML = "text/html";
+
+ /**
+ * Constructs a new HttpTransport capable of sending notifications via a
+ * RESTFUL API.
+ *
+ * @param session Session state.
+ * @param urlname Set value to null for HTTP.
+ */
+ public HttpTransport( Session session, URLName urlname ) {
+ // This should set the protected instance variables of "this.session"
+ // and "this.urlname".
+ super( session, urlname );
+ }
+
+ /**
+ * Sets the credentials to use for permission to send messages.
+ *
+ * @param username The username authorised to send messages over HTTP.
+ * @param password The password (or API key) associated with the username.
+ */
+ public void setAuthentication( String username, String password ) {
+ getSession().setPasswordAuthentication(
+ getURLName(),
+ new PasswordAuthentication( username, password )
+ );
+ }
+
+ /**
+ * Sends the given data over the wire.
+ *
+ * @param data The URL-encoded parameters to send.
+ *
+ * @throws javax.mail.MessagingException
+ */
+ protected void send( String data ) throws MessagingException {
+ HttpURLConnection connection = open();
+
+ write( data, connection );
+ String response = read( connection );
+ }
+
+ /**
+ * Convenience method to help create a message with text and HTML body parts.
+ *
+ * @param text The message's text body part.
+ * @param html The message's HTML body part.
+ *
+ * @return A Multipart instance that wraps the text and HTML.
+ *
+ * @throws MessagingException
+ */
+ public Multipart multipart( String text, String html )
+ throws MessagingException {
+ Multipart multipart = new MimeMultipart( "alternative" );
+
+ multipart.addBodyPart( textBodyPart( text ) );
+ multipart.addBodyPart( htmlBodyPart( html ) );
+
+ return multipart;
+ }
+
+ /**
+ * Helper method to create a new text body part.
+ *
+ * @param text The text body part to create.
+ *
+ * @return The text wrapped in a body part.
+ *
+ * @throws MessagingException
+ */
+ protected BodyPart textBodyPart( String text )
+ throws MessagingException {
+ return bodyPart( text, MIME_TYPE_PLAIN );
+ }
+
+ /**
+ * Helper method to create a new HTML body part.
+ *
+ * @param html The HTML body part to create.
+ *
+ * @return The HTML wrapped in a body part.
+ *
+ * @throws MessagingException
+ */
+ protected BodyPart htmlBodyPart( String html )
+ throws MessagingException {
+ return bodyPart( html, MIME_TYPE_HTML );
+ }
+
+ /**
+ * Create a new body part with the given content and MIME type.
+ *
+ * @param content The content to wrap in a MimeBodyPart.
+ * @param mimeType The content's MIME type (text or HTML).
+ *
+ * @return A body part that wraps the content.
+ *
+ * @throws MessagingException
+ */
+ protected BodyPart bodyPart( String content, String mimeType )
+ throws MessagingException {
+ MimeBodyPart part = new MimeBodyPart();
+ part.setContent( content, mimeType );
+
+ return part;
+ }
+
+ /**
+ * Returns the default encoding to use for converting plain text into safe URL
+ * parameters.
+ *
+ * @return StandardCharsets.UTF_8 by default.
+ */
+ protected Charset getDefaultCharset() {
+ return StandardCharsets.UTF_8;
+ }
+
+ /**
+ * Returns the given text string encoded for passing as a URL parameter.
+ *
+ * @param text The text to encode.
+ *
+ * @return The encoded text.
+ *
+ * @throws MessagingException The text could not be encoded.
+ */
+ protected String encode( final String text ) throws MessagingException {
+ String charset = getDefaultCharset().displayName();
+
+ try {
+ return URLEncoder.encode( text, charset );
+ } catch( UnsupportedEncodingException e ) {
+ throw new MessagingException(
+ "Invalid default encoding: '" + charset + "'", e );
+ }
+ }
+
+ /**
+ * Helper method to return a URL-encoded string.
+ *
+ * @param text Text to encode.
+ *
+ * @return The StringBuilder's text, encoded to be URL-friendly.
+ *
+ * @throws MessagingException
+ */
+ protected String encode( final StringBuilder text )
+ throws MessagingException {
+ return encode( text.toString() );
+ }
+
+ /**
+ * Answers whether the string contains any characters.
+ *
+ * @param string Can be null or empty.
+ *
+ * @return true The string is empty or zero length or contains only
+ * whitespace.
+ */
+ private boolean isEmpty( final String string ) {
+ return string == null || string.trim().length() == 0;
+ }
+
+ /**
+ *
+ * @param message Message that contains content to extract.
+ *
+ * @return The text and HTML portions of the message in array index 0 and 1,
+ * respectively.
+ *
+ * @throws javax.mail.MessagingException Message body not extracted.
+ */
+ protected String[] extractMessageContents( Message message )
+ throws MessagingException {
+ String result[] = new String[]{ "", "" };
+
+ System.out.println( "Extract Message Contents" );
+
+ try {
+ // Ensure the mime types are applied.
+ //
+ // See also: http://stackoverflow.com/a/5031057/59087
+ message.saveChanges();
+ Object content = message.getContent();
+
+ if( content instanceof Multipart ) {
+ Multipart multipart = (Multipart)content;
+
+ result[ 0 ] = extractMultipart( multipart, MIME_TYPE_PLAIN );
+ result[ 1 ] = extractMultipart( multipart, MIME_TYPE_HTML );
+ } else if( message.isMimeType( MIME_TYPE_PLAIN ) ) {
+ result[ 0 ] = content.toString();
+ }
+ } catch( IOException e ) {
+ throw new MessagingException( "Could not extract message content.", e );
+ }
+
+ return result;
+ }
+
+ /**
+ * Extracts the content from the multipart instance that matches the given
+ * mimeType.
+ *
+ * @param multipart The multipart instance with contents to extract.
+ * @param mimeType The mimeType to match against the multipart.
+ *
+ * @return The contents from the multipart that match the mimeType.
+ *
+ * @throws MessagingException Failed to extract the content.
+ */
+ private String extractMultipart( Multipart multipart, String mimeType )
+ throws MessagingException {
+ String result = "";
+ int count = multipart.getCount();
+
+ for( int i = 0; i < count; i++ ) {
+ BodyPart bodyPart = multipart.getBodyPart( i );
+ Object content = exhumeBodyPart( bodyPart );
+
+ if( content instanceof Multipart ) {
+ result += extractMultipart( (Multipart)content, mimeType );
+ } else if( bodyPart.isMimeType( mimeType ) ) {
+ result += "\n" + content.toString();
+ }
+ }
+
+ return result;
+ }
+
+ /**
+ * Returns the body part content.
+ *
+ * @param bodyPart The body part containing content to retrieve.
+ *
+ * @return The body part's contents.
+ *
+ * @throws MessagingException Error extracting the body part contents.
+ */
+ private Object exhumeBodyPart( BodyPart bodyPart )
+ throws MessagingException {
+ try {
+ return bodyPart.getContent();
+ } catch( IOException e ) {
+ throw new MessagingException( "Error opening body part contents.", e );
+ }
+ }
+
+ /**
+ * Writes the given URL-encoded parameters to the given connection.
+ *
+ * @param urlParameters The URL-encoded parameters to write.
+ * @param connection The connection that can be written to.
+ *
+ * @throws MessagingException Error writing the parameters to the given
+ * connection.
+ */
+ protected void write( String urlParameters, HttpURLConnection connection )
+ throws MessagingException {
+ try {
+ write( urlParameters, connection.getOutputStream() );
+
+ int response = connection.getResponseCode();
+
+ // Ensure a 200 response code is returned.
+ if( response != HttpURLConnection.HTTP_OK ) {
+ throw new MessagingException( "Unexpected response code: " + response );
+ }
+ } catch( IOException e ) {
+ throw new MessagingException( "Could not stream output connection.", e );
+ }
+ }
+
+ /**
+ * Writes a set of encoded URL parameters to the given stream.
+ *
+ * @param urlParameters The parameters to send.
+ * @param outputStream The stream to receive the parameters.
+ *
+ * @throws MessagingException Message could not be sent.
+ */
+ protected void write( String urlParameters, OutputStream outputStream )
+ throws MessagingException {
+ try( OutputStreamWriter writer = new OutputStreamWriter( outputStream ); ) {
+ writer.write( urlParameters );
+ writer.flush();
+ } catch( IOException e ) {
+ throw new MessagingException( "Could not send message.", e );
+ }
+ }
+
+ /**
+ * Reads the response code returned from a previous call to write. This must
+ * be called after write, not before.
+ *
+ * @param connection The connection that had data written to it.
+ *
+ * @return A non-null, possibly empty string.
+ *
+ * @throws MessagingException Response could not be read.
+ */
+ protected String read( URLConnection connection ) throws MessagingException {
+ try {
+ return read( new InputStreamReader( connection.getInputStream() ) );
+ } catch( IOException e ) {
+ throw new MessagingException( "Could not stream input connection.", e );
+ }
+ }
+
+ /**
+ * Helper method.
+ *
+ * @param in The stream from a connection that had data written to it.
+ *
+ * @return A non-null, possibly empty string.
+ *
+ * @throws MessagingException Response could not be read.
+ */
+ protected String read( InputStreamReader in ) throws MessagingException {
+ try( BufferedReader rd = new BufferedReader( in ) ) {
+ return rd.readLine();
+ } catch( IOException e ) {
+ throw new MessagingException( "Could not read status.", e );
+ }
+ }
+
+ /**
+ * Connects to the REST API over HTTP.
+ *
+ * @return An opened URLConnection instance.
+ *
+ * @throws javax.mail.MessagingException The REST API is unavailable.
+ */
+ protected HttpURLConnection open() throws MessagingException {
+ HttpURLConnection connection = openConnection();
+ connection.setDoOutput( true );
+
+ return connection;
+ }
+
+ /**
+ * Helper method to return an opened URL connection.
+ *
+ * @return An opened URLConnection instance.
+ *
+ * @throws javax.mail.MessagingException Error connecting to the provider.
+ */
+ public HttpURLConnection openConnection() throws MessagingException {
+ try {
+ return (HttpURLConnection)getURL().openConnection();
+ } catch( NoSuchProviderException | IOException e ) {
+ throw new MessagingException( "Could not connect to provider.", e );
+ }
+ }
+
+ /**
+ * Helper method to return the REST API URL for sending messages.
+ *
+ * @return An HTTP(S) URL, unopened.
+ *
+ * @return @throws javax.mail.MessagingException
+ */
+ private URL getURL() throws MessagingException {
+ try {
+ return getURLName().getURL();
+ } catch( Exception e ) {
+ throw new MessagingException( "Provider URL not set.", e );
+ }
+ }
+
+ /**
+ * Returns the wrapper for the user name and password.
+ *
+ * @return The session's credentials wrapper.
+ */
+ private PasswordAuthentication getPasswordAuthentication() {
+ return getSession().getPasswordAuthentication( getURLName() );
+ }
+
+ /**
+ * Answers whether credentials have been set.
+ *
+ * @return true Messages will be subjected to authentication.
+ */
+ public boolean hasPasswordAuthentication() {
+ return getPasswordAuthentication() != null;
}
src/test/java/com/whitemagicsoftware/notify/.gitignore
+Credentials.java
src/test/java/com/whitemagicsoftware/notify/HttpTransportTest.java
+/*
+ * The MIT License
+ *
+ * Copyright 2016 White Magic Software, Ltd..
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+package com.whitemagicsoftware.notify;
+
+import static com.whitemagicsoftware.notify.Credentials.PASSWORD;
+import static com.whitemagicsoftware.notify.Credentials.SENDER;
+import static com.whitemagicsoftware.notify.Credentials.SENDER_NAME;
+import static com.whitemagicsoftware.notify.Credentials.USERNAME;
+import java.io.UnsupportedEncodingException;
+import java.util.Properties;
+import javax.mail.Address;
+import javax.mail.Message;
+import javax.mail.MessagingException;
+import javax.mail.Session;
+import javax.mail.internet.InternetAddress;
+import javax.mail.internet.MimeMessage;
+import org.junit.After;
+import org.junit.AfterClass;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+/**
+ * @author White Magic Software, Ltd.
+ */
+public class HttpTransportTest {
+
+ private final static String TEST_BODY = "# Hello World!";
+ private final static String TEST_HTML = "<h1>Hello, World!</h1>";
+ private final static String TEST_SUBJECT = "Test Subject";
+
+ public HttpTransportTest() {
+ }
+
+ @BeforeClass
+ public static void setUpClass() {
+ }
+
+ @AfterClass
+ public static void tearDownClass() {
+ }
+
+ @Before
+ public void setUp() {
+ }
+
+ @After
+ public void tearDown() {
+ }
+
+ @Test
+ public void testTransport() throws MessagingException {
+ try {
+ String textBody = TEST_BODY;
+ String htmlBody = TEST_HTML;
+ String subject = TEST_SUBJECT;
+
+ Session session = createSession();
+ Assert.assertNotNull( "Cannot create session.", session );
+
+ HttpTransport transport = new ElasticEmailTransport( session, null );
+ Assert.assertNotNull( "Cannot create transport.", transport );
+
+ transport.setAuthentication( USERNAME.toString(), PASSWORD.toString() );
+ Assert.assertTrue( "Authentication not set.",
+ transport.hasPasswordAuthentication() );
+
+ Address recipient = new InternetAddress( USERNAME.toString() );
+ Address sender = new InternetAddress(
+ SENDER.toString(),
+ SENDER_NAME.toString() );
+
+ Message message = new MimeMessage( session );
+
+ message.setFrom( sender );
+ message.addRecipient( Message.RecipientType.TO, recipient );
+ message.setSubject( subject );
+ message.setContent( transport.multipart( textBody, htmlBody ) );
+
+ transport.sendMessage( message, message.getAllRecipients() );
+ transport.close();
+ } catch( UnsupportedEncodingException e ) {
+ Assert.fail( e.getMessage() );
+ }
+ }
+
+ private Session createSession() {
+ return Session.getInstance( createProperties() );
+ }
+
+ private Properties createProperties() {
+ return new Properties();
+ }
+}
src/test/java/com/whitemagicsoftware/notify/test/.gitignore
-Credentials.java
src/test/java/com/whitemagicsoftware/notify/test/HttpTransportTests.java
-/*
- * The MIT License
- *
- * Copyright 2016 White Magic Software, Ltd..
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in
- * all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- * THE SOFTWARE.
- */
-package com.whitemagicsoftware.notify.test;
-
-import com.whitemagicsoftware.notify.ElasticEmailTransport;
-import com.whitemagicsoftware.notify.HttpTransport;
-import static com.whitemagicsoftware.notify.test.Credentials.PASSWORD;
-import static com.whitemagicsoftware.notify.test.Credentials.USERNAME;
-import java.io.UnsupportedEncodingException;
-import java.util.Properties;
-import javax.mail.Address;
-import javax.mail.Authenticator;
-import javax.mail.Message;
-import javax.mail.MessagingException;
-import javax.mail.PasswordAuthentication;
-import javax.mail.Session;
-import javax.mail.internet.InternetAddress;
-import javax.mail.internet.MimeMessage;
-import org.junit.After;
-import org.junit.AfterClass;
-import org.junit.Assert;
-import org.junit.Before;
-import org.junit.BeforeClass;
-import org.junit.Test;
-
-/**
- *
- * @author White Magic Software, Ltd.
- */
-public abstract class HttpTransportTests {
-
- public HttpTransportTests() {
- }
-
- @BeforeClass
- public static void setUpClass() {
- }
-
- @AfterClass
- public static void tearDownClass() {
- }
-
- @Before
- public void setUp() {
- }
-
- @After
- public void tearDown() {
- }
-
- @Test
- public void testNotify() throws MessagingException {
- try {
- String textBody = "# Hello World!";
- String htmlBody = "<html><body><h1>Hello, World!</h1></body></html>";
- String subject = "Subject";
-
- Session session = createSession();
- HttpTransport transport = new ElasticEmailTransport( session, null );
-
- Address sender = new InternetAddress( "Penny.Savr@gmail.com", "Penny Saver" );
- Address recipient = new InternetAddress( "Dave.Jarvis@gmail.com" );
- Message message = new MimeMessage( session );
-
- message.setFrom( sender );
- message.addRecipient( Message.RecipientType.TO, recipient );
- message.setSubject( subject );
-
- message.setContent( transport.multipart( textBody, htmlBody ) );
-
- transport.sendMessage( message, message.getAllRecipients() );
- transport.close();
- } catch( UnsupportedEncodingException e ) {
- Assert.fail( e.getMessage() );
- }
- }
-
- private Session createSession() {
- return Session.getDefaultInstance(
- createProperties(),
- createAuthenticator() );
- }
-
- private Properties createProperties() {
- return new Properties();
- }
-
- /**
- * Returns the authenticator to use for HTTP requests.
- *
- * @param user The user name to use for sending mail.
- * @param pass The password (API key) to use for sending mail.
- *
- * @return A non-null Authenticator instance.
- */
- private Authenticator createAuthenticator() {
- return new Authenticator() {
- @Override
- protected PasswordAuthentication getPasswordAuthentication() {
- return new PasswordAuthentication(
- USERNAME.toString(),
- PASSWORD.toString() );
- }
- };
- }
-}

Fixed bug with content type.

Author djarvis <email>
Date 2016-06-04 19:53:33 GMT-0700
Commit 3bebba3f9bcf90dcec33eb198c5d4b402c8bd0e1
Parent 71ab584
Delta 540 lines added, 546 lines removed, 6-line decrease