Revision 6186
Added by ben leinfelder over 13 years ago
src/edu/ucsb/nceas/metacat/dataone/D1NodeService.java | ||
---|---|---|
24 | 24 |
package edu.ucsb.nceas.metacat.dataone; |
25 | 25 |
|
26 | 26 |
import java.io.InputStream; |
27 |
import java.util.Calendar; |
|
27 | 28 |
import java.util.Date; |
29 |
import java.util.Vector; |
|
28 | 30 |
|
31 |
import org.apache.commons.logging.LogFactory; |
|
32 |
import org.apache.log4j.Logger; |
|
29 | 33 |
import org.dataone.service.exceptions.InvalidRequest; |
30 | 34 |
import org.dataone.service.exceptions.InvalidToken; |
31 | 35 |
import org.dataone.service.exceptions.NotAuthorized; |
... | ... | |
37 | 41 |
import org.dataone.service.types.Event; |
38 | 42 |
import org.dataone.service.types.Identifier; |
39 | 43 |
import org.dataone.service.types.Log; |
44 |
import org.dataone.service.types.LogEntry; |
|
45 |
import org.dataone.service.types.NodeReference; |
|
40 | 46 |
import org.dataone.service.types.Permission; |
41 | 47 |
import org.dataone.service.types.Session; |
48 |
import org.dataone.service.types.Subject; |
|
42 | 49 |
import org.dataone.service.types.SystemMetadata; |
43 | 50 |
|
51 |
import edu.ucsb.nceas.metacat.EventLog; |
|
52 |
import edu.ucsb.nceas.metacat.IdentifierManager; |
|
53 |
|
|
44 | 54 |
public abstract class D1NodeService { |
55 |
|
|
56 |
private static Logger logMetacat = Logger.getLogger(CNCoreImpl.class); |
|
57 |
|
|
45 | 58 |
|
46 | 59 |
/* Methods common to CNCore and MNCore APIs */ |
47 | 60 |
|
... | ... | |
71 | 84 |
Event event, Integer start, Integer count) throws InvalidToken, ServiceFailure, |
72 | 85 |
NotAuthorized, InvalidRequest, NotImplemented { |
73 | 86 |
|
74 |
return null; |
|
87 |
|
|
88 |
Log log = new Log(); |
|
89 |
Vector<LogEntry> logs = new Vector<LogEntry>(); |
|
90 |
IdentifierManager im = IdentifierManager.getInstance(); |
|
91 |
EventLog el = EventLog.getInstance(); |
|
92 |
if (fromDate == null) { |
|
93 |
logMetacat.debug("setting fromdate from null"); |
|
94 |
fromDate = new Date(1); |
|
95 |
} |
|
96 |
if (toDate == null) { |
|
97 |
logMetacat.debug("setting todate from null"); |
|
98 |
toDate = new Date(); |
|
99 |
} |
|
100 |
|
|
101 |
logMetacat.debug("fromDate: " + fromDate); |
|
102 |
logMetacat.debug("toDate: " + toDate); |
|
103 |
|
|
104 |
String report = el.getReport(null, null, null, null, |
|
105 |
new java.sql.Timestamp(fromDate.getTime()), |
|
106 |
new java.sql.Timestamp(toDate.getTime()), false); |
|
107 |
|
|
108 |
logMetacat.debug("report: " + report); |
|
109 |
|
|
110 |
String logEntry = "<logEntry>"; |
|
111 |
String endLogEntry = "</logEntry>"; |
|
112 |
int startIndex = 0; |
|
113 |
int foundIndex = report.indexOf(logEntry, startIndex); |
|
114 |
while (foundIndex != -1) { |
|
115 |
// parse out each entry |
|
116 |
int endEntryIndex = report.indexOf(endLogEntry, foundIndex); |
|
117 |
String entry = report.substring(foundIndex, endEntryIndex); |
|
118 |
logMetacat.debug("entry: " + entry); |
|
119 |
startIndex = endEntryIndex + endLogEntry.length(); |
|
120 |
foundIndex = report.indexOf(logEntry, startIndex); |
|
121 |
|
|
122 |
String entryId = getLogEntryField("entryid", entry); |
|
123 |
String ipAddress = getLogEntryField("ipAddress", entry); |
|
124 |
String principal = getLogEntryField("principal", entry); |
|
125 |
String docid = getLogEntryField("docid", entry); |
|
126 |
String eventS = getLogEntryField("event", entry); |
|
127 |
String dateLogged = getLogEntryField("dateLogged", entry); |
|
128 |
|
|
129 |
LogEntry le = new LogEntry(); |
|
130 |
|
|
131 |
Event e = Event.convert(eventS); |
|
132 |
if (e == null) { // skip any events that are not Dataone Crud events |
|
133 |
continue; |
|
134 |
} |
|
135 |
le.setEvent(e); |
|
136 |
Identifier entryid = new Identifier(); |
|
137 |
entryid.setValue(entryId); |
|
138 |
le.setEntryId(entryid); |
|
139 |
Identifier identifier = new Identifier(); |
|
140 |
try { |
|
141 |
logMetacat.debug("converting docid '" + docid + "' to a guid."); |
|
142 |
if (docid == null || docid.trim().equals("") || docid.trim().equals("null")) { |
|
143 |
continue; |
|
144 |
} |
|
145 |
docid = docid.substring(0, docid.lastIndexOf(".")); |
|
146 |
identifier.setValue(im.getGUID(docid, im.getLatestRevForLocalId(docid))); |
|
147 |
} catch (Exception ex) { |
|
148 |
// try to get the guid, if that doesn't |
|
149 |
// work, just use the local id |
|
150 |
// throw new ServiceFailure("1030", |
|
151 |
// "Error getting guid for localId " + |
|
152 |
// docid + ": " + ex.getMessage());\ |
|
153 |
|
|
154 |
// skip it if the guid can't be found |
|
155 |
continue; |
|
156 |
} |
|
157 |
|
|
158 |
le.setIdentifier(identifier); |
|
159 |
le.setIpAddress(ipAddress); |
|
160 |
Calendar c = Calendar.getInstance(); |
|
161 |
String year = dateLogged.substring(0, 4); |
|
162 |
String month = dateLogged.substring(5, 7); |
|
163 |
String date = dateLogged.substring(8, 10); |
|
164 |
logMetacat.debug("year: " + year + " month: " + month + " day: " + date); |
|
165 |
c.set(new Integer(year).intValue(), new Integer(month).intValue(), |
|
166 |
new Integer(date).intValue()); |
|
167 |
Date logDate = c.getTime(); |
|
168 |
le.setDateLogged(logDate); |
|
169 |
NodeReference memberNode = new NodeReference(); |
|
170 |
memberNode.setValue(ipAddress); |
|
171 |
le.setMemberNode(memberNode); |
|
172 |
Subject princ = new Subject(); |
|
173 |
princ.setValue(principal); |
|
174 |
le.setSubject(princ); |
|
175 |
le.setUserAgent("metacat/RESTService"); |
|
176 |
|
|
177 |
if (event == null) { |
|
178 |
logs.add(le); |
|
179 |
} |
|
180 |
|
|
181 |
if (event != null |
|
182 |
&& e.toString().toLowerCase().trim().equals( |
|
183 |
event.toString().toLowerCase().trim())) { |
|
184 |
logs.add(le); |
|
185 |
} |
|
186 |
} |
|
187 |
|
|
188 |
log.setLogEntryList(logs); |
|
189 |
logMetacat.info("getLogRecords"); |
|
190 |
return log; |
|
75 | 191 |
} |
76 | 192 |
|
77 | 193 |
/* End methods common to CNCore and MNCore APIs */ |
... | ... | |
150 | 266 |
} |
151 | 267 |
|
152 | 268 |
/* End methods common to CNAuthorization and MNAuthorization APIs */ |
269 |
|
|
270 |
/** |
|
271 |
* parse a logEntry and get the relevant field from it |
|
272 |
* |
|
273 |
* @param fieldname |
|
274 |
* @param entry |
|
275 |
* @return |
|
276 |
*/ |
|
277 |
private String getLogEntryField(String fieldname, String entry) { |
|
278 |
String begin = "<" + fieldname + ">"; |
|
279 |
String end = "</" + fieldname + ">"; |
|
280 |
// logMetacat.debug("looking for " + begin + " and " + end + |
|
281 |
// " in entry " + entry); |
|
282 |
String s = entry.substring(entry.indexOf(begin) + begin.length(), entry |
|
283 |
.indexOf(end)); |
|
284 |
logMetacat.debug("entry " + fieldname + " : " + s); |
|
285 |
return s; |
|
286 |
} |
|
153 | 287 |
|
154 | 288 |
} |
Also available in: Unified diff
implement getLogRecords