Revision 5319
Added by Matt Jones almost 14 years ago
CrudService.java | ||
---|---|---|
22 | 22 |
*/ |
23 | 23 |
package edu.ucsb.nceas.metacat.dataone; |
24 | 24 |
|
25 |
import java.io.File; |
|
26 |
import java.io.FileNotFoundException; |
|
27 |
import java.io.FileOutputStream; |
|
25 | 28 |
import java.io.IOException; |
26 | 29 |
import java.io.InputStream; |
27 | 30 |
import java.io.OutputStream; |
... | ... | |
35 | 38 |
import javax.servlet.http.HttpServletRequest; |
36 | 39 |
import javax.servlet.http.HttpServletResponse; |
37 | 40 |
|
41 |
import org.apache.commons.io.IOUtils; |
|
42 |
import org.apache.log4j.Logger; |
|
38 | 43 |
import org.dataone.service.exceptions.IdentifierNotUnique; |
39 | 44 |
import org.dataone.service.exceptions.InsufficientResources; |
40 | 45 |
import org.dataone.service.exceptions.InvalidRequest; |
... | ... | |
55 | 60 |
|
56 | 61 |
import com.gc.iotools.stream.is.InputStreamFromOutputStream; |
57 | 62 |
|
63 |
import edu.ucsb.nceas.metacat.AccessionNumberException; |
|
64 |
import edu.ucsb.nceas.metacat.DocumentImpl; |
|
65 |
import edu.ucsb.nceas.metacat.EventLog; |
|
58 | 66 |
import edu.ucsb.nceas.metacat.IdentifierManager; |
59 | 67 |
import edu.ucsb.nceas.metacat.McdbDocNotFoundException; |
60 | 68 |
import edu.ucsb.nceas.metacat.McdbException; |
61 | 69 |
import edu.ucsb.nceas.metacat.MetacatHandler; |
62 | 70 |
import edu.ucsb.nceas.metacat.client.InsufficientKarmaException; |
71 |
import edu.ucsb.nceas.metacat.properties.PropertyService; |
|
72 |
import edu.ucsb.nceas.metacat.replication.ForceReplicationHandler; |
|
63 | 73 |
import edu.ucsb.nceas.metacat.service.SessionService; |
64 | 74 |
import edu.ucsb.nceas.metacat.util.SessionData; |
65 | 75 |
import edu.ucsb.nceas.utilities.ParseLSIDException; |
... | ... | |
84 | 94 |
// private String sessionId; |
85 | 95 |
// private String[] groupNames; |
86 | 96 |
private Hashtable<String, String[]> params; |
97 |
Logger logMetacat = null; |
|
87 | 98 |
|
88 | 99 |
/** |
89 | 100 |
* Initializes new instance by setting servlet context,request and response. |
... | ... | |
95 | 106 |
this.servletContext = servletContext; |
96 | 107 |
this.request = request; |
97 | 108 |
this.response = response; |
109 |
logMetacat = Logger.getLogger(CrudService.class); |
|
98 | 110 |
|
99 | 111 |
handler = new MetacatHandler(this.servletContext, new Timer()); |
100 | 112 |
initParams(); |
... | ... | |
165 | 177 |
InputStream object, SystemMetadata sysmeta) throws InvalidToken, |
166 | 178 |
ServiceFailure, NotAuthorized, IdentifierNotUnique, UnsupportedType, |
167 | 179 |
InsufficientResources, InvalidSystemMetadata, NotImplemented { |
168 |
throw new NotImplemented(1000, "This method not yet implemented."); |
|
180 |
|
|
181 |
logMetacat.debug("Starting CrudService.create()..."); |
|
182 |
|
|
183 |
// TODO: authenticate & get user info |
|
184 |
String username = "GARBAGETOBEREPLACED"; |
|
185 |
String[] groups = null; |
|
186 |
|
|
187 |
// verify that guid == SystemMetadata.getIdentifier() |
|
188 |
logMetacat.debug("Comparing guid|sysmeta_guid: " + guid.getIdentifier() + "|" + sysmeta.getIdentifier().getIdentifier()); |
|
189 |
// if (!guid.getIdentifier().equals(sysmeta.getIdentifier().getIdentifier())) { |
|
190 |
// throw new InvalidSystemMetadata(1180, |
|
191 |
// "GUID in method call does not match GUID in system metadata."); |
|
192 |
// } |
|
193 |
|
|
194 |
logMetacat.debug("Checking if identifier exists..."); |
|
195 |
// Check that the identifier does not already exist |
|
196 |
IdentifierManager im = IdentifierManager.getInstance(); |
|
197 |
if (im.identifierExists(guid.getIdentifier())) { |
|
198 |
throw new IdentifierNotUnique(1120, |
|
199 |
"GUID is already in use by an existing object."); |
|
200 |
} |
|
201 |
|
|
202 |
// generate guid/localId pair for object |
|
203 |
logMetacat.debug("Generating a guid/localId mapping"); |
|
204 |
String localId = im.generateLocalId(guid.getIdentifier(), 1); |
|
205 |
|
|
206 |
// TODO: generate guid/localId pair for sysmeta |
|
207 |
// TODO: update system metadata fields |
|
208 |
// TODO: insert system metadata to metacat (probably at end) |
|
209 |
|
|
210 |
// TODO: Check if we are handling metadata or data |
|
211 |
|
|
212 |
// TODO: CASE METADATA: |
|
213 |
// Setup and call handleInsertOrUpdate() |
|
214 |
|
|
215 |
// TODO: DEFAULT CASE: DATA |
|
216 |
try { |
|
217 |
logMetacat.debug("Case DATA: starting to write to disk."); |
|
218 |
if (DocumentImpl.getDataFileLockGrant(localId)) { |
|
219 |
|
|
220 |
// Save the data file to disk using "localId" as the name |
|
221 |
try { |
|
222 |
String datafilepath = PropertyService.getProperty("application.datafilepath"); |
|
223 |
|
|
224 |
File dataDirectory = new File(datafilepath); |
|
225 |
dataDirectory.mkdirs(); |
|
226 |
|
|
227 |
File newFile = writeStreamToFile(dataDirectory, localId, object); |
|
228 |
|
|
229 |
// TODO: Check that the file size matches SystemMetadata |
|
230 |
// long size = newFile.length(); |
|
231 |
// if (size == 0) { |
|
232 |
// throw new IOException("Uploaded file is 0 bytes!"); |
|
233 |
// } |
|
234 |
|
|
235 |
//register the file in the database (which generates an exception |
|
236 |
// if the docid is not acceptable or other untoward things happen |
|
237 |
try { |
|
238 |
logMetacat.debug("Registering document..."); |
|
239 |
DocumentImpl.registerDocument(localId, "BIN", localId, |
|
240 |
username, groups); |
|
241 |
logMetacat.debug("Registrtion step completed."); |
|
242 |
} catch (SQLException e) { |
|
243 |
//newFile.delete(); |
|
244 |
logMetacat.debug("SQLE: " + e.getMessage()); |
|
245 |
e.printStackTrace(System.out); |
|
246 |
throw new ServiceFailure(1190, "Registration failed: " + e.getMessage()); |
|
247 |
} catch (AccessionNumberException e) { |
|
248 |
//newFile.delete(); |
|
249 |
logMetacat.debug("ANE: " + e.getMessage()); |
|
250 |
e.printStackTrace(System.out); |
|
251 |
throw new ServiceFailure(1190, "Registration failed: " + e.getMessage()); |
|
252 |
} catch (Exception e) { |
|
253 |
//newFile.delete(); |
|
254 |
logMetacat.debug("Exception: " + e.getMessage()); |
|
255 |
e.printStackTrace(System.out); |
|
256 |
throw new ServiceFailure(1190, "Registration failed: " + e.getMessage()); |
|
257 |
} |
|
258 |
|
|
259 |
logMetacat.debug("Logging the creation event."); |
|
260 |
EventLog.getInstance().log(request.getRemoteAddr(), |
|
261 |
username, localId, "create"); |
|
262 |
|
|
263 |
// Force replication this data file |
|
264 |
// To data file, "insert" and update is same |
|
265 |
// The fourth parameter is null. Because it is notification |
|
266 |
// server and this method is in MetaCatServerlet. It is |
|
267 |
// original command, not get force replication info from |
|
268 |
// another metacat |
|
269 |
// TODO: note that GUID mapping is not being replicated |
|
270 |
logMetacat.debug("Scheduling replication."); |
|
271 |
ForceReplicationHandler frh = new ForceReplicationHandler( |
|
272 |
localId, "insert", false, null); |
|
273 |
|
|
274 |
} catch (PropertyNotFoundException e) { |
|
275 |
throw new ServiceFailure(1190, "Could not lock file for writing:" + e.getMessage()); |
|
276 |
} |
|
277 |
|
|
278 |
} |
|
279 |
} catch (Exception e) { |
|
280 |
// Could not get a lock on the document, so we can not update the file now |
|
281 |
throw new ServiceFailure(1190, "Failed to lock file: " + e.getMessage()); |
|
282 |
} |
|
283 |
|
|
284 |
logMetacat.debug("Returning from CrudService.create()"); |
|
285 |
return guid; |
|
169 | 286 |
} |
170 | 287 |
|
171 | 288 |
public IdentifierType delete(AuthToken token, IdentifierType guid) |
... | ... | |
296 | 413 |
public SystemMetadata getSystemMetadata(AuthToken token, IdentifierType guid) |
297 | 414 |
throws InvalidToken, ServiceFailure, NotAuthorized, NotFound, |
298 | 415 |
InvalidRequest, NotImplemented { |
416 |
|
|
417 |
// Look up ID of system metadata based on guid |
|
418 |
// Initially from document, later from entry in table? |
|
419 |
|
|
420 |
// Read system metadata from disk and create SystemMetadata object |
|
421 |
// Follows same implementation plan as get() |
|
422 |
|
|
423 |
// return it |
|
299 | 424 |
throw new NotImplemented(1000, "This method not yet implemented."); |
300 | 425 |
} |
301 | 426 |
|
... | ... | |
307 | 432 |
throw new NotImplemented(1000, "This method not yet implemented."); |
308 | 433 |
} |
309 | 434 |
|
435 |
private File writeStreamToFile(File dir, String fileName, InputStream data) |
|
436 |
throws ServiceFailure { |
|
437 |
// logMetacat.debug("***********************************************"); |
|
438 |
// logMetacat.debug("** FILE TEST FROM CS **"); |
|
439 |
// logMetacat.debug("***********************************************"); |
|
440 |
|
|
441 |
File newFile = new File(dir, fileName); |
|
442 |
logMetacat.debug("Filename for write is: " + newFile.getAbsolutePath()); |
|
443 |
|
|
444 |
try { |
|
445 |
if (newFile.createNewFile()) { |
|
446 |
// logMetacat.debug("File doesn't yet exist, so write to it."); |
|
447 |
// logMetacat.debug("Creating file stream..."); |
|
448 |
|
|
449 |
// write data stream to desired file |
|
450 |
OutputStream os = new FileOutputStream(newFile); |
|
451 |
// logMetacat.debug("Copying file stream..."); |
|
452 |
long length = IOUtils.copyLarge(data, os); |
|
453 |
os.flush(); |
|
454 |
// logMetacat.debug("Closing file stream..."); |
|
455 |
os.close(); |
|
456 |
// logMetacat.debug("Done with file stream..."); |
|
457 |
} else { |
|
458 |
logMetacat.debug("File creation failed, or file already exists."); |
|
459 |
throw new ServiceFailure(1190, "File already exists: " + fileName); |
|
460 |
} |
|
461 |
} catch (FileNotFoundException e) { |
|
462 |
logMetacat.debug("FNF: " + e.getMessage()); |
|
463 |
throw new ServiceFailure(1190, "File not found: " + fileName + " " |
|
464 |
+ e.getMessage()); |
|
465 |
} catch (IOException e) { |
|
466 |
logMetacat.debug("IOE: " + e.getMessage()); |
|
467 |
throw new ServiceFailure(1190, "File was not written: " + fileName |
|
468 |
+ " " + e.getMessage()); |
|
469 |
} |
|
470 |
|
|
471 |
// logMetacat.debug("***********************************************"); |
|
472 |
// logMetacat.debug("** END FILE TEST **"); |
|
473 |
// logMetacat.debug("***********************************************"); |
|
474 |
|
|
475 |
return newFile; |
|
476 |
} |
|
310 | 477 |
} |
Also available in: Unified diff
Modifications to support the DataONE service API version 0.1.0. For DataONE, the get() and
create() services are partially complete. Several more functions and checks need to be added to
create() before it is viable. This DataONE support is not complete, and the current support breaks the MetacatRestClientTest for the time being (this client will eventually be removed).