Tuesday, July 06, 2010

Integration of Ext-GWT with Jersey Web Service


TestExtGWTJerseyWebServiceClient : This example provide the usage of Ext-GWT along with REST WebService to perform Database operation on My-SQL Db.

TestExtGWTJerseyWebServiceClient.gwt.xml:

xml version="1.0" encoding="UTF-8"?>

<module rename-to='testextgwtjerseywebserviceclient'>

<inherits name='com.google.gwt.user.User'/>

<inherits name='com.google.gwt.user.theme.standard.Standard'/>

<inherits name='com.extjs.gxt.ui.GXT' />

<entry-point class='testExtGWTJerseyWebServiceClient.client.TestExtGWTJerseyWebServiceClient'/>

<source path='client'/>

<source path='shared'/>

module>

GreetingService:

package testExtGWTJerseyWebServiceClient.client;

import com.google.gwt.user.client.rpc.RemoteService;

import com.google.gwt.user.client.rpc.RemoteServiceRelativePath;

/**

* The client side stub for the RPC service.

*/

@RemoteServiceRelativePath("greet")

public interface GreetingService extends RemoteService {

String greetServer(String name) throws IllegalArgumentException;

}

GreetingServiceAsync:

package testExtGWTJerseyWebServiceClient.client;

import com.google.gwt.user.client.rpc.AsyncCallback;

/**

* The async counterpart of GreetingService.

*/

public interface GreetingServiceAsync {

void greetServer(String input, AsyncCallback callback)

throws IllegalArgumentException;

}

TestExtGWTJerseyWebServiceClient:

package testExtGWTJerseyWebServiceClient.client;

import com.extjs.gxt.ui.client.Style.HorizontalAlignment;

import com.extjs.gxt.ui.client.event.ButtonEvent;

import com.extjs.gxt.ui.client.event.SelectionListener;

import com.extjs.gxt.ui.client.widget.Layout;

import com.extjs.gxt.ui.client.widget.LayoutContainer;

import com.extjs.gxt.ui.client.widget.VerticalPanel;

import com.extjs.gxt.ui.client.widget.button.Button;

import com.extjs.gxt.ui.client.widget.form.FormButtonBinding;

import com.extjs.gxt.ui.client.widget.form.FormPanel;

import com.extjs.gxt.ui.client.widget.layout.AnchorLayout;

import com.extjs.gxt.ui.client.widget.layout.FormData;

import com.google.gwt.core.client.EntryPoint;

import com.google.gwt.core.client.GWT;

import com.google.gwt.user.client.Element;

import com.google.gwt.user.client.rpc.AsyncCallback;

import com.google.gwt.user.client.rpc.ServiceDefTarget;

import com.google.gwt.user.client.ui.RootPanel;

/*

* Here first we need to make the REST Webservice started on the local host (Tomcat) and then made the call

* and then we need to run this EXTGWT using GWT Plugin

*/

/**

* Entry point classes define onModuleLoad().

*/

public class TestExtGWTJerseyWebServiceClient implements EntryPoint {

/**

* The message displayed to the user when the server cannot be reached or

* returns an error.

*/

private static final String SERVER_ERROR = "An error occurred while "

+ "attempting to contact the server. Please check your network "

+ "connection and try again.";

//private final GreetingServiceAsync greetingService = GWT.create(GreetingService.class);

private static testExtGWTJerseyWebServiceClient.client.GreetingServiceAsync ourInstance = getInstance();

public static synchronized testExtGWTJerseyWebServiceClient.client.GreetingServiceAsync getInstance() {

if (ourInstance == null) {

ourInstance = (testExtGWTJerseyWebServiceClient.client.GreetingServiceAsync) GWT.create(GreetingService.class);

((ServiceDefTarget) ourInstance).setServiceEntryPoint(GWT.getModuleBaseURL() + "GreetingService");

}

return ourInstance;

}

private VerticalPanel vp;

private FormData formData;

public void onModuleLoad() {

//to add something TBD

@SuppressWarnings("unused")

Layout junk = new AnchorLayout();

TemplateJerseyRESTExample objTemplateJerseyRESTExample = new TemplateJerseyRESTExample();

RootPanel.get().add(objTemplateJerseyRESTExample);

}

class TemplateJerseyRESTExample extends LayoutContainer

{

@Override

protected void onRender(Element parent, int index) {

super.onRender(parent, index);

formData = new FormData("-20");

vp = new VerticalPanel();

vp.setSpacing(10);

//createForm1();

FormPanel simple = new FormPanel();

simple.setHeading("Test Ext-GWT Jersey REST Webservice Client Project Form");

simple.setFrame(true);

simple.setWidth(350);

/*

final TextField id = new TextField();

id.setFieldLabel("Id");

//firstName.setAllowBlank(false);

simple.add(id, formData);

final TextField name = new TextField();

name.setFieldLabel("Name");

//name.setAllowBlank(false);

simple.add(name, formData);

*/

SelectionListener l = new SelectionListener() {

@Override

public void componentSelected(ButtonEvent ce) {

//Info.display("Click", ce.getButton().getText() + " clicked");

if(ce.getButton().getText()!= null && (!ce.getButton().getText().equals("")&&ce.getButton().getText().equals("Insert")))

{

//String idText = id.getValue();

//String nameText = name.getValue();

ourInstance.greetServer("insert",new AsyncCallback() {

public void onFailure(Throwable caught) {

// Show the RPC error message to the user

//RootPanel.get().add(new HTML("RPC call failed. :-("));

}

public void onSuccess(String result) {

//RootPanel.get().add(new HTML("RPC call Success:"));

}

});

//System.out.println(service.path("resources").path("dbOperation/insert/20/Twenty").accept(MediaType.TEXT_PLAIN).get(ClientResponse.class).toString());

}

if(ce.getButton().getText()!= null && (!ce.getButton().getText().equals("")&&ce.getButton().getText().equals("Update")))

{

ourInstance.greetServer("update",new AsyncCallback() {

public void onFailure(Throwable caught) {

// Show the RPC error message to the user

//RootPanel.get().add(new HTML("RPC call failed. :-("));

}

public void onSuccess(String result) {

//RootPanel.get().add(new HTML("RPC call Success:"));

}

});

//System.out.println(service.path("resources").path("dbOperation/update/20/Twenty Changed").accept(MediaType.TEXT_HTML).get(ClientResponse.class).toString());

}

if(ce.getButton().getText()!= null && (!ce.getButton().getText().equals("")&&ce.getButton().getText().equals("Delete")))

{

ourInstance.greetServer("delete",new AsyncCallback() {

public void onFailure(Throwable caught) {

// Show the RPC error message to the user

//RootPanel.get().add(new HTML("RPC call failed. :-("));

}

public void onSuccess(String result) {

//RootPanel.get().add(new HTML("RPC call Success:"));

}

});

//System.out.println(service.path("resources").path("dbOperation/delete/20/Twenty").accept(MediaType.TEXT_XML).get(ClientResponse.class).toString());

}

}

};

Button insert = new Button("Insert",l);

simple.addButton(insert);

Button update = new Button("Update",l);

simple.addButton(update);

Button delete = new Button("Delete",l);

simple.addButton(delete);

simple.setButtonAlign(HorizontalAlignment.CENTER);

FormButtonBinding binding = new FormButtonBinding(simple);

binding.addButton(insert);

binding.addButton(update);

binding.addButton(delete);

vp.add(simple);

add(vp);

//idText = id.getSelectedText();

//nameText = name.getSelectedText();

}

}

}

GreetingServiceImpl:

package testExtGWTJerseyWebServiceClient.server;

import testExtGWTJerseyWebServiceClient.client.GreetingService;

import testExtGWTJerseyWebServiceClient.shared.FieldVerifier;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;

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.ClientResponse;

import com.sun.jersey.api.client.WebResource;

import com.sun.jersey.api.client.config.ClientConfig;

import com.sun.jersey.api.client.config.DefaultClientConfig;

/**

* The server side implementation of the RPC service.

*/

@SuppressWarnings("serial")

public class GreetingServiceImpl extends RemoteServiceServlet implements

GreetingService {

public String greetServer(String input) throws IllegalArgumentException {

ClientConfig config = new DefaultClientConfig();

Client client = Client.create(config);

WebResource service = client.resource(getBaseURI());

if(input.equals("insert"))

{

System.out.println(service.path("resources").path("dbOperation/insert/20/Twenty").accept(MediaType.TEXT_PLAIN).get(ClientResponse.class).toString());

}

if(input.equals("update"))

{

System.out.println(service.path("resources").path("dbOperation/update/20/Twenty Changed").accept(MediaType.TEXT_HTML).get(ClientResponse.class).toString());

}

if(input.equals("delete"))

{

System.out.println(service.path("resources").path("dbOperation/delete/20/Twenty").accept(MediaType.TEXT_XML).get(ClientResponse.class).toString());

}

return "1";

}

private static URI getBaseURI() {

return UriBuilder.fromUri("http://localhost:8080/JerseyDataBaseProject/").build();

}

}

Web.xml:

xml version="1.0" encoding="UTF-8"?>

DOCTYPE web-app

PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"

"http://java.sun.com/dtd/web-app_2_3.dtd">

<web-app>

<servlet>

<servlet-name>greetServletservlet-name>

<servlet-class>testExtGWTJerseyWebServiceClient.server.GreetingServiceImplservlet-class>

servlet>

<servlet-name>Jersey Web Applicationservlet-name>

<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainerservlet-class>

<init-param>

<param-name>com.sun.jersey.api.core.packagesparam-name>

<param-value>com.dclonline.jerseytestparam-value>

init-param>

<load-on-startup>1load-on-startup>

<servlet-mapping>

<servlet-name>greetServletservlet-name>

<url-pattern>/testextgwtjerseywebserviceclient/greeturl-pattern>

<servlet-name>Jersey Web Applicationservlet-name>

<url-pattern>/resources/*url-pattern>

servlet-mapping>

<welcome-file-list>

<welcome-file>TestExtGWTJerseyWebServiceClient.htmlwelcome-file>

welcome-file-list>

<servlet-mapping>

<servlet-name>greetServletservlet-name>

<url-pattern>/testextgwtjerseywebserviceclient/GreetingServiceurl-pattern>

servlet-mapping>

<session-config>

<session-timeout>30session-timeout>

session-config>

web-app>

TestExtGWTJerseyWebServiceClient.html:

doctype html>

<html>

<head>

<meta http-equiv="content-type" content="text/html; charset=UTF-8">

<link rel="stylesheet" type="text/css" href="resources/css/gxt-all.css" />

<title>Web Application Starter Projecttitle>

<script type="text/javascript" language="javascript" src="testextgwtjerseywebserviceclient/testextgwtjerseywebserviceclient.nocache.js">script>

head>

<body>

<iframe src="javascript:''" id="__gwt_historyFrame" tabIndex='-1' style="position:absolute;width:0;height:0;border:0">iframe>

<noscript>

<div style="width: 22em; position: absolute; left: 50%; margin-left: -11em; color: red; background-color: white; border: 1px solid red; padding: 4px; font-family: sans-serif">

Your web browser must have JavaScript enabled

in order for this application to display correctly.

div>

noscript>

<h1>Test Ext-GWT Jersey REST Webservice Client Projecth1>

body>

html>

No comments: