Showing posts with label j2ee. Show all posts
Showing posts with label j2ee. Show all posts

Saturday, October 20, 2012

Build a RESTful Web service with Java

Representational state transfer (REST) is a architectural style of designing loosely coupled applications based on web-standards and the HTTP protocol. It relies on a stateless, client-server, cacheable communications protocol -- and in virtually all cases, the HTTP protocol is used.The hardest part of building a RESTful application is deciding on the resources you want to expose. Once you've done that, using the open source Restlet framework makes building RESTful Web services a snap. This tutorial guides you step-by-step through the fundamental concepts of REST and building applications with Restlets.

Step 1: Create a dynamic web project in Eclipse and add the jars shown in the figure in the lib folder under the WEB-INF



























Step 2: Add the Restful nature in the the project using fallowing entry in the web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>FirstProject</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  <servlet>
  <servlet-name>ServletAdaptor</servlet-name>
  <servlet-class>
   com.sun.jersey.server.impl.container.servlet.ServletAdaptor</servlet-class>
  <load-on-startup>1</load-on-startup>
 </servlet>
 <servlet-mapping>
  <servlet-name>ServletAdaptor</servlet-name>
  <url-pattern>/REST/*</url-pattern>
 </servlet-mapping>
</web-app>


Step 3:Create the Employee class as fallows and annotted it with the @Path,@Produce and @Get annotations
package com.rajkrrsingh.blogspot;


import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

@Path("/employee")
public class Employee {
 
 @GET
 @Produces(MediaType.TEXT_HTML)
 public String getEmployee(){
  return "<html> " + "<title>" + "Employee Details" + "</title>"
          + "<body><h1>" + "Employee Name is Dummy Employee" + "</body></h1>" + "</html> ";
 }

}

Step 4: now run the dynamic web application on the tomcat server and access the fallowing URL


http://localhost:8080/RESTfulProject/REST/employee


Here is the browser output


















Step 5:you can use jersy api or Apache HttpClient api to the test the webservice by creating a client,here I have created the client using the jersy api

package com.rajkrrsingh.blogspot.client;

import java.net.URI;

import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.UriBuilder;

import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.config.ClientConfig;
import com.sun.jersey.api.client.config.DefaultClientConfig;

public class Main {
 
 public static void main(String[] args) {
  ClientConfig config = new DefaultClientConfig();
     Client client = Client.create(config);
     WebResource service = client.resource(getBaseURI());
     System.out.println(service.path("REST").path("employee").accept(MediaType.TEXT_HTML).get(String.class));
  
 }
 
 private static URI getBaseURI() {
     return UriBuilder.fromUri("http://localhost:8080/RESTfulProject").build();
   }


}

After running this main class as a java application you will find the fallowing output on the console


<html> <title>Employee Details</title><body><h1>Employee Name is Dummy Employee</body></h1></html>

Sunday, June 10, 2012

Java Decompiler (Jad) : A Smart guide

Jad is a command line Java decompiler written by Pavel Kouznetsov,written for several plateform.
can be downloaded from
1. http://www.kpdus.com/jad.html
2. http://www.varaneckas.com/jad

Installation is very easy,just extract the downloaded zip .
Set the environment variable path for your plateform here I am using windows.
After setting the path variable open the cmd to check the proper installation type jad on
command prompt,if every thing goes fine you will find the fallowing outupt.








To decompile single java class use the command
jad -sjava MyClass.class

To decompile Multiple java class use the command
jad -o -r -sjava -dsrc bin/**/*.class

Jadclipse plugin for Eclipse
Download the Jadclipse for eclipse from http://sourceforge.net/projects/jadclipse/
Copy the extracted jars in the Eclipse plugin folder.
Restart the Eclipse and set the decompliler path as shown









Now your Eclipse is ready to decompile any java class whose source file is not provided with you,just press
F3 on the name of the class and see the magic.

Ant Build File to decompile all the classes inside the jar

<project name="Decompile_jars" default="main" basedir=".">


<taskdef resource="net/sf/antcontrib/antcontrib.properties"/>


<property name="jad" location="c:/program files/jad1.5.8/jad.exe"/>
<property name="unjar.dir" location="loc_unjar"/>

<target name="main">
    <input
        message="Please enter jar file name"
        addproperty="input.file"
        defaultvalue="source"
    />

    <!-- clean temp dir -->
    <delete dir="${unjar.dir}"/>

    <!-- unpack jar file -->    
    <unjar src="${input.file}.jar" dest="${unjar.dir}"/>

    <!-- decompile all class files -->
    <exec executable="cmd.exe">
        <arg line="/c ${jad} -o -r -sjava -d${unjar.dir} ${unjar.dir}/**/*.class"/>
    </exec>     
     
    <!-- create jar file -->
    <jar destfile="${input.file}_decompile.jar">
        <fileset dir="${unjar.dir}"
            excludes="**/*.class"
        />
    </jar>

    <!-- clean temp dir -->
    <delete dir="${unjar.dir}"/>
    
</target>


</project>