001: /*************************************************************************
002: * *
003: * EJBCA: The OpenSource Certificate Authority *
004: * *
005: * This software is free software; you can redistribute it and/or *
006: * modify it under the terms of the GNU Lesser General Public *
007: * License as published by the Free Software Foundation; either *
008: * version 2.1 of the License, or any later version. *
009: * *
010: * See terms of license at gnu.org. *
011: * *
012: *************************************************************************/package org.ejbca.core.model.log;
013:
014: import java.io.Serializable;
015: import java.io.UnsupportedEncodingException;
016: import java.security.MessageDigest;
017: import java.security.NoSuchAlgorithmException;
018: import java.security.NoSuchProviderException;
019: import java.util.Date;
020:
021: import org.bouncycastle.util.encoders.Hex;
022: import org.ejbca.core.model.protect.Protectable;
023: import org.ejbca.core.model.protect.TableVerifyResult;
024:
025: /**
026: * This is a class containing information about one log event in the database. Used mainly during database queries by the web interface.
027: *
028: * @author TomSelleck
029: * @version $Id: LogEntry.java,v 1.13 2007/12/04 14:21:53 jeklund Exp $
030: */
031: public class LogEntry implements Serializable, Protectable {
032:
033: /**
034: * Determines if a de-serialized file is compatible with this class.
035: *
036: * Maintainers must change this value if and only if the new version
037: * of this class is not compatible with old versions. See Sun docs
038: * for <a href=http://java.sun.com/products/jdk/1.1/docs/guide
039: * /serialization/spec/version.doc.html> details. </a>
040: *
041: */
042: private static final long serialVersionUID = -1L;
043:
044: /**
045: * Function used by EJBCA to log information.
046: *
047: * @param admintype is pricipally the type of data stored in the admindata field, should be one of org.ejbca.core.model.log.Admin.TYPE_ constants.
048: * @param admindata is the data identifying the administrator, should be certificate snr or ip-address when no certificate could be retrieved.
049: * @param module indicates from which module the event was logged. i.e one of the constans LogConstants.MODULE_RA, LogConstants.MODULE_CA ....
050: * @param time the time the event occured.
051: * @param username the name of the user involved or null if no user is involved.
052: * @param certificate the certificate involved in the event or null if no certificate is involved.
053: * @param event id of the event, should be one of the org.ejbca.core.model.log.LogConstants.EVENT_ constants.
054: * @param comment comment of the event.
055: */
056:
057: public LogEntry(int id, int admintype, String admindata, int caid,
058: int module, Date time, String username,
059: String certificatesnr, int event, String comment) {
060: this .id = id;
061: this .admintype = admintype;
062: this .admindata = admindata;
063: this .caid = caid;
064: this .module = module;
065: this .time = time;
066: this .username = username;
067: this .certificatesnr = certificatesnr;
068: this .event = event;
069: this .comment = comment;
070: }
071:
072: // Public methods
073:
074: /**
075: * Method used to map between event id and a string representation of event
076: *
077: * @return a string representation of the event.
078: */
079: public String getEventName() {
080: if (this .event < LogConstants.EVENT_ERROR_BOUNDRARY) {
081: return LogConstants.EVENTNAMES_INFO[this .event];
082: }
083: if (this .event < LogConstants.EVENT_SYSTEM_BOUNDRARY) {
084: return LogConstants.EVENTNAMES_ERROR[this .event
085: - LogConstants.EVENT_ERROR_BOUNDRARY];
086: }
087: return LogConstants.EVENTNAMES_SYSTEM[this .event
088: - LogConstants.EVENT_SYSTEM_BOUNDRARY];
089: }
090:
091: /**
092: * Method used to map between event id and a string representation of event
093: *
094: * @return a string representation of the event.
095: */
096: static public String getEventName(int eventId) {
097: if (eventId < LogConstants.EVENT_ERROR_BOUNDRARY) {
098: return LogConstants.EVENTNAMES_INFO[eventId];
099: }
100: if (eventId < LogConstants.EVENT_SYSTEM_BOUNDRARY) {
101: return LogConstants.EVENTNAMES_ERROR[eventId
102: - LogConstants.EVENT_ERROR_BOUNDRARY];
103: }
104: return LogConstants.EVENTNAMES_SYSTEM[eventId
105: - LogConstants.EVENT_SYSTEM_BOUNDRARY];
106: }
107:
108: public int getId() {
109: return this .id;
110: }
111:
112: public int getAdminType() {
113: return this .admintype;
114: }
115:
116: public String getAdminData() {
117: return this .admindata;
118: }
119:
120: public int getCAId() {
121: return this .caid;
122: }
123:
124: public int getModule() {
125: return this .module;
126: }
127:
128: public Date getTime() {
129: return this .time;
130: }
131:
132: public String getUsername() {
133: return this .username;
134: }
135:
136: public String getCertificateSNR() {
137: return this .certificatesnr;
138: }
139:
140: public int getEvent() {
141: return this .event;
142: }
143:
144: public String getComment() {
145: return this .comment;
146: }
147:
148: public String getVerifyResult() {
149: return this .verifyResult;
150: }
151:
152: public void setVerifyResult(String result) {
153: this .verifyResult = result;
154: }
155:
156: //
157: // Protectable
158: //
159: public int getHashVersion() {
160: return 1;
161: }
162:
163: public String getDbKeyString() {
164: return Integer.toString(id);
165: }
166:
167: public String getEntryType() {
168: return "LOGENTRY";
169: }
170:
171: public String getHash() throws NoSuchAlgorithmException,
172: NoSuchProviderException, UnsupportedEncodingException {
173: StringBuffer buf = new StringBuffer();
174: buf.append(id).append(admintype).append(admindata).append(caid)
175: .append(module).append(time.getTime()).append(username)
176: .append(certificatesnr).append(event).append(comment);
177: MessageDigest digest = MessageDigest.getInstance("SHA-256",
178: "BC");
179: byte[] result = digest.digest(buf.toString().getBytes("UTF-8"));
180: return new String(Hex.encode(result));
181: }
182:
183: public String getHash(int version) throws NoSuchAlgorithmException,
184: NoSuchProviderException, UnsupportedEncodingException {
185: return getHash();
186: }
187:
188: // Private methods
189:
190: // Private fields
191: private int id;
192: private int admintype;
193: private String admindata;
194: private int caid;
195: private int module;
196: private Date time;
197: private String username;
198: private String certificatesnr;
199: private int event;
200: private String comment;
201: private String verifyResult = TableVerifyResult.VERIFY_DISABLED_MSG;
202:
203: }
|