This sample demonstrates the capabilities of the WSO2 Application Server to handle both SOAP and Representational State Transfer (REST) with the help of a Web service form which a shorter URL can be obtained by submitting a longer URL. The sample can be found in <AS_HOME>\samples\TinyURL directory.
You can submit a long URL and obtain a shorter URL fr om www.tinyurl.com . You can simply develop a Web service that provides the same functionality. The beauty of this Web service is that the same service can be used by a desktop client or a Web browser to obtain a URL. It can be used by a browser to go to the Web page represented by the URL obtained without any additional Servlet or JSP logic or any shared database instances.
Two basic requirements can be identified at this point.
- Any user of the service should be able to obtain a URL from the service by submitting any UR.
- Any user should be able to use a URL provided by the service to access the original web page.
The service should have two operations to fulfill the two requirements above.
- addUrl - Allows a URL to be added to the service and provides a new URL which can be used to access the page of the original URL.
- go - When this operation is invoked with a URL obtained from the service, it will return HTML text to that which contains the original URL in an iframe.
Building the sample
The sample is located in <AS_HOME>\Samples\TinyURL
directory.
For more information, see Building Axis2 Samples.
The following should be displayed:
Buildfile: build.xml clean: delete Deleting directory /home/user/wso2appserver-4.1.0/samples/TinyURL/temp delete Deleting directory /home/user/wso2appserver-4.1.0/samples/TinyURL/services init: mkdir Created dir: /home/user/wso2appserver-4.1.0/samples/TinyURL/temp/TinyUrl/META-INF mkdir Created dir: /home/user/wso2appserver-4.1.0/samples/TinyURL/temp/classes mkdir Created dir: /home/user/wso2appserver-4.1.0/samples/TinyURL/services mkdir Created dir: /home/user/wso2appserver-4.1.0/samples/TinyURL/temp/TinyUrl/www/extensions/core compile-all: javac Compiling 4 source files to /home/user/wso2appserver-4.1.0/samples/TinyURL/temp/TinyUrl build-service: copy Copying 1 file to /home/user/wso2appserver-4.1.0/samples/TinyURL/temp/TinyUrl/META-INF copy Copying 2 files to /home/user/wso2appserver-4.1.0/samples/TinyURL/temp/TinyUrl/www/extensions/core copy Copying 1 file to /home/user/wso2appserver-4.1.0/samples/TinyURL/temp/TinyUrl/www jar Building jar: /home/user/wso2appserver-4.1.0/samples/TinyURL/services/TinyURL.aar build-all: echo Copying the created aar files in to the repository copy Copying 1 file to /home/user/wso2appserver-4.1.0/repository/deployment/server/axis2services BUILD SUCCESSFUL Total time: 2 seconds
The build automatically copies the .aar
file containing the service into the repository. On the Main menu, under Services, click List. The Deployed Services page will appear with the TinyUrl service deployed.
If you do not see the services deployed, wait for a while for the deployment engine of Axis2 to pick up the newly added .aar
and the refresh/reload the page.
Examples
If the URL we need to submit is http://wso2.com then, type the longer URL in the text-box and submit it. You can access the TinyURL service via http://localhost:9763/TinyURL
When you submit the shorter URL, a HTTP GET request is sent to http://127.0.0.1:9763/services/TinyURL/addUrl with the "URL" parameter holding the longer URL.
http://127.0.0.1:9763/services/TinyURL/addUrl?url=http://www.wso2.com
http://127.0.0.1:9763/services/TinyURL/addUrl?url=http://www.wso2.com
http://localhost:9763/services/TinyURL/addUrl?url=http://www.wso2.com is a REST call to the "TinyURL" Web service.
Then you should see a URL similar to this: https://127.0.0.1:9443/services/TinyURL/go?id=1340973355
Click the above URL to access http://www.wso2.com