Project

General

Profile

1
 #
2
 #  '$RCSfile$'
3
 #  Copyright: 2000 Regents of the University of California 
4
 #
5
 #   '$Author: brooke $'
6
 #     '$Date: 2003-11-21 15:05:54 -0800 (Fri, 21 Nov 2003) $'
7
 # '$Revision: 1929 $' 
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

    
24
package Metacat;
25

    
26
require 5.005_62;
27
use strict;
28
use warnings;
29

    
30
require Exporter;
31
use AutoLoader qw(AUTOLOAD);
32

    
33
use LWP::UserAgent;
34
use HTTP::Cookies;
35

    
36
our @ISA = qw(Exporter);
37

    
38
# Items to export into callers namespace by default. Note: do not export
39
# names by default without a very good reason. Use EXPORT_OK instead.
40
# Do not simply export all your public functions/methods/constants.
41

    
42
# This allows declaration	use Metacat ':all';
43
# If you do not need this, moving things directly into @EXPORT or @EXPORT_OK
44
# will save memory.
45
our %EXPORT_TAGS = ( 'all' => [ qw(
46
	
47
) ] );
48

    
49
our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
50

    
51
our @EXPORT = qw(
52
	
53
);
54
our $VERSION = '0.01';
55

    
56

    
57
# Preloaded methods go here.
58

    
59
#############################################################
60
# Constructor creates a new class instance and inits all
61
# of the instance variables to their proper default values,
62
# which can later be changed using "set_options"
63
#############################################################
64
sub new {
65
  my $cookie_jar = HTTP::Cookies->new;
66

    
67
  my $type = {
68
    metacatUrl     => 'http://dev.nceas.ucsb.edu/jones/servlet/metacat',
69
    message        => '',
70
    cookies        => \$cookie_jar
71
  };
72

    
73
  bless $type, shift;
74
  return $type;
75
}
76

    
77
#############################################################
78
# subroutine to set options for the class, including the URL 
79
# for the Metacat database to which we would connect
80
#############################################################
81
sub set_options {
82
  my $self = shift;
83
  my %newargs = ( @_ );
84

    
85
  my $arg;
86
  foreach $arg (keys %newargs) {
87
    $self->{$arg} = $newargs{$arg};
88
  }
89
}
90

    
91
#############################################################
92
# subroutine to send data to metacat and get the response
93
# return response from metacat
94
#############################################################
95
sub sendData {
96
  my $self = shift;
97
  my %postData = ( @_ );
98

    
99
  my $userAgent = new LWP::UserAgent;
100
  $userAgent->agent("MetacatClient/1.0");
101
  
102
  my $request = new HTTP::Request('POST' => "$self->{'metacatUrl'}");
103
  my $cookie_jar = $self->{'cookies'};
104
  $$cookie_jar->add_cookie_header($request);
105
  $request->content_type('application/x-www-form-urlencoded');
106
  foreach my $key (keys %postData) {
107
    $request->add_content("$key=$postData{$key}&")
108
  }
109

    
110
  my $response = $userAgent->request($request);
111
  
112
  if ($response->is_success) {
113
    # save the cookies
114
    $$cookie_jar->extract_cookies($response);
115
    # save the metacat response message
116
    $self->{'message'} = $response->content;
117
  } else {
118
    #print "SendData content is: ", $response->content, "\n";
119
    return 0;
120
  } 
121
  return $response;
122
}
123

    
124
#############################################################
125
# subroutine to log into Metacat and save the cookie if the
126
# login is valid.  If valid, return 1, else return 0
127
#############################################################
128
sub login {
129
  my $self = shift;
130
  my $username = shift;
131
  my $password = shift;
132

    
133
  my $returnval = 0;
134

    
135
  my %postData = ( action => 'login',
136
                   qformat => 'xml',
137
                   username => $username,
138
                   password => $password
139
                 );
140
  my $response = $self->sendData(%postData);
141
  if (($response) && $response->content =~ /<login>/) {
142
    $returnval = 1;
143
  }
144

    
145
  return $returnval;
146
}
147

    
148
#############################################################
149
# subroutine to insert an XML document into Metacat
150
# If success, return 1, else return 0
151
#############################################################
152
sub insert {
153
  my $self = shift;
154
  my $docid = shift;
155
  my $xmldocument = shift;
156
  my $dtd = shift;
157

    
158
  my $returnval = 0;
159

    
160
  my %postData = ( action => 'insert',
161
                   docid => $docid,
162
                   doctext => $xmldocument
163
                 );
164
  if ($dtd) {
165
    $postData{'dtdtext'} = $dtd;
166
  }
167

    
168
  my $response = $self->sendData(%postData);
169
  if (($response) && $response->content =~ /<success>/) {
170
    $returnval = 1;
171
  } elsif (($response)) {
172
    $returnval = 0;
173
    #print "Error response from sendData!\n";
174
    #print $response->content, "\n";
175
  } else {
176
    $returnval = 0;
177
    #print "Invalid response from sendData!\n";
178
  }
179

    
180
  return $returnval;
181
}
182

    
183
#############################################################
184
# subroutine to update an XML document in Metacat
185
# If success, return 1, else return 0
186
#############################################################
187
sub update {
188
  my $self = shift;
189
  my $docid = shift;
190
  my $xmldocument = shift;
191
  my $dtd = shift;
192

    
193
  my $returnval = 0;
194

    
195
  my %postData = ( action => 'update',
196
                   docid => $docid,
197
                   doctext => $xmldocument
198
                 );
199
  if ($dtd) {
200
    $postData{'dtdtext'} = $dtd;
201
  }
202

    
203
  my $response = $self->sendData(%postData);
204
  if (($response) && $response->content =~ /<success>/) {
205
    $returnval = 1;
206
  }
207

    
208
  return $returnval;
209
}
210

    
211
#############################################################
212
# subroutine to delete an XML document in Metacat
213
# If success, return 1, else return 0
214
#############################################################
215
sub delete {
216
  my $self = shift;
217
  my $docid = shift;
218

    
219
  my $returnval = 0;
220

    
221
  my %postData = ( action => 'delete',
222
                   docid => $docid
223
                 );
224

    
225
  my $response = $self->sendData(%postData);
226
  if (($response) && $response->content =~ /<success>/) {
227
    $returnval = 1;
228
  }
229

    
230
  return $returnval;
231
}
232

    
233
#############################################################
234
# subroutine to read an XML document from Metacat
235
# returns the XML from Metacat, which may be an error response
236
#############################################################
237
sub read {
238
  my $self = shift;
239
  my $docid = shift;
240

    
241
  my %postData = ( action => 'read',
242
                   qformat => 'xml',
243
                   docid => $docid
244
                 );
245

    
246
  my $response = $self->sendData(%postData);
247
  
248
  my $returnval = 0;
249
  if ($response) {
250
    $returnval = $response;
251
  } 
252
    
253
  return $returnval;
254
}
255

    
256
#############################################################
257
# subroutine to query metacat using a structured path query
258
# returns the XML from Metacat, which may be an error response
259
#############################################################
260
sub squery {
261
  my $self = shift;
262
  my $query = shift;
263

    
264
  my %postData = ( action => 'squery',
265
                   qformat => 'xml',
266
                   query => $query
267
                 );
268

    
269
  my $response = $self->sendData(%postData);
270

    
271
  my $returnval = 0;
272
  if ($response) {
273
    $returnval = $response;
274
  } 
275
    
276
  return $returnval;
277
}
278

    
279
#############################################################
280
# subroutine to get the message returned from the last executed
281
# metacat action.  These are generally XML formatted messages.
282
#############################################################
283
sub getMessage {
284
  my $self = shift;
285

    
286
  return $self->{'message'};
287
}
288

    
289
#############################################################
290
# subroutine to get the cookies returned from the metacat 
291
# server to establish (and pass on) session info (JSESSIONID).
292
#############################################################
293
sub getCookies {
294
  my $self = shift;
295

    
296
  return $self->{'cookies'};
297
}
298

    
299
# Autoload methods go after =cut, and are processed by the autosplit program.
300

    
301
1;
302
__END__
303
# Below is stub documentation for your module. You better edit it!
304

    
305
=head1 NAME
306

    
307
Metacat - Perl extension for communicating with the Metacat XML database
308

    
309
=head1 SYNOPSIS
310

    
311
  use Metacat;
312
  my $metacat = Metacat->new();
313
  my $response = $metacat->login($username, $password); 
314
  print $metacat->getMessage();
315
  $response = $metacat->insert($docid, $xmldoc); 
316
  print $metacat->getMessage();
317
  $response = $metacat->insert($docid, $xmldoc, $dtd); 
318
  print $metacat->getMessage();
319
  $response = $metacat->update($docid, $xmldoc); 
320
  print $metacat->getMessage();
321
  $htmlResponse = $metacat->read($docid); 
322
  $xmldoc = $htmlResponse->content();
323
  print $xmldoc;
324
  $resultset = $metacat->squery($pathquery); 
325
  print $resultset;
326
  $response = $metacat->delete($docid); 
327
  print $metacat->getMessage();
328
  $response = $metacat->getCookies(); 
329
  print $metacat->getMessage();
330

    
331
=head1 DESCRIPTION
332

    
333
This is a client library for accessing the Metacat XML database.  Metacat
334
is a Java servlet that accepts commands over HTTP and returns XML and
335
HTML responses.  See http://knb.ecoinformatics.org for details about
336
Metacat and its interface.
337

    
338
=head2 EXPORT
339

    
340
None by default.
341

    
342

    
343
=head1 AUTHOR
344

    
345
Matthew B. Jones, jones@nceas.ucsb.edu
346

    
347
=head1 SEE ALSO
348

    
349
perl(1).
350

    
351
=cut
(5-5/7)