diff options
author | Instrumental <jonathan.gathman@att.com> | 2018-08-31 11:35:20 -0500 |
---|---|---|
committer | Instrumental <jonathan.gathman@att.com> | 2018-08-31 11:35:32 -0500 |
commit | 72026fb568523f794ab9289c8f443535969b6df6 (patch) | |
tree | 2d4c8a3cfd76053f073bceec6c3381333389fd91 /auth | |
parent | 9f52db1c94a0e772825cfe985c21cf0b46850d76 (diff) |
Isolate JavaxMail
Issue-ID: AAF-419
Change-Id: Ic0d0a44d3078e998449fe69481281552c24083a6
Signed-off-by: Instrumental <jonathan.gathman@att.com>
Diffstat (limited to 'auth')
4 files changed, 247 insertions, 150 deletions
diff --git a/auth/auth-core/src/main/java/org/onap/aaf/auth/org/OrganizationFactory.java b/auth/auth-core/src/main/java/org/onap/aaf/auth/org/OrganizationFactory.java index f9507038..ffc79169 100644 --- a/auth/auth-core/src/main/java/org/onap/aaf/auth/org/OrganizationFactory.java +++ b/auth/auth-core/src/main/java/org/onap/aaf/auth/org/OrganizationFactory.java @@ -105,7 +105,6 @@ public class OrganizationFactory { org.addSupportedRealm(r); } } - } catch (ClassNotFoundException | NoSuchMethodException | SecurityException | InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { 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 b36c6f24..f3c73216 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 @@ -31,14 +31,6 @@ import java.util.List; import java.util.Set; import java.util.regex.Pattern; -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.EmailWarnings; import org.onap.aaf.auth.org.Executor; @@ -76,11 +68,20 @@ public class DefaultOrg implements Organization { if(mailFrom==null) { throw new OrganizationException(s + PROPERTY_IS_REQUIRED); } + + // Note: This code is to avoid including javax.mail into ONAP, because there are security/licence + // exceptions + try { + Class.forName("javax.mail.Session"); // ensure package is loaded + @SuppressWarnings("unchecked") + Class<Mailer> minst = (Class<Mailer>)Class.forName("org.onap.aaf.org.JavaxMailer"); + mailer = minst.newInstance(); + } catch (ClassNotFoundException | InstantiationException | IllegalAccessException e1) { + env.warn().log("JavaxMailer not loaded. Mailing disabled"); + } System.getProperties().setProperty("mail.smtp.host",mailHost); System.getProperties().setProperty("mail.user", mailFrom); - // Get the default Session object. - session = Session.getDefaultInstance(System.getProperties()); try { String defFile; @@ -130,7 +131,7 @@ public class DefaultOrg implements Organization { public Identities identities; private boolean dryRun; - private Session session; + private Mailer mailer; public enum Types {Employee, Contractor, Application, NotActive}; private final static Set<String> typeSet; @@ -400,113 +401,6 @@ public class DefaultOrg implements Organization { return Response.OK; } - @Override - public int sendEmail(AuthzTrans trans, List<String> toList, List<String> ccList, String subject, String body, - Boolean urgent) throws OrganizationException { - - int status = 1; - - List<String> to = new ArrayList<>(); - for(String em : toList) { - if(em.indexOf('@')<0) { - to.add(new DefaultOrgIdentity(trans, em, this).email()); - } else { - to.add(em); - } - } - - List<String> cc = new ArrayList<>(); - if(ccList!=null) { - if(!ccList.isEmpty()) { - - for(String em : ccList) { - if(em.indexOf('@')<0) { - cc.add(new DefaultOrgIdentity(trans, em, this).email()); - } else { - cc.add(em); - } - } - } - - // for now, I want all emails so we can see what goes out. Remove later - if (!ccList.contains(mailFrom)) { - ccList.add(mailFrom); - } - } - - try { - // Create a default MimeMessage object. - MimeMessage message = new MimeMessage(session); - - // Set From: header field of the header. - message.setFrom(new InternetAddress(mailFrom)); - - if (!dryRun) { - // 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 ((ccList != null) && (ccList.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<String> 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; - } /** * Default Policy is to set to 6 Months for Notification Types. @@ -661,37 +555,6 @@ public class DefaultOrg implements Organization { this.dryRun = dryRun; } - /** - * Convert the delimiter String into Internet addresses with the default - * delimiter of ";" - * @param strAddress - * @return - */ - private Address[] getAddresses(List<String> 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<String> 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; - } - private String extractRealm(final String r) { int at; if((at=r.indexOf('@'))>=0) { @@ -719,4 +582,41 @@ public class DefaultOrg implements Organization { supportedRealms.add(extractRealm(r)); } + @Override + public int sendEmail(AuthzTrans trans, List<String> toList, List<String> ccList, String subject, String body, + Boolean urgent) throws OrganizationException { + if (mailer!=null) { + List<String> to = new ArrayList<>(); + for(String em : toList) { + if(em.indexOf('@')<0) { + to.add(new DefaultOrgIdentity(trans, em, this).email()); + } else { + to.add(em); + } + } + + List<String> cc = new ArrayList<>(); + if(ccList!=null) { + if(!ccList.isEmpty()) { + + for(String em : ccList) { + if(em.indexOf('@')<0) { + cc.add(new DefaultOrgIdentity(trans, em, this).email()); + } else { + cc.add(em); + } + } + } + + // for now, I want all emails so we can see what goes out. Remove later + if (!ccList.contains(mailFrom)) { + ccList.add(mailFrom); + } + } + + return mailer.sendEmail(trans,dryRun,mailFrom,to,cc,subject,body,urgent); + } else { + return 0; + } + } } 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 new file mode 100644 index 00000000..f50493dd --- /dev/null +++ b/auth/auth-deforg/src/main/java/org/onap/aaf/org/JavaxMailer.java @@ -0,0 +1,159 @@ +/** + * ============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<String> to, List<String> 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<String> 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<String> 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<String> 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 new file mode 100644 index 00000000..0824e1f3 --- /dev/null +++ b/auth/auth-deforg/src/main/java/org/onap/aaf/org/Mailer.java @@ -0,0 +1,39 @@ +/** + * ============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<String> toList, + List<String> ccList, + String subject, + String body, + Boolean urgent) throws OrganizationException; + +} |