Spring WS - Part 2

The Service endpoint class is:

@Endpoint
public class CountryEndpoint {
 
 static final Logger log = LoggerFactory.getLogger(CountryEndpoint.class);
 
 private static final String NAMESPACE_URI = "http://spring.io/guides/gs-producing-web-service";

 private CountryRepository countryRepository;

 /*@Autowired
 public CountryEndpoint(CountryRepository countryRepository) {
  this.countryRepository = countryRepository;
 }*/

 @PayloadRoot(namespace = NAMESPACE_URI, localPart = "getCountryRequest")
 public void getOriginalDetails(@RequestPayload GetCountryRequest request) throws Exception {
  log.debug("***** Web Service Endpoint is Hit ****** ");
  DataHandler fileDataHandler =  request.getFile();
  log.debug("The DataHandler is {}",fileDataHandler);
  log.debug("The ContentType {},  {}-----> {}",fileDataHandler.getContentType(),fileDataHandler.getDataSource().getClass(),fileDataHandler.getAllCommands().toString());
  InputStream is = fileDataHandler.getDataSource().getInputStream();
  log.debug("The Read count is: {}",is.read());
  while(is.read() != -1)
  {
   System.out.print(  (((char)is.read())) );
  }
  GetCountryResponse response = new GetCountryResponse();
  
  throw new SpringWSException(" **** Test Exceptions through Handle Fault In Interceptor ");
  //response.setCountry(countryRepository.findCountry(request.getName()));

  //return response;
 }
}

The Schema that we have used here is: (countries.xsd)

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xmime="http://www.w3.org/2005/05/xmlmime"
 xmlns:tns="http://spring.io/guides/gs-producing-web-service"
           targetNamespace="http://spring.io/guides/gs-producing-web-service" elementFormDefault="qualified">

    <xs:element name="getCountryRequest">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="name" type="xs:string"/>
                <xs:element name="file" type="xs:base64Binary" xmime:expectedContentTypes="application/octet-stream"></xs:element>
            </xs:sequence>
        </xs:complexType>
    </xs:element>

    <xs:element name="getCountryResponse">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="country" type="tns:country"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>

    <xs:complexType name="country">
        <xs:sequence>
            <xs:element name="name" type="xs:string"/>
            <xs:element name="population" type="xs:int"/>
            <xs:element name="capital" type="xs:string"/>
            <xs:element name="currency" type="tns:currency"/>
        </xs:sequence>
    </xs:complexType>

    <xs:simpleType name="currency">
        <xs:restriction base="xs:string">
            <xs:enumeration value="GBP"/>
            <xs:enumeration value="EUR"/>
            <xs:enumeration value="PLN"/>
        </xs:restriction>
    </xs:simpleType>
</xs:schema>

We use jaxb2-maven-plugin to autogenerate classes from the schema. These auto generated artifacts acts as request response entities, as we can see from the Endpoint code. Our client is a simple Spring MVC controller, which is:


@Controller
public class TestController {

 static Logger log = LoggerFactory.getLogger(TestController.class);

 @Autowired
 WebServiceTemplate webServiceTemplate;

 String data;
 
 @PostConstruct
 public void PostConstrut()
 {
  Assert.notNull(webServiceTemplate, "WebServiceTemplate Must not be null");
 }
 
 
 @RequestMapping(value="/testWSService", method=RequestMethod.GET)
 public ModelAndView testWebService(ModelMap model)
 {
  log.debug(" ***** In testWebService() method, Testing Services with Web Service Template ***** ");
  GetCountryRequest getCountryRequest = new ObjectFactory().createGetCountryRequest();
  getCountryRequest.setName("OK");
  FileDataSource fileDataSource = new FileDataSource("C:/Users/MIC/Desktop/Please Delete/Spring_Sec_Oauth.txt");
  DataHandler dataHandler = new DataHandler(fileDataSource);
  getCountryRequest.setFile(dataHandler);
  webServiceTemplate.marshalSendAndReceive(getCountryRequest);
  ModelAndView modelAndView = new ModelAndView();
  return modelAndView;
 }
}

Now here comes the Interceptors. Interceptors are the integral part of Spring WS. Configuring interceptors for Spring WS is quick and easy. and the Custom interceptor we have used here is CountryInterceptor.java

public class CountryInterceptor implements EndpointInterceptor {

 static Logger log = LoggerFactory.getLogger(CountryInterceptor.class);
 
 public void afterCompletion(MessageContext messageContext, Object object, Exception exception)
   throws Exception {
  // TODO Auto-generated method stub
  log.debug(" ***** In afterCompletion of {}",CountryInterceptor.class.getName());
 }

 public boolean handleFault(MessageContext messageContext, Object object)
   throws Exception {
  // TODO Auto-generated method stub
  log.debug(" ***** In handleFault of {}",CountryInterceptor.class.getName());
  return true;
 }

 public boolean handleRequest(MessageContext messageContext, Object object)
   throws Exception {
  // TODO Auto-generated method stub
  log.debug(" ***** In handleRequest of {}",CountryInterceptor.class.getName());
  /*DefaultMessageContext soapMessageContext = (DefaultMessageContext)messageContext;
  
  while(it.hasNext())
  {
   log.debug("Attachment Iterator {}",it.next().getClass().getName());
  }*/
  return true;
 }

 public boolean handleResponse(MessageContext messageContext, Object object)
   throws Exception {
  // TODO Auto-generated method stub
  log.debug(" ***** In handleResponse of {}",CountryInterceptor.class.getName());
  return true;
 }

}


For Custom Interceptors in Spring WS we need to implement the EndpointInterface and provide implementation to its appropriate methods and then can be injected into Spring container for action as:
interceptors.add(new PayloadRootSmartSoapEndpointInterceptor(new CountryInterceptor(), "http://spring.io/guides/gs-producing-web-service", 
I am not going into the details of contract of EndpointInterface methods as they are very well described in official docs.

spring-ws-part-3

Comments

Popular posts from this blog

Use of @Configurable annotation.

Spring WS - Part 5

Spring WS - Part 4