4.9 RESTful Web services in CloudTran
By default, applications access CloudTran services using protocols provided by the underlying grid.
For example, in a GigaSpaces environment, CloudTran creates a generic proxy for use by clients; under the covers,
it uses GigaSpaces' service remoting feature.
You can also expose CloudTran services as RESTful Web services.
This is in addition to the standard proxy (i.e. using the grid's proxy feature), which is always generated.
RESTful Web services are generated by setting the 'Generate Rest Service' flag on the service (or application) in the model.
Web services are then using
a combination of Servlets, Jersey and JSON.
All the RESTful services in the application are collected in an additional Eclipse project and packaged in a WAR.
4.9.1 Modelling RESTful Web services
|
By default, services in CloudTran are not exposed as RESTful services.
You can change that for a whole application by setting the 'Generate Rest Service' flag in the application object.
This is a boolean property; the default value is "false".
When it is set "true", the default is that services will be exposed via the RESTful facade.
Then, individual services also have a 'Generate Rest Service', which is a tri-state - it can be "true", "false" or "".
- The default is "<empty>", in which case the value of the application object's 'Generate Rest Service'
flag determines whether the service is exposed through REST.
- If the value is "true", the service is exposed through REST.
- If the value is "false", the service is not exposed through REST.
4.9.2 Implementing RESTful Web services
|
Exposing services through REST merely adds an extra route to the business logic for each operation;
the implementation is the same.
The tutorial on Adding Implementation Code has information on
implementing services.
4.9.3 Using RESTful Web services
|
The following is a blow-by-blow summary of how a client makes a call for a RESTful service operation into your busines method.
- The client constructs the input to the operation as a JSON object.
- The client POSTs the input to the URL for the operation.
- The input is received by an embedded Jetty web server and forwarded to the Jersey REST infrastructure.
- Jersey does the JSON/Java serialisation/deserialisation and calls, via the generated CloudTran REST facade,
into your business method.
- Your business methods returns a Java value which is converted by the facade and Jersey into JSON and returned via HTTP.
There are too many details to explain how to use JSON and REST, but there is a comprehensive "RestfulServices" example in
the jeewiz/examples/CloudTran/RestfulServices directory.
This example shows how to construct a client - see the RestfulServicesClient project.
This has full end-to-end scenarios that you should be able to copy and adapt.
4.9.4 RESTful Web services
|
The URL of service operation is based on the application name, the service name and the business-method name. It is:
http://<IP_Address>:<port-number>/<application-name>/rest/<service-name>/<businessMethod-name>
|
All defined service operations the services use a POST operation,
so they are not accessible by a web browser.
However the following URL uses a GET operation and will return
'Welcome to the <serviceName> service!'.
It can be used to check whether the services are running.
http://<IP_Address>:<port-number>/<application-name>/rest/<service-name>
|
4.9.5 Generated Artifacts
|
If any service in the application is to be exposed as a RESTful service an additional Eclipse project will be generated. This additional project will by default
be called
<application-name>_<application-name>Rest
|
This project will be another processing unit, but one that is deployed as WAR rather than a JAR. The WAR name will be
<application-name>Rest.war
|
This is in the top level of the '...Rest' project.
Finally notice the 'ping' method for the service, getServicePingMessage().
This is called if the service URL is hit with no additional business-method path i.e.
http://<IP_Address>:<port-number>/MyApp/rest/MyAppServices
|
This CAN be accessed from a browser.
4.9.6 Deployment
|
You cannot run the web application as n
|