summaryrefslogtreecommitdiffstats
path: root/pki/base/common/src/com/netscape/cmscore/notification
diff options
context:
space:
mode:
Diffstat (limited to 'pki/base/common/src/com/netscape/cmscore/notification')
-rw-r--r--pki/base/common/src/com/netscape/cmscore/notification/EmailFormProcessor.java250
-rw-r--r--pki/base/common/src/com/netscape/cmscore/notification/EmailResolverKeys.java86
-rw-r--r--pki/base/common/src/com/netscape/cmscore/notification/EmailTemplate.java185
-rw-r--r--pki/base/common/src/com/netscape/cmscore/notification/ReqCertEmailResolver.java151
-rw-r--r--pki/base/common/src/com/netscape/cmscore/notification/ReqCertSANameEmailResolver.java268
5 files changed, 940 insertions, 0 deletions
diff --git a/pki/base/common/src/com/netscape/cmscore/notification/EmailFormProcessor.java b/pki/base/common/src/com/netscape/cmscore/notification/EmailFormProcessor.java
new file mode 100644
index 000000000..b04186c3a
--- /dev/null
+++ b/pki/base/common/src/com/netscape/cmscore/notification/EmailFormProcessor.java
@@ -0,0 +1,250 @@
+// --- BEGIN COPYRIGHT BLOCK ---
+// This program is free software; you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation; version 2 of the License.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License along
+// with this program; if not, write to the Free Software Foundation, Inc.,
+// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+//
+// (C) 2007 Red Hat, Inc.
+// All rights reserved.
+// --- END COPYRIGHT BLOCK ---
+package com.netscape.cmscore.notification;
+
+
+import com.netscape.certsrv.logging.*;
+import com.netscape.certsrv.apps.*;
+import com.netscape.certsrv.notification.*;
+import java.util.*;
+import java.lang.*;
+
+
+/**
+ * formulates the final email. Escape character '\' is understood.
+ * '$' is used preceeding a token name. A token name should not be a
+ * substring of any other token name
+ * <p>
+ * @author cfu
+ * @version $Revision$, $Date$
+ */
+public class EmailFormProcessor implements IEmailFormProcessor {
+
+ protected final static String TOK_PREFIX = "$";
+ protected final static String TOK_ESC = "\\";
+ protected final static char TOK_END = ' ';
+ protected final static String TOK_VALUE_UNKNOWN = "VALUE UNKNOWN";
+ protected final static String TOK_TOKEN_UNKNOWN = "UNKNOWN TOKEN:";
+ protected ILogger mLogger = CMS.getLogger();
+
+ // stores all the available token keys; added so that we can
+ // parse strings to replace unresolvable token keys and replace
+ // them by the words "VALUE UNKNOWN"
+ protected static String[] token_keys = {
+ TOKEN_ID,
+ TOKEN_SERIAL_NUM,
+ TOKEN_HTTP_HOST,
+ TOKEN_HTTP_PORT,
+ TOKEN_ISSUER_DN,
+ TOKEN_SUBJECT_DN,
+ TOKEN_REQUESTOR_EMAIL,
+ TOKEN_CERT_TYPE,
+ TOKEN_REQUEST_TYPE,
+ TOKEN_STATUS,
+ TOKEN_NOT_AFTER,
+ TOKEN_NOT_BEFORE,
+ TOKEN_SENDER_EMAIL,
+ TOKEN_RECIPIENT_EMAIL,
+ TOKEN_SUMMARY_ITEM_LIST,
+ TOKEN_SUMMARY_TOTAL_NUM,
+ TOKEN_SUMMARY_SUCCESS_NUM,
+ TOKEN_SUMMARY_FAILURE_NUM,
+ TOKEN_EXECUTION_TIME
+ };
+
+ // stores the eventual content of the email
+ Vector mContent = new Vector();
+ Hashtable mTok2vals = null;
+
+ public EmailFormProcessor() {
+ }
+
+ /*
+ * takes the form template, parse and replace all $tokens with the
+ * right values. It handles escape character '\'
+ * @param form The locale specific form template,
+ * @param tok2vals a hashtable containing one to one mapping
+ * from $tokens used by the admins in the form template to the real
+ * values corresponding to the $tokens
+ * @return mail content
+ */
+ public String getEmailContent(String form,
+ Hashtable tok2vals) {
+ mTok2vals = tok2vals;
+
+ if (form == null) {
+ log(ILogger.LL_FAILURE, CMS.getLogMessage("CMSCORE_NOTIFY_TEMPLATE_NULL"));
+ return null;
+ }
+
+ if (mTok2vals == null) {
+ log(ILogger.LL_FAILURE, CMS.getLogMessage("CMSCORE_NOTIFY_TOKEN_NULL"));
+ return null;
+ }
+
+ /**
+ * first, take care of the escape characters '\'
+ */
+ StringTokenizer es = new StringTokenizer(form, TOK_ESC);
+
+ if (es.hasMoreTokens() && !form.startsWith(TOK_ESC)) {
+ dollarProcess(es.nextToken());
+ }
+
+ // rest of them start with '\'
+ while (es.hasMoreTokens()) {
+ String t = es.nextToken();
+
+ // put first character (escaped char) in mContent
+ char c = t.charAt(0);
+
+ Character ch = Character.valueOf(c);
+
+ mContent.add(ch.toString());
+
+ // process the rest for $tokens
+ String r = t.substring(1);
+
+ dollarProcess(r);
+ }
+
+ return formContent(mContent);
+ }
+
+ private void dollarProcess(String sub) {
+ StringTokenizer st = new StringTokenizer(sub, TOK_PREFIX);
+
+ // if first token is not a $token, put in mContent as is
+ if (st.hasMoreTokens() && !sub.startsWith(TOK_PREFIX)) {
+ String a = st.nextToken();
+
+ mContent.add(a);
+ }
+
+ /*
+ * all of the string tokens below begin with a '$'
+ * match it one by one with the mTok2vals table
+ */
+ while (st.hasMoreTokens()) {
+ String t = st.nextToken();
+
+ /*
+ * We don't know when a token ends. Compare with every
+ * token in the table for the first match. Which means, a
+ * token name should not be a substring of any token name
+ */
+ boolean matched = false;
+ String tok = null;
+
+ for (Enumeration e = mTok2vals.keys(); e.hasMoreElements();) {
+ // get key
+ tok = (String) e.nextElement();
+
+ // compare key with $token
+ if (t.startsWith(tok)) {
+ // match, put val in mContent
+ Object o = mTok2vals.get(tok);
+
+ if (o != null) {
+ String s = (String) o;
+
+ if (!s.equals("")) {
+ mContent.add(o);
+ } else {
+ break;
+ }
+ } else { // no value, bail out
+ break;
+ }
+
+ // now, put the rest of the non-token string in mContent
+ if (t.length() != tok.length()) {
+ mContent.add(t.substring(tok.length()));
+ }
+
+ matched = true;
+
+ // replaced! bail out.
+ break;
+ }
+ }
+
+ if (!matched) {
+ boolean keyFound = false;
+
+ // no match, put the token back, as is
+ // -- for bug 382162, don't remove the following line, in
+ // case John changes his mind for the better
+ // mContent.add(TOK_PREFIX+t);
+ int tl = token_keys.length;
+
+ for (int i = 0; i < token_keys.length; i++) {
+ if (t.startsWith(token_keys[i])) {
+ // match, replace it with the TOK_VALUE_UNKNOWN
+ mContent.add(TOK_VALUE_UNKNOWN);
+
+ // now, put the rest of the non-token string
+ // in mContent
+ if (t.length() != token_keys[i].length()) {
+ mContent.add(t.substring(token_keys[i].length()));
+ }
+ keyFound = true;
+ break;
+ }
+ // keep looking
+ }
+ if (keyFound == false) {
+ mContent.add(TOK_TOKEN_UNKNOWN + TOK_PREFIX + t);
+ }
+ }
+ }
+ }
+
+ /**
+ * takes a vector of strings and concatenate them
+ */
+ public String formContent(Vector vec) {
+ String content = null;
+
+ Enumeration e = vec.elements();
+
+ // initialize content with first element
+ if (e.hasMoreElements()) {
+ content = (String) e.nextElement();
+ }
+
+ while (e.hasMoreElements()) {
+ String v = (String) e.nextElement();
+
+ content += v;
+ }
+
+ return content;
+ }
+
+ /**
+ * logs an entry in the log file.
+ */
+ public void log(int level, String msg) {
+ if (mLogger == null)
+ return;
+ mLogger.log(ILogger.EV_SYSTEM, null, ILogger.S_OTHER,
+ level, "EmailFormProcessor: " + msg);
+ }
+}
+
diff --git a/pki/base/common/src/com/netscape/cmscore/notification/EmailResolverKeys.java b/pki/base/common/src/com/netscape/cmscore/notification/EmailResolverKeys.java
new file mode 100644
index 000000000..5f053c297
--- /dev/null
+++ b/pki/base/common/src/com/netscape/cmscore/notification/EmailResolverKeys.java
@@ -0,0 +1,86 @@
+// --- BEGIN COPYRIGHT BLOCK ---
+// This program is free software; you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation; version 2 of the License.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License along
+// with this program; if not, write to the Free Software Foundation, Inc.,
+// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+//
+// (C) 2007 Red Hat, Inc.
+// All rights reserved.
+// --- END COPYRIGHT BLOCK ---
+package com.netscape.cmscore.notification;
+
+
+import com.netscape.certsrv.base.*;
+import com.netscape.certsrv.notification.*;
+import java.util.*;
+
+
+/**
+ * Email resolver keys as input to email resolvers
+ * <P>
+ *
+ * @author cfu
+ * @version $Revision$, $Date$
+ */
+public class EmailResolverKeys implements IEmailResolverKeys {
+ private Hashtable mKeys = null;
+
+ public EmailResolverKeys() {
+ mKeys = new Hashtable();
+ }
+
+ /**
+ * sets a key with key name and the key
+ * @param name key name
+ * @param key key
+ * @exception com.netscape.certsrv.base.EBaseException NullPointerException
+ */
+ public void set(String name, Object key)throws EBaseException {
+ try {
+ mKeys.put(name, key);
+ } catch (NullPointerException e) {
+ System.out.println(e.toString());
+ throw new EBaseException("EmailResolverKeys.set()");
+ }
+ }
+
+ /**
+ * returns the key to which the specified name is mapped in this
+ * key set
+ * @param name key name
+ * @return the named email resolver key
+ */
+ public Object get(String name) {
+ return ((Object) mKeys.get(name));
+ }
+
+ /**
+ * removes the name and its corresponding key from this
+ * key set. This method does nothing if the named
+ * key is not in the key set.
+ * @param name key name
+ */
+ public void delete(String name) {
+ mKeys.remove(name);
+ }
+
+ /**
+ * returns an enumeration of the keys in this key
+ * set. Use the Enumeration methods on the returned object to
+ * fetch the elements sequentially.
+ * @return an enumeration of the values in this key set
+ * @see java.util.Enumeration
+ */
+ public Enumeration getElements() {
+ return (mKeys.elements());
+ }
+}
+
diff --git a/pki/base/common/src/com/netscape/cmscore/notification/EmailTemplate.java b/pki/base/common/src/com/netscape/cmscore/notification/EmailTemplate.java
new file mode 100644
index 000000000..483426414
--- /dev/null
+++ b/pki/base/common/src/com/netscape/cmscore/notification/EmailTemplate.java
@@ -0,0 +1,185 @@
+// --- BEGIN COPYRIGHT BLOCK ---
+// This program is free software; you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation; version 2 of the License.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License along
+// with this program; if not, write to the Free Software Foundation, Inc.,
+// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+//
+// (C) 2007 Red Hat, Inc.
+// All rights reserved.
+// --- END COPYRIGHT BLOCK ---
+package com.netscape.cmscore.notification;
+
+
+import java.lang.*;
+import java.io.*;
+import java.util.*;
+import javax.servlet.*;
+import javax.servlet.http.*;
+
+import com.netscape.certsrv.logging.*;
+import com.netscape.certsrv.notification.*;
+import com.netscape.certsrv.apps.*;
+import com.netscape.cmscore.util.*;
+
+
+/**
+ * Files to be processed and returned to the requested parties. It
+ * is a template with $tokens to be used by the form/template processor.
+ *
+ *
+ * @author cfu
+ * @version $Revision$, $Date$
+ */
+
+public class EmailTemplate implements IEmailTemplate {
+
+ /*==========================================================
+ * variables
+ *==========================================================*/
+
+ /* private variables */
+ private String mTemplateFile = new String();
+ private ILogger mLogger = CMS.getLogger();
+
+ /* public vaiables */
+ public String mFileContents;
+
+ /*==========================================================
+ * constructors
+ *==========================================================*/
+
+ /**
+ * Default Constructor
+ *
+ * @param templateFile File name of the template including the full path and
+ * file extension
+ */
+ public EmailTemplate(String templatePath) {
+ mTemplateFile = templatePath;
+ }
+
+ /*==========================================================
+ * public methods
+ *==========================================================*/
+
+ /*
+ * Load the template from the file
+ *
+ * @return true if successful
+ */
+ public boolean init() {
+
+ File template = new File(mTemplateFile);
+
+ /* check if file exists and is accessible */
+ if ((!template.exists()) || (!template.canRead()) || (template.isDirectory())) {
+ String error = "Template: " + mTemplateFile + " does not exist or invalid";
+
+ log(ILogger.LL_FAILURE, CMS.getLogMessage("CMSCORE_NOTIFY_TEMPLATE_NOT_EXIST"));
+ return false;
+ }
+
+ /* create input stream */
+ FileReader input;
+
+ try {
+ input = new FileReader(template);
+ } catch (FileNotFoundException e) {
+ String error = "Template: " + mTemplateFile + " not found";
+
+ log(ILogger.LL_FAILURE, CMS.getLogMessage("CMSCORE_NOTIFY_TEMPLATE_NOT_FOUND"));
+
+ return false;
+ }
+
+ /* load template */
+ mFileContents = loadFile(input);
+ if (mFileContents == null) {
+ String error = "Template: Error loading file into string";
+
+ log(ILogger.LL_FAILURE, CMS.getLogMessage("CMSCORE_NOTIFY_TEMPLATE_LOAD_ERROR"));
+ return false;
+ }
+
+ // close the stream
+ try {
+ input.close();
+ } catch (IOException e) {
+ return false;
+ }
+ return true;
+ }
+
+ /**
+ * @return Template Name in string form
+ */
+ public String getTemplateName() {
+ return mTemplateFile;
+ }
+
+ /**
+ * @return true if template is an html file, false otherwise
+ */
+ public boolean isHTML() {
+ if (mTemplateFile.endsWith(".html") ||
+ mTemplateFile.endsWith(".HTML") ||
+ mTemplateFile.endsWith(".htm") ||
+ mTemplateFile.endsWith(".HTM"))
+ return true;
+ else
+ return false;
+ }
+
+ /**
+ * @return Content of the template
+ */
+ public String toString() {
+ return mFileContents;
+ }
+
+ /*==========================================================
+ * private methods
+ *==========================================================*/
+
+ /* load file into string */
+ private String loadFile(FileReader input) {
+
+ BufferedReader in = new BufferedReader(input);
+ StringBuffer buf = new StringBuffer();
+ String line;
+
+ try {
+ while ((line = in.readLine()) != null) {
+ buf.append(line);
+ buf.append("\n");
+ }
+ } catch (IOException e) {
+ String error = "Template: Error loading file";
+
+ log(ILogger.LL_FAILURE, CMS.getLogMessage("CMSCORE_NOTIFY_TEMPLATE_LOADING"));
+ return null;
+ }
+
+ return buf.toString();
+ }
+
+ public int length() {
+ return (mFileContents == null) ? 0 : mFileContents.length();
+ }
+
+ private void log(int level, String msg) {
+ if (mLogger == null)
+ return;
+ mLogger.log(ILogger.EV_SYSTEM, null, ILogger.S_OTHER,
+ level, msg);
+ }
+
+}
diff --git a/pki/base/common/src/com/netscape/cmscore/notification/ReqCertEmailResolver.java b/pki/base/common/src/com/netscape/cmscore/notification/ReqCertEmailResolver.java
new file mode 100644
index 000000000..28f0e364c
--- /dev/null
+++ b/pki/base/common/src/com/netscape/cmscore/notification/ReqCertEmailResolver.java
@@ -0,0 +1,151 @@
+// --- BEGIN COPYRIGHT BLOCK ---
+// This program is free software; you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation; version 2 of the License.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License along
+// with this program; if not, write to the Free Software Foundation, Inc.,
+// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+//
+// (C) 2007 Red Hat, Inc.
+// All rights reserved.
+// --- END COPYRIGHT BLOCK ---
+package com.netscape.cmscore.notification;
+
+
+import com.netscape.certsrv.base.*;
+import com.netscape.certsrv.logging.*;
+import com.netscape.certsrv.request.*;
+import com.netscape.certsrv.notification.*;
+import com.netscape.certsrv.apps.*;
+import java.security.*;
+import java.security.cert.*;
+import java.io.IOException;
+import netscape.security.x509.*;
+import com.netscape.certsrv.common.*;
+
+
+/**
+ * An email resolver that first checks the request email, if none,
+ * then follows by checking the subjectDN of the certificate
+ * <p>
+ * @author cfu
+ * @version $Revision$, $Date$
+ */
+public class ReqCertEmailResolver implements IEmailResolver {
+ private ILogger mLogger = CMS.getLogger();
+
+ public static final String KEY_REQUEST = "request";
+ public static final String KEY_CERT = "cert";
+ // required keys for this resolver to figure out the email address
+ // protected static String[] mRequiredKeys = {KEY_REQUEST, KEY_CERT};
+
+ public ReqCertEmailResolver() {
+ }
+
+ /**
+ * returns an email address by using the resolver keys. The
+ * return value can possibly be null
+ * @param keys list of keys used for resolving the email address
+ */
+ public String getEmail(IEmailResolverKeys keys)
+ throws EBaseException, ENotificationException {
+ IRequest req = (IRequest) keys.get(KEY_REQUEST);
+
+ String mEmail = null;
+
+ if (req != null) {
+ mEmail = req.getExtDataInString(IRequest.HTTP_PARAMS,
+ "csrRequestorEmail");
+ if (mEmail == null) {
+ String mail = req.getExtDataInString("requestor_email");
+ log(ILogger.LL_INFO, "REQUESTOR_EMAIL = " + mail);
+ if (mail != null && !mail.equals(""))
+ return mail;
+ } else {
+ if (!mEmail.equals(""))
+ return mEmail;
+ }
+ } else {
+ log(ILogger.LL_INFO, "request null in keys");
+ }
+
+ X509Certificate cert = (X509Certificate) keys.get(KEY_CERT);
+
+ X500Name subjectDN = null;
+
+ if (cert != null) {
+ subjectDN =
+ (X500Name) cert.getSubjectDN();
+
+ try {
+ mEmail = subjectDN.getEmail();
+ } catch (IOException e) {
+ System.out.println("X500Name getEmail failed");
+ throw new ENotificationException (
+ CMS.getUserMessage("CMS_NOTIFICATION_EMAIL_RESOLVE_FAILED",
+ subjectDN.toString()));
+ }
+ } else {
+ log(ILogger.LL_INFO, "cert null in keys");
+ }
+
+ // log it
+ if (mEmail == null) {
+ if (cert != null) {
+ log(ILogger.LL_FAILURE,
+ CMS.getLogMessage("CMSCORE_NOTIFY_NO_EMAIL", subjectDN.toString()));
+ CMS.debug(
+ "no email resolved, throwing NotificationResources.EMAIL_RESOLVE_FAILED_1 for " +
+ subjectDN.toString());
+ throw new ENotificationException (
+ CMS.getUserMessage("CMS_NOTIFICATION_EMAIL_RESOLVE_FAILED",
+ "subjectDN= " + subjectDN.toString()));
+ } else if (req != null) {
+ log(ILogger.LL_FAILURE,
+ "no email resolved for request id =" +
+ req.getRequestId().toString());
+ CMS.debug(
+ "no email resolved, throwing NotificationResources.EMAIL_RESOLVE_FAILED_1 for request id =" +
+ req.getRequestId().toString());
+ throw new ENotificationException (
+ CMS.getUserMessage("CMS_NOTIFICATION_EMAIL_RESOLVE_FAILED",
+ "requestId= " + req.getRequestId().toString()));
+ } else {
+ log(ILogger.LL_FAILURE,
+ CMS.getLogMessage("CMSCORE_NOTIFY_NO_EMAIL_REQUEST"));
+ CMS.debug(
+ "no email resolved, throwing NotificationResources.EMAIL_RESOLVE_FAILED_1. No request id or cert info found");
+ throw new ENotificationException (
+ CMS.getUserMessage("CMS_NOTIFICATION_EMAIL_RESOLVE_FAILED",
+ ": No request id or cert info found"));
+ }
+ } else {
+ log(ILogger.LL_INFO, "email resolved: " + mEmail);
+ }
+
+ return mEmail;
+ }
+
+ /**
+ * Returns array of required keys for this email resolver
+ * @return Array of required keys.
+ */
+
+ /* public String[] getRequiredKeys() {
+ return mRequiredKeys;
+ }*/
+
+ private void log(int level, String msg) {
+ if (mLogger == null)
+ return;
+ mLogger.log(ILogger.EV_SYSTEM, null, ILogger.S_OTHER,
+ level, "ReqCertEmailResolver: " + msg);
+ }
+
+}
diff --git a/pki/base/common/src/com/netscape/cmscore/notification/ReqCertSANameEmailResolver.java b/pki/base/common/src/com/netscape/cmscore/notification/ReqCertSANameEmailResolver.java
new file mode 100644
index 000000000..440e62e85
--- /dev/null
+++ b/pki/base/common/src/com/netscape/cmscore/notification/ReqCertSANameEmailResolver.java
@@ -0,0 +1,268 @@
+// --- BEGIN COPYRIGHT BLOCK ---
+// This program is free software; you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation; version 2 of the License.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License along
+// with this program; if not, write to the Free Software Foundation, Inc.,
+// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+//
+// (C) 2007 Red Hat, Inc.
+// All rights reserved.
+// --- END COPYRIGHT BLOCK ---
+package com.netscape.cmscore.notification;
+
+
+import com.netscape.certsrv.base.*;
+import com.netscape.certsrv.logging.*;
+import com.netscape.certsrv.request.*;
+import com.netscape.certsrv.apps.*;
+import com.netscape.certsrv.notification.*;
+import java.util.Enumeration;
+import java.security.*;
+import java.security.cert.*;
+import java.io.IOException;
+import netscape.security.x509.*;
+import com.netscape.certsrv.common.*;
+import com.netscape.certsrv.ca.*;
+import com.netscape.certsrv.dbs.certdb.ICertificateRepository;
+
+
+/**
+ * An email resolver that first checks the request email, if none,
+ * then follows by checking the subjectDN of the certificate, if none,
+ * then follows by checking the subjectalternatename extension
+ * <p>
+ * @author cfu
+ * @version $Revision$, $Date$
+ */
+public class ReqCertSANameEmailResolver implements IEmailResolver {
+ private ILogger mLogger = CMS.getLogger();
+
+ public static final String KEY_REQUEST = IEmailResolverKeys.KEY_REQUEST;
+ public static final String KEY_CERT = IEmailResolverKeys.KEY_CERT;
+
+ // required keys for this resolver to figure out the email address
+ // protected static String[] mRequiredKeys = {KEY_REQUEST, KEY_CERT};
+
+ public ReqCertSANameEmailResolver() {
+ }
+
+ /**
+ * returns an email address by using the resolver keys. The
+ * return value can possibly be null
+ * @param keys list of keys used for resolving the email address
+ */
+ public String getEmail(IEmailResolverKeys keys)
+ throws EBaseException, ENotificationException {
+ IRequest req = (IRequest) keys.get(KEY_REQUEST);
+
+ String mEmail = null;
+
+ if (req != null) {
+ mEmail = req.getExtDataInString(IRequest.HTTP_PARAMS,
+ IRequest.REQUESTOR_EMAIL);
+ if (mEmail == null) {
+ String mail = req.getExtDataInString("requestor_email");
+ log(ILogger.LL_INFO, "REQUESTOR_EMAIL = " + mail);
+ if (mail != null && !mail.equals(""))
+ return mail;
+ } else {
+ if (!mEmail.equals("")) {
+ log(ILogger.LL_INFO, "REQUESTOR_EMAIL = " + mEmail);
+ return mEmail;
+ }
+ log(ILogger.LL_INFO, "REQUESTOR_EMAIL is null ");
+ }
+ } else {
+ log(ILogger.LL_INFO, "request null in keys");
+ }
+ Object request = keys.get(KEY_CERT);
+ X509Certificate cert = null;
+
+ if (request instanceof RevokedCertImpl) {
+ RevokedCertImpl revCert = (RevokedCertImpl) request;
+ ICertificateAuthority ca = (ICertificateAuthority) CMS.getSubsystem(CMS.SUBSYSTEM_CA);
+ ICertificateRepository certDB = ca.getCertificateRepository();
+
+ cert = certDB.getX509Certificate(revCert.getSerialNumber());
+ }else
+ cert = (X509Certificate) request;
+
+ X500Name subjectDN = null;
+
+ if (cert != null) {
+ subjectDN =
+ (X500Name) cert.getSubjectDN();
+
+ try {
+ mEmail = subjectDN.getEmail();
+ if (mEmail != null) {
+ if (!mEmail.equals("")) {
+ log(ILogger.LL_INFO, "cert subjectDN E=" +
+ mEmail);
+ }
+ } else {
+ log(ILogger.LL_INFO, "no E component in subjectDN ");
+ }
+ } catch (IOException e) {
+ System.out.println("X500Name getEmail failed");
+ throw new ENotificationException (
+ CMS.getUserMessage("CMS_NOTIFICATION_EMAIL_RESOLVE_FAILED",
+ subjectDN.toString()));
+ }
+
+ // try subjectalternatename
+ if (mEmail == null) {
+ X509CertInfo certInfo = null;
+
+ CMS.debug("about to try subjectalternatename");
+ try {
+ certInfo = (X509CertInfo)
+ ((X509CertImpl) cert).get(
+ X509CertImpl.NAME + "." + X509CertImpl.INFO);
+ } catch (CertificateParsingException ex) {
+ log(ILogger.LL_FAILURE,
+ CMS.getLogMessage("CMSCORE_NOTIFY_NO_CERTINFO"));
+ throw new ENotificationException (
+ CMS.getUserMessage("CMS_NOTIFICATION_EMAIL_RESOLVE_FAILED",
+ "subjectDN= " + subjectDN.toString()));
+ }
+
+ CertificateExtensions exts;
+
+ try {
+ exts = (CertificateExtensions)
+ certInfo.get(CertificateExtensions.NAME);
+ } catch (IOException e) {
+ log(ILogger.LL_FAILURE,
+ CMS.getLogMessage("CMSCORE_NOTIFY_GET_EXT", e.toString()));
+ throw new ENotificationException (
+ CMS.getUserMessage("CMS_NOTIFICATION_EMAIL_RESOLVE_FAILED",
+ "subjectDN= " + subjectDN.toString()));
+
+ } catch (CertificateException e) {
+ log(ILogger.LL_FAILURE,
+ CMS.getLogMessage("CMSCORE_NOTIFY_GET_EXT", e.toString()));
+ throw new ENotificationException (
+ CMS.getUserMessage("CMS_NOTIFICATION_EMAIL_RESOLVE_FAILED",
+ "subjectDN= " + subjectDN.toString()));
+ }
+
+ if (exts != null) {
+ SubjectAlternativeNameExtension ext;
+
+ try {
+ ext =
+ (SubjectAlternativeNameExtension)
+ exts.get(SubjectAlternativeNameExtension.NAME);
+ } catch (IOException e) {
+ log(ILogger.LL_FAILURE,
+ CMS.getLogMessage("CMSCORE_NOTIFY_GET_EXT", e.toString()));
+ throw new ENotificationException (
+ CMS.getUserMessage("CMS_NOTIFICATION_EMAIL_RESOLVE_FAILED",
+ "subjectDN= " + subjectDN.toString()));
+
+ }
+
+ try {
+ if (ext != null) {
+ GeneralNames gn =
+ (GeneralNames) ext.get(SubjectAlternativeNameExtension.SUBJECT_NAME);
+
+ Enumeration e = gn.elements();
+
+ while (e.hasMoreElements()) {
+ Object g = (Object) e.nextElement();
+
+ GeneralName gni =
+ (GeneralName) g;
+
+ if (gni.getType() ==
+ GeneralNameInterface.NAME_RFC822) {
+ CMS.debug("got an subjectalternatename email");
+
+ String nameString = g.toString();
+
+ // "RFC822Name: " + name
+ mEmail =
+ nameString.substring(nameString.indexOf(' ') + 1);
+ log(ILogger.LL_INFO,
+ "subjectalternatename email used:" +
+ mEmail);
+
+ break;
+ } else {
+ CMS.debug("not an subjectalternatename email");
+ }
+ }
+ }
+ } catch (IOException e) {
+ log(ILogger.LL_FAILURE,
+ CMS.getLogMessage("CMSCORE_NOTIFY_SUBJECTALTNAME"));
+ }
+ }
+ }
+ } else {
+ log(ILogger.LL_INFO, "cert null in keys");
+ }
+
+ // log it
+ if (mEmail == null) {
+ if (cert != null) {
+ log(ILogger.LL_FAILURE,
+ CMS.getLogMessage("CMSCORE_NOTIFY_NO_EMAIL", subjectDN.toString()));
+ CMS.debug(
+ "no email resolved, throwing NotificationResources.EMAIL_RESOLVE_FAILED_1 for " +
+ subjectDN.toString());
+ throw new ENotificationException (
+ CMS.getUserMessage("CMS_NOTIFICATION_EMAIL_RESOLVE_FAILED",
+ "subjectDN= " + subjectDN.toString()));
+ } else if (req != null) {
+ log(ILogger.LL_FAILURE,
+ CMS.getLogMessage("CMSCORE_NOTIFY_NO_EMAIL_ID",
+ req.getRequestId().toString()));
+ CMS.debug(
+ "no email resolved, throwing NotificationResources.EMAIL_RESOLVE_FAILED_1 for request id =" +
+ req.getRequestId().toString());
+ throw new ENotificationException (
+ CMS.getUserMessage("CMS_NOTIFICATION_EMAIL_RESOLVE_FAILED",
+ "requestId= " + req.getRequestId().toString()));
+ } else {
+ log(ILogger.LL_FAILURE,
+ CMS.getLogMessage("CMSCORE_NOTIFY_NO_EMAIL_REQUEST"));
+ CMS.debug(
+ "no email resolved, throwing NotificationResources.EMAIL_RESOLVE_FAILED_1. No request id or cert info found");
+ throw new ENotificationException (
+ CMS.getUserMessage("CMS_NOTIFICATION_EMAIL_RESOLVE_FAILED",
+ ": No request id or cert info found"));
+ }
+ } else {
+ log(ILogger.LL_INFO, "email resolved: " + mEmail);
+ }
+
+ return mEmail;
+ }
+
+ /**
+ * Returns array of required keys for this email resolver
+ * @return Array of required keys.
+ */
+
+ /* public String[] getRequiredKeys() {
+ return mRequiredKeys;
+ }*/
+
+ private void log(int level, String msg) {
+ if (mLogger == null)
+ return;
+ mLogger.log(ILogger.EV_SYSTEM, null, ILogger.S_OTHER,
+ level, "ReqCertSANameEmailResolver: " + msg);
+ }
+
+}