1
|
#!/usr/bin/python
|
2
|
#
|
3
|
# '$RCSfile$'
|
4
|
# Copyright: 2000 Regents of the University of California
|
5
|
#
|
6
|
# '$Author: perry $'
|
7
|
# '$Date: 2006-12-15 10:23:32 -0800 (Fri, 15 Dec 2006) $'
|
8
|
# '$Revision: 3122 $'
|
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
|
# TODO:
|
25
|
# validate
|
26
|
# getNextDocid
|
27
|
# getNextRevision(docid)
|
28
|
# getDocids(keyword=None)
|
29
|
# query(keyword, returnfields)
|
30
|
# queryDict => same as above but returns python dictionary data structure
|
31
|
|
32
|
import httplib, urllib
|
33
|
|
34
|
class MetacatClient:
|
35
|
|
36
|
def __init__(self, server="knb.ecoinformatics.org", urlPath="/metacat/metacat"):
|
37
|
self.metacatUrlPath = urlPath
|
38
|
self.metacatServer = server
|
39
|
self.sessionid = None
|
40
|
|
41
|
def getMetacatUrl(self):
|
42
|
return "http://" + self.metacatServer + self.metacatUrlPath
|
43
|
|
44
|
def login(self, username, password, organization=None):
|
45
|
|
46
|
if organization == 'NCEAS':
|
47
|
uid = 'uid=%s,o=NCEAS,dc=ecoinformatics,dc=org' % username
|
48
|
else:
|
49
|
uid = username
|
50
|
|
51
|
postdata = { 'action' : 'login',
|
52
|
'qformat' : 'xml',
|
53
|
'username' : uid,
|
54
|
'password' : password }
|
55
|
|
56
|
response = self.postRequest(postdata)
|
57
|
if response.find("<login>") != -1:
|
58
|
return True
|
59
|
else:
|
60
|
print response
|
61
|
return False
|
62
|
|
63
|
def logout(self):
|
64
|
postdata = { 'action' : 'logout',
|
65
|
'qformat' : 'xml'}
|
66
|
|
67
|
response = self.postRequest(postdata)
|
68
|
if response.find("<logout>") != -1:
|
69
|
return True
|
70
|
else:
|
71
|
return False
|
72
|
|
73
|
def read(self, docid, qformat="xml"):
|
74
|
postdata = { 'action' : 'read',
|
75
|
'qformat' : qformat,
|
76
|
'docid' : docid }
|
77
|
response = self.postRequest(postdata)
|
78
|
# if error node returned
|
79
|
return response
|
80
|
|
81
|
|
82
|
def insert(self, docid, doctext):
|
83
|
postdata = { 'action' : 'insert',
|
84
|
'doctext' : doctext,
|
85
|
'docid' : docid }
|
86
|
response = self.postRequest(postdata)
|
87
|
# if error node returned
|
88
|
return response
|
89
|
|
90
|
def update(self, docid, doctext):
|
91
|
postdata = { 'action' : 'update',
|
92
|
'doctext' : doctext,
|
93
|
'docid' : docid }
|
94
|
response = self.postRequest(postdata)
|
95
|
return response
|
96
|
|
97
|
def delete(self, docid):
|
98
|
postdata = { 'action' : 'delete',
|
99
|
'docid' : docid }
|
100
|
response = self.postRequest(postdata)
|
101
|
return response
|
102
|
|
103
|
def squery(self, pathquery, qformat="xml"):
|
104
|
postdata = { 'action' : 'squery',
|
105
|
'qformat' : qformat,
|
106
|
'query' : pathquery }
|
107
|
response = self.postRequest(postdata)
|
108
|
return response
|
109
|
|
110
|
def postRequest(self, postdata):
|
111
|
conn = httplib.HTTPConnection( self.metacatServer )
|
112
|
params = urllib.urlencode( postdata )
|
113
|
headers = { "Content-type" : "application/x-www-form-urlencoded",
|
114
|
"Accept" : "*/*"}
|
115
|
|
116
|
# If we have an active session, set the cookie
|
117
|
if self.sessionid is not None:
|
118
|
headers['Cookie'] = self.sessionid
|
119
|
|
120
|
conn.request( "POST", self.metacatUrlPath, params, headers )
|
121
|
response = conn.getresponse()
|
122
|
|
123
|
# If metacat responds with a new session id,
|
124
|
# register it with the metacat client instance
|
125
|
setcookie = response.getheader("set-cookie", None)
|
126
|
if setcookie:
|
127
|
jsid = setcookie.split(';')[0]
|
128
|
if jsid[:11] == "JSESSIONID=":
|
129
|
self.sessionid = jsid
|
130
|
|
131
|
if response.status == 200:
|
132
|
content = response.read()
|
133
|
else:
|
134
|
print " SERVER DID NOT RETURN 'OK'.... STATUS is " + str(response.status)
|
135
|
content = ""
|
136
|
conn.close()
|
137
|
return content
|
138
|
|
139
|
def getRequest(self, queryString):
|
140
|
conn = httplib.HTTPConnection( self.metacatServer )
|
141
|
#params = urllib.urlencode( postdata )
|
142
|
#headers = { "Content-type" : "application/x-www-form-urlencoded",
|
143
|
# "Accept" : "*/*"}
|
144
|
|
145
|
# If we have an active session, set the cookie
|
146
|
#if self.sessionid is not None:
|
147
|
# headers['Cookie'] = self.sessionid
|
148
|
|
149
|
conn.request( "GET", self.metacatUrlPath + queryString )
|
150
|
response = conn.getresponse()
|
151
|
|
152
|
# If metacat responds with a new session id,
|
153
|
# register it with the metacat client instance
|
154
|
#setcookie = response.getheader("set-cookie", None)
|
155
|
#if setcookie:
|
156
|
# jsid = setcookie.split(';')[0]
|
157
|
# if jsid[:11] == "JSESSIONID=":
|
158
|
# self.sessionid = jsid
|
159
|
|
160
|
if response.status == 200:
|
161
|
content = response.read()
|
162
|
else:
|
163
|
print " SERVER DID NOT RETURN 'OK'.... STATUS is " + str(response.status)
|
164
|
content = ""
|
165
|
conn.close()
|
166
|
return content
|
167
|
|