Archive for 2010

Using Open Source Flash in Java

My Latest article on using open source flash in Java is out at dzone.
Please visit http://java.dzone.com/articles/custom-facelets-chart and learn about how we can create custom Facelets components.


The sample chart is shown below:
Saturday, December 11, 2010
Posted by Unknown

Install Flash Plugin Manually in Firefox

Today I was trying to test some flash componet in Mozilla, but due to some restrictions on network I was not able to download the Mozilla plugin. I clicked the “Install Missing Plugins” button, followed the wizard, restarted Firefox, and it was still not installed. Annoying!
So here are the steps to install flash plugin manually:
download -  http://fpdownload.macromedia.com/get/flashplayer/xpi/current/flashplayer-win.xpi

Once you have the file downloaded, rename the file with a .zip extension.

Next copy the following two files to: C:\Program Files\Mozilla Firefox\plugins
  • flashplayer.xpt
  • NPSWF32.dll
If you don’t have administrator access to your machine, or you simply want to install for only your account, you can browse to the following path: %APPDATA%\Mozilla (type is Windows Run) and copy these above files in Plugins folder (create if not already exist)

Now to test whether flash plugin was installed , just type 'about:plugins' in firefox's address bar.

Thanks!!!
Tuesday, December 7, 2010
Posted by Unknown

Using Sphinx and Java to implement free text search

This weekend I tried my hands on Sphinx and as promised I have published the article on how we can use Sphinx to perform free text search using Java API. If you have time and have some interest in that area, please visit dZone site, I have just published the article so its not moderated yet, will let you know once it will be available on the home page of java dzone :)


http://java.dzone.com/articles/using-sphinx-and-java
Thursday, December 2, 2010
Posted by Unknown

Free Text Search in Java using SPHINX

Folks my next article will be based on Sphinx (free text search using Sphinx and Java), please let me know what ingredients you would like to have in that article? I plan to cover: 
1. What is sphinx? 
2. vs Lucene 
3. Installing Sphinx 
4. Using Sphinx in Java to handle free text search. 
5. Working Code well that's all....


The examples would be based on Orders search for payment, equities or any other domain. 
Do you have any piece of code that you want to perform search on? Come one let's do it together then. 
Posted by Unknown

what happens after death?

Last night me, dharam paji and sanjay (not the one from mahabharat) had discussion on the basic questions which science has not been able to answer yet. questions like - what happens after death e.g.? we discussed about kriya yog and lot of meditation exercises using which we can control our breath, soul consciousness etc. it went over 3 hours with no results, so here i am putting a basic question -
1. what happens after death? 
2. do we chose our next life (parents, family etc)?
3. do we take our consciousness with us when we die?
4. being in kalyug and influenced by nears and dears, can we follow what Geeta says?
5. can we practice kriya yoga to convert ourself to pure energy (as everything in this universe is made of energy and Shiva being the supreme creator of that energy)?

there are lots of questions, lets start with the above set only ... any thoughts?
Tuesday, November 30, 2010
Posted by Unknown

Exposing existing business object as a web service and using JMS to publish events ...

I am thinking of my next article where in I will be taking one existing EJB 3.0 bean or a plain pojo doing some particular piece of business and publishing it as a web service and using JMS to publish the events (like auditing the events to the database).


I would love to have ideas from you guys about what should I focus and plan for my next article around exposing already existing piece of code as a web service. So if you have a piece of code that you want to expose as a web service, you are at the right place :)


Looking forward.
Sunday, November 28, 2010
Posted by Unknown

Creating and Deploying JAX-WS web service on Tomcat 6

Hi recently I had to provide a wrapper around an EJB 3.0 remote service to come up with a simple web service project that would be deployed over Tomcat and accessed in a simple http way due to some accessibility issues. Now as I cannot reveal the actual requirement I implemented last week so here i am presenting a simple demo kind of service with following signature.

public AccountDetails getAccountDetails(String accountNo, SecurityToken token);


The service will return the account details of a particular account number, provided the token is valid (generated using some Security module of the application). In nutshell, the client will ask for a token from Security module and then invoke this method. The service will validate the token to see if the caller can invoke the method or not?
Sounds good.. lets move ahead.

Libraries we are going to use include JAXB and JAX-WS, as both of them have sensible defaults, the number of annotations can be kept to the minimum. Also in my opinion, it is always best to develop WSDL and schemas by hand to ensure that the service contract is appropriately defined and also that the schema can be re-used (by other services) and extended if necessary. I do not prefer using annotations for automatically producing WSDL and schema at runtime so let's start with the definition, i am using inline schema as we have a very simple requirement here, though we should have schema definitions in separate xsd files.

accounts.wsdl:

<?xml version="1.0" encoding="UTF-8"?>
<definitions name="AccountDetails"
targetNamespace="http://gognamunish.com/accounts"
xmlns:tns="http://gognamunish.com/accounts"
xmlns="http://schemas.xmlsoap.org/wsdl/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/">

<types>
<xsd:schema xmlns="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified" targetNamespace="http://gognamunish.com/accounts">
<element name="geAccountDetails_INTO" type="tns:GeAccountDetails_INTO" />
<element name="accountDetails_TO" type="tns:AccountDetails_TO" />
<element name="accountDetailsFault" type="tns:AccountDetailsFault" />
<element name="securityToken" type="tns:SecurityToken" />
<complexType name="GeAccountDetails_INTO">
<sequence>
<element name="accountNo" type="xsd:string" />
</sequence>
</complexType>
<complexType name="AccountDetails_TO">
<sequence>
<element name="accNo" type="xsd:string" />
<element name="accType" type="xsd:string" />
<element name="balance" type="xsd:decimal" />
</sequence>
</complexType>
<complexType name="SecurityToken">
<sequence>
<element name="token" type="xsd:string" />
<element name="validTill" type="xsd:date" />
</sequence>
</complexType>
<complexType name="AccountDetailsFault">
<sequence>
<element name="faultInfo" type="xsd:string" />
<element name="message" type="xsd:string" />
</sequence>
</complexType>
</xsd:schema>
</types>

<!-- messages format -->
<message name="accountDetailsRequest">
<part name="parameters" element="tns:geAccountDetails_INTO" />
<part name="request_header" element="tns:securityToken"/>
</message>
<message name="accountDetailsResponse">
<part name="parameters" element="tns:accountDetails_TO" />
</message>
<message name="accountDetailsFault">
<part name="faultInfo" element="tns:accountDetailsFault" />
</message>

<!-- define getAccountDetails operation here -->
<portType name="AccountDetailsPortType">
<operation name="getAccountDetails">
<input message="tns:accountDetailsRequest" />
<output message="tns:accountDetailsResponse" />
<fault message="tns:accountDetailsFault" name="accountDetailsFault"/>
</operation>
</portType>

<!-- bind the operations -->
<binding name="AccountDetailsBinding" type="tns:AccountDetailsPortType">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document" />
<operation name="getAccountDetails">
<soap:operation soapAction="getAccountDetails" />
<input>
<soap:body use="literal" parts="parameters"/>
<soap:header use="literal" part="request_header" message="tns:accountDetailsRequest"></soap:header>
</input>
<output>
<soap:body use="literal" />
</output>
<fault name="accountDetailsFault">
<soap:fault name="accountDetailsFault" use="literal" />
</fault>
</operation>
</binding>

<!-- name and location of the service -->
<service name="AccountDetailsService">
<port name="AccountDetailsPort" binding="tns:AccountDetailsBinding">
<soap:address location="http://localhost:8080/account/details" />
</port>
</service>
</definitions>


I am not going to explain the In and outs of this wsdl , in a nutshell it defines one operation 'getAccountDetails' which takes accountNo and returns back the account details like account type, balance etc. Please note that I have also added a security header token that will validate the caller (left to the implementation of the service).

Now as we are done with our wsdl and schema, let's generate the portable artifacts from our service definition. JAX-WS includes a tool that can do this for us, we will use this tool to generate portable artifacts like Service Endpoint Interface (SEI), Service and Exception classes.These artifacts can be packaged in a WAR file with the WSDL and schema documents along with the endpoint implementation to be deployed.

To generate the artifacts , we run following command:
wsimport C:\devel\workspace\webservice\WebContent\WEB-INF\wsdl\accounts.wsdl   -p com.mg.ws -keep -Xnocompile

This will create the following artifacts in com.mg.ws package.
  • AccountDetails.java
  • AccountDetailsFault.java
  • AccountDetailsFault_Exception.java
  • AccountDetailsPortType.java
  • AccountDetailsTO.java
  • GeAccountDetailsINTO.java
  • ObjectFactory.java
  • package-info.java
  • SecurityToken.java
Now as we have got all the artifacts we are ready to implement our service, the interface we need to implement is AccountDetailsPortType , so let's do it.

Here is our dummy implementation class:



package com.mg.ws.impl;
import java.math.BigDecimal;
import javax.jws.WebService;
import com.mg.ws.AccountDetailsFault;
import com.mg.ws.AccountDetailsFault_Exception;
import com.mg.ws.AccountDetailsPortType;
import com.mg.ws.AccountDetailsTO;
import com.mg.ws.GeAccountDetailsINTO;
import com.mg.ws.SecurityToken;


@WebService(name = "AccountDetailsService", 
   portName = "AccountDetailsPort", 
   endpointInterface = "com.mg.ws.AccountDetailsPortType", 
   wsdlLocation = "WEB-INF/wsdl/accounts.wsdl",
   targetNamespace="http://gognamunish.com/accounts")
    
public class AccountDetailsServiceImpl implements AccountDetailsPortType {
public AccountDetailsTO getAccountDetails(GeAccountDetailsINTO parameters,
SecurityToken requestHeader) throws AccountDetailsFault_Exception {
AccountDetailsTO detailsTO = new AccountDetailsTO();
// validate token 
validateToken(requestHeader);
// populate response
detailsTO = getDetailsFromSomewhere (parameters.getAccountNo());
return detailsTO;
}


private AccountDetailsTO getDetailsFromSomewhere(String accountNo) throws AccountDetailsFault_Exception {
if(accountNo == null || accountNo.trim().length()==0){
AccountDetailsFault faultInfo = new AccountDetailsFault();
faultInfo.setFaultInfo("missing account number");
faultInfo.setMessage("acclount number is required field");
throw new AccountDetailsFault_Exception("account no missing", faultInfo);
}
AccountDetailsTO detailsTO = new AccountDetailsTO();
detailsTO.setAccNo(accountNo);
detailsTO.setAccType("SAVING");
detailsTO.setBalance(new BigDecimal(10000));
return detailsTO;
}


private void validateToken(SecurityToken requestHeader) throws AccountDetailsFault_Exception {

if ("83711070".equals(requestHeader.getToken()) && requestHeader.getValidTill() != null){
System.out.println("token processed successfully...");
} else {
AccountDetailsFault faultInfo = new AccountDetailsFault();
faultInfo.setFaultInfo("Header token Invalid");
faultInfo.setMessage("can't help");
throw new AccountDetailsFault_Exception("invalid token", faultInfo);
}
}
}

This is just a dummy implementation for illustration purpose only. Now as our service implementation is done, we proceed to package this in a war and deploy it on tomcat.


Now we create a standard web.xml, which defines WSServletContextListenerWSServlet and structure of a web project.




Next we create a sun-jaxws.xml, defines the web service implementation class.




This file is required regardless of whether we publish our web service on tomcat, glassfish or any other server. OK so far so good , let's build our application and deploy it on tomcat, here is the ant script.


build.xml


<project name="webservice" basedir="../" default="deploy">


<!-- Project settings -->
<property name="project.distname" value="account" />
<!-- Local system paths -->
<property file="${basedir}/ant/build.properties" />
<property name="webroot.dir" value="${basedir}/WebContent" />
<property name="webinf.dir" value="${webroot.dir}/WEB-INF" />
<property name="build.dir" value="build" />


<path id="compile.classpath">
<!-- classpath for Jax WS related stuff -->
<pathelement path="${webinf.dir}/lib/activation.jar" />
<pathelement path="${webinf.dir}/lib/jaxb-api.jar" />
<pathelement path="${webinf.dir}/lib/jaxb-impl.jar" />
<pathelement path="${webinf.dir}/lib/jaxp-api.jar" />
<pathelement path="${webinf.dir}/lib/jaxws-api.jar" />
<pathelement path="${webinf.dir}/lib/jaxws-rt.jar" />
<pathelement path="${webinf.dir}/lib/jsr173_api.jar" />
<pathelement path="${webinf.dir}/lib/jsr181_api.jar" />
<pathelement path="${webinf.dir}/lib/resolver.jar" />
<pathelement path="${webinf.dir}/lib/saaj-api.jar" />
<pathelement path="${webinf.dir}/lib/saaj-impl.jar" />
<pathelement path="${webinf.dir}/lib/sjsxp.jar" />
<pathelement path="${webinf.dir}/lib/stax-ex.jar" />
<pathelement path="${webinf.dir}/lib/streambuffer.jar" />
<pathelement path="${webinf.dir}/classes" />
<pathelement path="${classpath.external}" />
<pathelement path="${classpath}" />
</path>


<!-- define your folder for deployment -->
<property name="deploy.dir" value="deploy" />
<!-- Local system paths -->
<property file="${basedir}/ant/build.properties" />
<property name="webroot.dir" value="${basedir}/WebContent" />
<property name="webinf.dir" value="${webroot.dir}/WEB-INF" />
<property name="build.dir" value="build" />
<!-- Check timestamp on files -->
<target name="prepare">
<tstamp />
</target>
<!-- Copy any resource or configuration files -->
<target name="resources">
<copy todir="${webinf.dir}/classes" includeEmptyDirs="no">
<fileset dir="JavaSource">
<patternset>
<include name="**/*.conf" />
<include name="**/*.properties" />
<include name="**/*.xml" />
</patternset>
</fileset>
</copy>
</target>
<!-- Normal build of application -->
<target name="compile" depends="prepare,resources">
<javac srcdir="JavaSource" destdir="${webinf.dir}/classes">
<classpath refid="compile.classpath" />
</javac>
</target>
<!-- Remove classes directory for clean build -->
<target name="clean" description="Prepare for clean build">
<delete dir="${webinf.dir}/classes" />
<mkdir dir="${webinf.dir}/classes" />
</target>
<!-- Build entire project -->
<target name="build" depends="prepare,compile" />
<target name="rebuild" depends="clean,prepare,compile" />
<!-- Create binary distribution -->
<target name="war" depends="build">
<mkdir dir="${build.dir}" />
<war basedir="${webroot.dir}" warfile="${build.dir}/${project.distname}.war" webxml="${webinf.dir}/web.xml">
<exclude name="WEB-INF/${build.dir}/**" />
<exclude name="WEB-INF/src/**" />
<exclude name="WEB-INF/web.xml" />
</war>
</target>
<!-- Create Client -->
<target name="jar">
<jar destfile="${build.dir}/${project.distname}_client.jar"
basedir="${webinf.dir}/classes"
includes="com/mg/ws/*"/>
</target>
<!-- deploy on tomcat -->
<target name="deploy" depends="war,jar">
<delete file="${deploy.dir}/${project.distname}.war" />
<delete dir="${deploy.dir}/${project.distname}" />
<copy file="${build.dir}/${project.distname}.war" todir="${TOMCAT_HOME}\webapps" />
</target>
</project>


NOTE: jars in lib should be carefully chosen, otherwise it will make your life hell.


Now let's test the application, point your browser to http://localhost:8080/account/details , if you see something like this, it means you have successfully deployed the service.



Now let's test the service, we can use the client generated by the wsimport tool as :

public static void main(String[] args) throws Exception {
AccountDetails accountDetails = new AccountDetails();
AccountDetailsPortType port = accountDetails.getAccountDetailsPort();
AccountDetailsTO details = port.getAccountDetails(new GeAccountDetailsINTO(), new SecurityToken());
}


For those who want to invoke the service using soap stuff, they can use tool like soapUI (can be downloaded from www.soapui.org), lets make some soap calls now:


case: invalid security token




case : valid security token
Add caption





Directory structure of this example is as follow:
By default, Tomcat does not comes with any JAX-WS dependencies, So, you have to include it manually.
2. Download JAX-WS RI distribution.
3. Unzip it and copy the jars mentioned in build.xml to lib folder.

Thats all for now ... Please provide your valuable comments or any suggestion.


It's Munish Gogna signing off now, have to prepare Dinner :)





Posted by Unknown
Tag :

source code of the JAX WS example..

You can now download the source code of the 'Creating and Deploying JAX-WS web service on Tomcat 6' article at dzone (check the resource section at bottom).


Here are the details: http://www.dzone.com/articles/creating-and-deploying-jax-ws 
Posted by Unknown

Popular Post

Labels

enums (1) java (2) JAX-RS (1) JPA (1) mysql (1) request 2 (1) RESTful (1) sphinx (1) tomcat (1) web service (2) ws (2)