Showing posts with label web service. Show all posts
Showing posts with label web service. Show all posts

Tuesday, December 8, 2009

RESTfull Webservices Using Java with Introduction to JSON

Introduction
What are RESTfull or Rest web services and how can they be implemented in with Java? Also what is JSON and how to make a webservice that returns a JSON object?  This is what this blog is about. Specifically, first, about installing and running a sample RESTfull application available from Jersey project.

What are RESTfull webservices? See this article. Or any other; there are many.

Approach 1

This approach uses one of the samples provided by the Jersey project and uses Maven to install the libraries. It's a bit heavy approach, but it works. Approach 2 below also uses Jersey, but is way lighter and simpler.


Server Side
I'm using Eclipse IDE with Glassfish Application Server. The steps for this configuration are:
  • Add Jersey and Jersey Documentation and Examples components to Glassfish using its admin console. Note, use GlassFish v3 Prelude version.  When I used GlassFish v3 Promoted Build, the Examples module was not showing in its Update Tool.
  • Restart the GF server.
  • Find the Jersey samples in GF_Home/glassfish/jersey folder.  Go into helloworld-webapp folder. You should see a .pom file there, that is used by maven. From that folder, run "mvn clean package" command. Install maven if needed.
  • Maven installs many files, and for me it ended with an error saying it can't read the glassfish-embedded-all-3.0-Prelude-Embedded-b10.jar file.  I have located this file in the maven repository (Documents and Settings/user/.m2/...) and found that it was broken.  I had to download it manually from internet (search by the file name).
  • Rerun the mvn command. It finished successfully and created a war file in the target directory.
  • Open GF console, and from Web Applications do a deploy. Point to the above war file and click OK. That did it.
  • Point your browser to http://localhost:8080/helloworld-webapp/helloworld. The string "Hello World" appeared.
Analyzing the Sample
After I had the sample running, I have exploded and analyzed the contents of the generated war file. The file had a class file implementing the resource plus two more short config files.

sun-web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sun-web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Application Server 9.0 Servlet 2.5//EN" "http://www.sun.com/software/appserver/dtds/sun-web-app_2_5-0.dtd">
<sun-web-app error-url="">
  <context-root>/helloworld-webapp</context-root>
  <class-loader delegate="true"/>
  <jsp-config>
    <property name="keepgenerated" value="true">
      <description>Keep a copy of the generated servlet class' java code.</description>
    </property>
  </jsp-config>
</sun-web-app> 

web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
    <servlet>
        <servlet-name>Jersey Web Application</servlet-name>
        <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
        <init-param>
            <param-name>com.sun.jersey.config.property.packages</param-name>
            <param-value>com.sun.jersey.samples.helloworld.resources</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>Jersey Web Application</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>
</web-app> 

Approach 2
Approach 1 worked OK, but I wasn't able to apply the sample to deploy an application of my own. And so I searched for a tutorial that uses Eclipse and possibly no Maven. I have run into this mini tutorial which worked great. I was able to drop this application into a Tomcat 6.0 and GlassFish 3 Prelude containers and it worked in both fine.
Also, this example illustrates how the service can be implemented for different response formats, leaving to the requestor to decide which one to use.

JSON
 JSON is simply a format, in a way an alternative to XML, that is especially useful for (de-)serializing objects, for example in order to send them over internet. The format is derived from JavaScript native format, but it became language neutral.  Here's a sample:
{"name":"Alfred Alfredo","age":"47"}
A nice and concise description of JSON syntax is on JSON website. To have your RESTfull webservice return a JSON object, download and add to the project jettison.jar that Jersey uses to create a JSON object. Now, for the sake of the demonstration, say that the webservice need to return a Person object:

@XmlRootElement
public class Person {
 public String name;
 public int age;
 
 public Person() {} // required by JAXB
 
 public Person(String name, int age) {
  this.name = name;
  this.age = age;
 }
}

Thanks to the annotations in this class, in order to return a JSON object we just need to define a method:

@Path("/person")
public class PersonResource {
  @GET  @Produces("application/json")
  public Person get() {
   return new Person("Alfred Alfredo",47);
  }
}

The JSON object as specified above, in the first part of this section will be returned. You can test it using a browser by pointing it to http://localhost:8080/jersey1/rest/person, where jersey1 is the context of my web app, rest is my servlet mapping and person is what I defined with @Path in my RESTful resource class.

Tuesday, December 1, 2009

Presenting Business Objects Reports in PDF Format Using Web Services

Suppose you have a web application, whether of an RIA type or Web 1.0 type, that needs to present a report. One of the attractive options, is to have the report show in the browser as a PDF document. It seems an interesting option, because most users already have Adobe Acrobat Reader installed and are familiar with it. They usually know how to navigate through the document, print it or save it for emailing or just for later use.
If your organization is also one of the many that use Business Objects (BO) as their standard reporting technology, the solution presented below may be for you.

Since version XII, the Business Object Enterprise server is bundled and by default installs the web services module which allows to access reports via SOAP web services. Here's the solution:
  • The web application requests a report by calling the special reporting servlet, passing to it the report ID and required parameters.
  • The servlet uses web services to login to BO server and request the report, which then is returned to the client and displayed in a new browser tab or window in Acrobat Reader.
The example below, shows the solution for the case when the client is a Flex application and the servlet is implemented in Java.

Reporting Servlet
The servlet consists of the following modules:
  • login module (login to BO server)
  • report parms module (requests a list of the parameters required by the report and prepares them)
  • report viewer module (configures the request to return the report in PDF format)
  • retrieve and deliver module (requests the report from BO server and sends it on its way back to the client)
Login Module:

  SessionSoap session = new SessionSoapProxy("http://crystalbo:8080/dswsbobje/services/session");
  EnterpriseCredential credents = new EnterpriseCredential();
  credents.setLogin(businObjectsLoginName);
  credents.setPassword(businObjectsLoginPwd);
  SessionInfo si = session.login(credents);
  if( si == null ) { login failed... }
  // Instantiate Report Engine
  ReportEngineSoap rptEngine = new ReportEngineSoapProxy();

Monday, November 16, 2009

FlashPlayer and Flex Security Restrictions When Consuming a Webservice

Here our system configuration (scenario):
  • Server A: hosts your Flex application
  • Server B: hosts a webservice consumed by your Flex application
 In order for your Flex application to be able to consume the webservice in above scenario, you have two options:
  • Place in the root of the application server on Server B a crossdomain.xml file specifying: allow-access-from-domain: server-A-domain, or
  • Use the Proxy service on Server A, which is part of LCDS and BlazeDS dataservices.

Friday, January 2, 2009

Search Web Services or API for Commercial Use

Suppose, you need to collect some data based on Internet search for your business. What options are there to use existing web search services? Google? Yahoo? Anyone else? Here's what I have found.

Google
Here's what I have found:

  • Google SOAP search web services seem not to be available for new applications any more. In 2006, Google seems to have stopped issuing new API keys.
  • There is another option from Google: API not SOAP based. The terms don't exclude commercial use, but there is a restriction on caching the results of the search. I guess, if we process the results and store the processed data, it wouldn't be considered caching.
  • The API services have exclusivity requirement: you are not supposed to use on the same site any other search provider.
Yahoo
Here's what I have found:
  • Y! makes the search available via web services (REST but not SOAP webservices).
  • Each application has to use Application ID, which Y! issues for free. There is a rate limit for each application id.
  • Allowed usage: web search API is available to commercial use, however a form of exclusivity restraint is imposed: you are not supposed to integrate the results with results from other search providers.
  • Storage restrictions: "you may not store any user data collected through the Yahoo! APIs for more than 24 hours". Again, would storing the processed, for example aggregated data, be subject to this restriction? I suppose, not.
MSDN: Live Search WebServices API
Terms of use summary:
  • SOAP-based webservices call Silky Road.
  • No restrictions other than an application ID needed. But their license is more complex and I might have missed something.
Other
I'm sure there are/may be other search web services available, and some of them may be free. DoubleClick? I didn't analyze other options.