Wednesday, July 20, 2011

Register dynamic Web Service Client Handler


package javawiki.webservice.consumer;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import javawiki.webservice.common.LogHandler;
import javawiki.webservice.provider.GetBookDetails;
import javawiki.webservice.provider.LibraryService;
import javax.xml.namespace.QName;
import javax.xml.ws.BindingProvider;
import javax.xml.ws.Service;
import javax.xml.ws.handler.Handler;
public class ConsumeLibraryService {
 public static void main(String arg[])
 {
  try
  {
   URL url = new URL("http://localhost:8080/libraryservice/LibraryService");
   QName qName = new QName("http://javawikiforbeginners.blogspot.com/JavaLibraryService/","JavaLibraryService");
   Service service = Service.create(url, qName);
   LibraryService libraryService = service.getPort(LibraryService.class);
   BindingProvider bindingProvider = (BindingProvider) libraryService;
   
   List<Handler> handlerChain = new ArrayList<Handler>();
   
   handlerChain.add(new LogHandler());
   
   bindingProvider.getBinding().setHandlerChain(handlerChain);
   
   GetBookDetails aBookDtls = libraryService.getBook("123456");
   System.out.println(aBookDtls.getBook());
  }
  catch(Exception ex)
  {
   ex.printStackTrace();
  }
 }
}



package javawiki.webservice.common;
import java.util.Set;
import javax.xml.namespace.QName;
import javax.xml.ws.handler.MessageContext;
import javax.xml.ws.handler.soap.SOAPHandler;
import javax.xml.ws.handler.soap.SOAPMessageContext;
public class LogHandler implements SOAPHandler<SOAPMessageContext>{
 @Override
 public Set<QName> getHeaders() {
  // TODO Auto-generated method stub
  return null;
 }
 @Override
 public void close(MessageContext context) {
  System.out.println("Closing message");
 }
 @Override
 public boolean handleFault(SOAPMessageContext context) {
  // TODO Auto-generated method stub
  return false;
 }
 @Override
 public boolean handleMessage(SOAPMessageContext context) {
  Boolean outbound = (Boolean) context
  .get(SOAPMessageContext.MESSAGE_OUTBOUND_PROPERTY);
  
  System.out.println(context.keySet());
  
  if(outbound)
  {
   System.out.println("=====================this is outbound log handler===============");
   return true;
  }
  else
  {
   System.out.println("=====================this is inbound log handler===============");
   return false;
  }
 }
}

Tuesday, July 5, 2011

Validate JAXB Object with XML Schema

We can validate JAXB Object with XML Schema(XSD). JAXB provide a class named Validator to validate against a schema. This class is available since Java 1.5


package javawiki.jaxb.validation;

import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;

@XmlRootElement
@XmlType(name = "Customer", propOrder = {
"name",
"phoneNumber",
})
public class Customer {

private String name;
private List phoneNumber;

public Customer() {
phoneNumber = new ArrayList();
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public List getPhoneNumber() {
return phoneNumber;
}

public void setPhoneNumber(List phoneNumber) {
this.phoneNumber = phoneNumber;
}
}

package javawiki.jaxb.validation;

public class PhoneNumber {

}


<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
 <xs:element name="customer">
  <xs:complexType>
   <xs:sequence>
    <xs:element name="name" type="stringMaxSize5" />
    <xs:element ref="phoneNumber" maxOccurs="3" />
   </xs:sequence>
  </xs:complexType>
 </xs:element>

 <xs:element name="phoneNumber">
  <xs:complexType />
 </xs:element>

 <xs:simpleType name="stringMaxSize5">
  <xs:restriction base="xs:string">
   <xs:maxLength value="5" />
  </xs:restriction>
 </xs:simpleType>

</xs:schema>


package javawiki.jaxb.validation;

import org.xml.sax.ErrorHandler;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;

public class MyErrorHandler implements ErrorHandler {

 @Override
 public void error(SAXParseException exception) throws SAXException {
  System.out.println(exception.getLocalizedMessage());
 }

 @Override
 public void fatalError(SAXParseException exception) throws SAXException {
  System.out.println(exception.getLocalizedMessage());
 }

 @Override
 public void warning(SAXParseException exception) throws SAXException {
  System.out.println(exception.getLocalizedMessage());
 }

}


package javawiki.jaxb.validation;

import java.io.File;

import javax.xml.XMLConstants;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.util.JAXBSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Validator;

public class DemoSchemaValidation {

 public static void main(String[] args) {
  Customer customer = new Customer();
        customer.setName("Jane ");
        customer.getPhoneNumber().add(new PhoneNumber());
        customer.getPhoneNumber().add(new PhoneNumber());
        customer.getPhoneNumber().add(new PhoneNumber());
        
        try
        {
         JAXBContext jc = JAXBContext.newInstance(Customer.class);
         JAXBSource source = new JAXBSource(jc, customer);
 
         SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); 
         Schema schema = sf.newSchema(new File("src/javawiki/jaxb/validation/customer.xsd")); 
 
         Validator validator = schema.newValidator();
         validator.setErrorHandler(new MyErrorHandler());
         validator.validate(source);
        }catch(Exception ex)
        {
         ex.printStackTrace();
        }
 }

}

Output:
cvc-maxLength-valid: Value 'Jane Doe' with length = '8' is not facet-valid with respect to maxLength '5' for type 'stringMaxSize5'.
cvc-type.3.1.3: The value 'Jane Doe' of element 'name' is not valid.
cvc-complex-type.2.4.d: Invalid content was found starting with element 'phoneNumber'. No child element is expected at this point.