Project

General

Profile

1 1929 brooke
 #
2
 #  '$RCSfile$'
3
 #  Copyright: 2000 Regents of the University of California
4
 #
5
 #   '$Author$'
6
 #     '$Date$'
7
 # '$Revision$'
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 1955 jones
  $self->{'message'} = '';
100 1929 brooke
  my $userAgent = new LWP::UserAgent;
101
  $userAgent->agent("MetacatClient/1.0");
102
103
  my $request = new HTTP::Request('POST' => "$self->{'metacatUrl'}");
104
  my $cookie_jar = $self->{'cookies'};
105
  $$cookie_jar->add_cookie_header($request);
106
  $request->content_type('application/x-www-form-urlencoded');
107
  foreach my $key (keys %postData) {
108
    $request->add_content("$key=$postData{$key}&")
109
  }
110
111
  my $response = $userAgent->request($request);
112
113
  if ($response->is_success) {
114
    # save the cookies
115
    $$cookie_jar->extract_cookies($response);
116
    # save the metacat response message
117
    $self->{'message'} = $response->content;
118
  } else {
119
    #print "SendData content is: ", $response->content, "\n";
120
    return 0;
121
  }
122
  return $response;
123
}
124
125
#############################################################
126
# subroutine to log into Metacat and save the cookie if the
127 2684 sgarg
# login is valid.  If not valid, return 0. If valid then send
128
# following values to indicate user status
129
# 1 - user
130
# 2 - moderator
131
# 3 - administrator
132
# 4 - moderator and administrator
133 1929 brooke
#############################################################
134
sub login {
135
  my $self = shift;
136
  my $username = shift;
137
  my $password = shift;
138
139
  my $returnval = 0;
140
141
  my %postData = ( action => 'login',
142
                   qformat => 'xml',
143
                   username => $username,
144
                   password => $password
145
                 );
146
  my $response = $self->sendData(%postData);
147
  if (($response) && $response->content =~ /<login>/) {
148
    $returnval = 1;
149
  }
150
151 2684 sgarg
  if (($response) && $response->content =~ /<isAdministrator>/) {
152
	if (($response) && $response->content =~ /<isModerator>/) {
153
    		$returnval = 4;
154
	} else {
155
		$returnval = 3;
156
	}
157
  } elsif (($response) && $response->content =~ /<isModerator>/){
158
	$returnval = 2;
159
  }
160
161 1929 brooke
  return $returnval;
162
}
163
164
#############################################################
165
# subroutine to insert an XML document into Metacat
166
# If success, return 1, else return 0
167
#############################################################
168
sub insert {
169
  my $self = shift;
170
  my $docid = shift;
171
  my $xmldocument = shift;
172
  my $dtd = shift;
173
174
  my $returnval = 0;
175
176
  my %postData = ( action => 'insert',
177
                   docid => $docid,
178
                   doctext => $xmldocument
179
                 );
180
  if ($dtd) {
181
    $postData{'dtdtext'} = $dtd;
182
  }
183
184
  my $response = $self->sendData(%postData);
185
  if (($response) && $response->content =~ /<success>/) {
186
    $returnval = 1;
187
  } elsif (($response)) {
188
    $returnval = 0;
189
    #print "Error response from sendData!\n";
190
    #print $response->content, "\n";
191
  } else {
192
    $returnval = 0;
193
    #print "Invalid response from sendData!\n";
194
  }
195
196
  return $returnval;
197
}
198
199
#############################################################
200
# subroutine to update an XML document in Metacat
201
# If success, return 1, else return 0
202
#############################################################
203
sub update {
204
  my $self = shift;
205
  my $docid = shift;
206
  my $xmldocument = shift;
207
  my $dtd = shift;
208
209
  my $returnval = 0;
210
211
  my %postData = ( action => 'update',
212
                   docid => $docid,
213
                   doctext => $xmldocument
214
                 );
215
  if ($dtd) {
216
    $postData{'dtdtext'} = $dtd;
217
  }
218
219
  my $response = $self->sendData(%postData);
220
  if (($response) && $response->content =~ /<success>/) {
221
    $returnval = 1;
222
  }
223
224
  return $returnval;
225
}
226
227
#############################################################
228
# subroutine to delete an XML document in Metacat
229
# If success, return 1, else return 0
230
#############################################################
231
sub delete {
232
  my $self = shift;
233
  my $docid = shift;
234
235
  my $returnval = 0;
236
237
  my %postData = ( action => 'delete',
238
                   docid => $docid
239
                 );
240
241
  my $response = $self->sendData(%postData);
242
  if (($response) && $response->content =~ /<success>/) {
243
    $returnval = 1;
244
  }
245
246
  return $returnval;
247
}
248
249
#############################################################
250
# subroutine to read an XML document from Metacat
251
# returns the XML from Metacat, which may be an error response
252
#############################################################
253
sub read {
254
  my $self = shift;
255
  my $docid = shift;
256
257
  my %postData = ( action => 'read',
258
                   qformat => 'xml',
259
                   docid => $docid
260
                 );
261
262
  my $response = $self->sendData(%postData);
263
264
  my $returnval = 0;
265
  if ($response) {
266
    $returnval = $response;
267
  }
268
269
  return $returnval;
270
}
271
272
#############################################################
273
# subroutine to query metacat using a structured path query
274
# returns the XML from Metacat, which may be an error response
275
#############################################################
276
sub squery {
277
  my $self = shift;
278
  my $query = shift;
279
280
  my %postData = ( action => 'squery',
281
                   qformat => 'xml',
282
                   query => $query
283
                 );
284
285
  my $response = $self->sendData(%postData);
286
287
  my $returnval = 0;
288
  if ($response) {
289
    $returnval = $response;
290
  }
291
292
  return $returnval;
293
}
294
295
#############################################################
296 1953 jones
# subroutine to get the maximimum id in a series
297
# If success, return max id, else return 0
298
#############################################################
299
sub getLastId {
300
  my $self = shift;
301
  my $scope = shift;
302
303
  my $returnval = 0;
304
305
  my %postData = ( action => 'getlastdocid',
306
                   scope => $scope
307
                 );
308
309
  my $response = $self->sendData(%postData);
310
  if (($response) && $response->content =~ /<docid>(.*)<\/docid>/s) {
311
      $returnval = "$1";
312
  } elsif (($response)) {
313
    $returnval = 0;
314
    #print "Error response from sendData!\n";
315
    #print $response->content, "\n";
316
  } else {
317
    $returnval = 0;
318
    #print "Invalid response from sendData!\n";
319
  }
320
321
  return $returnval;
322
}
323
#############################################################
324 1929 brooke
# subroutine to get the message returned from the last executed
325
# metacat action.  These are generally XML formatted messages.
326
#############################################################
327
sub getMessage {
328
  my $self = shift;
329
330
  return $self->{'message'};
331
}
332
333
#############################################################
334
# subroutine to get the cookies returned from the metacat
335
# server to establish (and pass on) session info (JSESSIONID).
336
#############################################################
337
sub getCookies {
338
  my $self = shift;
339
340
  return $self->{'cookies'};
341
}
342
343
# Autoload methods go after =cut, and are processed by the autosplit program.
344
345
1;
346
__END__
347
# Below is stub documentation for your module. You better edit it!
348
349
=head1 NAME
350
351
Metacat - Perl extension for communicating with the Metacat XML database
352
353
=head1 SYNOPSIS
354
355
  use Metacat;
356
  my $metacat = Metacat->new();
357
  my $response = $metacat->login($username, $password);
358
  print $metacat->getMessage();
359
  $response = $metacat->insert($docid, $xmldoc);
360
  print $metacat->getMessage();
361
  $response = $metacat->insert($docid, $xmldoc, $dtd);
362
  print $metacat->getMessage();
363
  $response = $metacat->update($docid, $xmldoc);
364
  print $metacat->getMessage();
365
  $htmlResponse = $metacat->read($docid);
366
  $xmldoc = $htmlResponse->content();
367
  print $xmldoc;
368
  $resultset = $metacat->squery($pathquery);
369
  print $resultset;
370
  $response = $metacat->delete($docid);
371 1953 jones
  my $lastid = $metacat->getLastId("obfs");
372 1929 brooke
  print $metacat->getMessage();
373
  $response = $metacat->getCookies();
374
  print $metacat->getMessage();
375
376
=head1 DESCRIPTION
377
378
This is a client library for accessing the Metacat XML database.  Metacat
379
is a Java servlet that accepts commands over HTTP and returns XML and
380
HTML responses.  See http://knb.ecoinformatics.org for details about
381
Metacat and its interface.
382
383
=head2 EXPORT
384
385
None by default.
386
387
388
=head1 AUTHOR
389
390
Matthew B. Jones, jones@nceas.ucsb.edu
391
392
=head1 SEE ALSO
393
394
perl(1).
395
396
=cut