Spring WS - Part 1

So lets start.....
My configurations files are as follows:
1) SpringWSApplicationInitializer.java
2) SpringWSApplicationConfig.java
Instead of web.xml I am using WebApplicationInitializer

public class SpringWSApplicationInitializer implements WebApplicationInitializer {

 static final Logger log = LoggerFactory.getLogger(SpringWSApplicationInitializer.class);
 
 
 public void onStartup(ServletContext servletContext) throws ServletException {
  // TODO Auto-generated method stub
  log.debug(" ********* Spring WS Application StartUP in Progress............ ********** ");
  AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
  context.register(SpringWSApplicationConfig.class);
  
  /**
   * Configuring MessageDispatcherServlet for Spring Web Services  
   * 
   */
  
  ServletRegistration.Dynamic dispatcherWS = servletContext.addServlet("springws", new MessageDispatcherServlet(context));
  dispatcherWS.setInitParameter("transformWsdlLocations", "true");
  dispatcherWS.setLoadOnStartup(1);
  //dispatcher.setAsyncSupported(true);
  dispatcherWS.addMapping("/ws/*");
  
  /**
   * Configuring Dispatcher Servlet for Spring MVC
   * 
   */
  
  
  ServletRegistration.Dynamic dispatcherMVC = servletContext.addServlet("dispatcher", new DispatcherServlet(context));
  dispatcherMVC.setLoadOnStartup(1);
  dispatcherMVC.setAsyncSupported(true);
  dispatcherMVC.addMapping("/config/*");
  
  
  /**
   *  Register Character Encoding Filter
   */

  FilterRegistration.Dynamic characterEncodingFilter =  
  servletContext.addFilter("CharacterEncodingFilter", CharacterEncodingFilter.class); 
  characterEncodingFilter.setInitParameter("encoding", "UTF-8");
  characterEncodingFilter.setInitParameter("forceEncoding", "true");
  characterEncodingFilter.addMappingForUrlPatterns(null, false, "/*"); 
  characterEncodingFilter.setAsyncSupported(true);
  
  servletContext.addListener(new ContextLoaderListener(context));

  log.debug(" ********* Spring WS Application StartUP Completed ********** ");
  
 }

 
}


I have configured here two servlets, One for Spring Web MVC and another for Spring WS
@EnableWs
@Configuration
@EnableWebMvc
@ComponentScan({"com.springws.service","com.springws.controller"})
public class SpringWSApplicationConfig extends WsConfigurerAdapter {

 static final Logger log = LoggerFactory.getLogger(SpringWSApplicationConfig.class);


 @Bean(name = "countries")
 public DefaultWsdl11Definition defaultWsdl11Definition(XsdSchema countriesSchema) {
  DefaultWsdl11Definition wsdl11Definition = new DefaultWsdl11Definition();
  wsdl11Definition.setPortTypeName("CountriesPort");
  wsdl11Definition.setLocationUri("/countryService/");
  wsdl11Definition.setTargetNamespace("http://spring.io/guides/gs-producing-web-service");
  wsdl11Definition.setSchema(countriesSchema);
  return wsdl11Definition;
 }

 @Bean
 public XsdSchema countriesSchema() {
  return new SimpleXsdSchema(new ClassPathResource("countries.xsd"));
 }


 @Bean
 public Jaxb2Marshaller marshaller()
 {
  Jaxb2Marshaller jaxb2Marshaller = new Jaxb2Marshaller();
  jaxb2Marshaller.setContextPath("io.spring.guides.gs_producing_web_service");
  jaxb2Marshaller.setMtomEnabled(true);
  return jaxb2Marshaller;
 }

 @Bean
 public WebServiceTemplate webServiceTemplate() throws Exception
 {
  WebServiceTemplate webServiceTemplate = new WebServiceTemplate();
  webServiceTemplate.setMarshaller(marshaller());
  webServiceTemplate.setUnmarshaller(marshaller());
  webServiceTemplate.setDefaultUri("http://localhost:8080/ws/countryService/countries");
  webServiceTemplate.setInterceptors(new ClientInterceptor[]{wss4jSecurityInterceptorClient()});
  webServiceTemplate.setMessageFactory(messageFactory());
  return webServiceTemplate;
 }

 /**
  * Adding Interceptors
  * 
  */

 @Override
 public void addInterceptors(List interceptors)  {
  interceptors.add(new SoapEnvelopeLoggingInterceptor());
  interceptors.add(new PayloadLoggingInterceptor());
  interceptors.add(new PayloadRootSmartSoapEndpointInterceptor(new CountryInterceptor(), "http://spring.io/guides/gs-producing-web-service", "getCountryRequest"));

  //Adding Interceptor for Spring Security

  //interceptors.add(wss4jSecurityInterceptor());
  interceptors.add(wss4jSecurityInterceptor());
 }

 
 @Bean
 public SaajSoapMessageFactory messageFactory() throws Exception
 {
  SaajSoapMessageFactory saajSoapMessageFactory = new SaajSoapMessageFactory(MessageFactory.newInstance());
  saajSoapMessageFactory.setSoapVersion(SoapVersion.SOAP_11);
  return saajSoapMessageFactory;
  
 }
 
 @Bean
 public DefaultMethodEndpointAdapter defaultMethodEndpointAdapter()
 {
  DefaultMethodEndpointAdapter defaultMethodEndpointAdapter = new DefaultMethodEndpointAdapter();
  List  list = new ArrayList();
  List  list2 = new ArrayList();
  list.add(marshallingPayloadMethodProcessor());
  defaultMethodEndpointAdapter.setMethodArgumentResolvers(list);
  defaultMethodEndpointAdapter.setMethodReturnValueHandlers(list2);
  return defaultMethodEndpointAdapter;
 }
 
 
 @Bean
 public MarshallingPayloadMethodProcessor marshallingPayloadMethodProcessor()
 {
  MarshallingPayloadMethodProcessor marshallingPayloadMethodProcessor = new MarshallingPayloadMethodProcessor(marshaller());
  return marshallingPayloadMethodProcessor;
 }

 /**
  * Adding WS-Security - UserNameTokenAuthentication Interceptor
  * 
  */

 @Bean
 public Wss4jSecurityInterceptor wss4jSecurityInterceptor()
 {
  /**
   * The SecurementActions for UsernameToken is commented, as the
   * UsernameToken is to be added by the client, the server-side
   * interceptor would only validate that.
   * 
   */

  Wss4jSecurityInterceptor wss4jSecurityInterceptor = new Wss4jSecurityInterceptor();
  wss4jSecurityInterceptor.setValidationActions("UsernameToken");
  //wss4jSecurityInterceptor.setSecurementActions("UsernameToken");
  wss4jSecurityInterceptor.setSecurementUsername("Ernie");
  wss4jSecurityInterceptor.setSecurementPassword("Bert");
  wss4jSecurityInterceptor.setSecurementPasswordType("PasswordText");
  wss4jSecurityInterceptor.setValidationCallbackHandler(callbackHandler());
  return wss4jSecurityInterceptor;
 }

 /**
  * Validation CallBack Handler for UserNameToken Authentication
  * 
  */

 @Bean
 public SimplePasswordValidationCallbackHandler callbackHandler()
 {
  SimplePasswordValidationCallbackHandler simplePasswordValidationCallbackHandler = new SimplePasswordValidationCallbackHandler();
  Map userMap = new HashMap();
  userMap.put("admin", "secret");
  userMap.put("user", "pass");
  userMap.put("Ernie","Bert");
  simplePasswordValidationCallbackHandler.setUsersMap(userMap);
  return simplePasswordValidationCallbackHandler;
 }


 /**
  * Adding WS-Security - UserNameTokenAuthentication Interceptor
  * for CLIENT
  */

 @Bean
 public Wss4jSecurityInterceptor wss4jSecurityInterceptorClient()
 {

  /* Please note If ValidationActions needs to be mentioned
   * then Validdation CallBack Handler must be there
   */ 


  Wss4jSecurityInterceptor wss4jSecurityInterceptor = new Wss4jSecurityInterceptor();
  //wss4jSecurityInterceptor.setValidationActions("UsernameToken");
  wss4jSecurityInterceptor.setSecurementActions("UsernameToken");
  wss4jSecurityInterceptor.setSecurementUsername("Ernie");
  wss4jSecurityInterceptor.setSecurementPassword("Bert");
  wss4jSecurityInterceptor.setSecurementPasswordType("PasswordText");
  //wss4jSecurityInterceptor.setValidationCallbackHandler(callbackHandler());
  return wss4jSecurityInterceptor;
 }



 /**
  * Adding WS-Security - DigitalSignature Interceptor
  * 
  */



 private CryptoFactoryBean signatureCrypto()
 {
  CryptoFactoryBean cryptoFactoryBean = new CryptoFactoryBean();
  cryptoFactoryBean.setKeyStorePassword("springws");
  try {
   cryptoFactoryBean.setKeyStoreLocation(new FileSystemResource("F:/My_Work/New_Spring_Workspaces/SpringWebServices/springwskeystore.jks"));
  } catch (IOException e) {
   // TODO Auto-generated catch block
   log.error(e.getLocalizedMessage(),e);
  }
  return cryptoFactoryBean;
 }

 //@Bean
 /*public Wss4jSecurityInterceptor wss4jSecurityInterceptorForDigitalSignature()
 { 
  try{
   Wss4jSecurityInterceptor wss4jSecurityInterceptor = new Wss4jSecurityInterceptor();
   signatureCrypto().afterPropertiesSet();
   wss4jSecurityInterceptor.setValidationActions("Signature");
   wss4jSecurityInterceptor.setValidationSignatureCrypto(signatureCrypto().getObject());
   return wss4jSecurityInterceptor;
  }
  catch(Exception e)
  {
   log.debug(e.getLocalizedMessage(),e);
   return null;
  }
 }*/

 //@Bean
 /*public Wss4jSecurityInterceptor wss4jSecurityInterceptorForDigitalSignatureClient()
 { 
  try{
   Wss4jSecurityInterceptor wss4jSecurityInterceptor = new Wss4jSecurityInterceptor();
   wss4jSecurityInterceptor.setSecurementActions("Signature");
   wss4jSecurityInterceptor.setSecurementUsername("springws");
   wss4jSecurityInterceptor.setSecurementPassword("springws");
   //wss4jSecurityInterceptor.setSecurementSignatureKeyIdentifier("DirectReference");
   signatureCrypto().afterPropertiesSet();
   wss4jSecurityInterceptor.setValidationActions("Signature");
   wss4jSecurityInterceptor.setValidationSignatureCrypto(signatureCrypto().getObject());
   wss4jSecurityInterceptor.setSecurementSignatureCrypto(signatureCrypto().getObject());
   return wss4jSecurityInterceptor;
  }
  catch(Exception e)
  {
   log.debug(e.getLocalizedMessage(),e);
   return null;
  }
 }*/


}

Now in the above Spring WS configuration, there are lots of beans which corresponds to Interceptor, WS-Security, MTOM features and they are not required at implementing Basic Web Service. I have included all of them at the same time and we will be referencing them while exploring features on the go.

spring-ws-part-2

Comments

Popular posts from this blog

Use of @Configurable annotation.

Spring WS - Part 5

Spring WS - Part 4