1 |
5298
|
jones
|
/**
|
2 |
|
|
* '$RCSfile$'
|
3 |
|
|
* Copyright: 2000 Regents of the University of California and the
|
4 |
|
|
* National Center for Ecological Analysis and Synthesis
|
5 |
|
|
*
|
6 |
5299
|
jones
|
* '$Author: $'
|
7 |
5298
|
jones
|
* '$Date: 2009-06-13 15:28:13 +0300 $'
|
8 |
|
|
*
|
9 |
|
|
* This program is free software; you can redistribute it and/or modify
|
10 |
|
|
* it under the terms of the GNU General Public License as published by
|
11 |
|
|
* the Free Software Foundation; either version 2 of the License, or
|
12 |
|
|
* (at your option) any later version.
|
13 |
|
|
*
|
14 |
|
|
* This program is distributed in the hope that it will be useful,
|
15 |
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
16 |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
17 |
|
|
* GNU General Public License for more details.
|
18 |
|
|
*
|
19 |
|
|
* You should have received a copy of the GNU General Public License
|
20 |
|
|
* along with this program; if not, write to the Free Software
|
21 |
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
22 |
|
|
*/
|
23 |
|
|
package edu.ucsb.nceas.metacat.dataone;
|
24 |
|
|
|
25 |
5329
|
jones
|
import java.io.ByteArrayOutputStream;
|
26 |
5319
|
jones
|
import java.io.File;
|
27 |
|
|
import java.io.FileNotFoundException;
|
28 |
|
|
import java.io.FileOutputStream;
|
29 |
5298
|
jones
|
import java.io.IOException;
|
30 |
|
|
import java.io.InputStream;
|
31 |
|
|
import java.io.OutputStream;
|
32 |
5329
|
jones
|
import java.io.PrintWriter;
|
33 |
5298
|
jones
|
import java.sql.SQLException;
|
34 |
|
|
import java.util.Date;
|
35 |
|
|
import java.util.Enumeration;
|
36 |
|
|
import java.util.Hashtable;
|
37 |
|
|
import java.util.Timer;
|
38 |
|
|
|
39 |
|
|
import javax.servlet.ServletContext;
|
40 |
|
|
import javax.servlet.http.HttpServletRequest;
|
41 |
|
|
import javax.servlet.http.HttpServletResponse;
|
42 |
|
|
|
43 |
5319
|
jones
|
import org.apache.commons.io.IOUtils;
|
44 |
|
|
import org.apache.log4j.Logger;
|
45 |
5298
|
jones
|
import org.dataone.service.exceptions.IdentifierNotUnique;
|
46 |
|
|
import org.dataone.service.exceptions.InsufficientResources;
|
47 |
|
|
import org.dataone.service.exceptions.InvalidRequest;
|
48 |
|
|
import org.dataone.service.exceptions.InvalidSystemMetadata;
|
49 |
|
|
import org.dataone.service.exceptions.InvalidToken;
|
50 |
|
|
import org.dataone.service.exceptions.NotAuthorized;
|
51 |
|
|
import org.dataone.service.exceptions.NotFound;
|
52 |
|
|
import org.dataone.service.exceptions.NotImplemented;
|
53 |
|
|
import org.dataone.service.exceptions.ServiceFailure;
|
54 |
|
|
import org.dataone.service.exceptions.UnsupportedType;
|
55 |
|
|
import org.dataone.service.mn.MemberNodeCrud;
|
56 |
|
|
import org.dataone.service.types.AuthToken;
|
57 |
|
|
import org.dataone.service.types.Checksum;
|
58 |
|
|
import org.dataone.service.types.DescribeResponse;
|
59 |
5320
|
jones
|
import org.dataone.service.types.Identifier;
|
60 |
5298
|
jones
|
import org.dataone.service.types.LogRecordSet;
|
61 |
|
|
import org.dataone.service.types.SystemMetadata;
|
62 |
5329
|
jones
|
import org.jibx.runtime.BindingDirectory;
|
63 |
|
|
import org.jibx.runtime.IBindingFactory;
|
64 |
|
|
import org.jibx.runtime.IMarshallingContext;
|
65 |
5332
|
jones
|
import org.jibx.runtime.IUnmarshallingContext;
|
66 |
5329
|
jones
|
import org.jibx.runtime.JiBXException;
|
67 |
5298
|
jones
|
|
68 |
|
|
import com.gc.iotools.stream.is.InputStreamFromOutputStream;
|
69 |
|
|
|
70 |
5319
|
jones
|
import edu.ucsb.nceas.metacat.AccessionNumberException;
|
71 |
|
|
import edu.ucsb.nceas.metacat.DocumentImpl;
|
72 |
|
|
import edu.ucsb.nceas.metacat.EventLog;
|
73 |
5298
|
jones
|
import edu.ucsb.nceas.metacat.IdentifierManager;
|
74 |
|
|
import edu.ucsb.nceas.metacat.McdbDocNotFoundException;
|
75 |
|
|
import edu.ucsb.nceas.metacat.McdbException;
|
76 |
|
|
import edu.ucsb.nceas.metacat.MetacatHandler;
|
77 |
|
|
import edu.ucsb.nceas.metacat.client.InsufficientKarmaException;
|
78 |
5319
|
jones
|
import edu.ucsb.nceas.metacat.properties.PropertyService;
|
79 |
|
|
import edu.ucsb.nceas.metacat.replication.ForceReplicationHandler;
|
80 |
5298
|
jones
|
import edu.ucsb.nceas.metacat.service.SessionService;
|
81 |
5329
|
jones
|
import edu.ucsb.nceas.metacat.util.DocumentUtil;
|
82 |
5298
|
jones
|
import edu.ucsb.nceas.metacat.util.SessionData;
|
83 |
|
|
import edu.ucsb.nceas.utilities.ParseLSIDException;
|
84 |
|
|
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
|
85 |
|
|
|
86 |
|
|
/**
|
87 |
|
|
*
|
88 |
|
|
* Implements DataONE MemberNode CRUD API for Metacat.
|
89 |
|
|
*
|
90 |
|
|
* @author Matthew Jones
|
91 |
|
|
*/
|
92 |
|
|
public class CrudService implements MemberNodeCrud {
|
93 |
|
|
|
94 |
5337
|
berkley
|
/*private ServletContext servletContext;
|
95 |
5298
|
jones
|
private HttpServletRequest request;
|
96 |
5337
|
berkley
|
private HttpServletResponse response;*/
|
97 |
|
|
|
98 |
|
|
private static CrudService crudService = null;
|
99 |
5298
|
jones
|
|
100 |
|
|
private MetacatHandler handler;
|
101 |
|
|
private Hashtable<String, String[]> params;
|
102 |
5319
|
jones
|
Logger logMetacat = null;
|
103 |
5337
|
berkley
|
|
104 |
|
|
private String metacatUrl;
|
105 |
5298
|
jones
|
|
106 |
|
|
/**
|
107 |
5337
|
berkley
|
* singleton accessor
|
108 |
|
|
*/
|
109 |
|
|
public static CrudService getInstance()
|
110 |
|
|
{
|
111 |
|
|
if(crudService == null)
|
112 |
|
|
{
|
113 |
|
|
crudService = new CrudService();
|
114 |
|
|
}
|
115 |
|
|
|
116 |
|
|
return crudService;
|
117 |
|
|
}
|
118 |
|
|
|
119 |
|
|
/**
|
120 |
5298
|
jones
|
* Initializes new instance by setting servlet context,request and response.
|
121 |
|
|
* TODO: remove dependency on Servlet infrastructure
|
122 |
|
|
* TODO: Make this a real service, and make it a Singleton
|
123 |
|
|
*/
|
124 |
5337
|
berkley
|
public CrudService() {
|
125 |
5333
|
berkley
|
//change crud service into a singleton. dont pass servlet data structures here
|
126 |
5337
|
berkley
|
logMetacat = Logger.getLogger(CrudService.class);
|
127 |
|
|
try
|
128 |
|
|
{
|
129 |
|
|
String server = PropertyService.getProperty("server.name");
|
130 |
|
|
String port = PropertyService.getProperty("server.httpPort");
|
131 |
|
|
String context = PropertyService.getProperty("application.context");
|
132 |
|
|
metacatUrl = "http://" + server + ":" + port + "/" + context;
|
133 |
|
|
logMetacat.debug("Initializing CrudService with url " + metacatUrl);
|
134 |
|
|
}
|
135 |
|
|
catch(Exception e)
|
136 |
|
|
{
|
137 |
|
|
logMetacat.error("Could not find servlet url in CrudService: " + e.getMessage());
|
138 |
|
|
e.printStackTrace();
|
139 |
|
|
throw new RuntimeException("Error getting servlet url in CrudService: " + e.getMessage());
|
140 |
|
|
}
|
141 |
|
|
|
142 |
|
|
/*this.servletContext = servletContext;
|
143 |
5298
|
jones
|
this.request = request;
|
144 |
5337
|
berkley
|
this.response = response;*/
|
145 |
|
|
|
146 |
|
|
params = new Hashtable<String, String[]>();
|
147 |
5298
|
jones
|
|
148 |
5337
|
berkley
|
handler = new MetacatHandler(new Timer());
|
149 |
|
|
|
150 |
5298
|
jones
|
}
|
151 |
5337
|
berkley
|
|
152 |
5298
|
jones
|
/**
|
153 |
5337
|
berkley
|
* return the context url CrudService is using.
|
154 |
5298
|
jones
|
*/
|
155 |
5337
|
berkley
|
public String getContextUrl()
|
156 |
|
|
{
|
157 |
|
|
return metacatUrl;
|
158 |
|
|
}
|
159 |
|
|
|
160 |
|
|
/**
|
161 |
|
|
* set the params for this service from an HttpServletRequest param list
|
162 |
|
|
*/
|
163 |
|
|
public void setParamsFromRequest(HttpServletRequest request)
|
164 |
|
|
{
|
165 |
5298
|
jones
|
Enumeration paramlist = request.getParameterNames();
|
166 |
|
|
while (paramlist.hasMoreElements()) {
|
167 |
5337
|
berkley
|
String name = (String) paramlist.nextElement();
|
168 |
|
|
String[] value = (String[])request.getParameterValues(name);
|
169 |
5298
|
jones
|
params.put(name, value);
|
170 |
|
|
}
|
171 |
|
|
}
|
172 |
|
|
|
173 |
5337
|
berkley
|
/**
|
174 |
|
|
* set the parameter values needed for this request
|
175 |
|
|
*/
|
176 |
|
|
public void setParameter(String name, String[] value)
|
177 |
|
|
{
|
178 |
|
|
params.put(name, value);
|
179 |
|
|
}
|
180 |
|
|
|
181 |
5320
|
jones
|
public Identifier create(AuthToken token, Identifier guid,
|
182 |
5298
|
jones
|
InputStream object, SystemMetadata sysmeta) throws InvalidToken,
|
183 |
|
|
ServiceFailure, NotAuthorized, IdentifierNotUnique, UnsupportedType,
|
184 |
|
|
InsufficientResources, InvalidSystemMetadata, NotImplemented {
|
185 |
5319
|
jones
|
|
186 |
|
|
logMetacat.debug("Starting CrudService.create()...");
|
187 |
|
|
|
188 |
5329
|
jones
|
// authenticate & get user info
|
189 |
|
|
SessionData sessionData = getSessionData(token);
|
190 |
|
|
String username = sessionData.getUserName();
|
191 |
|
|
String[] groups = sessionData.getGroupNames();
|
192 |
5319
|
jones
|
|
193 |
|
|
// verify that guid == SystemMetadata.getIdentifier()
|
194 |
5320
|
jones
|
logMetacat.debug("Comparing guid|sysmeta_guid: " + guid.getValue() + "|" + sysmeta.getIdentifier().getValue());
|
195 |
5329
|
jones
|
if (!guid.getValue().equals(sysmeta.getIdentifier().getValue())) {
|
196 |
|
|
throw new InvalidSystemMetadata(1180,
|
197 |
|
|
"GUID in method call does not match GUID in system metadata.");
|
198 |
|
|
}
|
199 |
5319
|
jones
|
|
200 |
|
|
logMetacat.debug("Checking if identifier exists...");
|
201 |
|
|
// Check that the identifier does not already exist
|
202 |
|
|
IdentifierManager im = IdentifierManager.getInstance();
|
203 |
5320
|
jones
|
if (im.identifierExists(guid.getValue())) {
|
204 |
5319
|
jones
|
throw new IdentifierNotUnique(1120,
|
205 |
|
|
"GUID is already in use by an existing object.");
|
206 |
|
|
}
|
207 |
|
|
|
208 |
5329
|
jones
|
// Check if we are handling metadata or data
|
209 |
|
|
boolean isScienceMetadata = isScienceMetadata(sysmeta);
|
210 |
5319
|
jones
|
|
211 |
5329
|
jones
|
if (isScienceMetadata) {
|
212 |
5331
|
jones
|
// CASE METADATA:
|
213 |
|
|
try {
|
214 |
|
|
String xml = IOUtils.toString(object);
|
215 |
|
|
this.insertDocument(xml, guid, sessionData);
|
216 |
|
|
} catch (IOException e) {
|
217 |
|
|
String msg = "Could not create string from XML stream: " +
|
218 |
|
|
" " + e.getMessage();
|
219 |
|
|
logMetacat.debug(msg);
|
220 |
|
|
throw new ServiceFailure(1190, msg);
|
221 |
|
|
}
|
222 |
5319
|
jones
|
|
223 |
5329
|
jones
|
} else {
|
224 |
5331
|
jones
|
// DEFAULT CASE: DATA (needs to be checked and completed)
|
225 |
|
|
insertDataObject(object, guid, sessionData);
|
226 |
|
|
|
227 |
|
|
}
|
228 |
5319
|
jones
|
|
229 |
5331
|
jones
|
// For Metadata and Data, insert the system metadata into the object store too
|
230 |
|
|
insertSystemMetadata(sysmeta, sessionData);
|
231 |
5319
|
jones
|
|
232 |
|
|
logMetacat.debug("Returning from CrudService.create()");
|
233 |
|
|
return guid;
|
234 |
5298
|
jones
|
}
|
235 |
|
|
|
236 |
5320
|
jones
|
public Identifier delete(AuthToken token, Identifier guid)
|
237 |
5298
|
jones
|
throws InvalidToken, ServiceFailure, NotAuthorized, NotFound,
|
238 |
|
|
NotImplemented {
|
239 |
|
|
throw new NotImplemented(1000, "This method not yet implemented.");
|
240 |
|
|
}
|
241 |
|
|
|
242 |
5320
|
jones
|
public DescribeResponse describe(AuthToken token, Identifier guid)
|
243 |
5298
|
jones
|
throws InvalidToken, ServiceFailure, NotAuthorized, NotFound,
|
244 |
|
|
NotImplemented {
|
245 |
|
|
throw new NotImplemented(1000, "This method not yet implemented.");
|
246 |
|
|
}
|
247 |
|
|
|
248 |
5320
|
jones
|
public InputStream get(AuthToken token, Identifier guid)
|
249 |
5298
|
jones
|
throws InvalidToken, ServiceFailure, NotAuthorized, NotFound,
|
250 |
|
|
NotImplemented {
|
251 |
|
|
|
252 |
|
|
// Retrieve the session information from the AuthToken
|
253 |
|
|
// If the session is expired, then the user is 'public'
|
254 |
|
|
final SessionData sessionData = getSessionData(token);
|
255 |
|
|
|
256 |
|
|
// Look up the localId for this global identifier
|
257 |
|
|
IdentifierManager im = IdentifierManager.getInstance();
|
258 |
|
|
try {
|
259 |
5320
|
jones
|
final String localId = im.getLocalId(guid.getValue());
|
260 |
5298
|
jones
|
|
261 |
|
|
final InputStreamFromOutputStream<String> objectStream =
|
262 |
|
|
new InputStreamFromOutputStream<String>() {
|
263 |
|
|
|
264 |
|
|
@Override
|
265 |
|
|
public String produce(final OutputStream dataSink) throws Exception {
|
266 |
|
|
|
267 |
|
|
try {
|
268 |
5337
|
berkley
|
handler.readFromMetacat(/*request.getRemoteAddr()*/metacatUrl, null,
|
269 |
5298
|
jones
|
dataSink, localId, "xml",
|
270 |
|
|
sessionData.getUserName(),
|
271 |
|
|
sessionData.getGroupNames(), true, params);
|
272 |
|
|
} catch (PropertyNotFoundException e) {
|
273 |
|
|
throw new ServiceFailure(1030, e.getMessage());
|
274 |
|
|
} catch (ClassNotFoundException e) {
|
275 |
|
|
throw new ServiceFailure(1030, e.getMessage());
|
276 |
|
|
} catch (IOException e) {
|
277 |
|
|
throw new ServiceFailure(1030, e.getMessage());
|
278 |
|
|
} catch (SQLException e) {
|
279 |
|
|
throw new ServiceFailure(1030, e.getMessage());
|
280 |
|
|
} catch (McdbException e) {
|
281 |
|
|
throw new ServiceFailure(1030, e.getMessage());
|
282 |
|
|
} catch (ParseLSIDException e) {
|
283 |
|
|
throw new NotFound(1020, e.getMessage());
|
284 |
|
|
} catch (InsufficientKarmaException e) {
|
285 |
|
|
throw new NotAuthorized(1000, "Not authorized for get().");
|
286 |
|
|
}
|
287 |
|
|
|
288 |
|
|
return "Completed";
|
289 |
|
|
}
|
290 |
|
|
};
|
291 |
|
|
return objectStream;
|
292 |
|
|
|
293 |
|
|
} catch (McdbDocNotFoundException e) {
|
294 |
|
|
throw new NotFound(1020, e.getMessage());
|
295 |
|
|
}
|
296 |
|
|
}
|
297 |
|
|
|
298 |
5320
|
jones
|
public Checksum getChecksum(AuthToken token, Identifier guid)
|
299 |
5298
|
jones
|
throws InvalidToken, ServiceFailure, NotAuthorized, NotFound,
|
300 |
|
|
InvalidRequest, NotImplemented {
|
301 |
|
|
throw new NotImplemented(1000, "This method not yet implemented.");
|
302 |
|
|
}
|
303 |
|
|
|
304 |
5320
|
jones
|
public Checksum getChecksum(AuthToken token, Identifier guid,
|
305 |
5298
|
jones
|
String checksumAlgorithm) throws InvalidToken, ServiceFailure,
|
306 |
|
|
NotAuthorized, NotFound, InvalidRequest, NotImplemented {
|
307 |
|
|
throw new NotImplemented(1000, "This method not yet implemented.");
|
308 |
|
|
}
|
309 |
|
|
|
310 |
|
|
public LogRecordSet getLogRecords(AuthToken token, Date fromDate, Date toDate)
|
311 |
|
|
throws InvalidToken, ServiceFailure, NotAuthorized, InvalidRequest,
|
312 |
|
|
NotImplemented {
|
313 |
|
|
throw new NotImplemented(1000, "This method not yet implemented.");
|
314 |
|
|
}
|
315 |
|
|
|
316 |
5320
|
jones
|
public SystemMetadata getSystemMetadata(AuthToken token, Identifier guid)
|
317 |
5298
|
jones
|
throws InvalidToken, ServiceFailure, NotAuthorized, NotFound,
|
318 |
|
|
InvalidRequest, NotImplemented {
|
319 |
5319
|
jones
|
|
320 |
5332
|
jones
|
logMetacat.debug("CrudService.getSystemMetadata - for guid: " + guid.getValue());
|
321 |
|
|
|
322 |
|
|
// Retrieve the session information from the AuthToken
|
323 |
|
|
// If the session is expired, then the user is 'public'
|
324 |
|
|
final SessionData sessionData = getSessionData(token);
|
325 |
|
|
|
326 |
|
|
// TODO: Check access control rules
|
327 |
|
|
|
328 |
|
|
try {
|
329 |
|
|
IdentifierManager im = IdentifierManager.getInstance();
|
330 |
|
|
// This is a test id of an existing sysmeta document -- for temporary testing
|
331 |
|
|
String testGuid = "autogen.20101192441566.1";
|
332 |
|
|
// TODO: Look up ID of system metadata based on guid
|
333 |
5329
|
jones
|
// TODO: Initially from document, later from entry in table?
|
334 |
5332
|
jones
|
// String localId = im.getLocalId(guid.getValue());
|
335 |
|
|
final String localId = im.getLocalId(testGuid);
|
336 |
|
|
|
337 |
|
|
// Read system metadata from metacat's db
|
338 |
|
|
final InputStreamFromOutputStream<String> objectStream =
|
339 |
|
|
new InputStreamFromOutputStream<String>() {
|
340 |
|
|
|
341 |
|
|
@Override
|
342 |
|
|
public String produce(final OutputStream dataSink) throws Exception {
|
343 |
|
|
try {
|
344 |
5337
|
berkley
|
handler.readFromMetacat(/*request.getRemoteAddr()*/metacatUrl, null,
|
345 |
5332
|
jones
|
dataSink, localId, "xml",
|
346 |
|
|
sessionData.getUserName(),
|
347 |
|
|
sessionData.getGroupNames(), true, params);
|
348 |
|
|
} catch (PropertyNotFoundException e) {
|
349 |
|
|
throw new ServiceFailure(1030, e.getMessage());
|
350 |
|
|
} catch (ClassNotFoundException e) {
|
351 |
|
|
throw new ServiceFailure(1030, e.getMessage());
|
352 |
|
|
} catch (IOException e) {
|
353 |
|
|
throw new ServiceFailure(1030, e.getMessage());
|
354 |
|
|
} catch (SQLException e) {
|
355 |
|
|
throw new ServiceFailure(1030, e.getMessage());
|
356 |
|
|
} catch (McdbException e) {
|
357 |
|
|
throw new ServiceFailure(1030, e.getMessage());
|
358 |
|
|
} catch (ParseLSIDException e) {
|
359 |
|
|
throw new NotFound(1020, e.getMessage());
|
360 |
|
|
} catch (InsufficientKarmaException e) {
|
361 |
|
|
throw new NotAuthorized(1000, "Not authorized for get().");
|
362 |
|
|
}
|
363 |
|
|
|
364 |
|
|
return "Completed";
|
365 |
|
|
}
|
366 |
|
|
};
|
367 |
|
|
|
368 |
|
|
// Deserialize the xml to create a SystemMetadata object
|
369 |
|
|
SystemMetadata sysmeta = deserializeSystemMetadata(objectStream);
|
370 |
|
|
return sysmeta;
|
371 |
|
|
|
372 |
|
|
} catch (McdbDocNotFoundException e) {
|
373 |
|
|
throw new NotFound(1000, e.getMessage());
|
374 |
|
|
}
|
375 |
5298
|
jones
|
}
|
376 |
|
|
|
377 |
5320
|
jones
|
public Identifier update(AuthToken token, Identifier guid,
|
378 |
|
|
InputStream object, Identifier obsoletedGuid, SystemMetadata sysmeta)
|
379 |
5298
|
jones
|
throws InvalidToken, ServiceFailure, NotAuthorized, IdentifierNotUnique,
|
380 |
|
|
UnsupportedType, InsufficientResources, NotFound, InvalidSystemMetadata,
|
381 |
|
|
NotImplemented {
|
382 |
|
|
throw new NotImplemented(1000, "This method not yet implemented.");
|
383 |
|
|
}
|
384 |
|
|
|
385 |
5331
|
jones
|
/*
|
386 |
|
|
* Look up the information on the session using the token provided in
|
387 |
|
|
* the AuthToken. The Session should have all relevant user information.
|
388 |
|
|
* If the session has expired or is invalid, the 'public' session will
|
389 |
|
|
* be returned, giving the user anonymous access.
|
390 |
|
|
*/
|
391 |
|
|
private static SessionData getSessionData(AuthToken token) {
|
392 |
|
|
SessionData sessionData = null;
|
393 |
|
|
String sessionId = "PUBLIC";
|
394 |
|
|
if (token != null) {
|
395 |
|
|
sessionId = token.getToken();
|
396 |
|
|
}
|
397 |
|
|
|
398 |
|
|
// if the session id is registered in SessionService, get the
|
399 |
|
|
// SessionData for it. Otherwise, use the public session.
|
400 |
|
|
if (SessionService.isSessionRegistered(sessionId)) {
|
401 |
|
|
sessionData = SessionService.getRegisteredSession(sessionId);
|
402 |
|
|
} else {
|
403 |
|
|
sessionData = SessionService.getPublicSession();
|
404 |
|
|
}
|
405 |
|
|
|
406 |
|
|
return sessionData;
|
407 |
|
|
}
|
408 |
|
|
|
409 |
|
|
/**
|
410 |
|
|
* Determine if a given object should be treated as an XML science metadata
|
411 |
|
|
* object.
|
412 |
|
|
*
|
413 |
|
|
* TODO: This test should be externalized in a configuration dictionary rather than being hardcoded.
|
414 |
|
|
*
|
415 |
|
|
* @param sysmeta the SystemMetadata describig the object
|
416 |
|
|
* @return true if the object should be treated as science metadata
|
417 |
|
|
*/
|
418 |
|
|
private boolean isScienceMetadata(SystemMetadata sysmeta) {
|
419 |
|
|
boolean scimeta = false;
|
420 |
|
|
switch (sysmeta.getObjectFormat()) {
|
421 |
|
|
case EML_2_1_0: scimeta = true; break;
|
422 |
|
|
case EML_2_0_1: scimeta = true; break;
|
423 |
|
|
case EML_2_0_0: scimeta = true; break;
|
424 |
|
|
case FGDC_STD_001_1_1999: scimeta = true; break;
|
425 |
|
|
case FGDC_STD_001_1998: scimeta = true; break;
|
426 |
|
|
case NCML_2_2: scimeta = true; break;
|
427 |
|
|
}
|
428 |
|
|
|
429 |
|
|
return scimeta;
|
430 |
|
|
}
|
431 |
|
|
|
432 |
|
|
private void insertDataObject(InputStream object, Identifier guid,
|
433 |
|
|
SessionData sessionData) throws ServiceFailure {
|
434 |
|
|
|
435 |
|
|
String username = sessionData.getUserName();
|
436 |
|
|
String[] groups = sessionData.getGroupNames();
|
437 |
|
|
|
438 |
|
|
// generate guid/localId pair for object
|
439 |
|
|
logMetacat.debug("Generating a guid/localId mapping");
|
440 |
|
|
IdentifierManager im = IdentifierManager.getInstance();
|
441 |
|
|
String localId = im.generateLocalId(guid.getValue(), 1);
|
442 |
|
|
|
443 |
|
|
try {
|
444 |
|
|
logMetacat.debug("Case DATA: starting to write to disk.");
|
445 |
|
|
if (DocumentImpl.getDataFileLockGrant(localId)) {
|
446 |
|
|
|
447 |
|
|
// Save the data file to disk using "localId" as the name
|
448 |
|
|
try {
|
449 |
|
|
String datafilepath = PropertyService.getProperty("application.datafilepath");
|
450 |
|
|
|
451 |
|
|
File dataDirectory = new File(datafilepath);
|
452 |
|
|
dataDirectory.mkdirs();
|
453 |
|
|
|
454 |
|
|
File newFile = writeStreamToFile(dataDirectory, localId, object);
|
455 |
|
|
|
456 |
|
|
// TODO: Check that the file size matches SystemMetadata
|
457 |
|
|
// long size = newFile.length();
|
458 |
|
|
// if (size == 0) {
|
459 |
|
|
// throw new IOException("Uploaded file is 0 bytes!");
|
460 |
|
|
// }
|
461 |
|
|
|
462 |
|
|
// Register the file in the database (which generates an exception
|
463 |
|
|
// if the localId is not acceptable or other untoward things happen
|
464 |
|
|
try {
|
465 |
|
|
logMetacat.debug("Registering document...");
|
466 |
|
|
DocumentImpl.registerDocument(localId, "BIN", localId,
|
467 |
|
|
username, groups);
|
468 |
|
|
logMetacat.debug("Registration step completed.");
|
469 |
|
|
} catch (SQLException e) {
|
470 |
|
|
//newFile.delete();
|
471 |
|
|
logMetacat.debug("SQLE: " + e.getMessage());
|
472 |
|
|
e.printStackTrace(System.out);
|
473 |
|
|
throw new ServiceFailure(1190, "Registration failed: " + e.getMessage());
|
474 |
|
|
} catch (AccessionNumberException e) {
|
475 |
|
|
//newFile.delete();
|
476 |
|
|
logMetacat.debug("ANE: " + e.getMessage());
|
477 |
|
|
e.printStackTrace(System.out);
|
478 |
|
|
throw new ServiceFailure(1190, "Registration failed: " + e.getMessage());
|
479 |
|
|
} catch (Exception e) {
|
480 |
|
|
//newFile.delete();
|
481 |
|
|
logMetacat.debug("Exception: " + e.getMessage());
|
482 |
|
|
e.printStackTrace(System.out);
|
483 |
|
|
throw new ServiceFailure(1190, "Registration failed: " + e.getMessage());
|
484 |
|
|
}
|
485 |
|
|
|
486 |
|
|
logMetacat.debug("Logging the creation event.");
|
487 |
5337
|
berkley
|
EventLog.getInstance().log(/*request.getRemoteAddr()*/metacatUrl,
|
488 |
5331
|
jones
|
username, localId, "create");
|
489 |
|
|
|
490 |
|
|
// Schedule replication for this data file
|
491 |
|
|
logMetacat.debug("Scheduling replication.");
|
492 |
|
|
ForceReplicationHandler frh = new ForceReplicationHandler(
|
493 |
|
|
localId, "insert", false, null);
|
494 |
|
|
|
495 |
|
|
} catch (PropertyNotFoundException e) {
|
496 |
|
|
throw new ServiceFailure(1190, "Could not lock file for writing:" + e.getMessage());
|
497 |
|
|
}
|
498 |
|
|
|
499 |
|
|
}
|
500 |
|
|
} catch (Exception e) {
|
501 |
|
|
// Could not get a lock on the document, so we can not update the file now
|
502 |
|
|
throw new ServiceFailure(1190, "Failed to lock file: " + e.getMessage());
|
503 |
|
|
}
|
504 |
|
|
}
|
505 |
|
|
|
506 |
5319
|
jones
|
private File writeStreamToFile(File dir, String fileName, InputStream data)
|
507 |
|
|
throws ServiceFailure {
|
508 |
|
|
|
509 |
|
|
File newFile = new File(dir, fileName);
|
510 |
|
|
logMetacat.debug("Filename for write is: " + newFile.getAbsolutePath());
|
511 |
|
|
|
512 |
|
|
try {
|
513 |
|
|
if (newFile.createNewFile()) {
|
514 |
|
|
// write data stream to desired file
|
515 |
|
|
OutputStream os = new FileOutputStream(newFile);
|
516 |
|
|
long length = IOUtils.copyLarge(data, os);
|
517 |
|
|
os.flush();
|
518 |
|
|
os.close();
|
519 |
|
|
} else {
|
520 |
|
|
logMetacat.debug("File creation failed, or file already exists.");
|
521 |
|
|
throw new ServiceFailure(1190, "File already exists: " + fileName);
|
522 |
|
|
}
|
523 |
|
|
} catch (FileNotFoundException e) {
|
524 |
|
|
logMetacat.debug("FNF: " + e.getMessage());
|
525 |
|
|
throw new ServiceFailure(1190, "File not found: " + fileName + " "
|
526 |
|
|
+ e.getMessage());
|
527 |
|
|
} catch (IOException e) {
|
528 |
|
|
logMetacat.debug("IOE: " + e.getMessage());
|
529 |
|
|
throw new ServiceFailure(1190, "File was not written: " + fileName
|
530 |
|
|
+ " " + e.getMessage());
|
531 |
|
|
}
|
532 |
|
|
|
533 |
|
|
return newFile;
|
534 |
5329
|
jones
|
}
|
535 |
|
|
|
536 |
|
|
private void insertSystemMetadata(SystemMetadata sysmeta, SessionData sessionData)
|
537 |
|
|
throws ServiceFailure {
|
538 |
|
|
logMetacat.debug("Starting to insert SystemMetadata...");
|
539 |
|
|
|
540 |
|
|
// generate guid/localId pair for sysmeta
|
541 |
5331
|
jones
|
Identifier sysMetaGuid = new Identifier();
|
542 |
|
|
sysMetaGuid.setValue(DocumentUtil.generateDocumentId(1));
|
543 |
5329
|
jones
|
sysmeta.setDateSysMetadataModified(new Date());
|
544 |
|
|
|
545 |
|
|
String xml = new String(serializeSystemMetadata(sysmeta).toByteArray());
|
546 |
5333
|
berkley
|
String localId = insertDocument(xml, sysMetaGuid, sessionData);
|
547 |
|
|
//insert the system metadata doc id into the identifiers table to
|
548 |
|
|
//link it to the data or metadata document
|
549 |
|
|
IdentifierManager.getInstance().createSystemMetadataMapping(sysMetaGuid.getValue(), localId);
|
550 |
5329
|
jones
|
}
|
551 |
|
|
|
552 |
5333
|
berkley
|
/**
|
553 |
|
|
* insert a document, return the id of the document that was inserted
|
554 |
|
|
*/
|
555 |
|
|
private String insertDocument(String xml, Identifier guid, SessionData sessionData)
|
556 |
5329
|
jones
|
throws ServiceFailure {
|
557 |
|
|
logMetacat.debug("Starting to insert xml document...");
|
558 |
|
|
IdentifierManager im = IdentifierManager.getInstance();
|
559 |
|
|
|
560 |
|
|
// generate guid/localId pair for sysmeta
|
561 |
5331
|
jones
|
String localId = im.generateLocalId(guid.getValue(), 1);
|
562 |
|
|
logMetacat.debug("Metadata guid|localId: " + guid.getValue() + "|" +
|
563 |
5329
|
jones
|
localId);
|
564 |
|
|
|
565 |
|
|
String[] action = new String[1];
|
566 |
|
|
action[0] = "insert";
|
567 |
|
|
params.put("action", action);
|
568 |
|
|
String[] docid = new String[1];
|
569 |
|
|
docid[0] = localId;
|
570 |
|
|
params.put("docid", docid);
|
571 |
|
|
String[] doctext = new String[1];
|
572 |
|
|
doctext[0] = xml;
|
573 |
|
|
logMetacat.debug(doctext[0]);
|
574 |
|
|
params.put("doctext", doctext);
|
575 |
5331
|
jones
|
|
576 |
|
|
// TODO: refactor handleInsertOrUpdateAction() to not output XML directly
|
577 |
|
|
// onto output stream, or alternatively, capture that and parse it to
|
578 |
|
|
// generate the right exceptions
|
579 |
5337
|
berkley
|
//ByteArrayOutputStream output = new ByteArrayOutputStream();
|
580 |
|
|
//PrintWriter pw = new PrintWriter(output);
|
581 |
|
|
String result = handler.handleInsertOrUpdateAction(/*request.getRemoteAddr()*/metacatUrl, null,
|
582 |
|
|
null, params, sessionData.getUserName(), sessionData.getGroupNames());
|
583 |
|
|
//String outputS = new String(output.toByteArray());
|
584 |
|
|
logMetacat.debug("CrudService.insertDocument - Metacat returned: " + result);
|
585 |
5332
|
jones
|
// if (!(outputS.indexOf("<success>") > 0 && outputS.indexOf(localId) > 0)) {
|
586 |
|
|
// throw new ServiceFailure(1190, outputS);
|
587 |
|
|
// }
|
588 |
5333
|
berkley
|
logMetacat.debug("Finsished inserting xml document with id " + localId);
|
589 |
|
|
return localId;
|
590 |
5329
|
jones
|
}
|
591 |
|
|
|
592 |
5332
|
jones
|
public static ByteArrayOutputStream serializeSystemMetadata(SystemMetadata sysmeta)
|
593 |
5329
|
jones
|
throws ServiceFailure {
|
594 |
|
|
IBindingFactory bfact;
|
595 |
|
|
ByteArrayOutputStream sysmetaOut = null;
|
596 |
|
|
try {
|
597 |
|
|
bfact = BindingDirectory.getFactory(SystemMetadata.class);
|
598 |
|
|
IMarshallingContext mctx = bfact.createMarshallingContext();
|
599 |
|
|
sysmetaOut = new ByteArrayOutputStream();
|
600 |
|
|
mctx.marshalDocument(sysmeta, "UTF-8", null, sysmetaOut);
|
601 |
|
|
} catch (JiBXException e) {
|
602 |
|
|
throw new ServiceFailure(1190, "Failed to serialize and insert SystemMetadata: " + e.getMessage());
|
603 |
|
|
}
|
604 |
|
|
|
605 |
|
|
return sysmetaOut;
|
606 |
|
|
}
|
607 |
5332
|
jones
|
|
608 |
|
|
public static SystemMetadata deserializeSystemMetadata(InputStream xml)
|
609 |
|
|
throws ServiceFailure {
|
610 |
|
|
try {
|
611 |
|
|
IBindingFactory bfact = BindingDirectory.getFactory(SystemMetadata.class);
|
612 |
|
|
IUnmarshallingContext uctx = bfact.createUnmarshallingContext();
|
613 |
|
|
SystemMetadata sysmeta = (SystemMetadata) uctx.unmarshalDocument(xml, null);
|
614 |
|
|
return sysmeta;
|
615 |
|
|
} catch (JiBXException e) {
|
616 |
|
|
throw new ServiceFailure(1190, "Failed to serialize and insert SystemMetadata: " + e.getMessage());
|
617 |
|
|
}
|
618 |
|
|
}
|
619 |
5298
|
jones
|
}
|