From a4682ceae6774956461edd03b2485bbacea445f4 Mon Sep 17 00:00:00 2001 From: mharmsen Date: Tue, 4 Oct 2011 01:17:41 +0000 Subject: Bugzilla Bug #688225 - (dogtagIPAv2.1) TRACKER: of the Dogtag fixes for freeIPA 2.1 git-svn-id: svn+ssh://svn.fedorahosted.org/svn/pki/tags/IPA_v2_RHEL_6_2_20111003@2252 c9f7a03b-bd48-0410-a16d-cbbf54688b0b --- .../netscape/certsrv/logging/SignedAuditEvent.java | 334 +++++++++++++++++++++ 1 file changed, 334 insertions(+) create mode 100644 pki/base/common/src/com/netscape/certsrv/logging/SignedAuditEvent.java (limited to 'pki/base/common/src/com/netscape/certsrv/logging/SignedAuditEvent.java') diff --git a/pki/base/common/src/com/netscape/certsrv/logging/SignedAuditEvent.java b/pki/base/common/src/com/netscape/certsrv/logging/SignedAuditEvent.java new file mode 100644 index 000000000..ea97fe3d6 --- /dev/null +++ b/pki/base/common/src/com/netscape/certsrv/logging/SignedAuditEvent.java @@ -0,0 +1,334 @@ +// --- 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.certsrv.logging; + + +import java.io.*; +import java.util.*; +import java.text.*; +import com.netscape.certsrv.base.*; +import com.netscape.certsrv.logging.*; + + +/** + * The log event object that carries message detail of a log event + * that goes into the Signed Audit Event log. This log has the + * property of being digitally signed for security considerations. + * + * + * @version $Revision$, $Date$ + * @see java.text.MessageFormat + * @see com.netscape.certsrv.logging.LogResources + */ +public class SignedAuditEvent implements IBundleLogEvent { + + protected Object mParams[] = null; + + private String mEventType = null; + private String mMessage = null; + private int mLevel = -1; + private int mNTEventType = -1; + private int mSource = -1; + private boolean mMultiline = false; + private long mTimeStamp = System.currentTimeMillis(); + + private static final String INVALID_LOG_LEVEL="log level: {0} is invalid, should be 0-6"; + + /** + * The bundle name for this event. + * ....not anymore...keep for now and clean up later + */ + private String mBundleName = LogResources.class.getName(); + + /** + * Constructs a SignedAuditEvent message event. + *

+ * + * @param msgFormat The message string. + */ + public SignedAuditEvent(String msgFormat) { + mMessage = msgFormat; + mParams = null; + } + + /** + * Constructs a message with a parameter. For example, + *

+     * 		new SignedAuditEvent("failed to load {0}", fileName);
+     * 
+ *

+ * + * @param msgFormat Details in message string format. + * @param param Message string parameter. + */ + public SignedAuditEvent(String msgFormat, String param) { + this(msgFormat); + mParams = new String[1]; + mParams[0] = param; + } + + /** + * Constructs a message from an exception. It can be used to carry + * a signed audit exception that may contain information about + * the context. For example, + *

+     * 		try {
+     *  		...
+     * 		} catch (IOExeption e) {
+     * 		 	logHandler.log(new SignedAuditEvent("Encountered Signed Audit Error {0}", e);
+     *      }
+     * 
+ *

+ * + * @param msgFormat Exception details in message string format. + * @param exception System exception. + */ + public SignedAuditEvent(String msgFormat, Exception exception) { + this(msgFormat); + mParams = new Exception[1]; + mParams[0] = exception; + } + + /** + * Constructs a message from a base exception. This will use the msgFormat + * from the exception itself. + *

+     * 		try {
+     *  		...
+     * 		} catch (Exception e) {
+     * 		 	logHandler.log(new SignedAuditEvent(e));
+     *      }
+     * 
+ *

+ * + * @param e CMS exception. + */ + public SignedAuditEvent(Exception e) { + this(e.getMessage()); + if (e instanceof EBaseException) { + mParams = ((EBaseException) e).getParameters(); + } else { + mParams = new Exception[1]; + mParams[0] = e; + } + } + + /** + * Constructs a message event with a list of parameters + * that will be substituted into the message format. + *

+ * + * @param msgFormat Message string format. + * @param params List of message format parameters. + */ + public SignedAuditEvent(String msgFormat, Object params[]) { + this(msgFormat); + mParams = params; + } + + /** + * Returns the current message format string. + *

+ * + * @return Details message. + */ + public String getMessage() { + return mMessage; + } + + /** + * Returns a list of parameters. These parameters can be + * used to assist in formatting the message. + *

+ * + * @return List of message format parameters. + */ + public Object[] getParameters() { + return mParams; + } + + /** + * Returns localized message string. This method should + * only be called if a localized string is necessary. + *

+ * + * @return Details message. + */ + public String toContent() { + return toContent(Locale.getDefault()); + } + + /** + * Returns the string based on the given locale. + *

+ * + * @param locale Locale. + * @return Details message. + */ + public String toContent(Locale locale) { + return MessageFormatter.getLocalizedString(locale, getBundleName(), + getMessage(), + getParameters()); + } + + /** + * Sets the resource bundle name for this class instance. This should + * be overridden by subclasses who have their own resource bundles. + * @param bundle String with name of resource bundle. + */ + public void setBundleName(String bundle) { + mBundleName = bundle; + } + + /** + * Retrieves bundle name. + * @return String with name of resource bundle. + */ + protected String getBundleName() { + return mBundleName; + } + + /** + * Retrieves log source. + * This is an id of the subsystem responsible + * for creating the log event. + * @return Integer source id. + */ + public int getSource() { + return mSource; + } + + /** + * Sets log source. + * @param source Integer id of log source. + */ + public void setSource(int source) { + mSource = source; + } + + /** + * Retrieves log level. + * The log level of an event represents its relative importance + * or severity within CMS. + * @return Integer log level value. + */ + public int getLevel() { + return mLevel; + } + + + /** + * Retrieves NT specific log event type. + * @return Integer NTEventType value. + */ + public int getNTEventType() { + return mNTEventType; + } + + /** + * Sets log level, NT log event type. + * For certain log levels the NT log event type gets + * set as well. + * @param level Integer log level value. + */ + public void setLevel(int level) { + mLevel = level; + switch (level) { + case ILogger.LL_DEBUG: + case ILogger.LL_INFO: + mNTEventType = ILogger.NT_INFO; + break; + + case ILogger.LL_WARN: + mNTEventType = ILogger.NT_WARN; + break; + + case ILogger.LL_FAILURE: + case ILogger.LL_MISCONF: + case ILogger.LL_CATASTRPHE: + case ILogger.LL_SECURITY: + mNTEventType = ILogger.NT_ERROR; + break; + + default: + ConsoleError.send(new SignedAuditEvent(INVALID_LOG_LEVEL, + Integer.toString(level))); + break; + } + } + + /** + * Retrieves log multiline attribute. + * @return Boolean whether or not this event is multiline. + * A multiline message simply consists of more than one line. + */ + public boolean getMultiline() { + return mMultiline; + } + + /** + * Sets log multiline attribute. A multiline message consists of + * more than one line. + * @param multiline Boolean multiline value. + */ + public void setMultiline(boolean multiline) { + mMultiline = multiline; + } + + + /** + * Retrieves event time stamp. + * @return Long integer of the time the event was created. + */ + public long getTimeStamp() { + return mTimeStamp; + } + + + /** + * Retrieves log event type. Each type of event + * has an associated String type value. + * @return String containing the type of event. + */ + public String getEventType() { + return mEventType; + } + + + /** + * Sets log event type. Each type of event + * has an associated String type value. + * @param eventType String containing the type of event. + */ + public void setEventType(String eventType) { + mEventType = eventType; + } + + /** + * Return string representation of log message. + * @return String containing log message. + */ + public String toString() { + if (getBundleName() == null) { + MessageFormat detailMessage = new MessageFormat(mMessage); + + return detailMessage.format(mParams); + } else + return toContent(); + } +} -- cgit