Project

General

Profile

1
/**
2
 *  '$RCSfile$'
3
 *  Copyright: 2000 Regents of the University of California and the
4
 *              National Center for Ecological Analysis and Synthesis
5
 *
6
 *   '$Author: Serhan AKIN $'
7
 *     '$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.restservice;
24

    
25
import java.io.*;
26
import java.util.*;
27

    
28
import javax.mail.BodyPart;
29
import javax.mail.MessagingException;
30
import javax.mail.internet.MimeMultipart;
31
import javax.servlet.ServletContext;
32
import javax.servlet.http.HttpServletRequest;
33
import javax.servlet.http.HttpServletResponse;
34
import java.text.DateFormat;
35
import java.text.ParseException;
36
import java.text.ParsePosition;
37
import java.text.SimpleDateFormat;
38

    
39

    
40
import org.apache.commons.httpclient.util.DateParser;
41
import org.apache.commons.io.IOUtils;
42
import org.apache.log4j.Logger;
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.NotImplemented;
51
import org.dataone.service.exceptions.ServiceFailure;
52
import org.dataone.service.exceptions.UnsupportedType;
53
import org.dataone.service.exceptions.NotFound;
54
import org.dataone.service.types.*;
55
import org.jibx.runtime.BindingDirectory;
56
import org.jibx.runtime.IBindingFactory;
57
import org.jibx.runtime.IMarshallingContext;
58
import org.jibx.runtime.IUnmarshallingContext;
59
import org.jibx.runtime.JiBXException;
60

    
61
import edu.ucsb.nceas.metacat.DBUtil;
62
import edu.ucsb.nceas.metacat.IdentifierManager;
63
import edu.ucsb.nceas.metacat.McdbDocNotFoundException;
64
import edu.ucsb.nceas.metacat.MetacatHandler;
65
import edu.ucsb.nceas.metacat.client.InsufficientKarmaException;
66
import edu.ucsb.nceas.metacat.dataone.CrudService;
67
import edu.ucsb.nceas.metacat.service.SessionService;
68
import edu.ucsb.nceas.metacat.util.RequestUtil;
69
import edu.ucsb.nceas.metacat.util.SessionData;
70

    
71
/**
72
 * 
73
 * Implements Earthgrid REST API to Metacat <br/><br/> 
74
 * 
75
 * <ul>
76
 * <li>
77
 * <h3> EarthGrid Query Service</h3>
78
 * <ul><li>
79
 * <h3>Get & Authenticated Get:</h3> 
80
 * is equal to Metacat's read action and returns a data file having
81
 * the specified <doc-id> in the resource path. For authenticated Get service, a session id must be provided 
82
 * in the query string. <br/><br/>
83
 * 
84
 * <b>REST URL:</b>	<code>GET, [context-root]/object/[doc-id]?sessionid=[sessionid] </code><br/>
85
 * <b>Returns:</b> data file <br/><br/>
86
 * </li>
87
 * 
88
 * <li>
89
 * <h3>Query & Authenticated Query:</h3> 
90
 * Metacat's equivalent is squery action but this function 
91
 * receives a Earthgrid query document and returns Earthgrid resultset document by transforming those documents
92
 * to Metacat's equivalents by the means of Metacat Implementation in Earthgrid library. For authenticated Query service 
93
 * a session id must be provided in the query string. See Earthgrid (a.k.a. Ecogrid) project for XSD files of 
94
 * query and resultset documents<br/><br/>
95
 * 
96
 * <b>REST URL:</b>	<code>POST, [context-root]/object?sessionid=[sessionid]</code>    <br/>
97
 * <b>POST Data:</b> Earthgrid query document , Content-type: <code>text/xml</code><br/>
98
 * <b>Returns:</b> Earthgrid resultset document<br/><br/>
99
 * 
100
 * </li>
101
 * </ul>
102
 * 
103
 * </li>
104
 * 
105
 * <li>
106
 * <h3> EarthGrid Authentication Service</h3>
107
 * <ul><li>
108
 * <h3>Login: </h3> 
109
 * Receives username and password parameters in POST data and 
110
 * returns SessionId (in XML format) or failure message and uses MetacatHandler's handleLoginAction function<br/><br/>
111
 *  
112
 * <b>REST URL:</b> <code>POST, [context-root]/session?op=login</code> <br/>
113
 * <b>POST Data:</b> username=[username]&password=[password], Content-type: <code>application/x-www-form-urlencoded</code>
114
 * <b>Returns:</b> sessionId in XML format<br/><br/>
115
 * </li>
116
 * 
117
 * <li>
118
 * <h3>Logout: </h3> 
119
 * Receives session Id parameters in querystring and returns xml message, calls 
120
 * MetacatHandler's handleLogoutAction function<br/><br/>
121
 *  
122
 * <b>REST URL:</b>	<code>GET, [context-root]/session?op=logout&sessionid=[sessionid]</code>   <br/>
123
 * <b>Returns:</b> message in XML format<br/><br/>
124
 * </li>
125
 * </ul>
126
 * 
127
 * <li>
128
 * <h3>EarthGrid Put Service</h3>
129
 * 
130
 * <ul>
131
 * <li><h3>Update/Insert: </h3>		
132
 * <br/>
133
 * <b>REST URL:</b>	<code>PUT, [context-root]/object/[doc-id]?op={update|insert}&sessionid=[sessionid]</code>   <br/>
134
 * <b>POST Data:</b> document object, Content-type: <code>text/xml</code><br/>
135
 * <b>Returns:</b> message in XML format<br/><br/>
136
 * </li>
137
 * 
138
 * <li><h3>Delete: </h3>		
139
 * <br/>
140
 * <b>REST URL:</b>	<code>DELETE, [context-root]/object/[doc-id]?sessionid=[sessionid]</code>   <br/>
141
 * <b>Returns:</b> message in XML format<br/><br/>
142
 * </li>
143

    
144
 * </ul>
145
 * </li>
146
 * 
147
 * <li>
148
 * <h3>EarthGrid Identifier Service</h3><br/>
149
 * 
150
 * <ul>
151
 * <li><h3>isRegistered: </h3>		<br/>
152
 * <b>REST URL:</b>	<code>GET, [context-root]/identifier/[doc-id]?op=isregistered</code>   <br/>
153
 * <b>Returns:</b> message in XML format<br/><br/>
154
 * </li>
155

    
156
 * <li><h3>getAllDocIds:</h3>		<br/>		
157
 * <b>REST URL:</b>	<code>GET, [context-root]/identifier?op=getalldocids</code>   <br/>
158
 * <b>Returns:</b> document id list in XML format<br/><br/>
159
 * </li>
160
 * 
161
 * <li><h3>addLSID Function:</h3> 
162
 * Metacat does not support this function 		<br/>
163
 * <b>REST URL:</b>	<code>PUT, [context-root]/identifier/[doc-id]</code>   <br/>
164
 * <b>Returns:</b> error message in XML format<br/><br/>
165
 * </li>
166
 * 
167
 * <li><h3>getNextRevision:</h3>		<br/>
168
 * <b>REST URL:</b>	<code>GET, [context-root]/identifier/[doc-id]?op=getnextrevision</code>   <br/>
169
 * <b>Returns:</b> message in XML format<br/><br/>
170
 * </li>
171
 * 
172
 * <li><h3>getNextObject:</h3>		<br/>
173
 * <b>REST URL:</b>	<code>GET, [context-root]/identifier?op=getnextobject&scope=[scope]</code>   <br/>
174
 * <b>Returns:</b> message in XML format<br/><br/>
175
 * </li>
176
 * 
177
 * </li>
178
 * </ul>
179
 * 
180
 */
181
public class ResourceHandler {
182

    
183
    /**HTTP Verb GET*/
184
    public static final byte GET = 1;
185
    /**HTTP Verb POST*/
186
    public static final byte POST = 2;
187
    /**HTTP Verb PUT*/
188
    public static final byte PUT = 3;
189
    /**HTTP Verb DELETE*/
190
    public static final byte DELETE = 4;
191

    
192
    /*
193
     * API Resources
194
     */
195
    private static final String RESOURCE_OBJECTS = "object";
196
    private static final String RESOURCE_META = "meta";
197
    private static final String RESOURCE_SESSION = "session";
198
    private static final String RESOURCE_IDENTIFIER = "identifier";
199
    private static final String RESOURCE_LOG = "log";
200

    
201
    /*
202
     * API Functions used as URL parameters
203
     */
204
    private static final String FUNCTION_KEYWORD = "op";
205
    private static final String FUNCTION_NAME_LOGIN = "login";
206
    private static final String FUNCTION_NAME_LOGOUT = "logout";
207
    private static final String FUNCTION_NAME_SET_ACCESS = "setaccess";
208
    private static final String FUNCTION_NAME_ISREGISTERED = "isregistered";
209
    private static final String FUNCTION_NAME_GETALLDOCS = "getalldocids";
210
    private static final String FUNCTION_NAME_GETNEXTREV = "getnextrevision";
211
    private static final String FUNCTION_NAME_GETNEXTOBJ = "getnextobject";
212
    private static final String FUNCTION_NAME_INSERT = "insert";
213
    private static final String FUNCTION_NAME_UPDATE = "update";
214
    private static final String FUNCTION_NAME_GENERATE_MISSING_SYSTEM_METADATA = "generatemissingsystemmetadata";
215

    
216
    private static final DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
217
    
218
    private ServletContext servletContext;
219
    private Logger logMetacat;
220
    private MetacatHandler handler;
221
    private HttpServletRequest request;
222
    private HttpServletResponse response;
223
    private String username;
224
    private String password;
225
    private String sessionId;
226
    private String[] groupNames;
227

    
228
    private Hashtable<String, String[]> params;
229

    
230
    /**Initializes new instance by setting servlet context,request and response*/
231
    public ResourceHandler(ServletContext servletContext,
232
            HttpServletRequest request, HttpServletResponse response) {
233
        this.servletContext = servletContext;
234
        this.request = request;
235
        this.response = response;
236
    }
237

    
238
    /**
239
     * This function is called from REST APU servlet and handles each request to the servlet 
240
     * 
241
     * @param httpVerb (GET, POST, PUT or DELETE)
242
     */
243
    public void handle(byte httpVerb) {
244
        logMetacat = Logger.getLogger(ResourceHandler.class);
245
        try {
246
            String resource = request.getServletPath();
247
            String verb = "";
248
            
249
            System.out.println("handling verb " + httpVerb + " request with resource " + resource);
250
            boolean status = false;
251
            loadSessionData();
252

    
253
            if (resource != null) {
254
            	resource = request.getServletPath().substring(1);
255

    
256
            	params = new Hashtable<String, String[]>();
257
            	initParams();
258

    
259
            	Timer timer = new Timer();
260
            	handler = new MetacatHandler(timer);
261

    
262
            	if (resource.equals(RESOURCE_SESSION) && 
263
            			httpVerb == POST && 
264
            			params.get(FUNCTION_KEYWORD) != null) {
265
            		//System.out.println("function_keyword: " + params.get(FUNCTION_KEYWORD)[0]);
266
            		if (params.get(FUNCTION_KEYWORD)[0].equals(FUNCTION_NAME_LOGIN)) {
267
            			login();
268
            			status = true;
269
            		} else if (params.get(FUNCTION_KEYWORD)[0].equals(FUNCTION_NAME_LOGOUT)) {
270
            			logout();
271
            			status = true;
272
            		} else if (params.get(FUNCTION_KEYWORD)[0].equals(FUNCTION_NAME_SET_ACCESS)) {
273
            			setaccess();
274
            			status = true;
275
            			//System.out.println("done setting access");
276
            		}
277
            	} else if (resource.equals(RESOURCE_META)) {
278
            		if(params != null && params.get(FUNCTION_KEYWORD) != null &&
279
            				params.get(FUNCTION_KEYWORD)[0].equals(FUNCTION_NAME_GENERATE_MISSING_SYSTEM_METADATA))
280
            		{ //generate system metadata for any object that is
281
            			//a) not system metadata itself
282
            			//b) does not already have a system metadata id in the systemmetadata table
283
            			//c) not a BIN object (data)
284
            		    //TODO: check if we need this anymore.  Might be superceded
285
            		    //by MetacatPopulator
286
            			generateMissingSystemMetadata();
287
            			status = true;
288
            		}
289
            		else
290
            		{
291
            			String objectId = request.getPathInfo();
292
            			if (objectId != null && objectId.length() > 1) 
293
            			{
294
            				objectId = request.getPathInfo().substring(1);
295
            			}
296
            			getSystemMetadataObject(objectId);
297
            			status = true;
298
            		}
299

    
300
            	} else if (resource.equals(RESOURCE_OBJECTS)) {
301
            		logMetacat.debug("D1 Rest: Starting resource processing...");
302
            		loadSessionData();
303

    
304
            		String objectId = request.getPathInfo();
305
            		if (objectId != null && objectId.length() > 1) 
306
            		{
307
            			objectId = request.getPathInfo().substring(1);
308
            		}
309
            		else
310
            		{
311
            			objectId = null;
312
            		}
313

    
314
            		logMetacat.debug("verb:" + httpVerb);
315

    
316
            		if (httpVerb == GET) {
317
            			getObject(objectId);
318
            			status = true;
319
            		} else if (httpVerb == POST) {
320
            			putObject(objectId, FUNCTION_NAME_INSERT);
321
            			status = true;
322
            		} else if (httpVerb == PUT) {
323
            			putObject(objectId, FUNCTION_NAME_UPDATE);
324
            			status = true;
325
            		} else if (httpVerb == DELETE) {
326
            			deleteObject(objectId);
327
            			status = true;
328
            		}
329

    
330
            	} else if (resource.equals(RESOURCE_IDENTIFIER)) {
331

    
332
            		String identifierId = request.getPathInfo();
333
            		if (identifierId != null && identifierId.length() > 1)
334
            			identifierId = request.getPathInfo().substring(1); //trim the slash
335

    
336
            		if (httpVerb == GET) {
337
            			String op = params.get(FUNCTION_KEYWORD)[0];
338
            			if (op.equals(FUNCTION_NAME_ISREGISTERED)) {
339
            				isRegistered(identifierId);
340
            				status = true;
341
            			} else if (op.equals(FUNCTION_NAME_GETALLDOCS)) {
342
            				getAllDocIds();
343
            				status = true;
344
            			} else if (op.equals(FUNCTION_NAME_GETNEXTREV)) {
345
            				getNextRevision(identifierId);
346
            				status = true;
347
            			} else if (op.equals(FUNCTION_NAME_GETNEXTOBJ)) {
348
            				getNextObject();
349
            				status = true;
350
            			} 
351

    
352
            		} else if (httpVerb == PUT) {
353
            			//Earthgrid API > Identifier Service > addLSID Function 
354
            		    response.setStatus(501);
355
            			printError(
356
            					"This method is not supported by metacat.  To "
357
            					+ "add a new LSID, add a document to metacat.",
358
            					response);
359
            			status = true;
360
            		}
361

    
362
            	} else if (resource.equals(RESOURCE_LOG)) {
363
            		//handle log events
364
            		if(httpVerb == GET)
365
            		{
366
            			getLog();
367
            			status = true;
368
            		}
369
            		else
370
            		{
371
            		    //change to D1 spec for specifying which http methods are allowed for a resource
372
            		    response.setStatus(501);
373
            			printError("POST, PUT, DELETE is not supported for logs.", response);
374
            			status = true;
375
            		}
376

    
377
            	}
378

    
379
            	if (!status)
380
            	{
381
            	    response.setStatus(400);
382
            		printError("Incorrect parameters!", response);
383
            	}
384
            } else {
385
                response.setStatus(400);
386
            	printError("Incorrect resource!", response);
387
            }
388
        } catch (Exception e) {
389
        	logMetacat.error(e.getMessage());
390
        	e.printStackTrace();
391
        }
392
    }
393
    
394
    /**
395
     * get the logs from the CrudService based on passed params.  Available 
396
     * params are token, fromDate, toDate, event.  See 
397
     * http://mule1.dataone.org/ArchitectureDocs/mn_api_crud.html#MN_crud.getLogRecords
398
     * for more info
399
     */
400
    private void getLog()
401
    {
402
        OutputStream out = null;
403
        try
404
        {
405
            out = response.getOutputStream();
406
            response.setStatus(200);
407
            response.setContentType("text/xml");
408
            AuthToken token = new AuthToken(sessionId);
409
            String fromDateS = params.get("fromDate")[0];
410
            System.out.println("param fromDateS: " + fromDateS);
411
            Date fromDate = null;
412
            String toDateS = params.get("toDate")[0];
413
            System.out.println("param toDateS: " + toDateS);
414
            Date toDate = null;
415
            String eventS = params.get("event")[0];
416
            Event event = null;
417
            if(fromDateS != null)
418
            {
419
                //fromDate = dateFormat.parse(fromDateS);
420
                fromDate = parseDateAndConvertToGMT(fromDateS);
421
            }
422
            if(toDateS != null)
423
            {
424
                //toDate = dateFormat.parse(toDateS);
425
                toDate = parseDateAndConvertToGMT(toDateS);
426
            }
427
            if(eventS != null)
428
            {
429
                event = Event.convert(eventS);
430
            }
431
            System.out.println("fromDate: " + fromDate + " toDate: " + toDate);
432
            
433
            System.out.println("calling crudservice.getLogRecords");
434
            Log log = CrudService.getInstance().getLogRecords(token, fromDate, toDate, event);
435
            serializeServiceType(Log.class, log, out);
436
        }
437
        catch(Exception e)
438
        {
439
            String msg = "Could not get logs from CrudService: " + e.getMessage();
440
            ServiceFailure sf = new ServiceFailure("1490", msg);
441
            logMetacat.error(msg);
442
            e.printStackTrace();
443
            serializeException(sf, out);
444
        }
445
    }
446
    
447
    /**
448
     *  copies request parameters to a hashtable which is given as argument to native metacathandler functions  
449
     */
450
    private void initParams() {
451

    
452
        String name = null;
453
        String[] value = null;
454
        Enumeration paramlist = request.getParameterNames();
455
        while (paramlist.hasMoreElements()) {
456
            name = (String) paramlist.nextElement();
457
            value = request.getParameterValues(name);
458
            params.put(name, value);
459
        }
460
    }
461

    
462
    /**
463
     * 
464
     * Load user details of metacat session from the request 
465
     * 
466
     */
467
    private void loadSessionData()
468
      throws Exception
469
    {
470
        SessionData sessionData = RequestUtil.getSessionData(request);
471
        try
472
        {
473
            username = null;
474
            password = null;
475
            groupNames = null;
476
            sessionId = null;
477
            
478
            boolean validSession = false;
479
            SessionService ss = SessionService.getInstance();
480
            System.out.println("sessionData: " + sessionData);
481
            if(sessionData == null)
482
            {
483
                username = "public";
484
                sessionId = "0";
485
                System.out.println("sessiondata is null.  Creating a public session.");
486
                return;
487
            }
488
            
489
            System.out.println("username: " + sessionData.getUserName());
490
            System.out.println("sessionid: " + sessionData.getId());
491
            //validate the session
492
            if(ss.isSessionRegistered(sessionData.getId()) && 
493
               !(sessionData.getUserName().equals("public") || sessionData.getId().equals("0")))
494
            {
495
                validSession = true;
496
            }
497
            
498
            if(validSession)
499
            {
500
                //if the session is valid, set these variables
501
                username = sessionData.getUserName();
502
                password = sessionData.getPassword();
503
                groupNames = sessionData.getGroupNames();
504
                sessionId = sessionData.getId();
505
                System.out.println("setting sessionid to " + sessionId);
506
                System.out.println("username: " + username);
507
            }
508
            
509
            //if the session is not valid or the username is null, set
510
            //username to public
511
            if (username == null) 
512
            {
513
                System.out.println("setting username to public.");
514
                username = "public";
515
            }
516
        }
517
        catch(Exception e)
518
        {
519
            e.printStackTrace();
520
            throw new Exception("Could not load the session data: " + e.getMessage());
521
        }
522
    }
523
    
524
    /**
525
     * generate missing system metadata for any science metadata objects
526
     * that don't already have it. https://trac.dataone.org/ticket/591
527
     * 
528
     * called with POST meta/?op=generatemissingsystemmetadata
529
     */
530
    private void generateMissingSystemMetadata()
531
    {
532
        AuthToken token = new AuthToken(sessionId);
533
        CrudService.getInstance().generateMissingSystemMetadata(token);
534
    }
535

    
536
    /**
537
     *  Earthgrid API > Identifier Service > isRegistered Function : 
538
     *  calls MetacatHandler > handleIdIsRegisteredAction
539
     * @param guid
540
     * @throws IOException
541
     */
542
    private void isRegistered(String guid) throws IOException
543
    {
544
        
545
        // Look up the localId for this guid
546
        IdentifierManager im = IdentifierManager.getInstance();
547
        String localId = "";
548
        try {
549
            localId = im.getLocalId(guid);
550
        } catch (McdbDocNotFoundException e) {
551
            // TODO: Need to return the proper DataONE exception
552
        }
553
        
554
        params.put("docid", new String[] { localId });
555
        PrintWriter out = response.getWriter();
556
        response.setStatus(200);
557
        response.setContentType("text/xml");
558
        handler.handleIdIsRegisteredAction(out, params, response);
559
        out.close();
560
    }
561

    
562
    /**
563
     * Earthgrid API > Identifier Service > getAllDocIds Function : 
564
     * calls MetacatHandler > handleGetAllDocidsAction
565
     * @throws IOException
566
     */
567
    private void getAllDocIds() throws IOException {
568
        PrintWriter out = response.getWriter();
569
        response.setStatus(200);
570
        response.setContentType("text/xml");
571
        handler.handleGetAllDocidsAction(out, params, response);
572
        out.close();
573
    }
574

    
575
    /**
576
     * Earthgrid API > Identifier Service > getNextRevision Function : 
577
     * calls MetacatHandler > handleGetRevisionAndDocTypeAction
578
     * @param guid
579
     * @throws IOException
580
     */
581
    private void getNextRevision(String guid) throws IOException 
582
    {
583
        params.put("docid", new String[] { guid });
584
        PrintWriter out = response.getWriter();
585
        response.setStatus(200);
586
        response.setContentType("text/xml");
587
        //handler.handleGetRevisionAndDocTypeAction(out, params);
588

    
589
        try {
590
            // Make sure there is a docid
591
            if (guid == null || guid.equals("")) {
592
                throw new Exception("User didn't specify docid!");
593
            }
594

    
595
            // Look up the localId for this guid
596
            IdentifierManager im = IdentifierManager.getInstance();
597
            String localId = "";
598
            try {
599
                localId = im.getLocalId(guid);
600
            } catch (McdbDocNotFoundException e) {
601
                // TODO: Need to return the proper DataONE exception
602
            }
603
           
604
            // Create a DBUtil object
605
            DBUtil dbutil = new DBUtil();
606
            // Get a rev and doctype
607
            String revAndDocType = dbutil
608
                    .getCurrentRevisionAndDocTypeForGivenDocument(localId);
609
            int revision = Integer.parseInt(revAndDocType.split(";")[0]) + 1;
610

    
611
            out.println("<?xml version=\"1.0\"?>");
612
            out.print("<next-revision>");
613
            out.print(revision);
614
            out.print("</next-revision>");
615

    
616
        } catch (Exception e) {
617
            // Handle exception
618
            response.setStatus(500);
619
            out.println("<?xml version=\"1.0\"?>");
620
            out.println("<error>");
621
            out.println(e.getMessage());
622
            out.println("</error>");
623
        }
624

    
625
        out.close();
626
    }
627

    
628
    /**
629
     * Earthgrid API > Identifier Service > getNextObject Function : 
630
     * calls MetacatHandler > handleGetMaxDocidAction
631
     * @throws IOException
632
     */
633
    private void getNextObject() throws IOException {
634
        PrintWriter out = response.getWriter();
635
        response.setStatus(200);
636
        response.setContentType("text/xml");
637
        handler.handleGetMaxDocidAction(out, params, response);
638
        out.close();
639
    }
640

    
641
    /**
642
     * Implements REST version of DataONE CRUD API --> get
643
     * @param guid ID of data object to be read
644
     */
645
    private void getObject(String guid) {
646
        CrudService cs = CrudService.getInstance();
647
        cs.setParamsFromRequest(request);
648
        AuthToken token = new AuthToken(sessionId);
649
        OutputStream out = null;
650
        try {
651
            out = response.getOutputStream();
652
            response.setStatus(200);
653
            response.setContentType("text/xml");
654
            if(guid != null)
655
            { //get a specific document                
656
                Identifier id = new Identifier();
657
                id.setValue(guid);
658
                try
659
                {
660
                    if(token == null)
661
                    {
662
                        token = new AuthToken("Public");
663
                    }
664
                    InputStream data = cs.get(token, id);
665
                    IOUtils.copyLarge(data, response.getOutputStream());
666
                }
667
                catch(InvalidToken it)
668
                {
669
                    serializeException(it, out); 
670
                }
671
                catch(ServiceFailure sf)
672
                {
673
                    serializeException(sf, out); 
674
                }
675
                catch(NotAuthorized na)
676
                {
677
                    serializeException(na, out); 
678
                }
679
                catch(NotFound nf)
680
                {
681
                    serializeException(nf, out); 
682
                }
683
                catch(NotImplemented ni)
684
                {
685
                    serializeException(ni, out); 
686
                }
687
                catch(Exception e)
688
                {
689
                    System.out.println("Error with Crud.get().  " +
690
                            "If this is an 'Exception producing data' error, " +
691
                            "go to CrudService.get() for better debugging.  " +
692
                            "Here's the error: " + e.getMessage());
693
                    e.printStackTrace();
694
                    ServiceFailure sf = new ServiceFailure("1030", 
695
                            "IO Error in ResourceHandler.getObject: " + e.getMessage());
696
                    serializeException(sf, out); 
697
                }
698
            }
699
            else
700
            { //call listObjects with specified params
701
                Date startTime = null;
702
                Date endTime = null;
703
                ObjectFormat objectFormat = null;
704
                boolean replicaStatus = false;
705
                int start = 0;
706
                //TODO: make the max count into a const
707
                int count = 1000;
708
                Enumeration paramlist = request.getParameterNames();
709
                while (paramlist.hasMoreElements()) 
710
                { //parse the params and make the crud call
711
                    String name = (String) paramlist.nextElement();
712
                    String[] value = (String[])request.getParameterValues(name);
713
                    /*for(int i=0; i<value.length; i++)
714
                    {
715
                        System.out.println("name: " + name + " value: " + value[i]);
716
                    }*/
717
                    if(name.equals("startTime") && value != null)
718
                    {
719
                        try
720
                        {
721
                          //startTime = dateFormat.parse(value[0]);
722
                            startTime = parseDateAndConvertToGMT(value[0]);
723
                        }
724
                        catch(Exception e)
725
                        {  //if we can't parse it, just don't use the startTime param
726
                            System.out.println("Could not parse startTime: " + value[0]);
727
                            startTime = null;
728
                        }
729
                    }
730
                    else if(name.equals("endTime") && value != null)
731
                    {
732
                        try
733
                        {
734
                          //endTime = dateFormat.parse(value[0]);
735
                            endTime = parseDateAndConvertToGMT(value[0]);
736
                        }
737
                        catch(Exception e)
738
                        {  //if we can't parse it, just don't use the endTime param
739
                            System.out.println("Could not parse endTime: " + value[0]);
740
                            endTime = null;
741
                        }
742
                    }
743
                    else if(name.equals("objectFormat") && value != null)
744
                    {
745
                        objectFormat = ObjectFormat.convert(value[0]);
746
                    }
747
                    else if(name.equals("replicaStatus") && value != null)
748
                    {
749
                        if(value != null && 
750
                           value.length > 0 && 
751
                           (value[0].equals("true") || value[0].equals("TRUE") || value[0].equals("YES")))
752
                        {
753
                            replicaStatus = true;
754
                        }
755
                    }
756
                    else if(name.equals("start") && value != null)
757
                    {
758
                        start = new Integer(value[0]).intValue();
759
                    }
760
                    else if(name.equals("count") && value != null)
761
                    {
762
                        count = new Integer(value[0]).intValue();
763
                    }
764
                }
765
                //make the crud call
766
                System.out.println("token: " + token + " startTime: " + startTime +
767
                        " endtime: " + endTime + " objectFormat: " + 
768
                        objectFormat + " replicaStatus: " + replicaStatus + 
769
                        " start: " + start + " count: " + count);
770
               
771
                ObjectList ol = cs.listObjects(token, startTime, endTime, 
772
                        objectFormat, replicaStatus, start, count);
773
                
774
                StringReader sr = new StringReader(ol.toString());                
775
                out = response.getOutputStream();  
776
                response.setStatus(200);
777
                response.setContentType("text/xml");
778
                // Serialize and write it to the output stream
779
                
780
                try {
781
                    serializeServiceType(ObjectList.class, ol, out);
782
                } catch (JiBXException e) {
783
                    throw new ServiceFailure("1190", "Failed to serialize ObjectList: " + e.getMessage());
784
                }
785
            }
786
        } catch (BaseException e) {
787
                serializeException(e, out);
788
        } catch (IOException e) {
789
            e.printStackTrace();
790
            ServiceFailure sf = new ServiceFailure("1030", 
791
                    "IO Error in ResourceHandler.getObject: " + e.getMessage());
792
            serializeException(sf, out); 
793
        } catch(NumberFormatException ne) {
794
            InvalidRequest ir = new InvalidRequest("1030", "Invalid format for parameter: " + ne.getMessage());
795
            serializeException(ir, out);
796
        } catch (Exception e) {
797
            e.printStackTrace();
798
            ServiceFailure sf = new ServiceFailure("1030", 
799
                    "Exception " + e.getClass().getName() + " raised while handling listObjects request: " + 
800
                    e.getMessage());
801
            serializeException(sf, out);
802
        }
803
    }
804
    
805
    /**
806
     * parse a date and return it in GMT if it ends with a 'Z'
807
     * @param date
808
     * @return
809
     * @throws ParseException
810
     */
811
    private Date parseDateAndConvertToGMT(String date) throws ParseException
812
    {
813
        try
814
        {   //the format we want
815
            return dateFormat.parse(date);
816
        }
817
        catch(java.text.ParseException pe)
818
        {   //try another legacy format
819
            DateFormat dateFormat2 = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss'Z'");
820
            dateFormat2.setTimeZone(TimeZone.getTimeZone("GMT-0"));
821
            return dateFormat2.parse(date);
822
        }    
823
        
824
        /*System.out.println("Parsing date " + date);
825
        Date d = dateFormat.parse(date);
826
        
827
        if(date.endsWith("Z"))
828
        {
829
            Calendar lTime = Calendar.getInstance();
830
            lTime.setTime(d);
831
            Calendar zTime = Calendar.getInstance(TimeZone.getTimeZone("GMT-0"));
832
            zTime.set(Calendar.MONTH, lTime.get(Calendar.MONTH));
833
            zTime.set(Calendar.DATE, lTime.get(Calendar.DATE));
834
            zTime.set(Calendar.YEAR, lTime.get(Calendar.YEAR));
835
            zTime.set(Calendar.HOUR, lTime.get(Calendar.HOUR));
836
            zTime.set(Calendar.MINUTE, lTime.get(Calendar.MINUTE));
837
            zTime.set(Calendar.SECOND, lTime.get(Calendar.SECOND));
838
            
839
            System.out.println("date parsed to " + zTime.getTime());
840
            return zTime.getTime();
841
        }
842
        
843
        System.out.println("date parsed, but not converted. returned as " + d);
844
        return d;*/
845
    }
846

    
847
    /**
848
     * Implements REST version of DataONE CRUD API --> getSystemMetadata
849
     * @param guid ID of data object to be read
850
     */
851
    private void getSystemMetadataObject(String guid) {
852
        CrudService cs = CrudService.getInstance();
853
        cs.setParamsFromRequest(request);
854
        AuthToken token = new AuthToken(sessionId);
855
        OutputStream out = null;
856
        try {
857
            response.setContentType("text/xml");
858
            response.setStatus(200);
859
            out = response.getOutputStream();
860
            Identifier id = new Identifier();
861
            id.setValue(guid);
862
            SystemMetadata sysmeta = cs.getSystemMetadata(token, id);
863
            
864
            // Serialize and write it to the output stream
865
            try {
866
                //TODO: look at the efficiency of this method.  The system metadata
867
                //is read from metacat (in CrudService) as xml, then serialized
868
                //to a SystemMetadat object, then returned here, then serizlized
869
                //back to XML to be sent to the response.
870
                serializeServiceType(SystemMetadata.class, sysmeta, out);
871
            } catch (JiBXException e) {
872
                throw new ServiceFailure("1190", "Failed to serialize SystemMetadata: " + e.getMessage());
873
            }
874
        } catch (BaseException e) {
875
                serializeException(e, out);
876
        } catch (IOException e) {
877
            ServiceFailure sf = new ServiceFailure("1030", 
878
                    "Error in ResourceHandler.getSystemMetadataObject: " + e.getMessage());
879
            serializeException(sf, out);
880
        } finally {
881
            IOUtils.closeQuietly(out);
882
        }
883
    }
884
    
885
    /**
886
     * serialize an object of type to out
887
     * @param type the class of the object to serialize (i.e. SystemMetadata.class)
888
     * @param object the object to serialize
889
     * @param out the stream to serialize it to
890
     * @throws JiBXException
891
     */
892
    private void serializeServiceType(Class type, Object object, OutputStream out)
893
      throws JiBXException
894
    {
895
        IBindingFactory bfact = BindingDirectory.getFactory(type);
896
        IMarshallingContext mctx = bfact.createMarshallingContext();
897
        mctx.marshalDocument(object, "UTF-8", null, out);
898
    }
899
    
900
    /**
901
     * deserialize an object of type from is
902
     * @param type the class of the object to serialize (i.e. SystemMetadata.class)
903
     * @param is the stream to deserialize from
904
     * @throws JiBXException
905
     */
906
    private Object deserializeServiceType(Class type, InputStream is)
907
      throws JiBXException
908
    {
909
        IBindingFactory bfact = BindingDirectory.getFactory(type);
910
        IUnmarshallingContext uctx = bfact.createUnmarshallingContext();
911
        Object o = (Object) uctx.unmarshalDocument(is, null);
912
        return o;
913
    }
914
    
915
    /**
916
     * Earthgrid API > Query Service > Query Function : translates ecogrid query document to metacat query 
917
     * then calls DBQuery > createResultDocument function and then again translate resultset to ecogrid resultset
918
     * 
919
     * NOTE:
920
     *      This is the only method that uses EcoGrid classes for its implementation.  
921
     *      It does so because it takes an EcoGrid Query as input, and outputs an
922
     *      EcoGrid ResultSet document.  These documents are parsed by the auto-generated
923
     *      EcoGrid classes from axis, and so we link to them here rather than re-inventing them.
924
     *      This creates a circular dependency, because the Metacat classes are needed
925
     *      to build the EcoGrid implementation, and the EcoGrid jars are needed to build this query()
926
     *      method.  This circularity could be resolved by moving the EcoGrid classes
927
     *      to Metacat directly.  As we transition away from EcoGrid SOAP methods in
928
     *      favor of these REST interfaces, this circular dependency can be eliminated.
929
     *        
930
     * @throws Exception
931
     */
932
    private void query() throws Exception {
933
        /*  This block commented out because of the EcoGrid circular dependency.
934
         *  For now, query will not be supported until the circularity can be
935
         *  resolved, probably by moving the ecogrid query syntax transformers
936
         *  directly into the Metacat codebase.  MBJ 2010-02-03
937
         
938
        try {
939
            EcogridQueryParser parser = new EcogridQueryParser(request
940
                    .getReader());
941
            parser.parseXML();
942
            QueryType queryType = parser.getEcogridQuery();
943
            EcogridJavaToMetacatJavaQueryTransformer queryTransformer = 
944
                new EcogridJavaToMetacatJavaQueryTransformer();
945
            QuerySpecification metacatQuery = queryTransformer
946
                    .transform(queryType);
947

    
948
            DBQuery metacat = new DBQuery();
949

    
950
            boolean useXMLIndex = (new Boolean(PropertyService
951
                    .getProperty("database.usexmlindex"))).booleanValue();
952
            String xmlquery = "query"; // we don't care the query in resultset,
953
            // the query can be anything
954
            PrintWriter out = null; // we don't want metacat result, so set out null
955

    
956
            // parameter: queryspecification, user, group, usingIndexOrNot
957
            StringBuffer result = metacat.createResultDocument(xmlquery,
958
                    metacatQuery, out, username, groupNames, useXMLIndex);
959

    
960
            // create result set transfer		
961
            String saxparser = PropertyService.getProperty("xml.saxparser");
962
            MetacatResultsetParser metacatResultsetParser = new MetacatResultsetParser(
963
                    new StringReader(result.toString()), saxparser, queryType
964
                            .getNamespace().get_value());
965
            ResultsetType records = metacatResultsetParser.getEcogridResult();
966

    
967
            System.out
968
                    .println(EcogridResultsetTransformer.toXMLString(records));
969
            response.setContentType("text/xml");
970
            out = response.getWriter();
971
            out.print(EcogridResultsetTransformer.toXMLString(records));
972

    
973
        } catch (Exception e) {
974
            e.printStackTrace();
975
        }*/
976
        response.setContentType("text/xml");
977
        response.setStatus(501);
978
        PrintWriter out = response.getWriter();
979
        out.print("<error>Query operation not yet supported by Metacat.</error>");
980
        out.close();
981
    }
982
    
983
    private String streamToString(InputStream is)
984
    throws IOException
985
    {
986
        byte b[] = new byte[1024];
987
        int numread = is.read(b, 0, 1024);
988
        String response = new String();
989
        while(numread != -1)
990
        {
991
            response += new String(b, 0, numread);
992
            numread = is.read(b, 0, 1024);
993
        }
994
        return response;
995
    }
996

    
997
    private InputStream stringToStream(String s)
998
    throws IOException
999
    {
1000
        ByteArrayInputStream bais = new ByteArrayInputStream(s.getBytes());
1001
        return bais;
1002
    }
1003
    
1004
    /**
1005
     * process a mime multipart message
1006
     * @param is
1007
     * @return
1008
     */
1009
    private Hashtable processMMP(InputStream is)
1010
      throws IOException
1011
    {
1012
        //TODO: verify that this is how the RFC for MMP should work
1013
        //SORTAHACK: Since mmp seems to have a bug where large object parts get truncated,
1014
        //parse the stream here.  This has the disavantage of putting the
1015
        //stream into memory.
1016
        InputStream object = null;
1017
        InputStream sysmeta = null;
1018
        String s = streamToString(is);
1019
        //System.out.println("s: " + s);
1020
        //figure out what the boundary marker is
1021
        String searchString = "boundary=";
1022
        int searchStringIndex = s.indexOf(searchString);
1023
        String boundary = s.substring(searchStringIndex + searchString.length() + 1, 
1024
                s.indexOf("\"", searchStringIndex + searchString.length() + 1));
1025
        boundary = "--" + boundary;
1026
        //System.out.println("boundary is " + boundary);
1027
        
1028
        //find the system metadata
1029
        searchString = "Content-Disposition: attachment; filename=systemmetadata";
1030
        searchStringIndex = s.indexOf(searchString);
1031
        sysmeta = new ByteArrayInputStream(
1032
                s.substring(searchStringIndex +
1033
                        searchString.length(), 
1034
                        s.indexOf(boundary, searchStringIndex)).trim().getBytes());
1035
        
1036
        //find the object
1037
        searchString = "Content-Disposition: attachment; filename=object";
1038
        searchStringIndex = s.indexOf(searchString);
1039
        object = new ByteArrayInputStream(
1040
                s.substring(searchStringIndex +
1041
                        searchString.length(), 
1042
                        s.indexOf(boundary, searchStringIndex)).trim().getBytes());
1043
        
1044
        Hashtable h = new Hashtable();
1045
        h.put("object", object);
1046
        h.put("systemmetadata", sysmeta);
1047
        return h;
1048
    }
1049
    
1050
    
1051
    /**
1052
     * Earthgrid API > Put Service >Put Function : calls MetacatHandler > handleInsertOrUpdateAction 
1053
     * 
1054
     * @param guid ID of data object to be inserted or updated
1055
     * @throws IOException
1056
     */
1057
    private void putObject(String guid, String action) {
1058
        logMetacat.debug("Entering putObject: " + guid + "/" + action);
1059
        OutputStream out = null;
1060
        try {
1061
            out = response.getOutputStream();
1062
            response.setStatus(200);
1063
            response.setContentType("text/xml");
1064
        } catch (IOException e1) {
1065
            logMetacat.error("Could not get the output stream for writing in putObject");
1066
        }
1067
        try {
1068
            
1069
            // Read the incoming data from its Mime Multipart encoding
1070
            logMetacat.debug("Disassembling MIME multipart form");
1071
            InputStream object = null;
1072
            InputStream sysmeta = null;
1073
            
1074
            Hashtable parts = processMMP(request.getInputStream());
1075
            object = (InputStream)parts.get("object");
1076
            sysmeta = (InputStream)parts.get("systemmetadata");
1077
            
1078
            if ( action.equals(FUNCTION_NAME_INSERT)) { //handle inserts
1079

    
1080
                // Check if the objectId exists
1081
                IdentifierManager im = IdentifierManager.getInstance();
1082
                if (im.identifierExists(guid)) {
1083
                    throw new IdentifierNotUnique("1000", "Identifier is already in use: " + guid);
1084
                }
1085

    
1086
                logMetacat.debug("Commence creation...");
1087
                IBindingFactory bfact =
1088
                    BindingDirectory.getFactory(SystemMetadata.class);
1089
                IUnmarshallingContext uctx = bfact.createUnmarshallingContext();
1090
                SystemMetadata m = (SystemMetadata) uctx.unmarshalDocument(sysmeta, null);
1091

    
1092
                CrudService cs = CrudService.getInstance();
1093
                AuthToken token = new AuthToken(sessionId); 
1094
                cs.setParamsFromRequest(request);
1095
                Identifier id = new Identifier();
1096
                id.setValue(guid);
1097
                cs.create(token, id, object, m);
1098
                
1099
            } else if (action.equals(FUNCTION_NAME_UPDATE)) { //handle updates
1100
                IdentifierManager im = IdentifierManager.getInstance();
1101
                CrudService cs = CrudService.getInstance();
1102
                Identifier obsoletedGuid = new Identifier();
1103
                Identifier id = new Identifier();
1104
                id.setValue(guid);
1105
                AuthToken token = new AuthToken(sessionId);
1106
                
1107
                //do some checks
1108
                if(params.get("obsoletedGuid") == null)
1109
                {
1110
                    throw new InvalidRequest("1202", "obsoletedGuid must be contained in the request parameters.");
1111
                }
1112
                //get the obsoletedGuid
1113
                String[] obsGuidS = params.get("obsoletedGuid");
1114
                obsoletedGuid.setValue(obsGuidS[0]);
1115
                
1116
                if (!im.identifierExists(obsoletedGuid.getValue())) 
1117
                {
1118
                    throw new InvalidRequest("1202", "The guid you are trying to update does not exist.");
1119
                }
1120
                
1121
                
1122
                logMetacat.debug("Commence update...");
1123
                
1124
                //get the systemmetadata
1125
                IBindingFactory bfact =
1126
                        BindingDirectory.getFactory(SystemMetadata.class);
1127
                    IUnmarshallingContext uctx = bfact.createUnmarshallingContext();
1128
                    SystemMetadata m = (SystemMetadata) uctx.unmarshalDocument(sysmeta, null);
1129
                
1130
                //do the update
1131
                try
1132
                {
1133
                    cs.setParamsFromRequest(request);
1134
                    Identifier rId = cs.update(token, id, object, obsoletedGuid, m);
1135
                }
1136
                catch(NotFound e)
1137
                {
1138
                    throw new InvalidRequest("1202", "The guid you are trying to update does not exist.");
1139
                }
1140
                
1141
            } else {
1142
                throw new InvalidRequest("1000", "Operation must be create or update.");
1143
            }
1144
        } catch (NotAuthorized e) {
1145
            serializeException(e, out);
1146
        } catch (InvalidToken e) {
1147
            serializeException(e, out);
1148
        } catch (ServiceFailure e) {
1149
            serializeException(e, out);
1150
        } catch (IdentifierNotUnique e) {
1151
            serializeException(e, out);
1152
        } catch (UnsupportedType e) {
1153
            serializeException(e, out);
1154
        } catch (InsufficientResources e) {
1155
            serializeException(e, out);
1156
        } catch (InvalidSystemMetadata e) {
1157
            serializeException(e, out);
1158
        } catch (NotImplemented e) {
1159
            serializeException(e, out);
1160
        } catch (InvalidRequest e) {
1161
            serializeException(e, out);
1162
        } /*catch (MessagingException e) {
1163
            ServiceFailure sf = new ServiceFailure("1000", e.getMessage());
1164
            serializeException(sf, out);
1165
        }*/ catch (IOException e) {
1166
            ServiceFailure sf = new ServiceFailure("1000", e.getMessage());
1167
            serializeException(sf, out);
1168
        } catch (JiBXException e) {
1169
            e.printStackTrace(System.out);
1170
            InvalidSystemMetadata ism = new InvalidSystemMetadata("1080", e.getMessage());
1171
            serializeException(ism, out);
1172
        }
1173
    }
1174

    
1175
    /**
1176
     * Earthgrid API > Put Service > Delete Function : calls MetacatHandler > handleDeleteAction  
1177
     * 
1178
     * @param guid ID of data object to be deleted
1179
     * @throws IOException
1180
     */
1181
    private void deleteObject(String guid) throws IOException 
1182
    {
1183
        // Look up the localId for this global identifier
1184
        IdentifierManager im = IdentifierManager.getInstance();
1185
        String localId = "";
1186
        try {
1187
            localId = im.getLocalId(guid);
1188
        } catch (McdbDocNotFoundException e) {
1189
            // TODO: Need to return the proper DataONE exception
1190
        }
1191
        
1192
        params.put("docid", new String[] { localId });
1193
        PrintWriter out = response.getWriter();
1194
        response.setStatus(200);
1195
        response.setContentType("text/xml");
1196
        handler.handleDeleteAction(out, params, request, response, username,
1197
                groupNames);
1198
        out.close();
1199
    }
1200
    
1201
    /**
1202
     * set the access perms on a document
1203
     * @throws IOException
1204
     */
1205
    private void setaccess() throws Exception
1206
    {
1207
        try
1208
        {
1209
            String guid = params.get("guid")[0];
1210
            Identifier id = new Identifier();
1211
            id.setValue(guid);
1212
            AuthToken token = new AuthToken(sessionId);
1213
            String principal = params.get("principal")[0];
1214
            String permission = params.get("permission")[0];
1215
            String permissionType = params.get("permissionType")[0];
1216
            String permissionOrder = params.get("permissionOrder")[0];
1217
            String setSystemMetadata = params.get("setsystemmetadata")[0];
1218
            boolean ssm = false;
1219
            if(setSystemMetadata.equals("true") || setSystemMetadata.equals("TRUE") ||
1220
                    setSystemMetadata.equals("yes"))
1221
            {
1222
                ssm = true;
1223
            }
1224
            CrudService cs = CrudService.getInstance();
1225
            //TODO: remove the setsystemmetadata param and set this so the systemmetadata always gets set
1226
            cs.setAccess(token, id, principal, permission, permissionType, permissionOrder, ssm);
1227
        }
1228
        catch(Exception e)
1229
        {
1230
            response.setStatus(500);
1231
            printError("Error setting access in ResourceHandler: " + e.getMessage(), response);
1232
            throw e;
1233
        }
1234
    }
1235

    
1236
    /**
1237
     * Earthgrid API > Authentication Service > Login Function : calls MetacatHandler > handleLoginAction
1238
     * 
1239
     * @throws IOException
1240
     */
1241
    private void login() throws IOException {
1242
        PrintWriter out = response.getWriter();
1243
        response.setStatus(200);
1244
        response.setContentType("text/xml");
1245
        handler.handleLoginAction(out, params, request, response);
1246
        out.close();
1247
    }
1248

    
1249
    /**
1250
     * Earthgrid API > Authentication Service > Logout Function : calls MetacatHandler > handleLogoutAction
1251
     * 
1252
     * @throws IOException
1253
     */
1254
    private void logout() throws IOException {
1255
        PrintWriter out = response.getWriter();
1256
        response.setStatus(200);
1257
        response.setContentType("text/xml");
1258
        handler.handleLogoutAction(out, params, request, response);
1259
        out.close();
1260
    }
1261

    
1262
    /**
1263
     * Prints xml response
1264
     * @param message Message to be displayed
1265
     * @param response Servlet response that xml message will be printed 
1266
     * */
1267
    private void printError(String message, HttpServletResponse response) {
1268
        try {
1269
            logMetacat.error("ResourceHandler: Printing error to servlet response: " + message);
1270
            PrintWriter out = response.getWriter();
1271
            response.setContentType("text/xml");
1272
            out.println("<?xml version=\"1.0\"?>");
1273
            out.println("<error>");
1274
            out.println(message);
1275
            out.println("</error>");
1276
            out.close();
1277
        } catch (IOException e) {
1278
            e.printStackTrace();
1279
        }
1280
    }
1281
    
1282
    private void serializeException(BaseException e, OutputStream out) {
1283
        // TODO: Use content negotiation to determine which return format to use
1284
        response.setContentType("text/xml");
1285
        response.setStatus(e.getCode());
1286
        
1287
        logMetacat.error("ResourceHandler: Serializing exception with code " + e.getCode() + ": " + e.getMessage());
1288
        e.printStackTrace();
1289
        
1290
        try {
1291
            IOUtils.write(e.serialize(BaseException.FMT_XML), out);
1292
        } catch (IOException e1) {
1293
            logMetacat.error("Error writing exception to stream. " 
1294
                    + e1.getMessage());
1295
        }
1296
    }
1297

    
1298
}
(2-2/3)