From e0a2e0d0c79dcaf4532d0ca3aeefd3f5546af404 Mon Sep 17 00:00:00 2001 From: Instrumental Date: Fri, 31 Aug 2018 14:34:41 -0500 Subject: Relocate JavaxMail Issue-ID: AAF-419 Change-Id: I73890b6f6db09f2f86018594bb4692a4e90a6df9 Signed-off-by: Instrumental --- auth/auth-batch/pom.xml | 5 + .../java/org/onap/aaf/auth/javax/JavaxMailer.java | 160 +++++++++++++++++++++ .../main/java/org/onap/aaf/auth/org/Mailer.java | 38 +++++ auth/auth-deforg/pom.xml | 10 -- .../src/main/java/org/onap/aaf/org/DefaultOrg.java | 1 + .../main/java/org/onap/aaf/org/JavaxMailer.java | 159 -------------------- .../src/main/java/org/onap/aaf/org/Mailer.java | 39 ----- 7 files changed, 204 insertions(+), 208 deletions(-) create mode 100644 auth/auth-batch/src/main/java/org/onap/aaf/auth/javax/JavaxMailer.java create mode 100644 auth/auth-core/src/main/java/org/onap/aaf/auth/org/Mailer.java delete mode 100644 auth/auth-deforg/src/main/java/org/onap/aaf/org/JavaxMailer.java delete mode 100644 auth/auth-deforg/src/main/java/org/onap/aaf/org/Mailer.java diff --git a/auth/auth-batch/pom.xml b/auth/auth-batch/pom.xml index a30ccaa7..1c49d6fb 100644 --- a/auth/auth-batch/pom.xml +++ b/auth/auth-batch/pom.xml @@ -118,6 +118,11 @@ org.onap.aaf.authz aaf-auth-cass + + + javax.mail + mail + org.slf4j diff --git a/auth/auth-batch/src/main/java/org/onap/aaf/auth/javax/JavaxMailer.java b/auth/auth-batch/src/main/java/org/onap/aaf/auth/javax/JavaxMailer.java new file mode 100644 index 00000000..17921c8a --- /dev/null +++ b/auth/auth-batch/src/main/java/org/onap/aaf/auth/javax/JavaxMailer.java @@ -0,0 +1,160 @@ +/** + * ============LICENSE_START==================================================== + * org.onap.aaf + * =========================================================================== + * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved. + * =========================================================================== + * 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. + * ============LICENSE_END==================================================== + * + */ +package org.onap.aaf.auth.javax; + +import java.util.ArrayList; +import java.util.List; + +import javax.mail.Address; +import javax.mail.Message; +import javax.mail.MessagingException; +import javax.mail.Session; +import javax.mail.Transport; +import javax.mail.internet.InternetAddress; +import javax.mail.internet.MimeMessage; + +import org.onap.aaf.auth.env.AuthzTrans; +import org.onap.aaf.auth.org.Mailer; +import org.onap.aaf.auth.org.OrganizationException; + +public class JavaxMailer implements Mailer { + private Session session; + + public JavaxMailer() { + + // Get the default Session object. + session = Session.getDefaultInstance(System.getProperties()); + + } + + @Override + public int sendEmail(AuthzTrans trans, boolean testMode, String mailFrom, List to, List cc, String subject, String body, + Boolean urgent) throws OrganizationException { + + int status = 1; + + + try { + // Create a default MimeMessage object. + MimeMessage message = new MimeMessage(session); + + // Set From: header field of the header. + message.setFrom(new InternetAddress(mailFrom)); + + if (!testMode) { + // Set To: header field of the header. This is a required field + // and calling module should make sure that it is not null or + // blank + message.addRecipients(Message.RecipientType.TO,getAddresses(to)); + + // Set CC: header field of the header. + if ((cc != null) && (cc.size() > 0)) { + message.addRecipients(Message.RecipientType.CC,getAddresses(cc)); + } + + // Set Subject: header field + message.setSubject(subject); + + if (urgent) { + message.addHeader("X-Priority", "1"); + } + + // Now set the actual message + message.setText(body); + } else { + + // override recipients + message.addRecipients(Message.RecipientType.TO, + InternetAddress.parse(mailFrom)); + + // Set Subject: header field + message.setSubject("[TESTMODE] " + subject); + + if (urgent) { + message.addHeader("X-Priority", "1"); + } + + ArrayList newBody = new ArrayList<>(); + + Address temp[] = getAddresses(to); + String headerString = "TO:\t" + InternetAddress.toString(temp) + "\n"; + + temp = getAddresses(cc); + headerString += "CC:\t" + InternetAddress.toString(temp) + "\n"; + + newBody.add(headerString); + + newBody.add("Text: \n"); + + newBody.add(body); + String outString = ""; + for (String s : newBody) { + outString += s + "\n"; + } + + message.setText(outString); + } + // Send message + Transport.send(message); + status = 0; + + } catch (MessagingException mex) { + System.out.println("Error messaging: "+ mex.getMessage()); + System.out.println("Error messaging: "+ mex.toString()); + throw new OrganizationException("Exception send email message " + + mex.getMessage()); + } + + return status; + } + + /** + * Convert the delimiter String into Internet addresses with the default + * delimiter of ";" + * @param strAddress + * @return + */ + private Address[] getAddresses(List strAddress) throws OrganizationException { + return this.getAddresses(strAddress,";"); + } + /** + * Convert the delimiter String into Internet addresses with the + * delimiter of provided + * @param strAddresses + * @param delimiter + * @return + */ + private Address[] getAddresses(List strAddresses, String delimiter) throws OrganizationException { + Address[] addressArray = new Address[strAddresses.size()]; + int count = 0; + for (String addr : strAddresses) + { + try{ + addressArray[count] = new InternetAddress(addr); + count++; + }catch(Exception e){ + throw new OrganizationException("Failed to parse the email address "+ addr +": "+e.getMessage()); + } + } + return addressArray; + } + +} diff --git a/auth/auth-core/src/main/java/org/onap/aaf/auth/org/Mailer.java b/auth/auth-core/src/main/java/org/onap/aaf/auth/org/Mailer.java new file mode 100644 index 00000000..86875a4a --- /dev/null +++ b/auth/auth-core/src/main/java/org/onap/aaf/auth/org/Mailer.java @@ -0,0 +1,38 @@ +/** + * ============LICENSE_START==================================================== + * org.onap.aaf + * =========================================================================== + * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved. + * =========================================================================== + * 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. + * ============LICENSE_END==================================================== + * + */ +package org.onap.aaf.auth.org; + +import java.util.List; + +import org.onap.aaf.auth.env.AuthzTrans; + +public interface Mailer { + public int sendEmail( + AuthzTrans trans, + boolean testMode, + String mailFrom, + List toList, + List ccList, + String subject, + String body, + Boolean urgent) throws OrganizationException; + +} diff --git a/auth/auth-deforg/pom.xml b/auth/auth-deforg/pom.xml index bce3199d..3420663e 100644 --- a/auth/auth-deforg/pom.xml +++ b/auth/auth-deforg/pom.xml @@ -102,16 +102,6 @@ aaf-auth-core - - javax.mail - mail - - - - org.jvnet.mock-javamail - mock-javamail - 1.9 - diff --git a/auth/auth-deforg/src/main/java/org/onap/aaf/org/DefaultOrg.java b/auth/auth-deforg/src/main/java/org/onap/aaf/org/DefaultOrg.java index f3c73216..0bfe7e2d 100644 --- a/auth/auth-deforg/src/main/java/org/onap/aaf/org/DefaultOrg.java +++ b/auth/auth-deforg/src/main/java/org/onap/aaf/org/DefaultOrg.java @@ -34,6 +34,7 @@ import java.util.regex.Pattern; import org.onap.aaf.auth.env.AuthzTrans; import org.onap.aaf.auth.org.EmailWarnings; import org.onap.aaf.auth.org.Executor; +import org.onap.aaf.auth.org.Mailer; import org.onap.aaf.auth.org.Organization; import org.onap.aaf.auth.org.OrganizationException; import org.onap.aaf.cadi.util.FQI; diff --git a/auth/auth-deforg/src/main/java/org/onap/aaf/org/JavaxMailer.java b/auth/auth-deforg/src/main/java/org/onap/aaf/org/JavaxMailer.java deleted file mode 100644 index f50493dd..00000000 --- a/auth/auth-deforg/src/main/java/org/onap/aaf/org/JavaxMailer.java +++ /dev/null @@ -1,159 +0,0 @@ -/** - * ============LICENSE_START==================================================== - * org.onap.aaf - * =========================================================================== - * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved. - * =========================================================================== - * 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. - * ============LICENSE_END==================================================== - * - */ -package org.onap.aaf.org; - -import java.util.ArrayList; -import java.util.List; - -import javax.mail.Address; -import javax.mail.Message; -import javax.mail.MessagingException; -import javax.mail.Session; -import javax.mail.Transport; -import javax.mail.internet.InternetAddress; -import javax.mail.internet.MimeMessage; - -import org.onap.aaf.auth.env.AuthzTrans; -import org.onap.aaf.auth.org.OrganizationException; - -public class JavaxMailer implements Mailer { - private Session session; - - public JavaxMailer() { - - // Get the default Session object. - session = Session.getDefaultInstance(System.getProperties()); - - } - - @Override - public int sendEmail(AuthzTrans trans, boolean testMode, String mailFrom, List to, List cc, String subject, String body, - Boolean urgent) throws OrganizationException { - - int status = 1; - - - try { - // Create a default MimeMessage object. - MimeMessage message = new MimeMessage(session); - - // Set From: header field of the header. - message.setFrom(new InternetAddress(mailFrom)); - - if (!testMode) { - // Set To: header field of the header. This is a required field - // and calling module should make sure that it is not null or - // blank - message.addRecipients(Message.RecipientType.TO,getAddresses(to)); - - // Set CC: header field of the header. - if ((cc != null) && (cc.size() > 0)) { - message.addRecipients(Message.RecipientType.CC,getAddresses(cc)); - } - - // Set Subject: header field - message.setSubject(subject); - - if (urgent) { - message.addHeader("X-Priority", "1"); - } - - // Now set the actual message - message.setText(body); - } else { - - // override recipients - message.addRecipients(Message.RecipientType.TO, - InternetAddress.parse(mailFrom)); - - // Set Subject: header field - message.setSubject("[TESTMODE] " + subject); - - if (urgent) { - message.addHeader("X-Priority", "1"); - } - - ArrayList newBody = new ArrayList<>(); - - Address temp[] = getAddresses(to); - String headerString = "TO:\t" + InternetAddress.toString(temp) + "\n"; - - temp = getAddresses(cc); - headerString += "CC:\t" + InternetAddress.toString(temp) + "\n"; - - newBody.add(headerString); - - newBody.add("Text: \n"); - - newBody.add(body); - String outString = ""; - for (String s : newBody) { - outString += s + "\n"; - } - - message.setText(outString); - } - // Send message - Transport.send(message); - status = 0; - - } catch (MessagingException mex) { - System.out.println("Error messaging: "+ mex.getMessage()); - System.out.println("Error messaging: "+ mex.toString()); - throw new OrganizationException("Exception send email message " - + mex.getMessage()); - } - - return status; - } - - /** - * Convert the delimiter String into Internet addresses with the default - * delimiter of ";" - * @param strAddress - * @return - */ - private Address[] getAddresses(List strAddress) throws OrganizationException { - return this.getAddresses(strAddress,";"); - } - /** - * Convert the delimiter String into Internet addresses with the - * delimiter of provided - * @param strAddresses - * @param delimiter - * @return - */ - private Address[] getAddresses(List strAddresses, String delimiter) throws OrganizationException { - Address[] addressArray = new Address[strAddresses.size()]; - int count = 0; - for (String addr : strAddresses) - { - try{ - addressArray[count] = new InternetAddress(addr); - count++; - }catch(Exception e){ - throw new OrganizationException("Failed to parse the email address "+ addr +": "+e.getMessage()); - } - } - return addressArray; - } - -} diff --git a/auth/auth-deforg/src/main/java/org/onap/aaf/org/Mailer.java b/auth/auth-deforg/src/main/java/org/onap/aaf/org/Mailer.java deleted file mode 100644 index 0824e1f3..00000000 --- a/auth/auth-deforg/src/main/java/org/onap/aaf/org/Mailer.java +++ /dev/null @@ -1,39 +0,0 @@ -/** - * ============LICENSE_START==================================================== - * org.onap.aaf - * =========================================================================== - * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved. - * =========================================================================== - * 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. - * ============LICENSE_END==================================================== - * - */ -package org.onap.aaf.org; - -import java.util.List; - -import org.onap.aaf.auth.env.AuthzTrans; -import org.onap.aaf.auth.org.OrganizationException; - -public interface Mailer { - public int sendEmail( - AuthzTrans trans, - boolean testMode, - String mailFrom, - List toList, - List ccList, - String subject, - String body, - Boolean urgent) throws OrganizationException; - -} -- cgit 1.2.3-korg