1 #!/usr/bin/env jython
 2 #
 3 # Listing 2
 4 #
 5 # Place this file in the module $MULE_HOME/lib/usr/article instead of
 6 # in a .jar if you want to donkey punch it.
 7
 8
 9 import simplejson as json
10
11 import org.mule as mule
12
13 import article.businessmodule as businessmodule
14 from businessmodule import BusinessObject
15
16 # Enable dynamic updates to the script:
17 reload(businessmodule)
18
19
20 class SampleMuleComponent(object):
21
22   # *** Public members ***
23
24   # Get a local reference - useful for testing outside of a Mule container:
25   def __init__(self, payload, log = None, eventContext = None, muleContext = None):
26     self.payload      = payload
27     self.muleContext  = muleContext
28     self.eventContext = eventContext
29     self.log          = log
30     self.response     = ''
31
32   def serviceRequest(self):
33     if self.eventContext is not None:
34       self.payload = self.eventContext.getMessage().getPayloadAsString()
35       method       = self.eventContext.getMessage().getProperty('http.method')
36
37     self.log.info('processing method = '+method)
38
39     nStatus = 200 # OK
40     if method == 'GET':
41       self.response = BusinessObject().today()
42     else:
43       nStatus = 400 # Bad request
44       self.response = 'Invalid HTTP method called!'
45
46     self.response   = json.dumps({ 'nStatus' : nStatus, 'response' : self.response})
47     responseMessage = mule.DefaultMuleMessage(self.response)
48     responseMessage.setIntProperty('http.status', nStatus)
49
50     return responseMessage
51