Project

General

Profile

1
/**
2
 *  '$RCSfile$'
3
 *  Copyright: 2000-2011 Regents of the University of California and the
4
 *              National Center for Ecological Analysis and Synthesis
5
 *
6
 *   '$Author: tao $'
7
 *     '$Date: 2014-12-24 09:47:25 -0800 (Wed, 24 Dec 2014) $'
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

    
24
package edu.ucsb.nceas.metacat.dataone.v1;
25

    
26
import java.io.InputStream;
27
import java.sql.SQLException;
28
import java.util.Date;
29

    
30
import javax.servlet.http.HttpServletRequest;
31

    
32
import org.apache.log4j.Logger;
33
import org.dataone.service.exceptions.IdentifierNotUnique;
34
import org.dataone.service.exceptions.InsufficientResources;
35
import org.dataone.service.exceptions.InvalidRequest;
36
import org.dataone.service.exceptions.InvalidSystemMetadata;
37
import org.dataone.service.exceptions.InvalidToken;
38
import org.dataone.service.exceptions.NotAuthorized;
39
import org.dataone.service.exceptions.NotFound;
40
import org.dataone.service.exceptions.NotImplemented;
41
import org.dataone.service.exceptions.ServiceFailure;
42
import org.dataone.service.exceptions.SynchronizationFailed;
43
import org.dataone.service.exceptions.UnsupportedType;
44
import org.dataone.service.mn.tier1.v1.MNCore;
45
import org.dataone.service.mn.tier1.v1.MNRead;
46
import org.dataone.service.mn.tier2.v1.MNAuthorization;
47
import org.dataone.service.mn.tier3.v1.MNStorage;
48
import org.dataone.service.mn.tier4.v1.MNReplication;
49
import org.dataone.service.mn.v1.MNQuery;
50
import org.dataone.service.types.v1.Checksum;
51
import org.dataone.service.types.v1.DescribeResponse;
52
import org.dataone.service.types.v1.Event;
53
import org.dataone.service.types.v1.Identifier;
54
import org.dataone.service.types.v1.Log;
55
import org.dataone.service.types.v1.Node;
56
import org.dataone.service.types.v1.NodeReference;
57
import org.dataone.service.types.v1.ObjectFormatIdentifier;
58
import org.dataone.service.types.v1.ObjectList;
59
import org.dataone.service.types.v1.Permission;
60
import org.dataone.service.types.v1.Session;
61
import org.dataone.service.types.v1.SystemMetadata;
62
import org.dataone.service.types.v1_1.QueryEngineDescription;
63
import org.dataone.service.types.v1_1.QueryEngineList;
64
import org.dataone.service.util.TypeMarshaller;
65

    
66
import edu.ucsb.nceas.metacat.IdentifierManager;
67

    
68
/**
69
 * Represents Metacat's implementation of the DataONE Member Node 
70
 * service API, v1. Methods typically pass through to the current 
71
 * version implementation performing type conversion as needed.
72
 * 
73
 */
74
public class MNodeService 
75
    implements MNAuthorization, MNCore, MNRead, MNReplication, MNStorage, MNQuery {
76

    
77
	/**
78
	 * current version implementation
79
	 */
80
	edu.ucsb.nceas.metacat.dataone.MNodeService impl = null;
81
	
82
	/* the logger instance */
83
    private Logger logMetacat = null;
84

    
85
    /**
86
     * Singleton accessor to get an instance of MNodeService.
87
     * 
88
     * @return instance - the instance of MNodeService
89
     */
90
    public static MNodeService getInstance(HttpServletRequest request) {
91
        return new MNodeService(request);
92
    }
93

    
94
    /**
95
     * Constructor, private for singleton access
96
     */
97
    private MNodeService(HttpServletRequest request) {
98
        logMetacat = Logger.getLogger(MNodeService.class);
99
        impl = edu.ucsb.nceas.metacat.dataone.MNodeService.getInstance(request);
100
    }
101
    
102
    public void setSession(Session session) {
103
    	impl.setSession(session);
104
    }
105
    
106
    public boolean isAdminAuthorized(Session session) throws ServiceFailure, InvalidToken, NotAuthorized, NotImplemented {
107
    	return impl.isAdminAuthorized(session);
108
    }
109

    
110
	@Override
111
	public QueryEngineDescription getQueryEngineDescription(String engine)
112
			throws InvalidToken, ServiceFailure, NotAuthorized, NotImplemented,
113
			NotFound {
114
		return impl.getQueryEngineDescription(null, engine);
115
	}
116

    
117
	@Override
118
	public QueryEngineList listQueryEngines() throws InvalidToken,
119
			ServiceFailure, NotAuthorized, NotImplemented {
120
		return impl.listQueryEngines(null);
121
	}
122

    
123
	@Override
124
	public InputStream query(String engine, String query) throws InvalidToken,
125
			ServiceFailure, NotAuthorized, InvalidRequest, NotImplemented,
126
			NotFound {
127
		return impl.query(null, engine, query);
128
	}
129

    
130
	@Override
131
	public Identifier archive(Identifier pid) throws InvalidToken,
132
			ServiceFailure, NotAuthorized, NotFound, NotImplemented {
133
		return impl.archive(null, pid);
134
	}
135

    
136
	@Override
137
	@Deprecated
138
	public Identifier archive(Session session, Identifier pid)
139
			throws InvalidToken, ServiceFailure, NotAuthorized, NotFound,
140
			NotImplemented {
141
		return impl.archive(session, pid);
142
	}
143

    
144
	@Override
145
	public Identifier create(Identifier pid, InputStream object,
146
			SystemMetadata sysmeta) throws IdentifierNotUnique,
147
			InsufficientResources, InvalidRequest, InvalidSystemMetadata,
148
			InvalidToken, NotAuthorized, NotImplemented, ServiceFailure,
149
			UnsupportedType {
150
		return this.create(null, pid, object, sysmeta);
151
	}
152

    
153
	@Override
154
	@Deprecated
155
	public Identifier create(Session session, Identifier pid, InputStream object,
156
			SystemMetadata sysmeta) throws IdentifierNotUnique,
157
			InsufficientResources, InvalidRequest, InvalidSystemMetadata,
158
			InvalidToken, NotAuthorized, NotImplemented, ServiceFailure,
159
			UnsupportedType {
160
		//convert sysmeta to newer version
161
		org.dataone.service.types.v2.SystemMetadata v2Sysmeta = null;
162
		try {
163
			v2Sysmeta = TypeMarshaller.convertTypeFromType(sysmeta, org.dataone.service.types.v2.SystemMetadata.class);
164
		} catch (Exception e) {
165
			// report as service failure
166
			ServiceFailure sf = new ServiceFailure("1190", e.getMessage());
167
			sf.initCause(e);
168
			throw sf;
169
		}
170
		
171
		return impl.create(session, pid, object, v2Sysmeta);
172
	}
173

    
174
	@Override
175
	public Identifier delete(Identifier pid) throws InvalidToken,
176
			ServiceFailure, NotAuthorized, NotFound, NotImplemented {
177
		return impl.delete(null, pid);
178
	}
179

    
180
	@Override
181
	@Deprecated
182
	public Identifier delete(Session session, Identifier pid)
183
			throws InvalidToken, ServiceFailure, NotAuthorized, NotFound,
184
			NotImplemented {
185
		return impl.delete(session, pid);
186
	}
187

    
188
	@Override
189
	public Identifier generateIdentifier(String scheme, String fragment)
190
			throws InvalidToken, ServiceFailure, NotAuthorized, NotImplemented,
191
			InvalidRequest {
192
		return impl.generateIdentifier(null, scheme, fragment);
193
	}
194

    
195
	@Override
196
	@Deprecated
197
	public Identifier generateIdentifier(Session session, String scheme, String fragment)
198
			throws InvalidToken, ServiceFailure, NotAuthorized, NotImplemented,
199
			InvalidRequest {
200
		return impl.generateIdentifier(session, scheme, fragment);
201
	}
202

    
203
	@Override
204
	public Identifier update(Identifier pid, InputStream object,
205
			Identifier newPid, SystemMetadata sysmeta) throws IdentifierNotUnique,
206
			InsufficientResources, InvalidRequest, InvalidSystemMetadata,
207
			InvalidToken, NotAuthorized, NotImplemented, ServiceFailure,
208
			UnsupportedType, NotFound {
209
		return this.update(null, pid, object, newPid, sysmeta);
210
	}
211

    
212
	@Override
213
	@Deprecated
214
	public Identifier update(Session session, Identifier pid, InputStream object,
215
			Identifier newPid, SystemMetadata sysmeta) throws IdentifierNotUnique,
216
			InsufficientResources, InvalidRequest, InvalidSystemMetadata,
217
			InvalidToken, NotAuthorized, NotImplemented, ServiceFailure,
218
			UnsupportedType, NotFound {
219
		//convert sysmeta to newer version
220
		org.dataone.service.types.v2.SystemMetadata v2Sysmeta = null;
221
		try {
222
			v2Sysmeta = TypeMarshaller.convertTypeFromType(sysmeta, org.dataone.service.types.v2.SystemMetadata.class);
223
		} catch (Exception e) {
224
			// report as service failure
225
			ServiceFailure sf = new ServiceFailure("1030", e.getMessage());
226
			sf.initCause(e);
227
			throw sf;
228
		}
229
		return impl.update(session, pid, object, newPid, v2Sysmeta);
230
	}
231

    
232
	@Override
233
	public boolean replicate(SystemMetadata sysmeta, NodeReference sourceNode)
234
			throws NotImplemented, ServiceFailure, NotAuthorized,
235
			InvalidRequest, InvalidToken, InsufficientResources,
236
			UnsupportedType {
237
		return this.replicate(null, sysmeta, sourceNode);
238
	}
239

    
240
	@Override
241
	@Deprecated
242
	public boolean replicate(Session session, SystemMetadata sysmeta,
243
			NodeReference sourceNode) throws NotImplemented, ServiceFailure,
244
			NotAuthorized, InvalidRequest, InvalidToken, InsufficientResources,
245
			UnsupportedType {
246
		//convert sysmeta to newer version
247
		org.dataone.service.types.v2.SystemMetadata v2Sysmeta = null;
248
		try {
249
			v2Sysmeta = TypeMarshaller.convertTypeFromType(sysmeta, org.dataone.service.types.v2.SystemMetadata.class);
250
		} catch (Exception e) {
251
			// report as service failure
252
			ServiceFailure sf = new ServiceFailure("1030", e.getMessage());
253
			sf.initCause(e);
254
			throw sf;
255
		}
256
		return impl.replicate(session, v2Sysmeta, sourceNode);
257
	}
258

    
259
	@Override
260
	public DescribeResponse describe(Identifier pid) throws InvalidToken,
261
			NotAuthorized, NotImplemented, ServiceFailure, NotFound {
262
	    String serviceFailure = "1030";
263
        String notFound = "1020";
264
        impl.checkV1SystemMetaPidExist(pid, serviceFailure, "The system metadata for given PID "+pid.getValue()+" couldn't be identified if it exists",  notFound, 
265
                "No system metadata could be found for given PID: "+pid.getValue());
266
		return impl.describe(null, pid);
267
	}
268

    
269
	@Override
270
	@Deprecated
271
	public DescribeResponse describe(Session session, Identifier pid)
272
			throws InvalidToken, NotAuthorized, NotImplemented, ServiceFailure,
273
			NotFound {
274
	    String serviceFailure = "1030";
275
        String notFound = "1020";
276
        impl.checkV1SystemMetaPidExist(pid, serviceFailure, "The system metadata for given PID "+pid.getValue()+" couldn't be identified if it exists",  notFound, 
277
                "No system metadata could be found for given PID: "+pid.getValue());
278
		return impl.describe(session, pid);
279
	}
280

    
281
	@Override
282
	public InputStream get(Identifier pid) throws InvalidToken, NotAuthorized,
283
			NotImplemented, ServiceFailure, NotFound, InsufficientResources {
284
	    String serviceFailure = "1030";
285
        String notFound = "1020";
286
        impl.checkV1SystemMetaPidExist(pid, serviceFailure, "The object specified by "+pid.getValue()+" couldn't be identified if it exists",  notFound, 
287
                "The object specified by "+pid.getValue()+" does not exist at this node.");
288
		return impl.get(null, pid);
289
	}
290

    
291
	@Override
292
	@Deprecated
293
	public InputStream get(Session session, Identifier pid) throws InvalidToken,
294
			NotAuthorized, NotImplemented, ServiceFailure, NotFound,
295
			InsufficientResources {
296
	    String serviceFailure = "1030";
297
        String notFound = "1020";
298
        impl.checkV1SystemMetaPidExist(pid, serviceFailure, "The object specified by "+pid.getValue()+" couldn't be identified if it exists",  notFound, 
299
                "The object specified by "+pid.getValue()+" does not exist at this node.");
300
		return impl.get(session, pid);
301
	}
302

    
303
	@Override
304
	public Checksum getChecksum(Identifier pid, String algorithm)
305
			throws InvalidRequest, InvalidToken, NotAuthorized, NotImplemented,
306
			ServiceFailure, NotFound {
307
		return impl.getChecksum(null, pid, algorithm);
308
	}
309

    
310
	@Override
311
	@Deprecated
312
	public Checksum getChecksum(Session session, Identifier pid, String algorithm)
313
			throws InvalidRequest, InvalidToken, NotAuthorized, NotImplemented,
314
			ServiceFailure, NotFound {
315
		return impl.getChecksum(session, pid, algorithm);
316
	}
317

    
318
	@Override
319
	public InputStream getReplica(Identifier pid) throws InvalidToken,
320
			NotAuthorized, NotImplemented, ServiceFailure, NotFound,
321
			InsufficientResources {
322
		return impl.getReplica(null, pid);
323
	}
324

    
325
	@Override
326
	@Deprecated
327
	public InputStream getReplica(Session session, Identifier pid)
328
			throws InvalidToken, NotAuthorized, NotImplemented, ServiceFailure,
329
			NotFound, InsufficientResources {
330
		return impl.get(session, pid);
331
	}
332

    
333
	@Override
334
	public SystemMetadata getSystemMetadata(Identifier pid)
335
			throws InvalidToken, NotAuthorized, NotImplemented, ServiceFailure,
336
			NotFound {
337
		
338
		return this.getSystemMetadata(null, pid);
339
		
340
	}
341

    
342
	@Override
343
	@Deprecated
344
	public SystemMetadata getSystemMetadata(Session session, Identifier pid)
345
			throws InvalidToken, NotAuthorized, NotImplemented, ServiceFailure,
346
			NotFound {
347
	    String serviceFailure = "1090";
348
        String notFound = "1060";
349
        impl.checkV1SystemMetaPidExist(pid, serviceFailure, "The system metadata for given PID "+pid.getValue()+" couldn't be identified if it exists",  notFound, 
350
                "No system metadata could be found for given PID: "+pid.getValue());
351
		org.dataone.service.types.v2.SystemMetadata sysMeta = impl.getSystemMetadata(session, pid);
352
		SystemMetadata retSysMeta = null;
353
		try {
354
			retSysMeta = TypeMarshaller.convertTypeFromType(sysMeta, SystemMetadata.class);
355
		} catch (Exception e) {
356
			// report as service failure
357
			ServiceFailure sf = new ServiceFailure("4801", e.getMessage());
358
			sf.initCause(e);
359
			throw sf;
360
		}
361
		return retSysMeta;
362
	}
363

    
364
	@Override
365
	public ObjectList listObjects(Date startTime, Date endTime,
366
			ObjectFormatIdentifier objectFormatId, Boolean replicaStatus, Integer start,
367
			Integer count) throws InvalidRequest, InvalidToken, NotAuthorized,
368
			NotImplemented, ServiceFailure {
369
		return this.listObjects(null, startTime, endTime, objectFormatId, replicaStatus, start, count);
370
	}
371

    
372
	@Override
373
	@Deprecated
374
	public ObjectList listObjects(Session session, Date startTime, Date endTime,
375
			ObjectFormatIdentifier objectFormatId, Boolean replicaStatus, Integer start,
376
			Integer count) throws InvalidRequest, InvalidToken, NotAuthorized,
377
			NotImplemented, ServiceFailure {
378
		return impl.listObjects(session, startTime, endTime, objectFormatId, null, replicaStatus, start, count);
379
	}
380

    
381
	@Override
382
	public boolean synchronizationFailed(SynchronizationFailed syncFailed)
383
			throws InvalidToken, NotAuthorized, NotImplemented, ServiceFailure {
384
		return impl.synchronizationFailed(null, syncFailed);
385
	}
386

    
387
	@Override
388
	@Deprecated
389
	public boolean synchronizationFailed(Session session,
390
			SynchronizationFailed syncFailed) throws InvalidToken, NotAuthorized,
391
			NotImplemented, ServiceFailure {
392
		return impl.synchronizationFailed(session, syncFailed);
393
	}
394

    
395
	@Override
396
	public Node getCapabilities() throws NotImplemented, ServiceFailure {
397
		org.dataone.service.types.v2.Node node = impl.getCapabilities();
398
		Node retNode = null;
399
		try {
400
			retNode = TypeMarshaller.convertTypeFromType(node, Node.class);
401
		} catch (Exception e) {
402
			// report as service failure
403
			ServiceFailure sf = new ServiceFailure("4801", e.getMessage());
404
			sf.initCause(e);
405
			throw sf;
406
		}
407
		return retNode;
408
	}
409

    
410
	@Override
411
	public Log getLogRecords(Date fromDate, Date toDate, Event event,
412
			String pidFilter, Integer start, Integer count) throws InvalidRequest, InvalidToken,
413
			NotAuthorized, NotImplemented, ServiceFailure {
414
		return this.getLogRecords(null, fromDate, toDate, event, pidFilter, start, count);
415
	}
416

    
417
	@Override
418
	@Deprecated
419
	public Log getLogRecords(Session session, Date fromDate, Date toDate, Event event,
420
			String pidFilter, Integer start, Integer count) throws InvalidRequest, InvalidToken,
421
			NotAuthorized, NotImplemented, ServiceFailure {
422
		org.dataone.service.types.v2.Log log = impl.getLogRecords(session, fromDate, toDate, event.xmlValue(), pidFilter, start, count);
423
		Log retLog = null;
424
		try {
425
			retLog = TypeMarshaller.convertTypeFromType(log, Log.class);
426
		} catch (Exception e) {
427
			// report as service failure
428
			ServiceFailure sf = new ServiceFailure("1490", e.getMessage());
429
			sf.initCause(e);
430
			throw sf;
431
		}
432
		return retLog;
433
	}
434

    
435
	@Override
436
	public Date ping() throws NotImplemented, ServiceFailure,
437
			InsufficientResources {
438
		return impl.ping();
439
	}
440

    
441
	@Override
442
	public boolean isAuthorized(Identifier pid, Permission permission)
443
			throws ServiceFailure, InvalidRequest, InvalidToken, NotFound,
444
			NotAuthorized, NotImplemented {
445
		return impl.isAuthorized(null, pid, permission);
446
	}
447

    
448
	@Override
449
	@Deprecated
450
	public boolean isAuthorized(Session session, Identifier pid, Permission permission)
451
			throws ServiceFailure, InvalidRequest, InvalidToken, NotFound,
452
			NotAuthorized, NotImplemented {
453
		return impl.isAuthorized(session, pid, permission);
454
	}
455

    
456
	@Override
457
	public boolean systemMetadataChanged(Identifier pid, long serialVersion, Date dateSysMetaLastModified)
458
			throws InvalidToken, ServiceFailure, NotAuthorized, NotImplemented,
459
			InvalidRequest {
460
		return impl.systemMetadataChanged(null, pid, serialVersion, dateSysMetaLastModified);
461

    
462
	}
463

    
464
	@Override
465
	@Deprecated
466
	public boolean systemMetadataChanged(Session session, Identifier pid,
467
			long serialVersion, Date dateSysMetaLastModified) throws InvalidToken, ServiceFailure,
468
			NotAuthorized, NotImplemented, InvalidRequest {
469
		return impl.systemMetadataChanged(session, pid, serialVersion, dateSysMetaLastModified);
470
	}
471
    
472
	// methods not defined in v1, but implemented in metacat pre-v2 release
473
	
474
	public Identifier publish(Session session, Identifier originalIdentifier) 
475
			throws InvalidToken, ServiceFailure, NotAuthorized, NotImplemented, InvalidRequest, NotFound, IdentifierNotUnique, UnsupportedType, InsufficientResources, InvalidSystemMetadata {
476
		return impl.publish(session, originalIdentifier);
477
		
478
	}
479
	
480
	public InputStream view(Session session, String format, Identifier pid)
481
			throws InvalidToken, ServiceFailure, NotAuthorized, InvalidRequest,
482
			NotImplemented, NotFound {
483
		
484
		return impl.view(session, format, pid);
485
	}
486
	
487
	public InputStream getPackage(Session session, ObjectFormatIdentifier formatId,
488
			Identifier pid) throws InvalidToken, ServiceFailure,
489
			NotAuthorized, InvalidRequest, NotImplemented, NotFound {
490
		return impl.getPackage(session, formatId, pid);
491
	}
492
	
493
    
494

    
495
   
496
}
(2-2/2)