Project

General

Profile

1
/**
2
 *  '$RCSfile$'
3
 *  Copyright: 2011 Regents of the University of California and the
4
 *              National Center for Ecological Analysis and Synthesis
5
 *
6
 *   '$Author: leinfelder $'
7
 *     '$Date: 2011-10-27 17:51:48 -0700 (Thu, 27 Oct 2011) $'
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.restservice;
24

    
25
import java.io.ByteArrayInputStream;
26
import java.io.File;
27
import java.io.FileInputStream;
28
import java.io.IOException;
29
import java.io.InputStream;
30
import java.io.OutputStream;
31
import java.util.Date;
32
import java.util.Map;
33

    
34
import javax.servlet.ServletContext;
35
import javax.servlet.http.HttpServletRequest;
36
import javax.servlet.http.HttpServletResponse;
37
import javax.xml.parsers.ParserConfigurationException;
38

    
39
import org.apache.commons.fileupload.FileUploadException;
40
import org.apache.commons.io.IOUtils;
41
import org.apache.log4j.Logger;
42
import org.dataone.client.ObjectFormatCache;
43
import org.dataone.service.exceptions.BaseException;
44
import org.dataone.service.exceptions.IdentifierNotUnique;
45
import org.dataone.service.exceptions.InsufficientResources;
46
import org.dataone.service.exceptions.InvalidRequest;
47
import org.dataone.service.exceptions.InvalidSystemMetadata;
48
import org.dataone.service.exceptions.InvalidToken;
49
import org.dataone.service.exceptions.NotAuthorized;
50
import org.dataone.service.exceptions.NotFound;
51
import org.dataone.service.exceptions.NotImplemented;
52
import org.dataone.service.exceptions.ServiceFailure;
53
import org.dataone.service.exceptions.UnsupportedType;
54
import org.dataone.service.types.v1.AccessPolicy;
55
import org.dataone.service.types.v1.Checksum;
56
import org.dataone.service.types.v1.Event;
57
import org.dataone.service.types.v1.Identifier;
58
import org.dataone.service.types.v1.Log;
59
import org.dataone.service.types.v1.ObjectFormat;
60
import org.dataone.service.types.v1.ObjectFormatIdentifier;
61
import org.dataone.service.types.v1.ObjectFormatList;
62
import org.dataone.service.types.v1.ObjectLocationList;
63
import org.dataone.service.types.v1.Permission;
64
import org.dataone.service.types.v1.Subject;
65
import org.dataone.service.types.v1.SystemMetadata;
66
import org.dataone.service.util.DateTimeMarshaller;
67
import org.dataone.service.util.TypeMarshaller;
68
import org.jibx.runtime.JiBXException;
69
import org.xml.sax.SAXException;
70

    
71
import edu.ucsb.nceas.metacat.dataone.CNodeService;
72

    
73
/**
74
 * CN REST service implementation handler
75
 * 
76
 * ******************
77
 	CNCore -- DONE
78
		create() - POST /d1/cn/object/PID
79
		listFormats() - GET /d1/cn/formats
80
		getFormat() - GET /d1/cn/formats/FMTID
81
		getLogRecords - GET /d1/cn/log
82
		reserveIdentifier() - POST /d1/cn/reserve
83
		listNodes() - Not implemented
84
		registerSystemMetadata() - POST /d1/meta/PID
85
	
86
	CNRead -- DONE
87
		get() - GET /d1/cn/object/PID
88
		getSystemMetadata() - GET /d1/cn/meta/PID
89
		resolve() - GET /d1/cn/resolve/PID
90
		assertRelation() - GET /d1/cn/assertRelation/PID
91
		getChecksum() - GET /d1/cn/checksum
92
		search() - Not implemented in Metacat
93
	
94
	CNAuthorization
95
		setOwner() - PUT /d1/cn/owner/PID
96
		isAuthorized() - GET /d1/cn/isAuthorized/PID
97
		setAccessPolicy() - POST /d1/cn/accessRules
98
		
99
	CNIdentity - not implemented at all on Metacat
100
	
101
	CNReplication
102
		setReplicationStatus() - Not exposed
103
		updateReplicationMetadata() - New method?
104
		setReplicationPolicy() - Not exposed
105
	
106
	CNRegister -- not implemented at all in Metacat
107
 * ******************
108
 * @author leinfelder
109
 *
110
 */
111
public class CNResourceHandler extends D1ResourceHandler {
112

    
113
	/** CN-specific operations **/
114
    protected static final String RESOURCE_RESERVE = "reserve";
115
    protected static final String RESOURCE_FORMATS = "formats";
116
    protected static final String RESOURCE_RESOLVE = "resolve";
117
    protected static final String RESOURCE_ASSERT_RELATION = "assertRelation";
118
    protected static final String RESOURCE_OWNER = "owner";
119
    protected static final String RESOURCE_NOTIFY = "notify";
120
    protected static final String RESOURCE_META_REPLICATION = "meta/replication";
121
    protected static final String RESOURCE_META_POLICY = "meta/policy";
122
	
123
    public CNResourceHandler(ServletContext servletContext,
124
			HttpServletRequest request, HttpServletResponse response) {
125
		super(servletContext, request, response);
126
        logMetacat = Logger.getLogger(CNResourceHandler.class);
127
	}
128

    
129
	/**
130
     * This function is called from REST API servlet and handles each request to the servlet 
131
     * 
132
     * @param httpVerb (GET, POST, PUT or DELETE)
133
     */
134
    @Override
135
    public void handle(byte httpVerb) {
136
    	// prepare the handler
137
    	super.handle(httpVerb);
138
    	
139
        try {
140

    
141
        	// get the resource
142
            String resource = request.getPathInfo();
143
            resource = resource.substring(resource.indexOf("/") + 1);
144
                        
145
            // for the rest of the resouce
146
            String extra = null;
147
            
148
            logMetacat.debug("handling verb " + httpVerb + " request with resource '" + resource + "'");
149
            boolean status = false;
150

    
151
            if (resource != null) {
152

    
153
                if (resource.startsWith(RESOURCE_ACCESS_RULES) && httpVerb == PUT) {
154
                    logMetacat.debug("Setting access policy");
155
                    // after the command
156
                    extra = parseTrailing(resource, RESOURCE_ACCESS_RULES);
157
                    setAccess(extra);
158
                    status = true;
159
                    logMetacat.debug("done setting access");
160
                    
161
                } else if (resource.startsWith(RESOURCE_META)) {
162
                    logMetacat.debug("Using resource: " + RESOURCE_META);
163
                    
164
                    // after the command
165
                    extra = parseTrailing(resource, RESOURCE_META);
166
                    
167
                    // get
168
                    if (httpVerb == GET) {
169
                        getSystemMetadataObject(extra);
170
                        status = true;
171
                    }
172
                    // post to register system metadata
173
                    if (httpVerb == POST) {
174
                    	registerSystemMetadata(extra);
175
                    	status = true;
176
                    }
177

    
178
                } else if (resource.startsWith(RESOURCE_RESERVE)) {
179
                    // reserve the ID (in params)
180
                    if (httpVerb == POST) {
181
                    	reserve();
182
                    	status = true;
183
                    }
184
                } else if (resource.startsWith(RESOURCE_ASSERT_RELATION)) {
185
                	
186
                	// after the command
187
                    extra = parseTrailing(resource, RESOURCE_ASSERT_RELATION);
188
                    
189
                    // reserve the ID (in params)
190
                    if (httpVerb == GET) {
191
                    	assertRelation(extra);
192
                    	status = true;
193
                    }    
194
                } else if (resource.startsWith(RESOURCE_RESOLVE)) {
195
                	
196
                	// after the command
197
                    extra = parseTrailing(resource, RESOURCE_RESOLVE);
198
                    
199
                    // resolve the object location
200
                    if (httpVerb == GET) {
201
                    	resolve(extra);
202
                    	status = true;
203
                    }
204
                } else if (resource.startsWith(RESOURCE_OWNER)) {
205
                	
206
                	// after the command
207
                    extra = parseTrailing(resource, RESOURCE_OWNER);
208
                    
209
                    // set the owner
210
                    if (httpVerb == PUT) {
211
                    	owner(extra);
212
                    	status = true;
213
                    }    
214
                } else if (resource.startsWith(RESOURCE_IS_AUTHORIZED)) {
215
                	
216
                	// after the command
217
                    extra = parseTrailing(resource, RESOURCE_IS_AUTHORIZED);
218
                    
219
                    // authorized?
220
                    if (httpVerb == GET) {
221
                    	isAuthorized(extra);
222
                    	status = true;
223
                    }    
224
                } else if (resource.startsWith(RESOURCE_OBJECTS)) {
225
                    logMetacat.debug("Using resource 'object'");
226
                    logMetacat.debug("D1 Rest: Starting resource processing...");
227
                    
228
                    // after the command
229
                    extra = parseTrailing(resource, RESOURCE_OBJECTS);
230
                    
231
                    logMetacat.debug("objectId: " + extra);
232
                    logMetacat.debug("verb:" + httpVerb);
233

    
234
                    if (httpVerb == GET) {
235
                    	if (extra != null) {
236
                    		getObject(extra);
237
                    	} else {
238
                    		query();
239
                    	}
240
                        status = true;
241
                    } else if (httpVerb == POST) {
242
                        putObject(extra, FUNCTION_NAME_INSERT);
243
                        status = true;
244
                    }
245
                    
246
                } else if (resource.startsWith(RESOURCE_FORMATS)) {
247
                  logMetacat.debug("Using resource: " + RESOURCE_FORMATS);
248
                  
249
                  // after the command
250
                  extra = parseTrailing(resource, RESOURCE_FORMATS);
251
                  
252
                  // handle each verb
253
                  if (httpVerb == GET) {
254
                  	if (extra == null) {
255
                  		// list the formats collection
256
                  		listFormats();
257
                  	} else {
258
                  		// get the specified format
259
                  		getFormat(extra);
260
                  	}
261
                  	status = true;
262
                  }
263
                  
264
                } else if (resource.startsWith(RESOURCE_LOG)) {
265
                    logMetacat.debug("Using resource: " + RESOURCE_LOG);
266
                    //handle log events
267
                    if (httpVerb == GET) {
268
                        getLog();
269
                        status = true;
270
                    }
271

    
272
                } else if (resource.startsWith(RESOURCE_CHECKSUM)) {
273
                    logMetacat.debug("Using resource: " + RESOURCE_CHECKSUM);
274
                    
275
                    // after the command
276
                    extra = parseTrailing(resource, RESOURCE_CHECKSUM);
277
                    
278
                    //handle checksum requests
279
                    if (httpVerb == GET) {
280
                    
281
                        checksum(extra);
282
                        status = true;
283
                    }
284
                } 
285
                    
286
                if (!status) {
287
                	throw new ServiceFailure("0000", "Unknown error, status = " + status);
288
                }
289
            } else {
290
            	throw new InvalidRequest("0000", "No resource matched for " + resource);
291
            }
292
        } catch (BaseException be) {
293
        	// report Exceptions as clearly and generically as possible
294
        	OutputStream out = null;
295
			try {
296
				out = response.getOutputStream();
297
			} catch (IOException ioe) {
298
				logMetacat.error("Could not get output stream from response", ioe);
299
			}
300
            serializeException(be, out);
301
        } catch (Exception e) {
302
            // report Exceptions as clearly and generically as possible
303
            logMetacat.error(e.getClass() + ": " + e.getMessage(), e);
304
        	OutputStream out = null;
305
			try {
306
				out = response.getOutputStream();
307
			} catch (IOException ioe) {
308
				logMetacat.error("Could not get output stream from response", ioe);
309
			}
310
			ServiceFailure se = new ServiceFailure("0000", e.getMessage());
311
            serializeException(se, out);
312
        }
313
    }
314
    
315
    
316
    /**
317
     * Get the checksum for the given guid
318
     * 
319
     * @param guid
320
     * @throws NotImplemented 
321
     * @throws InvalidRequest 
322
     * @throws NotFound 
323
     * @throws NotAuthorized 
324
     * @throws ServiceFailure 
325
     * @throws InvalidToken 
326
     * @throws IOException 
327
     * @throws JiBXException 
328
     */
329
    private void checksum(String guid) throws InvalidToken, ServiceFailure, NotAuthorized, NotFound, InvalidRequest, NotImplemented, JiBXException, IOException {
330
    	Identifier guidid = new Identifier();
331
        guidid.setValue(guid);
332
        logMetacat.debug("getting checksum for object " + guid);
333
        Checksum c = CNodeService.getInstance(request).getChecksum(session, guidid);
334
        logMetacat.debug("got checksum " + c.getValue());
335
        response.setStatus(200);
336
        logMetacat.debug("serializing response");
337
        TypeMarshaller.marshalTypeToOutputStream(c, response.getOutputStream());
338
        logMetacat.debug("done serializing response.");
339
        
340
    }
341
    
342
	/**
343
     * get the logs based on passed params.  Available 
344
     * params are token, fromDate, toDate, event.  See 
345
     * http://mule1.dataone.org/ArchitectureDocs/mn_api_crud.html#MN_crud.getLogRecords
346
     * for more info
347
	 * @throws NotImplemented 
348
	 * @throws InvalidRequest 
349
	 * @throws NotAuthorized 
350
	 * @throws ServiceFailure 
351
	 * @throws InvalidToken 
352
	 * @throws IOException 
353
	 * @throws JiBXException 
354
     */
355
    private void getLog() throws InvalidToken, ServiceFailure, NotAuthorized, InvalidRequest, NotImplemented, IOException, JiBXException
356
    {
357
        
358
        Date fromDate = null;
359
        Date toDate = null;
360
        Event event = null;
361
        Integer start = null;
362
        Integer count = null;
363
        
364
        try {
365
        	String fromDateS = params.get("fromDate")[0];
366
            logMetacat.debug("param fromDateS: " + fromDateS);
367
            fromDate = DateTimeMarshaller.deserializeDateToUTC(fromDateS);
368
        } catch (Exception e) {
369
        	logMetacat.warn("Could not parse fromDate: " + e.getMessage());
370
        }
371
        try {
372
        	String toDateS = params.get("toDate")[0];
373
            logMetacat.debug("param toDateS: " + toDateS);
374
            toDate = DateTimeMarshaller.deserializeDateToUTC(toDateS);
375
        } catch (Exception e) {
376
        	logMetacat.warn("Could not parse toDate: " + e.getMessage());
377
		}
378
        try {
379
        	String eventS = params.get("event")[0];
380
            event = Event.convert(eventS);
381
        } catch (Exception e) {
382
        	logMetacat.warn("Could not parse event: " + e.getMessage());
383
		}
384
        logMetacat.debug("fromDate: " + fromDate + " toDate: " + toDate);
385
        
386
        try {
387
        	start =  Integer.parseInt(params.get("start")[0]);
388
        } catch (Exception e) {
389
			logMetacat.warn("Could not parse start: " + e.getMessage());
390
		}
391
        try {
392
        	count =  Integer.parseInt(params.get("count")[0]);
393
        } catch (Exception e) {
394
			logMetacat.warn("Could not parse count: " + e.getMessage());
395
		}
396
        
397
        logMetacat.debug("calling getLogRecords");
398
        Log log = CNodeService.getInstance(request).getLogRecords(session, fromDate, toDate, event, start, count);
399
        
400
        OutputStream out = response.getOutputStream();
401
        response.setStatus(200);
402
        response.setContentType("text/xml");
403
        
404
        TypeMarshaller.marshalTypeToOutputStream(log, out);
405
  
406
    }
407

    
408
    /**
409
     * Implements REST version of DataONE CRUD API --> get
410
     * @param guid ID of data object to be read
411
     * @throws NotImplemented 
412
     * @throws InvalidRequest 
413
     * @throws NotFound 
414
     * @throws NotAuthorized 
415
     * @throws ServiceFailure 
416
     * @throws InvalidToken 
417
     * @throws IOException 
418
     */
419
    protected void getObject(String guid) throws InvalidToken, ServiceFailure, NotAuthorized, NotFound, InvalidRequest, NotImplemented, IOException {
420

    
421
        Identifier id = new Identifier();
422
        id.setValue(guid);
423
            
424
        SystemMetadata sm = CNodeService.getInstance(request).getSystemMetadata(session, id);
425
        
426
        //set the content type
427
        if(sm.getFormatId().getValue().trim().equals(
428
        		ObjectFormatCache.getInstance().getFormat("text/csv").getFormatId().getValue()))
429
        {
430
            response.setContentType("text/csv");
431
            response.setHeader("Content-Disposition", "inline; filename=" + id.getValue() + ".csv");
432
        }
433
        else if(sm.getFormatId().getValue().trim().equals(
434
        		ObjectFormatCache.getInstance().getFormat("text/plain").getFormatId().getValue()))
435
        {
436
            response.setContentType("text/plain");
437
            response.setHeader("Content-Disposition", "inline; filename=" + id.getValue() + ".txt");
438
        } 
439
        else if(sm.getFormatId().getValue().trim().equals(
440
        		ObjectFormatCache.getInstance().getFormat("application/octet-stream").getFormatId().getValue()))
441
        {
442
            response.setContentType("application/octet-stream");
443
        }
444
        else
445
        {
446
            response.setContentType("text/xml");
447
            response.setHeader("Content-Disposition", "inline; filename=" + id.getValue() + ".xml");
448
        }
449
        
450
        InputStream data = CNodeService.getInstance(request).get(session, id);
451
        
452
        OutputStream out = response.getOutputStream();
453
        response.setStatus(200);
454
        IOUtils.copyLarge(data, out);
455
            
456
    }
457
    
458

    
459
    /**
460
     * Implements REST version of DataONE CRUD API --> getSystemMetadata
461
     * @param guid ID of data object to be read
462
     * @throws NotImplemented 
463
     * @throws InvalidRequest 
464
     * @throws NotFound 
465
     * @throws NotAuthorized 
466
     * @throws ServiceFailure 
467
     * @throws InvalidToken 
468
     * @throws IOException 
469
     * @throws JiBXException 
470
     */
471
    protected void getSystemMetadataObject(String guid) throws InvalidToken, ServiceFailure, NotAuthorized, NotFound, InvalidRequest, NotImplemented, IOException, JiBXException {
472

    
473
        Identifier id = new Identifier();
474
        id.setValue(guid);
475
        SystemMetadata sysmeta = CNodeService.getInstance(request).getSystemMetadata(session, id);
476
        
477
        response.setContentType("text/xml");
478
        response.setStatus(200);
479
        OutputStream out = response.getOutputStream();
480
        
481
        // Serialize and write it to the output stream
482
        TypeMarshaller.marshalTypeToOutputStream(sysmeta, out);
483
   }
484
    
485
    /**
486
     * Earthgrid API > Put Service >Put Function : calls MetacatHandler > handleInsertOrUpdateAction 
487
     * 
488
     * @param guid - ID of data object to be inserted or updated.  If action is update, the pid
489
     *               is the existing pid.  If insert, the pid is the new one
490
     * @throws InvalidRequest 
491
     * @throws ServiceFailure 
492
     * @throws IdentifierNotUnique 
493
     * @throws JiBXException 
494
     * @throws NotImplemented 
495
     * @throws InvalidSystemMetadata 
496
     * @throws InsufficientResources 
497
     * @throws UnsupportedType 
498
     * @throws NotAuthorized 
499
     * @throws InvalidToken 
500
     * @throws IOException 
501
     * @throws IllegalAccessException 
502
     * @throws InstantiationException 
503
     */
504
    protected void putObject(String pid, String action) throws ServiceFailure, InvalidRequest, IdentifierNotUnique, JiBXException, InvalidToken, NotAuthorized, UnsupportedType, InsufficientResources, InvalidSystemMetadata, NotImplemented, IOException, InstantiationException, IllegalAccessException {
505
        logMetacat.debug("Entering putObject: " + pid + "/" + action);
506
        
507
        // Read the incoming data from its Mime Multipart encoding
508
    	Map<String, File> files = collectMultipartFiles();
509
        InputStream object = null;
510
        InputStream sysmeta = null;
511

    
512
        File smFile = files.get("sysmeta");
513
        sysmeta = new FileInputStream(smFile);
514
        File objFile = files.get("object");
515
        object = new FileInputStream(objFile);
516
       
517
        if (action.equals(FUNCTION_NAME_INSERT)) { //handle inserts
518

    
519
            logMetacat.debug("Commence creation...");
520
            SystemMetadata smd = TypeMarshaller.unmarshalTypeFromStream(SystemMetadata.class, sysmeta);
521

    
522
            Identifier id = new Identifier();
523
            id.setValue(pid);
524
            logMetacat.debug("creating object with pid " + id.getValue());
525
            Identifier rId = CNodeService.getInstance(request).create(session, id, object, smd);
526
            
527
            OutputStream out = response.getOutputStream();
528
            response.setStatus(200);
529
            response.setContentType("text/xml");
530
            
531
            TypeMarshaller.marshalTypeToOutputStream(rId, out);
532
            
533
        } else {
534
            throw new InvalidRequest("1000", "Operation must be create.");
535
        }
536
    }
537

    
538
    /**
539
     * List the object formats registered with the system
540
     * @throws NotImplemented 
541
     * @throws InsufficientResources 
542
     * @throws NotFound 
543
     * @throws ServiceFailure 
544
     * @throws InvalidRequest 
545
     * @throws IOException 
546
     * @throws JiBXException 
547
     */
548
	private void listFormats() throws InvalidRequest, ServiceFailure, NotFound, InsufficientResources, NotImplemented, IOException, JiBXException {
549
      logMetacat.debug("Entering listFormats()");
550

    
551
      ObjectFormatList objectFormatList = CNodeService.getInstance(request).listFormats();
552
      // get the response output stream
553
      OutputStream out = response.getOutputStream();
554
      response.setStatus(200);
555
      response.setContentType("text/xml");
556
      
557
      TypeMarshaller.marshalTypeToOutputStream(objectFormatList, out);
558
            
559
    }
560

    
561
		/**
562
     * Return the requested object format
563
     * 
564
     * @param fmtidStr the requested format identifier as a string
565
		 * @throws NotImplemented 
566
		 * @throws InsufficientResources 
567
		 * @throws NotFound 
568
		 * @throws ServiceFailure 
569
		 * @throws InvalidRequest 
570
		 * @throws IOException 
571
		 * @throws JiBXException 
572
     */
573
    private void getFormat(String fmtidStr) throws InvalidRequest, ServiceFailure, NotFound, InsufficientResources, NotImplemented, IOException, JiBXException {
574
      logMetacat.debug("Entering listFormats()");
575
      
576
      ObjectFormatIdentifier fmtid = new ObjectFormatIdentifier();
577
      fmtid.setValue(fmtidStr);
578
      
579
	  // get the specified object format
580
      ObjectFormat objectFormat = CNodeService.getInstance(request).getFormat(fmtid);
581
      
582
      OutputStream out = response.getOutputStream();
583
      response.setStatus(200);
584
      response.setContentType("text/xml");
585
      
586
      TypeMarshaller.marshalTypeToOutputStream(objectFormat, out);
587
      
588
    }
589
    
590
    /**
591
     * Reserve the given Identifier
592
     * @throws InvalidToken
593
     * @throws ServiceFailure
594
     * @throws NotAuthorized
595
     * @throws IdentifierNotUnique
596
     * @throws NotImplemented
597
     * @throws InvalidRequest
598
     * @throws IOException 
599
     * @throws JiBXException 
600
     */
601
    private void reserve() throws InvalidToken, ServiceFailure, NotAuthorized, IdentifierNotUnique, NotImplemented, InvalidRequest, IOException, JiBXException {
602
		Identifier pid = null;
603
		String scope = null;
604
    	String format = null;
605
    	// gather the params
606
		try {
607
	    	String id = params.get("pid")[0];
608
			pid = new Identifier();
609
			pid.setValue(id);
610
		} catch (Exception e) {
611
			logMetacat.warn("pid not specified");
612
		}
613
		try {
614
			scope = params.get("scope")[0];
615
		} catch (Exception e) {
616
			logMetacat.warn("pid not specified");
617
		}
618
		try {
619
			format = params.get("format")[0];
620
		} catch (Exception e) {
621
			logMetacat.warn("pid not specified");
622
		} 
623
		// call the implementation
624
		boolean result = CNodeService.getInstance(request).reserveIdentifier(session, pid);
625
		OutputStream out = response.getOutputStream();
626
		response.setStatus(200);
627
		response.setContentType("text/xml");
628
		// nothing to send back
629
    }
630
    
631
    /**
632
     * 
633
     * @param id
634
     * @throws InvalidRequest
635
     * @throws InvalidToken
636
     * @throws ServiceFailure
637
     * @throws NotAuthorized
638
     * @throws NotFound
639
     * @throws NotImplemented
640
     * @throws IOException
641
     * @throws JiBXException 
642
     */
643
    private void resolve(String id) throws InvalidRequest, InvalidToken, ServiceFailure, NotAuthorized, NotFound, NotImplemented, IOException, JiBXException {
644
		Identifier pid = new Identifier();
645
		pid.setValue(id);
646
		ObjectLocationList locationList = CNodeService.getInstance(request).resolve(session, pid);
647
	    OutputStream out = response.getOutputStream();
648
		response.setStatus(200);
649
		response.setContentType("text/xml");
650
		TypeMarshaller.marshalTypeToOutputStream(locationList, out);
651
		
652
    }
653

    
654
    /**
655
     * Assert that a relationship exists between two resources
656
     * @param id
657
     * @return
658
     * @throws InvalidToken
659
     * @throws ServiceFailure
660
     * @throws NotAuthorized
661
     * @throws NotFound
662
     * @throws InvalidRequest
663
     * @throws NotImplemented
664
     */
665
    private boolean assertRelation(String id) throws InvalidToken, ServiceFailure, NotAuthorized, NotFound, InvalidRequest, NotImplemented {
666
		Identifier pidOfSubject = new Identifier();
667
		pidOfSubject.setValue(id);
668
		String relationship = null;
669
		try {
670
			relationship = params.get("relationship")[0];
671
		} catch (Exception e) {
672
			logMetacat.warn("relationship not specified");
673
		}
674
		Identifier pidOfObject = new Identifier();
675
		try {
676
			String objPid = params.get("pidOfObject")[0];
677
			pidOfObject.setValue(objPid);
678
		} catch (Exception e) {
679
			logMetacat.warn("pidOfObject not specified");
680
		}
681
		boolean result = CNodeService.getInstance(request).assertRelation(session, pidOfSubject, relationship, pidOfObject);
682
		response.setStatus(200);
683
		response.setContentType("text/xml");
684
		return result;
685
    }
686
    
687
    /**
688
     * Set the owner of a resource
689
     * @param id
690
     * @throws JiBXException
691
     * @throws InvalidToken
692
     * @throws ServiceFailure
693
     * @throws NotFound
694
     * @throws NotAuthorized
695
     * @throws NotImplemented
696
     * @throws InvalidRequest
697
     * @throws IOException
698
     * @throws IllegalAccessException 
699
     * @throws InstantiationException 
700
     */
701
    private void owner(String id) throws JiBXException, InvalidToken, ServiceFailure, NotFound, NotAuthorized, NotImplemented, InvalidRequest, IOException, InstantiationException, IllegalAccessException {
702
		Identifier pid = new Identifier();
703
		pid.setValue(id);
704
		String subjectStr = params.get("subject")[0];
705
		Subject subject = TypeMarshaller.unmarshalTypeFromStream(Subject.class, new ByteArrayInputStream(subjectStr.getBytes("UTF-8")));
706
		Identifier retPid = CNodeService.getInstance(request).setOwner(session, pid, subject);
707
		OutputStream out = response.getOutputStream();
708
		response.setStatus(200);
709
		response.setContentType("text/xml");
710
		TypeMarshaller.marshalTypeToOutputStream(retPid, out);		
711
    }
712
    
713
    /**
714
     * Processes the authorization check for given id
715
     * @param id
716
     * @return
717
     * @throws ServiceFailure
718
     * @throws InvalidToken
719
     * @throws NotFound
720
     * @throws NotAuthorized
721
     * @throws NotImplemented
722
     * @throws InvalidRequest
723
     */
724
    private boolean isAuthorized(String id) throws ServiceFailure, InvalidToken, NotFound, NotAuthorized, NotImplemented, InvalidRequest {
725
		Identifier pid = new Identifier();
726
		pid.setValue(id);
727
		String permission = params.get("action")[0];
728
		boolean result = CNodeService.getInstance(request).isAuthorized(session, pid, Permission.convert(permission));
729
		response.setStatus(200);
730
		response.setContentType("text/xml");
731
		return result;
732
    }
733
    
734
    /**
735
     * Register System Metadata without data or metadata object
736
     * @param pid identifier for System Metadata entry
737
     * @throws JiBXException 
738
     * @throws FileUploadException 
739
     * @throws IOException 
740
     * @throws InvalidRequest 
741
     * @throws ServiceFailure 
742
     * @throws InvalidSystemMetadata 
743
     * @throws NotAuthorized 
744
     * @throws NotImplemented 
745
     * @throws IllegalAccessException 
746
     * @throws InstantiationException 
747
     */
748
    protected Identifier registerSystemMetadata(String pid) throws ServiceFailure, InvalidRequest, IOException, FileUploadException, JiBXException, NotImplemented, NotAuthorized, InvalidSystemMetadata, InstantiationException, IllegalAccessException {
749
		logMetacat.debug("Entering registerSystemMetadata: " + pid);
750

    
751
		// get the system metadata from the request
752
		SystemMetadata systemMetadata = collectSystemMetadata();
753

    
754
		Identifier guid = new Identifier();
755
		guid.setValue(pid);
756
		logMetacat.debug("registering system metadata with pid " + guid.getValue());
757
		Identifier retGuid = CNodeService.getInstance(request).registerSystemMetadata(session, guid, systemMetadata);
758
		
759
		response.setStatus(200);
760
		response.setContentType("text/xml");
761
		return retGuid;
762
			
763
	}
764
    
765
    /**
766
     * set the access perms on a document
767
     * @throws JiBXException 
768
     * @throws InvalidRequest 
769
     * @throws NotImplemented 
770
     * @throws NotAuthorized 
771
     * @throws NotFound 
772
     * @throws ServiceFailure 
773
     * @throws InvalidToken 
774
     * @throws IllegalAccessException 
775
     * @throws InstantiationException 
776
     * @throws IOException 
777
     * @throws SAXException 
778
     * @throws ParserConfigurationException 
779
     */
780
    protected void setAccess(String pid) throws JiBXException, InvalidToken, ServiceFailure, NotFound, NotAuthorized, NotImplemented, InvalidRequest, IOException, InstantiationException, IllegalAccessException, ParserConfigurationException, SAXException {
781

    
782
        Identifier id = new Identifier();
783
        id.setValue(pid);
784
        
785
        AccessPolicy accessPolicy = collectAccessPolicy();
786
        CNodeService.getInstance(request).setAccessPolicy(session, id, accessPolicy);
787

    
788
    }
789
    
790
    /**
791
     *	Pass to the CN search service
792
     *
793
     * @throws NotImplemented 
794
     * @throws InvalidRequest 
795
     * @throws NotAuthorized 
796
     * @throws ServiceFailure 
797
     * @throws InvalidToken 
798
     * @throws Exception
799
     */
800
    private void query() throws InvalidToken, ServiceFailure, NotAuthorized, InvalidRequest, NotImplemented {
801
        
802
    	String query = null;
803
		String queryType = null;
804
		try {
805
			query = params.get("query")[0];
806
		} catch (Exception e) {
807
			logMetacat.warn("query not specified");
808
		}
809
		try {
810
			String qt = params.get("queryType")[0];
811
			queryType = qt;
812
		} catch (Exception e) {
813
			logMetacat.warn("queryType not specified");
814
		}
815
		
816
    	// expecting to throw NotImplemented
817
		CNodeService.getInstance(request).search(session, queryType, query);
818
    }
819

    
820
}
(1-1/9)