11 | | * TODO |
| 11 | {{{ |
| 12 | Python 2.6 |
| 13 | Wolfgang Grunberg |
| 14 | Arizona Geological Survey |
| 15 | 10/20/2009 |
| 16 | """ |
| 17 | |
| 18 | # Library Imports |
| 19 | import urllib |
| 20 | import urllib2 |
| 21 | import cookielib |
| 22 | |
| 23 | # module globals and constants |
| 24 | __author__ = "Wolfgang Grunberg" |
| 25 | __credits__ = ["Wolfgang Grunberg", "the Internets"] |
| 26 | __email__ = "wgrunberg@azgs.az.gov" |
| 27 | __status__ = "Prototype" # "Prototype", "Development", or "Production" |
| 28 | |
| 29 | # GeoNetwork constants |
| 30 | gn_username = "admin" |
| 31 | gn_password = "admin" |
| 32 | gn_baseURL = "http://localhost:8080" |
| 33 | gn_loginURI = "/geonetwork/srv/en/xml.user.login" |
| 34 | gn_logoutURI = "/geonetwork/srv/en/xml.user.logout" |
| 35 | gn_cswURI = "/geonetwork/srv/en/csw" |
| 36 | |
| 37 | |
| 38 | def gn_csw_transaction(): |
| 39 | """ |
| 40 | Execute GeoNetwork authentication and CSW transaction post |
| 41 | """ |
| 42 | # HTTP header for authentication |
| 43 | header_urlencode = {"Content-type": "application/x-www-form-urlencoded", "Accept": "text/plain"} |
| 44 | # HTTP header for CSW request |
| 45 | header_xml = {"Content-type": "application/xml", "Accept": "text/plain"} |
| 46 | # authentication Post parameters |
| 47 | post_parameters = urllib.urlencode({"username": gn_username, "password": gn_password}) |
| 48 | |
| 49 | # Sample CSW transactions |
| 50 | xml_request = "<?xml version=\"1.0\"?>\ |
| 51 | <csw:DescribeRecord xmlns:csw=\"http://www.opengis.net/cat/csw/2.0.2\"\ |
| 52 | service=\"CSW\" version=\"2.0.2\" outputFormat=\"application/xml\"\ |
| 53 | schemaLanguage=\"http://www.w3.org/XML/Schema\"/>" |
| 54 | |
| 55 | # log in URL |
| 56 | url_in = gn_baseURL+gn_loginURI |
| 57 | # log out URL |
| 58 | url_out = gn_baseURL+gn_logoutURI |
| 59 | # CSW URL |
| 60 | url_csw = gn_baseURL+gn_cswURI |
| 61 | |
| 62 | |
| 63 | # first, always log out |
| 64 | request = urllib2.Request(url_out) |
| 65 | response = urllib2.urlopen(request) |
| 66 | #print response.read() # debug |
| 67 | |
| 68 | # send authentication request |
| 69 | request = urllib2.Request(url_in, post_parameters, header_urlencode) |
| 70 | response = urllib2.urlopen(request) |
| 71 | # a basic memory-only cookie jar instance |
| 72 | cookies = cookielib.CookieJar() |
| 73 | cookies.extract_cookies(response,request) |
| 74 | cookie_handler= urllib2.HTTPCookieProcessor( cookies ) |
| 75 | # a redirect handler |
| 76 | redirect_handler= urllib2.HTTPRedirectHandler() |
| 77 | # save cookie and redirect handler for future HTTP Posts |
| 78 | opener = urllib2.build_opener(redirect_handler,cookie_handler) |
| 79 | |
| 80 | # CSW request |
| 81 | request = urllib2.Request(url_csw, xml_request2, header_xml) |
| 82 | response = opener.open(request) |
| 83 | # CSW respons |
| 84 | xml_response = response.read() |
| 85 | print xml_response # debug |
| 86 | |
| 87 | # Do something with the response. For example: |
| 88 | #xmldoc = minidom.parseString(xml_response) |
| 89 | #for node in xmldoc.getElementsByTagName('ows:ExceptionText'): # display <ows:ExceptionText /> value(s) |
| 90 | # print " EXCEPTION: "+node.firstChild.nodeValue |
| 91 | #xmldoc.unlink() # cleanup DOM for improved performance |
| 92 | |
| 93 | # more CSW requests if desired |
| 94 | |
| 95 | # Last, alaways log out |
| 96 | request = urllib2.Request(url_out) |
| 97 | response = urllib2.urlopen(request) |
| 98 | #print response.read() # debug |
| 99 | |
| 100 | |
| 101 | if __name__=="__main__": |
| 102 | gn_csw_transaction() |
| 103 | }}} |