Project

General

Profile

1 1780 jones
/**
2
 *  '$RCSfile$'
3
 *  Copyright: 2000 Regents of the University of California and the
4
 *              National Center for Ecological Analysis and Synthesis
5
 *
6
 *   '$Author$'
7
 *     '$Date$'
8
 * '$Revision$'
9
 *
10
 * This program is free software; you can redistribute it and/or modify
11
 * it under the terms of the GNU General Public License as published by
12
 * the Free Software Foundation; either version 2 of the License, or
13
 * (at your option) any later version.
14
 *
15
 * This program is distributed in the hope that it will be useful,
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
 * GNU General Public License for more details.
19
 *
20
 * You should have received a copy of the GNU General Public License
21
 * along with this program; if not, write to the Free Software
22
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
23
 */
24
25
package edu.ucsb.nceas.metacat.client;
26
27 1786 tao
import java.io.BufferedReader;
28 1780 jones
import java.io.InputStream;
29 1783 jones
import java.io.InputStreamReader;
30 1784 jones
import java.io.PushbackReader;
31
import java.io.IOException;
32 1783 jones
import java.io.StringWriter;
33 1780 jones
import java.io.Reader;
34
import java.net.URL;
35
import java.util.Properties;
36
37
import edu.ucsb.nceas.utilities.HttpMessage;
38 1788 jones
import edu.ucsb.nceas.utilities.IOUtil;
39 2240 sgarg
import java.io.File;
40 1780 jones
41 1788 jones
42 1780 jones
/**
43 2240 sgarg
 *  This interface provides methods for initializing and logging in to a
44
 *  Metacat server, and then querying, reading, transforming, inserting,
45 1780 jones
 *  updating and deleting documents from that server.
46
 */
47
public class MetacatClient implements Metacat
48
{
49
    /** The URL string for the metacat server */
50
    private String metacatUrl;
51
52 1822 jones
    /** The session identifier for the session */
53
    private String sessionId;
54
55 1780 jones
    /**
56
     * Constructor to create a new instance. Protected because instances
57
     * should only be created by the factory MetacatFactory.
58
     */
59
    protected MetacatClient()
60
    {
61 1822 jones
        this.metacatUrl = null;
62
        this.sessionId = null;
63 1780 jones
    }
64
65
    /**
66
     *  Method used to log in to a metacat server. Implementations will need
67
     *  to cache a cookie value to make the session persistent.  Each time a
68
     *  call is made to one of the other methods (e.g., read), the cookie will
69
     *  need to be passed back to the metacat server along with the request.
70
     *
71
     *  @param username   the username of the user, like an LDAP DN
72
     *  @param password   the password for that user for authentication
73 1822 jones
     *  @return the response string from metacat in XML format
74 1780 jones
     *  @throws MetacatAuthException when the username/password could
75
     *                    not be authenticated
76
     */
77 2240 sgarg
    public String login(String username, String password)
78 1780 jones
           throws MetacatAuthException, MetacatInaccessibleException
79
    {
80 1783 jones
        Properties prop = new Properties();
81
        prop.put("action", "login");
82
        prop.put("qformat", "xml");
83
        prop.put("username", username);
84
        prop.put("password", password);
85
86
        String response = null;
87
        try {
88 2264 sgarg
            response = sendDataForString(prop, null, null, 0);
89 1783 jones
        } catch (Exception e) {
90
            throw new MetacatInaccessibleException(e.getMessage());
91
        }
92
93
        if (response.indexOf("<login>") == -1) {
94 1828 jones
            setSessionId("");
95 1783 jones
            throw new MetacatAuthException(response);
96 1822 jones
        } else {
97 1825 jones
            int start = response.indexOf("<sessionId>") + 11;
98 1822 jones
            int end = response.indexOf("</sessionId>");
99
            if ((start != -1) && (end != -1)) {
100 1828 jones
                setSessionId(response.substring(start,end));
101 1822 jones
            }
102 1783 jones
        }
103 1822 jones
        return response;
104 1780 jones
    }
105
106
    /**
107 1822 jones
     *  Method used to log out a metacat server. The Metacat server will end
108
     *  the session when this call is invoked.
109 1798 tao
     *
110 1822 jones
     *  @return the response string from metacat in XML format
111 1798 tao
     *  @throws MetacatInaccessibleException when the metacat server can not be
112
     *                                    reached or does not respond
113
     */
114 1822 jones
    public String logout() throws MetacatInaccessibleException, MetacatException
115 1798 tao
    {
116
        Properties prop = new Properties();
117
        prop.put("action", "logout");
118 2240 sgarg
        prop.put("qformat", "xml");
119
120 1798 tao
        String response = null;
121
        try {
122 2264 sgarg
            response = sendDataForString(prop, null, null, 0);
123 1798 tao
        } catch (Exception e) {
124
            throw new MetacatInaccessibleException(e.getMessage());
125
        }
126 2240 sgarg
127 1798 tao
        if (response.indexOf("<logout>") == -1) {
128
            throw new MetacatException(response);
129
        }
130 1828 jones
        setSessionId("");
131 1822 jones
        return response;
132 1798 tao
    }
133 2240 sgarg
134 1798 tao
    /**
135 1780 jones
     * Read an XML document from the metacat server session, accessed by docid,
136
     * and returned as a Reader.
137
     *
138
     * @param docid the identifier of the document to be read
139
     * @return a Reader for accessing the document
140 2240 sgarg
     * @throws InsufficientKarmaException when the user has insufficent rights
141 1780 jones
     *                                    for the operation
142 1784 jones
     * @throws MetacatInaccessibleException when the metacat server can not be
143
     *                                    reached or does not respond
144
     * @throws MetacatException when the metacat server generates another error
145 1780 jones
     */
146 1784 jones
    public Reader read(String docid) throws InsufficientKarmaException,
147
        MetacatInaccessibleException, MetacatException
148 1780 jones
    {
149 1784 jones
        PushbackReader pbr = null;
150
151
        Properties prop = new Properties();
152
        prop.put("action", "read");
153
        prop.put("qformat", "xml");
154
        prop.put("docid", docid);
155
156
        InputStream response = null;
157
        try {
158 2264 sgarg
            response = sendData(prop, null, null, 0);
159 1784 jones
        } catch (Exception e) {
160
            throw new MetacatInaccessibleException(e.getMessage());
161
        }
162 2240 sgarg
163 1784 jones
        pbr = new PushbackReader(new InputStreamReader(response), 512);
164
        try {
165
            char[] characters = new char[512];
166
            int len = pbr.read(characters, 0, 512);
167
            StringWriter sw = new StringWriter();
168
            sw.write(characters, 0, len);
169
            String message = sw.toString();
170
            sw.close();
171
            pbr.unread(characters, 0, len);
172
173
            if (message.indexOf("<error>") != -1) {
174
                if (message.indexOf("does not have permission") != -1) {
175
                    throw new InsufficientKarmaException(message);
176
                } else {
177
                    throw new MetacatException(message);
178
                }
179
            }
180
        } catch (IOException ioe) {
181
            throw new MetacatException(
182 2240 sgarg
                    "MetacatClient: Error converting Reader to String."
183 1784 jones
                    + ioe.getMessage());
184
        }
185
186
        return pbr;
187 1780 jones
    }
188
189 2261 sgarg
190 1780 jones
    /**
191 2261 sgarg
        * Read inline data from the metacat server session, accessed by
192
        * inlinedataid and returned as a Reader.
193
        *
194
        * @param inlinedataid the identifier of the data to be read
195
        * @return a Reader for accessing the document
196
        * @throws InsufficientKarmaException when the user has insufficent rights
197
        *                                    for the operation
198
        * @throws MetacatInaccessibleException when the metacat server can not be
199
        *                                    reached or does not respond
200
        * @throws MetacatException when the metacat server generates another error
201
        */
202
       public Reader readInlineData(String inlinedataid)
203
           throws InsufficientKarmaException,
204
           MetacatInaccessibleException, MetacatException
205
       {
206
           PushbackReader pbr = null;
207
208
           Properties prop = new Properties();
209
           prop.put("action", "readinlinedata");
210
           prop.put("inlinedataid", inlinedataid);
211
212
           InputStream response = null;
213
           try {
214 2264 sgarg
               response = sendData(prop, null, null, 0);
215 2261 sgarg
           } catch (Exception e) {
216
               throw new MetacatInaccessibleException(e.getMessage());
217
           }
218
219
           pbr = new PushbackReader(new InputStreamReader(response), 512);
220
           try {
221
               char[] characters = new char[512];
222
               int len = pbr.read(characters, 0, 512);
223
               StringWriter sw = new StringWriter();
224
               sw.write(characters, 0, len);
225
               String message = sw.toString();
226
               sw.close();
227
               pbr.unread(characters, 0, len);
228
229
               if (message.indexOf("<error>") != -1) {
230
                   if (message.indexOf("does not have permission") != -1) {
231
                       throw new InsufficientKarmaException(message);
232
                   } else {
233
                       throw new MetacatException(message);
234
                   }
235
               }
236
           } catch (IOException ioe) {
237
               throw new MetacatException(
238
                       "MetacatClient: Error converting Reader to String."
239
                       + ioe.getMessage());
240
           }
241
242
           return pbr;
243
       }
244
245
    /**
246 2240 sgarg
     * Query the metacat document store with the given metacat-compatible
247 1780 jones
     * query document, and return the result set as a Reader.
248
     *
249
     * @param xmlQuery a Reader for accessing the XML version of the query
250
     * @return a Reader for accessing the result set
251
     */
252 1786 tao
    public Reader query(Reader xmlQuery) throws MetacatInaccessibleException,
253
                                                IOException
254 1780 jones
    {
255 1786 tao
        Reader reader = null;
256
        String query = null;
257 1788 jones
        try {
258
          query = IOUtil.getAsString(xmlQuery, true);
259
        } catch (IOException ioE) {
260 1786 tao
          throw ioE;
261
        }
262
263
        //set up properties
264
        Properties prop = new Properties();
265
        prop.put("action", "squery");
266
        prop.put("qformat", "xml");
267
        prop.put("query", query);
268 2240 sgarg
269 1786 tao
        InputStream response = null;
270
        try {
271 2264 sgarg
            response = sendData(prop, null, null, 0);
272 1786 tao
        } catch (Exception e) {
273
            throw new MetacatInaccessibleException(e.getMessage());
274
        }
275
        reader = new InputStreamReader(response);
276
        return reader;
277 1780 jones
    }
278
279
    /**
280
     * Insert an XML document into the repository.
281
     *
282
     * @param docid the docid to insert the document
283
     * @param xmlDocument a Reader for accessing the XML document to be inserted
284 2240 sgarg
     * @param schema a Reader for accessing the DTD or XML Schema for
285 1780 jones
     *               the document
286 1789 jones
     * @return the metacat response message
287 2240 sgarg
     * @throws InsufficientKarmaException when the user has insufficent rights
288 1780 jones
     *                                    for the operation
289 1789 jones
     * @throws MetacatInaccessibleException when the metacat server can not be
290
     *                                    reached or does not respond
291
     * @throws MetacatException when the metacat server generates another error
292
     * @throws IOException when there is an error reading the xml document
293 1780 jones
     */
294 1789 jones
    public String insert(String docid, Reader xmlDocument, Reader schema)
295
        throws InsufficientKarmaException, MetacatException, IOException,
296
        MetacatInaccessibleException
297 1780 jones
    {
298 1789 jones
        Reader reader = null;
299
        String doctext = null;
300
        String schematext = null;
301
        try {
302
          doctext = IOUtil.getAsString(xmlDocument, true);
303
          if (schema != null) {
304
              schematext = IOUtil.getAsString(schema, true);
305
          }
306
        } catch (IOException ioE) {
307
          throw ioE;
308
        }
309
310
        //set up properties
311
        Properties prop = new Properties();
312
        prop.put("action", "insert");
313
        prop.put("docid", docid);
314
        prop.put("doctext", doctext);
315
        if (schematext != null) {
316
            prop.put("dtdtext", schematext);
317
        }
318 2240 sgarg
319 1789 jones
        String response = null;
320
        try {
321 2264 sgarg
            response = sendDataForString(prop, null, null, 0);
322 1789 jones
        } catch (Exception e) {
323
            throw new MetacatInaccessibleException(e.getMessage());
324
        }
325
326
        // Check for an error condition
327
        if (response.indexOf("<error>") != -1) {
328
            if (response.indexOf("does not have permission") != -1) {
329
                throw new InsufficientKarmaException(response);
330
            } else {
331
                throw new MetacatException(response);
332
            }
333
        }
334
335
        return response;
336 1780 jones
    }
337
338
    /**
339
     * Update an XML document in the repository.
340
     *
341
     * @param docid the docid to update
342
     * @param xmlDocument a Reader for accessing the XML text to be updated
343 2240 sgarg
     * @param schema a Reader for accessing the DTD or XML Schema for
344 1780 jones
     *               the document
345 1795 jones
     * @return the metacat response message
346 2240 sgarg
     * @throws InsufficientKarmaException when the user has insufficent rights
347 1780 jones
     *                                    for the operation
348 1795 jones
     * @throws MetacatInaccessibleException when the metacat server can not be
349
     *                                    reached or does not respond
350
     * @throws MetacatException when the metacat server generates another error
351
     * @throws IOException when there is an error reading the xml document
352 1780 jones
     */
353 1795 jones
    public String update(String docid, Reader xmlDocument, Reader schema)
354
        throws InsufficientKarmaException, MetacatException, IOException,
355
        MetacatInaccessibleException
356 1780 jones
    {
357 1795 jones
        Reader reader = null;
358
        String doctext = null;
359
        String schematext = null;
360
        try {
361
          doctext = IOUtil.getAsString(xmlDocument, true);
362
          if (schema != null) {
363
              schematext = IOUtil.getAsString(schema, true);
364
          }
365
        } catch (IOException ioE) {
366
          throw ioE;
367
        }
368
369
        //set up properties
370
        Properties prop = new Properties();
371
        prop.put("action", "update");
372
        prop.put("docid", docid);
373
        prop.put("doctext", doctext);
374
        if (schematext != null) {
375
            prop.put("dtdtext", schematext);
376
        }
377 2240 sgarg
378 1795 jones
        String response = null;
379
        try {
380 2264 sgarg
            response = sendDataForString(prop, null, null, 0);
381 1795 jones
        } catch (Exception e) {
382
            throw new MetacatInaccessibleException(e.getMessage());
383
        }
384
385
        // Check for an error condition
386
        if (response.indexOf("<error>") != -1) {
387
            if (response.indexOf("does not have permission") != -1) {
388
                throw new InsufficientKarmaException(response);
389
            } else {
390
                throw new MetacatException(response);
391
            }
392
        }
393
394
        return response;
395 1780 jones
    }
396
397
    /**
398 2240 sgarg
       * Upload a data document into the repository.
399
       *
400
       * @param docid the docid to insert the document
401
       * @param document a Reader for accessing the document to be uploaded
402
       * @return the metacat response message
403
       * @throws InsufficientKarmaException when the user has insufficent rights
404
       *                                    for the operation
405
       * @throws MetacatInaccessibleException when the metacat server can not be
406
       *                                    reached or does not respond
407
       * @throws MetacatException when the metacat server generates another error
408
       * @throws IOException when there is an error reading the xml document
409
       */
410
      public String upload(String docid, File file)
411
          throws InsufficientKarmaException, MetacatException, IOException,
412
          MetacatInaccessibleException
413
      {
414
415
          URL url = new URL(metacatUrl.trim());
416
          HttpMessage msg = new HttpMessage(url);
417
          //set up properties
418
          Properties arg = new Properties();
419
          arg.put("action", "upload");
420
          arg.put("docid", docid);
421
422
          Properties filenames = new Properties();
423
          String filename = file.getAbsolutePath();
424
          filenames.put("datafile", filename);
425
426
          String response = null;
427
          try {
428 2264 sgarg
            response = sendDataForString(arg, filenames, null, 0);
429 2240 sgarg
          } catch (Exception e) {
430
            throw new MetacatInaccessibleException(e.getMessage());
431
          }
432
433
          // Check for an error condition
434
          if (response.indexOf("<error>") != -1) {
435
            if (response.indexOf("does not have permission") != -1) {
436
              throw new InsufficientKarmaException(response);
437
            } else {
438
              throw new MetacatException(response);
439
            }
440
          }
441
442 2264 sgarg
          return response;
443
      }
444 2240 sgarg
445 2326 harris
        /**
446 2264 sgarg
          * Upload a data document into the repository.
447
          *
448
          * @param docid the docid to insert the document
449
          * @param document a Reader for accessing the document to be uploaded
450
          * @return the metacat response message
451
          * @throws InsufficientKarmaException when the user has insufficent rights
452
          *                                    for the operation
453
          * @throws MetacatInaccessibleException when the metacat server can not be
454
          *                                    reached or does not respond
455
          * @throws MetacatException when the metacat server generates another error
456
          * @throws IOException when there is an error reading the xml document
457
          */
458
459
460
      public String upload(String docid, String filename, InputStream fileData,
461
                           int size)
462
          throws InsufficientKarmaException, MetacatException, IOException,
463
          MetacatInaccessibleException {
464
465
          URL url = new URL(metacatUrl.trim());
466
          HttpMessage msg = new HttpMessage(url);
467
          //set up properties
468
          Properties arg = new Properties();
469
          arg.put("action", "upload");
470
          arg.put("docid", docid);
471
472
          Properties filenames = new Properties();
473
          filenames.put("datafile", filename);
474
475
          String response = null;
476
          try {
477
            response = sendDataForString(arg, filenames, fileData, size);
478
          } catch (Exception e) {
479
            throw new MetacatInaccessibleException(e.getMessage());
480
          }
481
482
          // Check for an error condition
483
          if (response.indexOf("<error>") != -1) {
484
            if (response.indexOf("does not have permission") != -1) {
485
              throw new InsufficientKarmaException(response);
486
            } else {
487
              throw new MetacatException(response);
488
            }
489
          }
490
491
          return response;
492 2240 sgarg
      }
493
494
    /**
495 1780 jones
     * Delete an XML document in the repository.
496
     *
497
     * @param docid the docid to delete
498 1795 jones
     * @return the metacat response message
499 2240 sgarg
     * @throws InsufficientKarmaException when the user has insufficent rights
500 1780 jones
     *                                    for the operation
501 1795 jones
     * @throws MetacatInaccessibleException when the metacat server can not be
502
     *                                    reached or does not respond
503
     * @throws MetacatException when the metacat server generates another error
504 1780 jones
     */
505 1795 jones
    public String delete(String docid)
506
        throws InsufficientKarmaException, MetacatException,
507
        MetacatInaccessibleException
508 1780 jones
    {
509 1795 jones
        //set up properties
510
        Properties prop = new Properties();
511
        prop.put("action", "delete");
512
        prop.put("docid", docid);
513 2240 sgarg
514 1795 jones
        String response = null;
515
        try {
516 2264 sgarg
            response = sendDataForString(prop, null, null, 0);
517 1795 jones
        } catch (Exception e) {
518
            throw new MetacatInaccessibleException(e.getMessage());
519
        }
520
521
        // Check for an error condition
522
        if (response.indexOf("<error>") != -1) {
523
            if (response.indexOf("does not have permission") != -1) {
524
                throw new InsufficientKarmaException(response);
525
            } else {
526
                throw new MetacatException(response);
527
            }
528
        }
529 2326 harris
        return response;
530
    }
531 1795 jones
532 2326 harris
533
    /**
534
     * set the access on an XML document in the repository.
535
     *
536 2327 harris
     * @param _docid the docid of the document for which the access should be applied.
537 2326 harris
     *
538
     * @param _principal the document's principal
539
     *
540
     * @param _permission the access permission to be applied to the docid
541
     *  {e.g. read,write,all}
542
     *
543
     * @param _permType the permission type to be applied to the document
544
     *  {e.g. allow or deny}
545
     *
546
     * @param _permOrder the order that the document's permissions should be
547
     *  processed {e.g. denyFirst or allowFirst}
548
     *
549
     *
550
     * @return the metacat response message
551
     *
552
     * @throws InsufficientKarmaException when the user has insufficent rights
553
     *                                    for the operation
554
     * @throws MetacatInaccessibleException when the metacat server can not be
555
     *                                    reached or does not respond
556
     * @throws MetacatException when the metacat server generates another error
557
     */
558
    public String setAccess(String _docid, String _principal, String
559
                            _permission, String _permType,
560
                            String _permOrder )
561
        throws InsufficientKarmaException, MetacatException,
562
        MetacatInaccessibleException
563
    {
564
        //set up properties
565
        Properties prop = new Properties();
566
        prop.put("action", "setaccess");
567
        prop.put("docid", _docid);
568
        prop.put("principal", _principal);
569
        prop.put("permission", _permission);
570
        prop.put("permType", _permType);
571
        prop.put("permOrder", _permOrder);
572
573
        String response = null;
574
        try {
575
            response = sendDataForString(prop, null, null, 0);
576
        } catch (Exception e) {
577
            throw new MetacatInaccessibleException(e.getMessage());
578
        }
579
580
        // Check for an error condition
581
        if (response.indexOf("<error>") != -1) {
582
            if (response.indexOf("does not have permission") != -1) {
583
                throw new InsufficientKarmaException(response);
584
            } else {
585
                throw new MetacatException(response);
586
            }
587
        }
588 1795 jones
        return response;
589 1780 jones
    }
590
591
    /**
592
     * When the MetacatFactory creates an instance it needs to set the
593
     * MetacatUrl to which connections should be made.
594
     *
595
     * @param metacatUrl the URL for the metacat server
596
     */
597
    public void setMetacatUrl(String metacatUrl)
598
    {
599 1783 jones
        this.metacatUrl = metacatUrl;
600 1780 jones
    }
601
602 1822 jones
    /**
603
     * Get the session identifier for this session.  This is only valid if
604
     * the login methods has been called successfully for this Metacat object
605
     * beforehand.
606
     *
607
     * @returns the sessionId as a String, or null if the session is invalid
608
     */
609
    public String getSessionId()
610
    {
611
        return this.sessionId;
612
    }
613
614 1826 jones
    /**
615 2240 sgarg
     * Set the session identifier for this session.  This identifier was
616
     * previously established with a call to login.  To continue to use the
617 1826 jones
     * same session, set the session id before making a call to one of the
618
     * metacat access methods (e.g., read, query, insert, etc.).
619
     *
620
     * @param String the sessionId from a previously established session
621
     */
622
    public void setSessionId(String sessionId)
623
    {
624
        this.sessionId = sessionId;
625
    }
626 2337 tao
627
    /**
628
     * The method will return the lasted revision in metacat server
629
     * for a given document id. If some error happent, this method will throw
630
     * a exception.
631
     * @param docId String  the given docid you want to use. the docid it self
632
     *                      can have or haven't revision number
633
     * @throws MetacatException
634
     */
635
     public int getNewestDocRevision(String docId) throws MetacatException
636
     {
637
       int rev = 0;
638
       //set up properties
639
       Properties prop = new Properties();
640
       prop.put("action", "getrevisionanddoctype");
641
       prop.put("docid", docId);
642
643
       String response = null;
644
        try
645
        {
646
            response = sendDataForString(prop, null, null, 0);
647
            String revStr = parserRevisionResponse(response);
648
            Integer revObj = new Integer(revStr);
649
            rev = revObj.intValue();
650
            // Check for an error condition
651
           if (response.indexOf("<error>") != -1)
652
           {
653
              throw new MetacatException(response);
654
           }
655 1826 jones
656 2337 tao
        }
657
        catch (Exception e)
658
        {
659
            throw new MetacatException(e.getMessage());
660
        }
661
        return rev;
662
     }
663
664
665 1780 jones
    /************************************************************************
666
     * PRIVATE METHODS
667
     ************************************************************************/
668
669 1783 jones
    /**
670
     * Send a request to metacat.
671
     *
672
     * @param prop the properties to be URL encoded and sent
673 2264 sgarg
     * @param filename  the properties to be sent to Metacat
674
     *                  in case of upload, otherwise null
675
     * @param fileData  the inputStream for the file data to be sent to Metacat
676
     *                  in case of upload, otherwise null
677
     * @param size      the size of the data being sent to Metacat
678
     *                  in case of upload, otherwise 0
679 1783 jones
     */
680 2240 sgarg
    synchronized private InputStream sendDataOnce(Properties args,
681 2264 sgarg
                                                  Properties filename,
682
                                                  InputStream fileData,
683
                                                  int size)
684 1780 jones
        throws Exception
685
    {
686
        InputStream returnStream = null;
687
        URL url = new URL(metacatUrl);
688
        HttpMessage msg = new HttpMessage(url);
689 1828 jones
        msg.setCookie("JSESSIONID="+this.sessionId);
690 2264 sgarg
        if (filename == null){
691
            returnStream = msg.sendPostData(args);
692
        } else if (fileData == null){
693
            returnStream = msg.sendPostData(args, filename);
694
        } else if (size > 0) {
695
            returnStream = msg.sendPostData(args, filename, fileData, size);
696 2240 sgarg
        } else {
697 2264 sgarg
            throw new MetacatException("Invalid size specified for " +
698
                                       "the input stream being passed");
699 2240 sgarg
        }
700 1780 jones
        return returnStream;
701
    }
702
703
    /**
704
     * Send a request to Metacat
705
     *
706 2264 sgarg
     * @param args  the properties to be sent to Metacat
707
     * @param filename  the properties to be sent to Metacat
708
     *                  in case of upload, otherwise null
709
     * @param fileData  the inputStream for the file data to be sent to Metacat
710
     *                  in case of upload, otherwise null
711
     * @param size      the size of the data being sent to Metacat
712
     *                  in case of upload, otherwise 0
713 1780 jones
     * @return      InputStream as returned by Metacat
714
     */
715 2240 sgarg
    synchronized private InputStream sendData(Properties args,
716 2264 sgarg
                                              Properties filename,
717
                                              InputStream fileData,
718
                                              int size)
719 2240 sgarg
        throws Exception
720
    {
721 1780 jones
        InputStream returnStream = null;
722
723
        /*
724
            Note:  The reason that there are three try statements all executing
725
            the same code is that there is a problem with the initial connection
726 2240 sgarg
            using the HTTPClient protocol handler.  These try statements make
727
            sure that a connection is made because it gives each connection a
728 1780 jones
            2nd and 3rd chance to work before throwing an error.
729
            THIS IS A TOTAL HACK.  THIS NEEDS TO BE LOOKED INTO AFTER THE BETA1
730
            RELEASE OF MORPHO!!!  cwb (7/24/01)
731
          */
732
        try {
733 2264 sgarg
           return sendDataOnce(args, filename, fileData, size);
734 1780 jones
        } catch (Exception e) {
735
            try {
736 2264 sgarg
                return sendDataOnce(args, filename, fileData, size);
737 1780 jones
            } catch (Exception e2) {
738
                try {
739 2264 sgarg
                    return sendDataOnce(args, filename, fileData, size);
740 1780 jones
                } catch (Exception e3) {
741
                    System.err.println(
742
                            "Failed to send data to metacat 3 times.");
743
                    throw e3;
744
                }
745
            }
746
        }
747
    }
748 1783 jones
749
    /**
750
     * Send a request to Metacat
751
     *
752 2264 sgarg
     * @param args      the properties to be sent to Metacat
753
     * @param filename  the properties to be sent to Metacat
754
     *                  in case of upload, otherwise null
755
     * @param fileData  the inputStream for the file data to be sent to Metacat
756
     *                  in case of upload, otherwise null
757
     * @param size      the size of the data being sent to Metacat
758
     *                  in case of upload, otherwise 0
759
     * @return          a string as returned by Metacat
760 1783 jones
     */
761 2240 sgarg
    synchronized private String sendDataForString(Properties args,
762 2264 sgarg
                                                  Properties filename,
763
                                                  InputStream fileData,
764
                                                  int size)
765 1783 jones
        throws Exception
766
    {
767
        String response = null;
768
769
        try {
770
            InputStreamReader returnStream =
771 2264 sgarg
                    new InputStreamReader(sendData(args, filename,
772
                                                   fileData, size));
773 1783 jones
            StringWriter sw = new StringWriter();
774
            int len;
775
            char[] characters = new char[512];
776
            while ((len = returnStream.read(characters, 0, 512)) != -1) {
777
                sw.write(characters, 0, len);
778
            }
779
            returnStream.close();
780
            response = sw.toString();
781
            sw.close();
782
        } catch (Exception e) {
783
            throw e;
784
        }
785
        return response;
786
    }
787 2337 tao
788
    /*
789
     * "getversionanddoctype" action will return a string from metacat server.
790
     * The string format is "revision;doctype"(This is bad idea, we should use xml)
791
     * This method will get revision string from the response string
792
     */
793
    private String parserRevisionResponse(String response) throws Exception
794
    {
795
      String revision = null;
796
      if (response != null)
797
      {
798
        int firstSemiCol = response.indexOf(";");
799
        revision = response.substring(0, firstSemiCol);
800
      }
801
      return revision;
802
    }
803 1780 jones
}