1 | # Copyright (C) 2004 CCLRC & NERC( Natural Environment Research Council ). |
---|
2 | # This software may be distributed under the terms of the |
---|
3 | # Q Public License, version 1.0 or later. http://ndg.nerc.ac.uk/public_docs/QPublic_license.txt |
---|
4 | |
---|
5 | """ |
---|
6 | WSCaller.py |
---|
7 | =========== |
---|
8 | |
---|
9 | Holds the WSCaller class used to make calls to the Web Service |
---|
10 | across the network to a remote server. |
---|
11 | |
---|
12 | """ |
---|
13 | |
---|
14 | # Import standard library modules |
---|
15 | import os, sys |
---|
16 | |
---|
17 | # Import SOAP library |
---|
18 | from ZSI.client import Binding |
---|
19 | |
---|
20 | # Import package modules |
---|
21 | from clientConfig import * |
---|
22 | from common import * |
---|
23 | from DisplayManager import DisplayManager |
---|
24 | |
---|
25 | |
---|
26 | class WSCaller: |
---|
27 | """ |
---|
28 | Used to call Web Service on a remote machine. |
---|
29 | """ |
---|
30 | |
---|
31 | def __init__(self): |
---|
32 | """ |
---|
33 | Sets up the instance. |
---|
34 | """ |
---|
35 | # Set up SOAP bindings |
---|
36 | trace=open('/tmp/tracefile.txt','w') |
---|
37 | self.server=Binding(url='', host=SOAP_SERVER_NAME, port=SOAP_SERVER_PORT, tracefile=trace) |
---|
38 | |
---|
39 | |
---|
40 | def callServerMethod(self, methodName, args, packArgs=None): |
---|
41 | """ |
---|
42 | |
---|
43 | Calls method "methodName(args)" and returns whatever is returned. |
---|
44 | """ |
---|
45 | if packArgs: |
---|
46 | args=self._packArgsAsList(args) |
---|
47 | |
---|
48 | # Make that call |
---|
49 | try: |
---|
50 | response=apply(getattr(self.server, methodName), args) |
---|
51 | except Exception, error: |
---|
52 | if str(error)=="(111, 'Connection refused')": |
---|
53 | error="GeoSPlAT server is currently unavailable. Please try again later." |
---|
54 | self._displayErrorPage(error) |
---|
55 | response=self._webServiceCallWrapper(response) |
---|
56 | return response |
---|
57 | |
---|
58 | |
---|
59 | def _packArgsAsList(self, args): |
---|
60 | """ |
---|
61 | In order to work with ZSI SOAP library need to pack up arguments as a list |
---|
62 | of [keyword, value] pairs rather than a dictionary. |
---|
63 | """ |
---|
64 | newList=[] |
---|
65 | for key, value in args.items(): |
---|
66 | newList.append([key, value]) |
---|
67 | return newList |
---|
68 | |
---|
69 | |
---|
70 | def _deUnicodeObject(self, obj): |
---|
71 | """ |
---|
72 | Returns an identical object with all unicode strings returned as normal strings. |
---|
73 | """ |
---|
74 | return deUnicodeObject(obj) |
---|
75 | |
---|
76 | |
---|
77 | def _webServiceCallWrapper(self, response): |
---|
78 | """ |
---|
79 | Analyses the response from a Web Service call to check if an error has |
---|
80 | occurred. If so it parses and reports the error. Otherwise it returns |
---|
81 | the object returned from the Web Service call. |
---|
82 | """ |
---|
83 | if type(response) in (type(""), type(u'w')): |
---|
84 | # String returns are errors |
---|
85 | self._displayErrorPage(response) |
---|
86 | else: |
---|
87 | return response |
---|
88 | |
---|
89 | |
---|
90 | def _displayErrorPage(self, error): |
---|
91 | """ |
---|
92 | Displays the error if one is found. |
---|
93 | """ |
---|
94 | self.displayer=DisplayManager() |
---|
95 | self.displayer._displayErrorPage(error) |
---|
96 | if DEBUG==1: raise Exception, error |
---|
97 | sys.exit() |
---|
98 | |
---|