Project

General

Profile

« Previous | Next » 

Revision 2240

Added by sgarg about 20 years ago

Added upload functionality to metacat client for upload online data

View differences:

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

  
37 37
import edu.ucsb.nceas.utilities.HttpMessage;
38 38
import edu.ucsb.nceas.utilities.IOUtil;
39
import java.io.File;
39 40

  
40 41

  
41 42
/**
42
 *  This interface provides methods for initializing and logging in to a 
43
 *  Metacat server, and then querying, reading, transforming, inserting, 
43
 *  This interface provides methods for initializing and logging in to a
44
 *  Metacat server, and then querying, reading, transforming, inserting,
44 45
 *  updating and deleting documents from that server.
45 46
 */
46 47
public class MetacatClient implements Metacat
......
73 74
     *  @throws MetacatAuthException when the username/password could
74 75
     *                    not be authenticated
75 76
     */
76
    public String login(String username, String password) 
77
    public String login(String username, String password)
77 78
           throws MetacatAuthException, MetacatInaccessibleException
78 79
    {
79 80
        Properties prop = new Properties();
......
84 85

  
85 86
        String response = null;
86 87
        try {
87
            response = sendDataForString(prop);
88
            response = sendDataForString(prop, null);
88 89
        } catch (Exception e) {
89 90
            throw new MetacatInaccessibleException(e.getMessage());
90 91
        }
......
114 115
    {
115 116
        Properties prop = new Properties();
116 117
        prop.put("action", "logout");
117
        prop.put("qformat", "xml"); 
118
       
118
        prop.put("qformat", "xml");
119

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

  
126 127
        if (response.indexOf("<logout>") == -1) {
127 128
            throw new MetacatException(response);
128 129
        }
129 130
        setSessionId("");
130 131
        return response;
131 132
    }
132
    
133

  
133 134
    /**
134 135
     * Read an XML document from the metacat server session, accessed by docid,
135 136
     * and returned as a Reader.
136 137
     *
137 138
     * @param docid the identifier of the document to be read
138 139
     * @return a Reader for accessing the document
139
     * @throws InsufficientKarmaException when the user has insufficent rights 
140
     * @throws InsufficientKarmaException when the user has insufficent rights
140 141
     *                                    for the operation
141 142
     * @throws MetacatInaccessibleException when the metacat server can not be
142 143
     *                                    reached or does not respond
......
154 155

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

  
162 163
        pbr = new PushbackReader(new InputStreamReader(response), 512);
163 164
        try {
164 165
            char[] characters = new char[512];
......
178 179
            }
179 180
        } catch (IOException ioe) {
180 181
            throw new MetacatException(
181
                    "MetacatClient: Error converting Reader to String." 
182
                    "MetacatClient: Error converting Reader to String."
182 183
                    + ioe.getMessage());
183 184
        }
184 185

  
......
186 187
    }
187 188

  
188 189
    /**
189
     * Query the metacat document store with the given metacat-compatible 
190
     * Query the metacat document store with the given metacat-compatible
190 191
     * query document, and return the result set as a Reader.
191 192
     *
192 193
     * @param xmlQuery a Reader for accessing the XML version of the query
......
208 209
        prop.put("action", "squery");
209 210
        prop.put("qformat", "xml");
210 211
        prop.put("query", query);
211
        
212

  
212 213
        InputStream response = null;
213 214
        try {
214
            response = sendData(prop);
215
            response = sendData(prop, null);
215 216
        } catch (Exception e) {
216 217
            throw new MetacatInaccessibleException(e.getMessage());
217 218
        }
......
224 225
     *
225 226
     * @param docid the docid to insert the document
226 227
     * @param xmlDocument a Reader for accessing the XML document to be inserted
227
     * @param schema a Reader for accessing the DTD or XML Schema for 
228
     * @param schema a Reader for accessing the DTD or XML Schema for
228 229
     *               the document
229 230
     * @return the metacat response message
230
     * @throws InsufficientKarmaException when the user has insufficent rights 
231
     * @throws InsufficientKarmaException when the user has insufficent rights
231 232
     *                                    for the operation
232 233
     * @throws MetacatInaccessibleException when the metacat server can not be
233 234
     *                                    reached or does not respond
......
258 259
        if (schematext != null) {
259 260
            prop.put("dtdtext", schematext);
260 261
        }
261
        
262

  
262 263
        String response = null;
263 264
        try {
264
            response = sendDataForString(prop);
265
            response = sendDataForString(prop, null);
265 266
        } catch (Exception e) {
266 267
            throw new MetacatInaccessibleException(e.getMessage());
267 268
        }
......
283 284
     *
284 285
     * @param docid the docid to update
285 286
     * @param xmlDocument a Reader for accessing the XML text to be updated
286
     * @param schema a Reader for accessing the DTD or XML Schema for 
287
     * @param schema a Reader for accessing the DTD or XML Schema for
287 288
     *               the document
288 289
     * @return the metacat response message
289
     * @throws InsufficientKarmaException when the user has insufficent rights 
290
     * @throws InsufficientKarmaException when the user has insufficent rights
290 291
     *                                    for the operation
291 292
     * @throws MetacatInaccessibleException when the metacat server can not be
292 293
     *                                    reached or does not respond
......
317 318
        if (schematext != null) {
318 319
            prop.put("dtdtext", schematext);
319 320
        }
320
        
321

  
321 322
        String response = null;
322 323
        try {
323
            response = sendDataForString(prop);
324
            response = sendDataForString(prop, null);
324 325
        } catch (Exception e) {
325 326
            throw new MetacatInaccessibleException(e.getMessage());
326 327
        }
......
337 338
        return response;
338 339
    }
339 340

  
341

  
340 342
    /**
343
       * Upload a data document into the repository.
344
       *
345
       * @param docid the docid to insert the document
346
       * @param document a Reader for accessing the document to be uploaded
347
       * @return the metacat response message
348
       * @throws InsufficientKarmaException when the user has insufficent rights
349
       *                                    for the operation
350
       * @throws MetacatInaccessibleException when the metacat server can not be
351
       *                                    reached or does not respond
352
       * @throws MetacatException when the metacat server generates another error
353
       * @throws IOException when there is an error reading the xml document
354
       */
355
      public String upload(String docid, File file)
356
          throws InsufficientKarmaException, MetacatException, IOException,
357
          MetacatInaccessibleException
358
      {
359

  
360
          URL url = new URL(metacatUrl.trim());
361
          HttpMessage msg = new HttpMessage(url);
362
          //set up properties
363
          Properties arg = new Properties();
364
          arg.put("action", "upload");
365
          arg.put("docid", docid);
366

  
367
          Properties filenames = new Properties();
368
          String filename = file.getAbsolutePath();
369
          filenames.put("datafile", filename);
370

  
371
          String response = null;
372
          try {
373
            response = sendDataForString(arg, filenames);
374
          } catch (Exception e) {
375
            e.printStackTrace();
376
            throw new MetacatInaccessibleException(e.getMessage());
377
          }
378

  
379
          // Check for an error condition
380
          if (response.indexOf("<error>") != -1) {
381
            if (response.indexOf("does not have permission") != -1) {
382
              throw new InsufficientKarmaException(response);
383
            } else {
384
              throw new MetacatException(response);
385
            }
386
          }
387

  
388
       return response;
389

  
390
      }
391

  
392
    /**
341 393
     * Delete an XML document in the repository.
342 394
     *
343 395
     * @param docid the docid to delete
344 396
     * @return the metacat response message
345
     * @throws InsufficientKarmaException when the user has insufficent rights 
397
     * @throws InsufficientKarmaException when the user has insufficent rights
346 398
     *                                    for the operation
347 399
     * @throws MetacatInaccessibleException when the metacat server can not be
348 400
     *                                    reached or does not respond
......
356 408
        Properties prop = new Properties();
357 409
        prop.put("action", "delete");
358 410
        prop.put("docid", docid);
359
        
411

  
360 412
        String response = null;
361 413
        try {
362
            response = sendDataForString(prop);
414
            response = sendDataForString(prop, null);
363 415
        } catch (Exception e) {
364 416
            throw new MetacatInaccessibleException(e.getMessage());
365 417
        }
......
400 452
    }
401 453

  
402 454
    /**
403
     * Set the session identifier for this session.  This identifier was 
404
     * previously established with a call to login.  To continue to use the 
455
     * Set the session identifier for this session.  This identifier was
456
     * previously established with a call to login.  To continue to use the
405 457
     * same session, set the session id before making a call to one of the
406 458
     * metacat access methods (e.g., read, query, insert, etc.).
407 459
     *
......
421 473
     *
422 474
     * @param prop the properties to be URL encoded and sent
423 475
     */
424
    synchronized private InputStream sendDataOnce(Properties prop) 
476
    synchronized private InputStream sendDataOnce(Properties args,
477
                                                  Properties filenames)
425 478
        throws Exception
426 479
    {
427 480
        InputStream returnStream = null;
428 481
        URL url = new URL(metacatUrl);
429 482
        HttpMessage msg = new HttpMessage(url);
430 483
        msg.setCookie("JSESSIONID="+this.sessionId);
431
        returnStream = msg.sendPostData(prop);
484
        if (filenames == null){
485
          returnStream = msg.sendPostData(args);
486
        } else {
487
          returnStream = msg.sendPostData(args, filenames);
488
        }
432 489
        return returnStream;
433 490
    }
434 491

  
......
438 495
     * @param prop  the properties to be sent to Metacat
439 496
     * @return      InputStream as returned by Metacat
440 497
     */
441
    synchronized private InputStream sendData(Properties prop) throws Exception
442
    {   
498
    synchronized private InputStream sendData(Properties args,
499
                                              Properties filenames)
500
        throws Exception
501
    {
443 502
        InputStream returnStream = null;
444 503

  
445 504
        /*
446 505
            Note:  The reason that there are three try statements all executing
447 506
            the same code is that there is a problem with the initial connection
448
            using the HTTPClient protocol handler.  These try statements make 
449
            sure that a connection is made because it gives each connection a 
507
            using the HTTPClient protocol handler.  These try statements make
508
            sure that a connection is made because it gives each connection a
450 509
            2nd and 3rd chance to work before throwing an error.
451 510
            THIS IS A TOTAL HACK.  THIS NEEDS TO BE LOOKED INTO AFTER THE BETA1
452 511
            RELEASE OF MORPHO!!!  cwb (7/24/01)
453 512
          */
454 513
        try {
455
           return sendDataOnce(prop);
514
           return sendDataOnce(args, filenames);
456 515
        } catch (Exception e) {
457 516
            try {
458
                return sendDataOnce(prop);
517
                return sendDataOnce(args, filenames);
459 518
            } catch (Exception e2) {
460 519
                try {
461
                    return sendDataOnce(prop);
520
                    return sendDataOnce(args, filenames);
462 521
                } catch (Exception e3) {
463 522
                    System.err.println(
464 523
                            "Failed to send data to metacat 3 times.");
......
474 533
     * @param prop  the properties to be sent to Metacat
475 534
     * @return      a string as returned by Metacat
476 535
     */
477
    synchronized private String sendDataForString(Properties prop) 
536
    synchronized private String sendDataForString(Properties args,
537
                                                  Properties filenames)
478 538
        throws Exception
479 539
    {
480 540
        String response = null;
481 541

  
482 542
        try {
483 543
            InputStreamReader returnStream =
484
                    new InputStreamReader(sendData(prop));
544
                    new InputStreamReader(sendData(args, filenames));
485 545
            StringWriter sw = new StringWriter();
486 546
            int len;
487 547
            char[] characters = new char[512];
src/edu/ucsb/nceas/metacat/client/Metacat.java
26 26

  
27 27
import java.io.Reader;
28 28
import java.io.IOException;
29
import java.io.File;
29 30

  
30 31
/**
31
 *  This interface provides methods for initializing and logging in to a 
32
 *  Metacat server, and then querying, reading, transforming, inserting, 
32
 *  This interface provides methods for initializing and logging in to a
33
 *  Metacat server, and then querying, reading, transforming, inserting,
33 34
 *  updating and deleting documents from that server.
34 35
 */
35 36
public interface Metacat
......
46 47
     *  @throws MetacatAuthException when the username/password could
47 48
     *                    not be authenticated
48 49
     */
49
    public String login(String username, String password) 
50
    public String login(String username, String password)
50 51
           throws MetacatAuthException, MetacatInaccessibleException;
51
    
52

  
52 53
    /**
53 54
     *  Method used to log out a metacat server. The Metacat server will end
54 55
     *  the session when this call is invoked.
......
57 58
     *  @throws MetacatInaccessibleException when the metacat server can not be
58 59
     *                                    reached or does not respond
59 60
     */
60
    public String logout() throws MetacatInaccessibleException, 
61
    public String logout() throws MetacatInaccessibleException,
61 62
        MetacatException;
62 63

  
63 64
    /**
......
66 67
     *
67 68
     * @param docid the identifier of the document to be read
68 69
     * @return a Reader for accessing the document
69
     * @throws InsufficientKarmaException when the user has insufficent rights 
70
     * @throws InsufficientKarmaException when the user has insufficent rights
70 71
     *                                    for the operation
71 72
     * @throws MetacatInaccessibleException when the metacat server can not be
72 73
     *                                    reached or does not respond
......
76 77
        MetacatInaccessibleException, MetacatException;
77 78

  
78 79
    /**
79
     * Query the metacat document store with the given metacat-compatible 
80
     * Query the metacat document store with the given metacat-compatible
80 81
     * query document, and return the result set as a Reader.
81 82
     *
82 83
     * @param xmlQuery a Reader for accessing the XML version of the query
......
90 91
     *
91 92
     * @param docid the docid to insert the document
92 93
     * @param xmlDocument a Reader for accessing the XML document to be inserted
93
     * @param schema a Reader for accessing the DTD or XML Schema for 
94
     * @param schema a Reader for accessing the DTD or XML Schema for
94 95
     *               the document
95 96
     * @return the metacat response message
96
     * @throws InsufficientKarmaException when the user has insufficent rights 
97
     * @throws InsufficientKarmaException when the user has insufficent rights
97 98
     *                                    for the operation
98 99
     * @throws MetacatInaccessibleException when the metacat server can not be
99 100
     *                                    reached or does not respond
......
109 110
     *
110 111
     * @param docid the docid to update
111 112
     * @param xmlDocument a Reader for accessing the XML text to be updated
112
     * @param schema a Reader for accessing the DTD or XML Schema for 
113
     * @param schema a Reader for accessing the DTD or XML Schema for
113 114
     *               the document
114 115
     * @return the metacat response message
115
     * @throws InsufficientKarmaException when the user has insufficent rights 
116
     * @throws InsufficientKarmaException when the user has insufficent rights
116 117
     *                                    for the operation
117 118
     * @throws MetacatInaccessibleException when the metacat server can not be
118 119
     *                                    reached or does not respond
......
124 125
        MetacatInaccessibleException;
125 126

  
126 127
    /**
128
     * Upload an XML document into the repository.
129
     *
130
     * @param docid the docid to insert the document
131
     * @param xmlDocument a Reader for accessing the document to be inserted
132
     * @return the metacat response message
133
     * @throws InsufficientKarmaException when the user has insufficent rights
134
     *                                    for the operation
135
     * @throws MetacatInaccessibleException when the metacat server can not be
136
     *                                    reached or does not respond
137
     * @throws MetacatException when the metacat server generates another error
138
     * @throws IOException when there is an error reading the xml document
139
     */
140
    public String upload(String docid, File file)
141
        throws InsufficientKarmaException, MetacatException, IOException,
142
        MetacatInaccessibleException;
143

  
144
    /**
127 145
     * Delete an XML document in the repository.
128 146
     *
129 147
     * @param docid the docid to delete
130 148
     * @return the metacat response message
131
     * @throws InsufficientKarmaException when the user has insufficent rights 
149
     * @throws InsufficientKarmaException when the user has insufficent rights
132 150
     *                                    for the operation
133 151
     * @throws MetacatInaccessibleException when the metacat server can not be
134 152
     *                                    reached or does not respond
......
154 172
    public String getSessionId();
155 173

  
156 174
    /**
157
     * Set the session identifier for this session.  This identifier was 
158
     * previously established with a call to login.  To continue to use the 
175
     * Set the session identifier for this session.  This identifier was
176
     * previously established with a call to login.  To continue to use the
159 177
     * same session, set the session id before making a call to one of the
160 178
     * metacat access methods (e.g., read, query, insert, etc.).
161 179
     *

Also available in: Unified diff