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 com.google.gson as gson
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   # *** Class members ***
22   converter = gson.Gson()
23
24   # *** Public members ***
25
26   # Get a local reference - useful for testing outside of a Mule container:
27   def __init__(self, payload, log = None, eventContext = None, muleContext = None):
28     self.payload      = payload
29     self.muleContext  = muleContext
30     self.eventContext = eventContext
31     self.log          = log
32     self.response     = ''
33
34   def serviceRequest(self):
35     if self.eventContext is not None:
36       self.payload = self.eventContext.getMessage().getPayloadAsString()
37       method       = self.eventContext.getMessage().getProperty('http.method')
38
39     self.log.info('processing method = '+method)
40
41     nStatus = 200 # OK
42     if method == 'GET':
43       self.response = BusinessObject().today()
44     else:
45       nStatus = 400 # Bad request
46       self.response = 'Invalid HTTP method called!'
47
48     self.response   = SampleMuleComponent.converter.toJson({ 'nStatus' : nStatus, 'response' : self.response, 'encoder' : 'Gson' })
49     responseMessage = mule.DefaultMuleMessage(self.response)
50     responseMessage.setIntProperty('http.status', nStatus)
51
52     return responseMessage
53