diff options
Diffstat (limited to 'id/ConfigWebTool/src/main/java/at/gv/egovernment/moa/id/configuration/helper')
6 files changed, 407 insertions, 417 deletions
diff --git a/id/ConfigWebTool/src/main/java/at/gv/egovernment/moa/id/configuration/helper/AuthenticationHelper.java b/id/ConfigWebTool/src/main/java/at/gv/egovernment/moa/id/configuration/helper/AuthenticationHelper.java index 4d47d8d96..25cf87aa9 100644 --- a/id/ConfigWebTool/src/main/java/at/gv/egovernment/moa/id/configuration/helper/AuthenticationHelper.java +++ b/id/ConfigWebTool/src/main/java/at/gv/egovernment/moa/id/configuration/helper/AuthenticationHelper.java @@ -29,29 +29,27 @@ import javax.crypto.SecretKeyFactory; import javax.crypto.spec.PBEKeySpec; import javax.crypto.spec.SecretKeySpec; -import org.apache.log4j.Logger; - import at.gv.egovernment.moa.util.Base64Utils; +import lombok.extern.slf4j.Slf4j; +@Slf4j public class AuthenticationHelper { - - private static final Logger log = Logger.getLogger(AuthenticationHelper.class); - - public static String generateKeyFormPassword(String password) { - SecretKeyFactory factory; - - try { - factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1"); - KeySpec spec = new PBEKeySpec(password.toCharArray(), "TestSALT".getBytes(), 1024, 128); - SecretKey tmp = factory.generateSecret(spec); - SecretKeySpec secret = new SecretKeySpec(tmp.getEncoded(), "AES"); - return Base64Utils.encode(secret.getEncoded()); - - } catch (Exception e) { - log.info("Key generation form password failed."); - return null; - } - - } + + public static String generateKeyFormPassword(String password) { + SecretKeyFactory factory; + + try { + factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1"); + final KeySpec spec = new PBEKeySpec(password.toCharArray(), "TestSALT".getBytes(), 1024, 128); + final SecretKey tmp = factory.generateSecret(spec); + final SecretKeySpec secret = new SecretKeySpec(tmp.getEncoded(), "AES"); + return Base64Utils.encode(secret.getEncoded()); + + } catch (final Exception e) { + log.info("Key generation form password failed."); + return null; + } + + } } diff --git a/id/ConfigWebTool/src/main/java/at/gv/egovernment/moa/id/configuration/helper/DateTimeHelper.java b/id/ConfigWebTool/src/main/java/at/gv/egovernment/moa/id/configuration/helper/DateTimeHelper.java index eed4aa32f..a6c8b93b1 100644 --- a/id/ConfigWebTool/src/main/java/at/gv/egovernment/moa/id/configuration/helper/DateTimeHelper.java +++ b/id/ConfigWebTool/src/main/java/at/gv/egovernment/moa/id/configuration/helper/DateTimeHelper.java @@ -26,34 +26,32 @@ import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; -import org.apache.log4j.Logger; - import at.gv.egovernment.moa.util.MiscUtil; +import lombok.extern.slf4j.Slf4j; +@Slf4j public class DateTimeHelper { - private static final Logger log = Logger.getLogger(DateTimeHelper.class); - - private static final String DATETIMEPATTERN = "dd.MM.yyy HH:mm"; - - public static String getDateTime(Date date) { - SimpleDateFormat f = new SimpleDateFormat(DATETIMEPATTERN); - return f.format(date); - } - - public static Date parseDateTime(String date) { - SimpleDateFormat f = new SimpleDateFormat(DATETIMEPATTERN); - - if (MiscUtil.isNotEmpty(date)) { - - try { - return f.parse(date); - - } catch (ParseException e) { - log.warn("Parse DATETIME String " + date + " failed", e); - - } - } - return null; - } + private static final String DATETIMEPATTERN = "dd.MM.yyy HH:mm"; + + public static String getDateTime(Date date) { + final SimpleDateFormat f = new SimpleDateFormat(DATETIMEPATTERN); + return f.format(date); + } + + public static Date parseDateTime(String date) { + final SimpleDateFormat f = new SimpleDateFormat(DATETIMEPATTERN); + + if (MiscUtil.isNotEmpty(date)) { + + try { + return f.parse(date); + + } catch (final ParseException e) { + log.warn("Parse DATETIME String " + date + " failed", e); + + } + } + return null; + } } diff --git a/id/ConfigWebTool/src/main/java/at/gv/egovernment/moa/id/configuration/helper/FormDataHelper.java b/id/ConfigWebTool/src/main/java/at/gv/egovernment/moa/id/configuration/helper/FormDataHelper.java index b4afcb5f2..406acf001 100644 --- a/id/ConfigWebTool/src/main/java/at/gv/egovernment/moa/id/configuration/helper/FormDataHelper.java +++ b/id/ConfigWebTool/src/main/java/at/gv/egovernment/moa/id/configuration/helper/FormDataHelper.java @@ -26,77 +26,76 @@ import java.util.ArrayList; import java.util.Date; import java.util.List; -import at.gv.egovernment.moa.id.commons.db.dao.config.deprecated.OnlineApplication; import at.gv.egovernment.moa.id.commons.db.dao.config.UserDatabase; +import at.gv.egovernment.moa.id.commons.db.dao.config.deprecated.OnlineApplication; import at.gv.egovernment.moa.id.configuration.auth.AuthenticatedUser; import at.gv.egovernment.moa.id.configuration.data.OAListElement; import at.gv.egovernment.moa.id.configuration.data.OAListElement.ServiceType; public class FormDataHelper { - public static ArrayList<OAListElement> populateFormWithInderfederationIDPs(List<OnlineApplication> dbOAs) { - - ArrayList<OAListElement> formOAs = new ArrayList<OAListElement>(); - - for (OnlineApplication dboa : dbOAs) { - - if (dboa.isIsInterfederationIDP()!= null && dboa.isIsInterfederationIDP()) - formOAs.add(addOAFormListElement(dboa, ServiceType.IDP)); - - else if (dboa.isIsInterfederationGateway()!= null && dboa.isIsInterfederationGateway()) - formOAs.add(addOAFormListElement(dboa, ServiceType.GWAY)); - - else if (dboa.getAuthComponentOA().getOASTORK() != null - && dboa.getAuthComponentOA().getOASTORK().isVidpEnabled() != null - && dboa.getAuthComponentOA().getOASTORK().isVidpEnabled()) - formOAs.add(addOAFormListElement(dboa, ServiceType.VIDP)); - } - return formOAs; - } - - public static ArrayList<OAListElement> populateFormWithOAs(List<OnlineApplication> dbOAs) { - - ArrayList<OAListElement> formOAs = new ArrayList<OAListElement>(); - - for (OnlineApplication dboa : dbOAs) { - - if ( !((dboa.isIsInterfederationIDP() != null && dboa.isIsInterfederationIDP()) || - (dboa.isIsInterfederationGateway() != null && dboa.isIsInterfederationGateway()) || - (dboa.getAuthComponentOA().getOASTORK() != null - && dboa.getAuthComponentOA().getOASTORK().isVidpEnabled() != null - && dboa.getAuthComponentOA().getOASTORK().isVidpEnabled()) || - (dboa.isIsInterfederationGateway() != null && dboa.isIsInterfederationGateway() ))) { - formOAs.add(addOAFormListElement(dboa, ServiceType.OA)); - } - } - return formOAs; - } - - private static OAListElement addOAFormListElement(OnlineApplication dboa, ServiceType type) { - OAListElement listoa = new OAListElement(type); - listoa.setActive(dboa.isIsActive()); - listoa.setDataBaseID(dboa.getHjid()); - listoa.setOaFriendlyName(dboa.getFriendlyName()); - listoa.setOaIdentifier(dboa.getPublicURLPrefix()); - listoa.setOaType(dboa.getType()); - return listoa; - } - - public static ArrayList<AuthenticatedUser> addFormUsers(List<UserDatabase> dbuserlist) { - ArrayList<AuthenticatedUser> userlist = new ArrayList<AuthenticatedUser>(); - - for (UserDatabase dbuser : dbuserlist) { - - boolean ismandate = false; - if (dbuser.isIsMandateUser() != null) - ismandate = dbuser.isIsMandateUser(); - - userlist.add(new AuthenticatedUser(dbuser, - dbuser.isIsActive(), - ismandate, - false, null, null, new Date()) - ); - } - return userlist; - } + public static ArrayList<OAListElement> populateFormWithInderfederationIDPs(List<OnlineApplication> dbOAs) { + + final ArrayList<OAListElement> formOAs = new ArrayList<>(); + + for (final OnlineApplication dboa : dbOAs) { + + if (dboa.isIsInterfederationIDP() != null && dboa.isIsInterfederationIDP()) { + formOAs.add(addOAFormListElement(dboa, ServiceType.IDP)); + } else if (dboa.isIsInterfederationGateway() != null && dboa.isIsInterfederationGateway()) { + formOAs.add(addOAFormListElement(dboa, ServiceType.GWAY)); + } else if (dboa.getAuthComponentOA().getOASTORK() != null + && dboa.getAuthComponentOA().getOASTORK().isVidpEnabled() != null + && dboa.getAuthComponentOA().getOASTORK().isVidpEnabled()) { + formOAs.add(addOAFormListElement(dboa, ServiceType.VIDP)); + } + } + return formOAs; + } + + public static ArrayList<OAListElement> populateFormWithOAs(List<OnlineApplication> dbOAs) { + + final ArrayList<OAListElement> formOAs = new ArrayList<>(); + + for (final OnlineApplication dboa : dbOAs) { + + if (!(dboa.isIsInterfederationIDP() != null && dboa.isIsInterfederationIDP() || + dboa.isIsInterfederationGateway() != null && dboa.isIsInterfederationGateway() || + dboa.getAuthComponentOA().getOASTORK() != null + && dboa.getAuthComponentOA().getOASTORK().isVidpEnabled() != null + && dboa.getAuthComponentOA().getOASTORK().isVidpEnabled() || + dboa.isIsInterfederationGateway() != null && dboa.isIsInterfederationGateway())) { + formOAs.add(addOAFormListElement(dboa, ServiceType.OA)); + } + } + return formOAs; + } + + private static OAListElement addOAFormListElement(OnlineApplication dboa, ServiceType type) { + final OAListElement listoa = new OAListElement(type); + listoa.setActive(dboa.isIsActive()); + listoa.setDataBaseID(dboa.getHjid()); + listoa.setOaFriendlyName(dboa.getFriendlyName()); + listoa.setOaIdentifier(dboa.getPublicURLPrefix()); + listoa.setOaType(dboa.getType()); + return listoa; + } + + public static ArrayList<AuthenticatedUser> addFormUsers(List<UserDatabase> dbuserlist) { + final ArrayList<AuthenticatedUser> userlist = new ArrayList<>(); + + for (final UserDatabase dbuser : dbuserlist) { + + boolean ismandate = false; + if (dbuser.isIsMandateUser() != null) { + ismandate = dbuser.isIsMandateUser(); + } + + userlist.add(new AuthenticatedUser(dbuser, + dbuser.isIsActive(), + ismandate, + false, null, null, new Date())); + } + return userlist; + } } diff --git a/id/ConfigWebTool/src/main/java/at/gv/egovernment/moa/id/configuration/helper/LanguageHelper.java b/id/ConfigWebTool/src/main/java/at/gv/egovernment/moa/id/configuration/helper/LanguageHelper.java index 29ab75b3e..d4f4d2129 100644 --- a/id/ConfigWebTool/src/main/java/at/gv/egovernment/moa/id/configuration/helper/LanguageHelper.java +++ b/id/ConfigWebTool/src/main/java/at/gv/egovernment/moa/id/configuration/helper/LanguageHelper.java @@ -22,81 +22,73 @@ *******************************************************************************/ package at.gv.egovernment.moa.id.configuration.helper; +import java.text.MessageFormat; +import java.util.Locale; +import java.util.ResourceBundle; + +import javax.servlet.http.HttpServletRequest; import at.gv.egovernment.moa.id.config.webgui.exception.ConfigurationException; import at.gv.egovernment.moa.id.configuration.Constants; import at.gv.egovernment.moa.id.configuration.config.ConfigurationProvider; -import at.gv.egovernment.moa.util.MiscUtil; +import lombok.extern.slf4j.Slf4j; -import javax.servlet.http.HttpServletRequest; -import java.text.MessageFormat; -import java.util.Locale; -import java.util.ResourceBundle; +@Slf4j +public class LanguageHelper { + private static String errorLanguage(String code, Locale locale) { + return ResourceBundle.getBundle("applicationResources", locale).getString(code); -import org.apache.log4j.Logger; + } + private static String guiLanguage(String code, Locale locale) { + return ResourceBundle.getBundle("applicationResources", locale).getString(code); -public class LanguageHelper { + } - private static Logger log = Logger.getLogger(LanguageHelper.class); - - private static String errorLanguage(String code, Locale locale) { - return ResourceBundle.getBundle("applicationResources", locale).getString(code); - - } + public static String getGUIString(String code, HttpServletRequest request) { + return guiLanguage(code, getLangFromRequest(request)); + } - private static String guiLanguage(String code, Locale locale) { - return ResourceBundle.getBundle("applicationResources", locale).getString(code); - - } + public static String getErrorString(String code, HttpServletRequest request) { + return errorLanguage(code, getLangFromRequest(request)); + } - public static String getGUIString(String code, HttpServletRequest request) { - return guiLanguage(code, getLangFromRequest(request)); - } + public static String getGUIString(String code, String parameter, HttpServletRequest request) { + return MessageFormat.format(getGUIString(code, request), parameter); + } + public static String getErrorString(String code, Object[] parameter, HttpServletRequest request) { - public static String getErrorString(String code, HttpServletRequest request) { - return errorLanguage(code, getLangFromRequest(request)); - } + return MessageFormat.format(getGUIString(code, request), parameter); + } - public static String getGUIString(String code, String parameter, HttpServletRequest request) { - return MessageFormat.format(getGUIString(code, request), parameter); - } + private static Locale getLangFromRequest(HttpServletRequest request) { + + Locale defaultLanguage = Locale.forLanguageTag("de"); - public static String getErrorString(String code, Object[] parameter, HttpServletRequest request) { + try { + final ConfigurationProvider configurationProvider = ConfigurationProvider.getInstance(); + defaultLanguage = Locale.forLanguageTag(configurationProvider.getDefaultLanguage()); - return MessageFormat.format(getGUIString(code, request), parameter); + } catch (final ConfigurationException e) { + log.error("Configuration exception while getting ConfigurationProvider instance", e); } - - private static Locale getLangFromRequest(HttpServletRequest request) { - - Locale defaultLanguage = Locale.forLanguageTag("de"); - - try { - ConfigurationProvider configurationProvider = ConfigurationProvider.getInstance(); - defaultLanguage = Locale.forLanguageTag(configurationProvider.getDefaultLanguage()); - - } catch (ConfigurationException e) { - log.error("Configuration exception while getting ConfigurationProvider instance", e); - } - - - if (request == null) { - return defaultLanguage; - - } else { - Object obj = request.getSession().getAttribute(Constants.SESSION_I18n); - - if (obj != null && obj instanceof Locale) { - return (Locale) obj; - - } else - return defaultLanguage; - - } - + if (request == null) { + return defaultLanguage; + + } else { + final Object obj = request.getSession().getAttribute(Constants.SESSION_I18n); + + if (obj != null && obj instanceof Locale) { + return (Locale) obj; + + } else { + return defaultLanguage; + } + } -} + } +} diff --git a/id/ConfigWebTool/src/main/java/at/gv/egovernment/moa/id/configuration/helper/MailHelper.java b/id/ConfigWebTool/src/main/java/at/gv/egovernment/moa/id/configuration/helper/MailHelper.java index 8f3b8f479..5d1f663a9 100644 --- a/id/ConfigWebTool/src/main/java/at/gv/egovernment/moa/id/configuration/helper/MailHelper.java +++ b/id/ConfigWebTool/src/main/java/at/gv/egovernment/moa/id/configuration/helper/MailHelper.java @@ -41,7 +41,6 @@ import javax.mail.internet.MimeMessage; import javax.mail.internet.MimeMultipart; import org.apache.commons.io.IOUtils; -import org.apache.log4j.Logger; import at.gv.egiz.eaaf.core.impl.utils.FileUtils; import at.gv.egovernment.moa.id.commons.db.dao.config.UserDatabase; @@ -49,207 +48,213 @@ import at.gv.egovernment.moa.id.config.webgui.exception.ConfigurationException; import at.gv.egovernment.moa.id.configuration.Constants; import at.gv.egovernment.moa.id.configuration.config.ConfigurationProvider; import at.gv.egovernment.moa.util.MiscUtil; +import lombok.extern.slf4j.Slf4j; +@Slf4j public class MailHelper { - private static final Logger log = Logger.getLogger(MailHelper.class); - - private static final String PATTERN_GIVENNAME = "#GIVENNAME#"; - private static final String PATTERN_FAMILYNAME = "#FAMILYNAME#"; - private static final String PATTERN_URL = "#MANDATE_SERVICE_LINK#"; - private static final String PATTERN_DATE = "#TODAY_DATE#"; - private static final String PATTERN_OPENOAS = "#NUMBER_OAS#"; - private static final String PATTERN_OPENUSERS = "#NUMBER_USERSS#"; - private static final String PATTERN_OANAME = "#OANAME#"; - - public static void sendUserMailAddressVerification(UserDatabase userdb) throws ConfigurationException { - - ConfigurationProvider config = ConfigurationProvider.getInstance(); - String templateurl = config.getMailUserAcountVerificationTemplate(); - - String template = readTemplateFromURL(templateurl, config.getConfigRootDir()); - - if (userdb.isIsMandateUser() != null && userdb.isIsMandateUser()) { - template = template.replace(PATTERN_GIVENNAME, userdb.getInstitut()); - template = template.replace(PATTERN_FAMILYNAME, ""); - - } else { - template = template.replace(PATTERN_GIVENNAME, userdb.getGivenname()); - template = template.replace(PATTERN_FAMILYNAME, userdb.getFamilyname()); - } - - SimpleDateFormat dateformat = new SimpleDateFormat("dd.MM.yyyy"); - template = template.replace(PATTERN_DATE, dateformat.format(new Date())); - - String verificationURL = config.getPublicUrlPreFix(null); - - if (!verificationURL.endsWith("/")) - verificationURL = verificationURL + "/"; - - verificationURL = verificationURL + Constants.SERVLET_ACCOUNTVERIFICATION + - "?" + Constants.REQUEST_USERREQUESTTOKKEN + - "=" + userdb.getUserRequestTokken(); - template = template.replace(PATTERN_URL, verificationURL); - - sendMail(config, config.getMailUserAcountVerificationSubject(), - userdb.getMail(), template); - - } - - public static void sendAdminMail(int numOpenOAs, int numOpenUsers) throws ConfigurationException { - ConfigurationProvider config = ConfigurationProvider.getInstance(); - String templateurl = config.getMailAdminTemplate(); - - String template = readTemplateFromURL(templateurl, config.getConfigRootDir()); - template = template.replace(PATTERN_OPENOAS, String.valueOf(numOpenOAs)); - template = template.replace(PATTERN_OPENUSERS, String.valueOf(numOpenUsers)); - - SimpleDateFormat dateformat = new SimpleDateFormat("dd.MM.yyyy"); - template = template.replace(PATTERN_DATE, dateformat.format(new Date())); - - sendMail(config, config.getMailAdminSubject(), config.getMailAdminAddress(), template); - - } - - public static void sendUserAccountActivationMail(String givenname, String familyname, String institut, String mailurl) throws ConfigurationException { - ConfigurationProvider config = ConfigurationProvider.getInstance(); - String templateurl = config.getMailUserAcountActivationTemplate(); - - String template = readTemplateFromURL(templateurl, config.getConfigRootDir()); - if (MiscUtil.isNotEmpty(institut)) { - template = template.replace(PATTERN_GIVENNAME, institut); - template = template.replace(PATTERN_FAMILYNAME, ""); - - } else { - template = template.replace(PATTERN_GIVENNAME, givenname); - template = template.replace(PATTERN_FAMILYNAME, familyname); - } - - - SimpleDateFormat dateformat = new SimpleDateFormat("dd.MM.yyyy"); - template = template.replace(PATTERN_DATE, dateformat.format(new Date())); - - String verificationURL = config.getPublicUrlPreFix(null); - if (!verificationURL.endsWith("/")) - verificationURL = verificationURL + "/"; - - template = template.replace(PATTERN_URL, verificationURL); - - sendMail(config, config.getMailUserAcountActivationSubject(), - mailurl, template); - } - - public static void sendUserOnlineApplicationActivationMail(String givenname, String familyname, String institut, String oaname, String mailurl) throws ConfigurationException { - ConfigurationProvider config = ConfigurationProvider.getInstance(); - String templateurl = config.getMailOAActivationTemplate(); - - String template = readTemplateFromURL(templateurl, config.getConfigRootDir()); - if (MiscUtil.isNotEmpty(institut)) { - template = template.replace(PATTERN_GIVENNAME, institut); - template = template.replace(PATTERN_FAMILYNAME, ""); - - } else { - template = template.replace(PATTERN_GIVENNAME, givenname); - template = template.replace(PATTERN_FAMILYNAME, familyname); - } - - template = template.replace(PATTERN_OANAME, oaname); - - SimpleDateFormat dateformat = new SimpleDateFormat("dd.MM.yyyy"); - template = template.replace(PATTERN_DATE, dateformat.format(new Date())); - - String verificationURL = config.getPublicUrlPreFix(null); - if (!verificationURL.endsWith("/")) - verificationURL = verificationURL + "/"; - - template = template.replace(PATTERN_URL, verificationURL); - - sendMail(config, config.getMailOAActivationSubject(), - mailurl, template); - } - - public static void sendUserAccountRevocationMail(UserDatabase userdb) throws ConfigurationException { - ConfigurationProvider config = ConfigurationProvider.getInstance(); - String templateurl = config.getMailUserAcountRevocationTemplate(); - - String template = readTemplateFromURL(templateurl, config.getConfigRootDir()); - - if (userdb.isIsMandateUser() != null && userdb.isIsMandateUser()) { - template = template.replace(PATTERN_GIVENNAME, userdb.getInstitut()); - template = template.replace(PATTERN_FAMILYNAME, ""); - - } else { - template = template.replace(PATTERN_GIVENNAME, userdb.getGivenname()); - template = template.replace(PATTERN_FAMILYNAME, userdb.getFamilyname()); - } - - SimpleDateFormat dateformat = new SimpleDateFormat("dd.MM.yyyy"); - template = template.replace(PATTERN_DATE, dateformat.format(new Date())); - - sendMail(config, config.getMailUserAcountActivationSubject(), - userdb.getMail(), template); - } - - private static String readTemplateFromURL(String templateurl, String rootDir) throws ConfigurationException { - InputStream input; - try { - - URL keystoreURL = new URL(FileUtils.makeAbsoluteURL(templateurl, rootDir)); - input = keystoreURL.openStream(); - StringWriter writer = new StringWriter(); - IOUtils.copy(input, writer); - input.close(); - return writer.toString(); - - } catch (Exception e) { - log.warn("Mailtemplate can not be read from source" + templateurl); - throw new ConfigurationException("Mailtemplate can not be read from source" + templateurl); - - } - } - - private static void sendMail(ConfigurationProvider config, String subject, String recipient, String content) throws ConfigurationException { - try { - log.debug("Sending mail."); - MiscUtil.assertNotNull(subject, "subject"); - MiscUtil.assertNotNull(recipient, "recipient"); - MiscUtil.assertNotNull(content, "content"); - - Properties props = new Properties(); - props.setProperty("mail.transport.protocol", "smtp"); - props.setProperty("mail.host", config.getSMTPMailHost()); - log.trace("Mail host: " + config.getSMTPMailHost()); - if (config.getSMTPMailPort() != null) { - log.trace("Mail port: " + config.getSMTPMailPort()); - props.setProperty("mail.port", config.getSMTPMailPort()); - } - if (config.getSMTPMailUsername() != null) { - log.trace("Mail user: " + config.getSMTPMailUsername()); - props.setProperty("mail.user", config.getSMTPMailUsername()); - } - if (config.getSMTPMailPassword() != null) { - log.trace("Mail password: " + config.getSMTPMailPassword()); - props.setProperty("mail.password", config.getSMTPMailPassword()); - } - - Session mailSession = Session.getDefaultInstance(props, null); - Transport transport = mailSession.getTransport(); - - MimeMessage message = new MimeMessage(mailSession); - message.setSubject(subject); - log.trace("Mail from: " + config.getMailFromName() + "/" + config.getMailFromAddress()); - message.setFrom(new InternetAddress(config.getMailFromAddress(), config.getMailFromName())); - log.trace("Recipient: " + recipient); - message.addRecipient(Message.RecipientType.TO, new InternetAddress(recipient)); - - log.trace("Creating multipart content of mail."); - MimeMultipart multipart = new MimeMultipart("related"); - - log.trace("Adding first part (html)"); - BodyPart messageBodyPart = new MimeBodyPart(); - messageBodyPart.setContent(content, "text/html; charset=ISO-8859-15"); - multipart.addBodyPart(messageBodyPart); - + private static final String PATTERN_GIVENNAME = "#GIVENNAME#"; + private static final String PATTERN_FAMILYNAME = "#FAMILYNAME#"; + private static final String PATTERN_URL = "#MANDATE_SERVICE_LINK#"; + private static final String PATTERN_DATE = "#TODAY_DATE#"; + private static final String PATTERN_OPENOAS = "#NUMBER_OAS#"; + private static final String PATTERN_OPENUSERS = "#NUMBER_USERSS#"; + private static final String PATTERN_OANAME = "#OANAME#"; + + public static void sendUserMailAddressVerification(UserDatabase userdb) throws ConfigurationException { + + final ConfigurationProvider config = ConfigurationProvider.getInstance(); + final String templateurl = config.getMailUserAcountVerificationTemplate(); + + String template = readTemplateFromURL(templateurl, config.getConfigRootDir()); + + if (userdb.isIsMandateUser() != null && userdb.isIsMandateUser()) { + template = template.replace(PATTERN_GIVENNAME, userdb.getInstitut()); + template = template.replace(PATTERN_FAMILYNAME, ""); + + } else { + template = template.replace(PATTERN_GIVENNAME, userdb.getGivenname()); + template = template.replace(PATTERN_FAMILYNAME, userdb.getFamilyname()); + } + + final SimpleDateFormat dateformat = new SimpleDateFormat("dd.MM.yyyy"); + template = template.replace(PATTERN_DATE, dateformat.format(new Date())); + + String verificationURL = config.getPublicUrlPreFix(null); + + if (!verificationURL.endsWith("/")) { + verificationURL = verificationURL + "/"; + } + + verificationURL = verificationURL + Constants.SERVLET_ACCOUNTVERIFICATION + + "?" + Constants.REQUEST_USERREQUESTTOKKEN + + "=" + userdb.getUserRequestTokken(); + template = template.replace(PATTERN_URL, verificationURL); + + sendMail(config, config.getMailUserAcountVerificationSubject(), + userdb.getMail(), template); + + } + + public static void sendAdminMail(int numOpenOAs, int numOpenUsers) throws ConfigurationException { + final ConfigurationProvider config = ConfigurationProvider.getInstance(); + final String templateurl = config.getMailAdminTemplate(); + + String template = readTemplateFromURL(templateurl, config.getConfigRootDir()); + template = template.replace(PATTERN_OPENOAS, String.valueOf(numOpenOAs)); + template = template.replace(PATTERN_OPENUSERS, String.valueOf(numOpenUsers)); + + final SimpleDateFormat dateformat = new SimpleDateFormat("dd.MM.yyyy"); + template = template.replace(PATTERN_DATE, dateformat.format(new Date())); + + sendMail(config, config.getMailAdminSubject(), config.getMailAdminAddress(), template); + + } + + public static void sendUserAccountActivationMail(String givenname, String familyname, String institut, + String mailurl) throws ConfigurationException { + final ConfigurationProvider config = ConfigurationProvider.getInstance(); + final String templateurl = config.getMailUserAcountActivationTemplate(); + + String template = readTemplateFromURL(templateurl, config.getConfigRootDir()); + if (MiscUtil.isNotEmpty(institut)) { + template = template.replace(PATTERN_GIVENNAME, institut); + template = template.replace(PATTERN_FAMILYNAME, ""); + + } else { + template = template.replace(PATTERN_GIVENNAME, givenname); + template = template.replace(PATTERN_FAMILYNAME, familyname); + } + + final SimpleDateFormat dateformat = new SimpleDateFormat("dd.MM.yyyy"); + template = template.replace(PATTERN_DATE, dateformat.format(new Date())); + + String verificationURL = config.getPublicUrlPreFix(null); + if (!verificationURL.endsWith("/")) { + verificationURL = verificationURL + "/"; + } + + template = template.replace(PATTERN_URL, verificationURL); + + sendMail(config, config.getMailUserAcountActivationSubject(), + mailurl, template); + } + + public static void sendUserOnlineApplicationActivationMail(String givenname, String familyname, + String institut, String oaname, String mailurl) throws ConfigurationException { + final ConfigurationProvider config = ConfigurationProvider.getInstance(); + final String templateurl = config.getMailOAActivationTemplate(); + + String template = readTemplateFromURL(templateurl, config.getConfigRootDir()); + if (MiscUtil.isNotEmpty(institut)) { + template = template.replace(PATTERN_GIVENNAME, institut); + template = template.replace(PATTERN_FAMILYNAME, ""); + + } else { + template = template.replace(PATTERN_GIVENNAME, givenname); + template = template.replace(PATTERN_FAMILYNAME, familyname); + } + + template = template.replace(PATTERN_OANAME, oaname); + + final SimpleDateFormat dateformat = new SimpleDateFormat("dd.MM.yyyy"); + template = template.replace(PATTERN_DATE, dateformat.format(new Date())); + + String verificationURL = config.getPublicUrlPreFix(null); + if (!verificationURL.endsWith("/")) { + verificationURL = verificationURL + "/"; + } + + template = template.replace(PATTERN_URL, verificationURL); + + sendMail(config, config.getMailOAActivationSubject(), + mailurl, template); + } + + public static void sendUserAccountRevocationMail(UserDatabase userdb) throws ConfigurationException { + final ConfigurationProvider config = ConfigurationProvider.getInstance(); + final String templateurl = config.getMailUserAcountRevocationTemplate(); + + String template = readTemplateFromURL(templateurl, config.getConfigRootDir()); + + if (userdb.isIsMandateUser() != null && userdb.isIsMandateUser()) { + template = template.replace(PATTERN_GIVENNAME, userdb.getInstitut()); + template = template.replace(PATTERN_FAMILYNAME, ""); + + } else { + template = template.replace(PATTERN_GIVENNAME, userdb.getGivenname()); + template = template.replace(PATTERN_FAMILYNAME, userdb.getFamilyname()); + } + + final SimpleDateFormat dateformat = new SimpleDateFormat("dd.MM.yyyy"); + template = template.replace(PATTERN_DATE, dateformat.format(new Date())); + + sendMail(config, config.getMailUserAcountActivationSubject(), + userdb.getMail(), template); + } + + private static String readTemplateFromURL(String templateurl, String rootDir) + throws ConfigurationException { + InputStream input; + try { + + final URL keystoreURL = new URL(FileUtils.makeAbsoluteURL(templateurl, rootDir)); + input = keystoreURL.openStream(); + final StringWriter writer = new StringWriter(); + IOUtils.copy(input, writer); + input.close(); + return writer.toString(); + + } catch (final Exception e) { + log.warn("Mailtemplate can not be read from source" + templateurl); + throw new ConfigurationException("Mailtemplate can not be read from source" + templateurl); + + } + } + + private static void sendMail(ConfigurationProvider config, String subject, String recipient, String content) + throws ConfigurationException { + try { + log.debug("Sending mail."); + MiscUtil.assertNotNull(subject, "subject"); + MiscUtil.assertNotNull(recipient, "recipient"); + MiscUtil.assertNotNull(content, "content"); + + final Properties props = new Properties(); + props.setProperty("mail.transport.protocol", "smtp"); + props.setProperty("mail.host", config.getSMTPMailHost()); + log.trace("Mail host: " + config.getSMTPMailHost()); + if (config.getSMTPMailPort() != null) { + log.trace("Mail port: " + config.getSMTPMailPort()); + props.setProperty("mail.port", config.getSMTPMailPort()); + } + if (config.getSMTPMailUsername() != null) { + log.trace("Mail user: " + config.getSMTPMailUsername()); + props.setProperty("mail.user", config.getSMTPMailUsername()); + } + if (config.getSMTPMailPassword() != null) { + log.trace("Mail password: " + config.getSMTPMailPassword()); + props.setProperty("mail.password", config.getSMTPMailPassword()); + } + + final Session mailSession = Session.getDefaultInstance(props, null); + final Transport transport = mailSession.getTransport(); + + final MimeMessage message = new MimeMessage(mailSession); + message.setSubject(subject); + log.trace("Mail from: " + config.getMailFromName() + "/" + config.getMailFromAddress()); + message.setFrom(new InternetAddress(config.getMailFromAddress(), config.getMailFromName())); + log.trace("Recipient: " + recipient); + message.addRecipient(Message.RecipientType.TO, new InternetAddress(recipient)); + + log.trace("Creating multipart content of mail."); + final MimeMultipart multipart = new MimeMultipart("related"); + + log.trace("Adding first part (html)"); + final BodyPart messageBodyPart = new MimeBodyPart(); + messageBodyPart.setContent(content, "text/html; charset=ISO-8859-15"); + multipart.addBodyPart(messageBodyPart); + // log.trace("Adding mail images"); // messageBodyPart = new MimeBodyPart(); // for (Image image : images) { @@ -257,20 +262,20 @@ public class MailHelper { // messageBodyPart.setHeader("Content-ID", "<" + image.getContentId() + ">"); // multipart.addBodyPart(messageBodyPart); // } - - message.setContent(multipart); - transport.connect(); - log.trace("Sending mail message."); - transport.sendMessage(message, message.getRecipients(Message.RecipientType.TO)); - log.trace("Successfully sent."); - transport.close(); - - } catch(MessagingException e) { - throw new ConfigurationException(e); - - } catch (UnsupportedEncodingException e) { - throw new ConfigurationException(e); - - } - } + + message.setContent(multipart); + transport.connect(); + log.trace("Sending mail message."); + transport.sendMessage(message, message.getRecipients(Message.RecipientType.TO)); + log.trace("Successfully sent."); + transport.close(); + + } catch (final MessagingException e) { + throw new ConfigurationException(e); + + } catch (final UnsupportedEncodingException e) { + throw new ConfigurationException(e); + + } + } } diff --git a/id/ConfigWebTool/src/main/java/at/gv/egovernment/moa/id/configuration/helper/StringHelper.java b/id/ConfigWebTool/src/main/java/at/gv/egovernment/moa/id/configuration/helper/StringHelper.java index 53afa59a0..be4cab9d7 100644 --- a/id/ConfigWebTool/src/main/java/at/gv/egovernment/moa/id/configuration/helper/StringHelper.java +++ b/id/ConfigWebTool/src/main/java/at/gv/egovernment/moa/id/configuration/helper/StringHelper.java @@ -26,37 +26,35 @@ import java.io.UnsupportedEncodingException; public class StringHelper { - public static String formatText(String strGivenText) - { - StringBuffer sbFormattedText = new StringBuffer(strGivenText); - - for(int i=0; i<sbFormattedText.length(); i++) - { - if(sbFormattedText.charAt(i) == '\n') { - sbFormattedText.deleteCharAt(i); - i--; - } - - if(sbFormattedText.charAt(i) == '\r') { - sbFormattedText.deleteCharAt(i); - i--; - } - - if(sbFormattedText.charAt(i) == '\t') { - sbFormattedText.deleteCharAt(i); - i--; - } - } - return sbFormattedText.toString(); + public static String formatText(String strGivenText) { + final StringBuffer sbFormattedText = new StringBuffer(strGivenText); + + for (int i = 0; i < sbFormattedText.length(); i++) { + if (sbFormattedText.charAt(i) == '\n') { + sbFormattedText.deleteCharAt(i); + i--; + } + + if (sbFormattedText.charAt(i) == '\r') { + sbFormattedText.deleteCharAt(i); + i--; + } + + if (sbFormattedText.charAt(i) == '\t') { + sbFormattedText.deleteCharAt(i); + i--; + } + } + return sbFormattedText.toString(); + } + + public static String getUTF8String(String input) { + try { + return new String(input.getBytes(), "UTF-8"); + + } catch (final UnsupportedEncodingException e) { + e.printStackTrace(); + return input; } - - public static String getUTF8String(String input) { - try { - return new String(input.getBytes(), "UTF-8"); - - } catch (UnsupportedEncodingException e) { - e.printStackTrace(); - return input; - } - } + } } |