Pages

Saturday, March 19, 2011

Axis1 SUNspecification. JAX-RPC JDK 1.4

Enabling the apache-axis1 engine in tomcat.
Download: axis-src-1_4
Just a sample example:
----------------------
Provider:
1)Create a dynamic web-project say. AxisLab1
2)Write the services that you want to expose.
Note:can also posses internal usage of custom beans.
3)Replace the existing web.xml with the one obtained from the axis.
4)Add the server-config.wsdd and update with the service information that is exposed.
5)Add i18n_ja.properties,i18n.properties and the .jsp pages.
6)Add the 11 jars..set from the buildpath the axis-src-1.4 jar files
7)Restart the server.

Client Side:
=============
1)creat the java project say:ClientAxisLab1
2)Add the 11 jars from the lib folder of axis-src-1_4
3)Write the client code and test it



Steps Provider Side:
--------------------
1)Project Name:AxisLab1
2)package com.tcs.address;
public class Address {
public City getAddress(String city1) {
System.out.println(city1 + "service side");
City city = new City();
city.setCity(city1);
return city;
}
}
Bean class:
------------
package com.tcs.address;

public class City {

String city;
String state = "KA";

public City() {
// TODO Auto-generated constructor stub
}

public void setCity(String city) {
System.out.println("*********" + city);
this.city = city;
}

public String getCity() {
return city;
}

public void setState(String state) {
this.state = state;
}

public String getState() {
return state;
}

}
3)Replace with the web.xml as said above.
4)Add the following in the server-config.wsdd



5)Add i18n_ja.properties,i18n.properties and the .jsp,.jws pages, that you see from "axis" folder.
6)Add the 11 jar files.
7)Restart the server.
Note:
------- This is similar to registering the .wsdd from the command line.
i.e say we have some "deploy.wsdd" file slr to server-config.wsdd,from the command
prompt we can register as:
java org.apache.axis.client.AdminClient -lhttp://:/axis/services/AdminService deploy.wsdd

Steps for Client Side Code:
---------------------------
1)Create a project say:ClientAxisLab1
2)Add the 11 jars
3)Client Code:
Client.java:
============
package com.tcs.client.axis1;

import java.net.MalformedURLException;
import java.rmi.RemoteException;

import javax.xml.namespace.QName;
import javax.xml.rpc.ParameterMode;
import javax.xml.rpc.ServiceException;
import javax.xml.rpc.encoding.XMLType;

import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.encoding.ser.BeanDeserializerFactory;
import org.apache.axis.encoding.ser.BeanSerializerFactory;

public class Client {
public static void main(String[] args) {
Service service = new Service();
String endPoint = "http://localhost:8080/AxisLab1/services/address";
try {
Call call = (Call) service.createCall();
try {
call.setTargetEndpointAddress(new java.net.URL(endPoint));
call.setOperationName("getAddress");
String city = "bangalore";
QName qName = new QName("ns1:City", "city");
BeanSerializerFactory ser = new BeanSerializerFactory(
City.class, qName);
BeanDeserializerFactory deser = new BeanDeserializerFactory(
City.class, qName);

call.registerTypeMapping(City.class, qName, ser, deser);
call.addParameter("city", qName, ParameterMode.IN);
call.setReturnType(qName);

try {
City cty = (City) call.invoke(new Object[] { city });
System.out.println(cty.getCity());
System.out.println(cty.getState());
/*
* String cty = (String) call.invoke(new Object[]{city});
* System.out.println(cty+"testst");
*/

} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

} catch (ServiceException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

}

City.java
---------
package com.tcs.client.axis1;

public class City {

String city;
String state;
public City() {
// TODO Auto-generated constructor stub
}

public void setCity(String city) {
this.city = city;
}
public String getCity() {
return city;
}

public void setState(String state) {
this.state = state;
}
public String getState() {
return state;
}

}

Run the Client.java.

oops!! you will get an error:
java.io.IOException: No serializer found for class com.tcs.address.City in registry org.apache.axis.encoding.TypeMappingDelegate@9b2a51
-------------
AxisFault
faultCode: {http://schemas.xmlsoap.org/soap/envelope/}Server.userException
faultSubcode:
faultString: org.xml.sax.SAXParseException: Premature end of file.
faultActor:
faultNode:
faultDetail:
{http://xml.apache.org/axis/}stackTrace:org.xml.sax.SAXParseException: Premature end of file.
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:236)
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.fatalError(ErrorHandlerWrapper.java:215)
at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:386)
at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:316)
at com.sun.org.apache.xerces.internal.impl.XMLVersionDetector.determineDocVersion(XMLVersionDetector.java:230)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:798)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:764)
at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:148)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1242)
at javax.xml.parsers.SAXParser.parse(SAXParser.java:375)
at org.apache.axis.encoding.DeserializationContext.parse(DeserializationContext.java:227)

How to resolve????
Reason:We haven't add the beanMapping (City.java).In general axis will take care of the serialization and deserialization between the exposed service and the client data(SOAP xml data),for the custom beans we need to tell the engine/wsManager which Java classes map with which XMLtypes.
so add the following before the end of the service tag :


beanMapping qname="ns1:City" xmlns:ns1="http://com.tcs.address1"
languageSpecificType="java:com.tcs.address.City"/>

now restart the tomcat and test from the client.
o/p:
bangalore
KA


Thanks/-
keely

Axis1 sun-specif JAX-RPC JDK1.4

Enabling the apache-axis1 engine in tomcat.
Download: axis-src-1_4
1)Create a dynamic web-project say. AxisLab1
2)Write the services that you want to expose.
Note:can also posses internal usage of custom beans.
3)Replace the existing web.xml with the one obtained from the axis


Monday, March 7, 2011

SAX and DOM Parsers

SAX:Simple API for XML.
1)SAX is read-only parser.We cann't delete,update,create/generate new XML.
More over SAX reads the data in a sequential order.
SAX is based on Event-Driven Model.
An Event-Driven Model implies that parsing an XML results into following events.
(1)start of document
(2)End of document
(3)Start of Element
(4)End of Element
(5)Character Data is started.
SAX occupies less memory as it loads one element at any time.

Consider the example below: Jars required are Xerces.jar
employees.xml
-------------
<?xml version="1.0" encoding="UTF-8"?>
< employees>
< employee id="768">
< ename>keely</ename>
< email>dileepkeely@gmail.com</email>
< phone>09880614099</phone>
< /employee>
< /employees>

XMLReader.java
--------------
package com.keelys.xml.reader;
import com.keely.handler.*;
import java.io.IOException;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.XMLReaderFactory;
public class XMLReader {
public static void main(String[] args) {
try {
org.xml.sax.XMLReader reader = XMLReaderFactory
.createXMLReader("org.apache.xerces.parsers.SAXParser");
reader.setContentHandler(new HandlerEvent());
try {
reader.parse("employees.xml");
} catch (IOException e) {
e.printStackTrace();
}
} catch (SAXException e) {
e.printStackTrace();
}
}
}
Note:
"org.apache.xerces.parsers.SAXParser" :It is the main Xerces SAX parser class.
It uses abstract SAX parser with a document scanner,DTD and attempt to create
an XMLReader.
"XMLReader": Interface for reading an XML document using callbacks

HandlerEvent.java
-----------------
package com.keely.handler;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class HandlerEvent extends DefaultHandler {
public void startDocument() {
System.out.println("Start of document");
}

public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
for (int i = 0; i < attributes.getLength(); i++) {
System.out.println(attributes.getType(0) + "\t"
+ attributes.getLocalName(0) + "\t"
+ attributes.getValue(i));
}
System.out.println(qName);
}

public void characters(char[] ch, int start, int length)
throws SAXException {
String characterData = new String(ch, start, length);
System.out.println(characterData);
}

public void endDocument() {
System.out.println("end document");
}
}

o/p:
Start of document
employees

CDATA id 768
employee

ename
keely

email
dileepkeely@gmail.com

phone
09880614099

end document



DOM:Document Object Model.

Sunday, March 6, 2011

WebService Implementation and Specification



SNO.Sun Specification Sun Implementation Apache JDK
1JAX-RPC JWSDPApache SOAP
Apache Axis1(1.4)
XFire
JDK 1.4+
2JAX-WS METROApache Axis2(1.4)
Apache CXF
JDK 1.5+
3JAX-RS JERSYApache Axis2(1.4)
Apache CXF
JDK 1.5+

Webservices can be developed in two approaches:
1)TOP-BOTTOM Approach(Contract-First Services)
i.e WSDL -> Classes
2)BOTTOM-TOP Approach (Code-First Services)
i.e Classes -> WSDL

**Contract-First services are always better than Code-First Services

Saturday, March 5, 2011

JAVA Reflection and Spring Proxy

http://java.sun.com/developer/technicalArticles/ALT/Reflection/

Best Java Stack for SOAP-based Web Services

http://www.developer.com//java/ent/spring-web-services-2.0-best-java-stack-for-soap-based-web-services.html

visual VM

http://download.oracle.com/javase/6/docs/technotes/guides/visualvm/index.html

JAXB using castor framework

http://castor.org/xml-mapping.html
JAXB:Java API for XML binding.
JAXB allows to map the java fields to XML tags i.e binding the java fields to the corresponding XML tags.Using JAXB we can perform a)Marshalling and b)UnMarshalling
a)Marshalling:It is the process of converting java object graph to XML document.This is also called XMLSerialization.
b)UnMarshalling:It is the process of converting the XML document to Java Object graph.This is also called as XMLDeserialization

Required Jars:
1)Xerces 2)castor-1.3.1-ddlgen, castor-1.3.1-xml,castor-1.3.1-xml-schema,castor-1.3.1,castor-1.3.1-codegen,castor-1.3.1-core.

Steps:
1)Create a project JAXBApp
1)Write a XSD(Schema document) file
2)Add the jar files to build path.
3)Create a class as below and run it for generating the java code.
package com.keely.jaxb.test;
import org.exolab.castor.builder.SourceGenerator;
public class Generator {
public static void main(String[] args) {

String[] arg = { "-i", "Customers.xsd", "-package", "com.tcs.castor" };
SourceGenerator generator = new SourceGenerator();
generator.main(arg);
}
}
"-i" denotes the inputfile in our case the schema definition file
"-package" denotes the package under which the generated java code should fall into.
4)Now you can see generated classes under the package.Which consists of the setters and getters along with the code for marshal and unmarshal.

public void marshal(final java.io.Writer out) throws org.exolab.castor.xml.MarshalException, org.exolab.castor.xml.ValidationException {
org.exolab.castor.xml.Marshaller.marshal(this, out);
}

public static com.tcs.castor.Customer unmarshal(final java.io.Reader reader) throws
org.exolab.castor.xml.MarshalException,org.exolab.castor.xml.ValidationException
{
return (com.tcs.castor.Customer) org.exolab.castor.xml.Unmarshaller.unmarshal(com.tcs.castor.Customer.class, reader);
}

5)Copy the generated classes to source folder
6)Create a package called com.keely.jaxb.test with a class called MarshalXMLGenerator.java and all the setters
7)Invoke the marshal method for creating the XML file as below.
Address address = new Address();
address.setCity("Banglore");
address.setPhone("09880614099");
address.setState("Karnataka");
address.setStreet("Marathalli");
Customer customer = new Customer();
customer.setCid("889");
customer.setCname("dileepkeely");
customer.setEmail("dileepkeely@gmail.com");
customer.addAddress(address);

try {
customer.marshal(new FileWriter("Customers.xml"));
} catch (MarshalException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
8)This will generate the XML file "Customers.xml".
This process is called XMLSerialization
9)For XMLDeserialization:
Customers customers = new Customers();
try {
customers.unmarshal(new FileReader("Customers.xml"));
} catch (MarshalException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

Marshalling and UnMarshalling using spring

http://static.springsource.org/spring-ws/sites/1.5/reference/html/oxm.html

Web Services

http://saloon.javaranch.com/forums/forum-051.html

"Under the Hood" with javap

http://www.javaranch.com/journal/200408/Journal200408.jsp#a1

Strings, Literally

http://www.javaranch.com/journal/200409/ScjpTipLine-StringsLiterally.html