Project

General

Profile

« Previous | Next » 

Revision 2264

Added by sgarg over 20 years ago

Added new upload function which takes InputStream as input.

View differences:

src/edu/ucsb/nceas/metacat/client/MetacatClient.java
85 85

  
86 86
        String response = null;
87 87
        try {
88
            response = sendDataForString(prop, null);
88
            response = sendDataForString(prop, null, null, 0);
89 89
        } catch (Exception e) {
90 90
            throw new MetacatInaccessibleException(e.getMessage());
91 91
        }
......
119 119

  
120 120
        String response = null;
121 121
        try {
122
            response = sendDataForString(prop, null);
122
            response = sendDataForString(prop, null, null, 0);
123 123
        } catch (Exception e) {
124 124
            throw new MetacatInaccessibleException(e.getMessage());
125 125
        }
......
155 155

  
156 156
        InputStream response = null;
157 157
        try {
158
            response = sendData(prop, null);
158
            response = sendData(prop, null, null, 0);
159 159
        } catch (Exception e) {
160 160
            throw new MetacatInaccessibleException(e.getMessage());
161 161
        }
......
211 211

  
212 212
           InputStream response = null;
213 213
           try {
214
               response = sendData(prop, null);
214
               response = sendData(prop, null, null, 0);
215 215
           } catch (Exception e) {
216 216
               throw new MetacatInaccessibleException(e.getMessage());
217 217
           }
......
268 268

  
269 269
        InputStream response = null;
270 270
        try {
271
            response = sendData(prop, null);
271
            response = sendData(prop, null, null, 0);
272 272
        } catch (Exception e) {
273 273
            throw new MetacatInaccessibleException(e.getMessage());
274 274
        }
......
318 318

  
319 319
        String response = null;
320 320
        try {
321
            response = sendDataForString(prop, null);
321
            response = sendDataForString(prop, null, null, 0);
322 322
        } catch (Exception e) {
323 323
            throw new MetacatInaccessibleException(e.getMessage());
324 324
        }
......
377 377

  
378 378
        String response = null;
379 379
        try {
380
            response = sendDataForString(prop, null);
380
            response = sendDataForString(prop, null, null, 0);
381 381
        } catch (Exception e) {
382 382
            throw new MetacatInaccessibleException(e.getMessage());
383 383
        }
......
394 394
        return response;
395 395
    }
396 396

  
397

  
398 397
    /**
399 398
       * Upload a data document into the repository.
400 399
       *
......
426 425

  
427 426
          String response = null;
428 427
          try {
429
            response = sendDataForString(arg, filenames);
428
            response = sendDataForString(arg, filenames, null, 0);
430 429
          } catch (Exception e) {
431
            e.printStackTrace();
432 430
            throw new MetacatInaccessibleException(e.getMessage());
433 431
          }
434 432

  
......
441 439
            }
442 440
          }
443 441

  
444
       return response;
442
          return response;
443
      }
445 444

  
445
      /**
446
          * 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;
446 492
      }
447 493

  
448 494
    /**
......
467 513

  
468 514
        String response = null;
469 515
        try {
470
            response = sendDataForString(prop, null);
516
            response = sendDataForString(prop, null, null, 0);
471 517
        } catch (Exception e) {
472 518
            throw new MetacatInaccessibleException(e.getMessage());
473 519
        }
......
528 574
     * Send a request to metacat.
529 575
     *
530 576
     * @param prop the properties to be URL encoded and sent
577
     * @param filename  the properties to be sent to Metacat
578
     *                  in case of upload, otherwise null
579
     * @param fileData  the inputStream for the file data to be sent to Metacat
580
     *                  in case of upload, otherwise null
581
     * @param size      the size of the data being sent to Metacat
582
     *                  in case of upload, otherwise 0
531 583
     */
532 584
    synchronized private InputStream sendDataOnce(Properties args,
533
                                                  Properties filenames)
585
                                                  Properties filename,
586
                                                  InputStream fileData,
587
                                                  int size)
534 588
        throws Exception
535 589
    {
536 590
        InputStream returnStream = null;
537 591
        URL url = new URL(metacatUrl);
538 592
        HttpMessage msg = new HttpMessage(url);
539 593
        msg.setCookie("JSESSIONID="+this.sessionId);
540
        if (filenames == null){
541
          returnStream = msg.sendPostData(args);
594
        if (filename == null){
595
            returnStream = msg.sendPostData(args);
596
        } else if (fileData == null){
597
            returnStream = msg.sendPostData(args, filename);
598
        } else if (size > 0) {
599
            returnStream = msg.sendPostData(args, filename, fileData, size);
542 600
        } else {
543
          returnStream = msg.sendPostData(args, filenames);
601
            throw new MetacatException("Invalid size specified for " +
602
                                       "the input stream being passed");
544 603
        }
545 604
        return returnStream;
546 605
    }
......
548 607
    /**
549 608
     * Send a request to Metacat
550 609
     *
551
     * @param prop  the properties to be sent to Metacat
610
     * @param args  the properties to be sent to Metacat
611
     * @param filename  the properties to be sent to Metacat
612
     *                  in case of upload, otherwise null
613
     * @param fileData  the inputStream for the file data to be sent to Metacat
614
     *                  in case of upload, otherwise null
615
     * @param size      the size of the data being sent to Metacat
616
     *                  in case of upload, otherwise 0
552 617
     * @return      InputStream as returned by Metacat
553 618
     */
554 619
    synchronized private InputStream sendData(Properties args,
555
                                              Properties filenames)
620
                                              Properties filename,
621
                                              InputStream fileData,
622
                                              int size)
556 623
        throws Exception
557 624
    {
558 625
        InputStream returnStream = null;
......
567 634
            RELEASE OF MORPHO!!!  cwb (7/24/01)
568 635
          */
569 636
        try {
570
           return sendDataOnce(args, filenames);
637
           return sendDataOnce(args, filename, fileData, size);
571 638
        } catch (Exception e) {
572 639
            try {
573
                return sendDataOnce(args, filenames);
640
                return sendDataOnce(args, filename, fileData, size);
574 641
            } catch (Exception e2) {
575 642
                try {
576
                    return sendDataOnce(args, filenames);
643
                    return sendDataOnce(args, filename, fileData, size);
577 644
                } catch (Exception e3) {
578 645
                    System.err.println(
579 646
                            "Failed to send data to metacat 3 times.");
......
586 653
    /**
587 654
     * Send a request to Metacat
588 655
     *
589
     * @param prop  the properties to be sent to Metacat
590
     * @return      a string as returned by Metacat
656
     * @param args      the properties to be sent to Metacat
657
     * @param filename  the properties to be sent to Metacat
658
     *                  in case of upload, otherwise null
659
     * @param fileData  the inputStream for the file data to be sent to Metacat
660
     *                  in case of upload, otherwise null
661
     * @param size      the size of the data being sent to Metacat
662
     *                  in case of upload, otherwise 0
663
     * @return          a string as returned by Metacat
591 664
     */
592 665
    synchronized private String sendDataForString(Properties args,
593
                                                  Properties filenames)
666
                                                  Properties filename,
667
                                                  InputStream fileData,
668
                                                  int size)
594 669
        throws Exception
595 670
    {
596 671
        String response = null;
597 672

  
598 673
        try {
599 674
            InputStreamReader returnStream =
600
                    new InputStreamReader(sendData(args, filenames));
675
                    new InputStreamReader(sendData(args, filename,
676
                                                   fileData, size));
601 677
            StringWriter sw = new StringWriter();
602 678
            int len;
603 679
            char[] characters = new char[512];
src/edu/ucsb/nceas/metacat/client/Metacat.java
27 27
import java.io.Reader;
28 28
import java.io.IOException;
29 29
import java.io.File;
30
import java.io.InputStream;
30 31

  
31 32
/**
32 33
 *  This interface provides methods for initializing and logging in to a
......
158 159
        MetacatInaccessibleException;
159 160

  
160 161
    /**
162
     * Upload an XML document into the repository.
163
     *
164
     * @param docid the docid to insert the document
165
     * @param fileName the name of the document
166
     * @param fileData InputStream of the document that has to be inserted
167
     * @param size size of the data being sent. If more data is
168
     *             found in the InputStream, an error would be reported.
169
     * @return the metacat response message
170
     * @throws InsufficientKarmaException when the user has insufficent rights
171
     *                                    for the operation
172
     * @throws MetacatInaccessibleException when the metacat server can not be
173
     *                                    reached or does not respond
174
     * @throws MetacatException when the metacat server generates another error
175
     * @throws IOException when there is an error reading the xml document
176
     */
177
    public String upload(String docid, String fileName,
178
                         InputStream fileData, int size)
179
        throws InsufficientKarmaException, MetacatException, IOException,
180
        MetacatInaccessibleException;
181

  
182
    /**
161 183
     * Delete an XML document in the repository.
162 184
     *
163 185
     * @param docid the docid to delete

Also available in: Unified diff