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 from 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 and Running the Sample
To build the samples you need the Apache Ant build tool.
The steps are as follows:
1. Install and run the WSO2 Application Server. Refer to the Installation Guide for instructions.
2. In a command prompt, switch to the sample directory: cd <AS_HOME>\samples\TinyURL
3. From there, type ant.
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.If you go to "Deployed Services" (Manage -> Services -> List) in the Application Server Management Console, you will see the "TinyUrl" service deployed.
Note:
If you don't see the service deployed, wait for a while for the deployment engine of Axis2 to pick up the newly added .aar. Then refresh/reload the page.
Refer to the Installation Guide for details on how to install and login/browse the AS Management Console.
Illustration through 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.