spring-ws


package org.upyaya.sample.echo.client;

import java.io.FileNotFoundException;
import java.io.IOException;

import javax.xml.soap.Name;
import javax.xml.soap.SOAPElement;
import javax.xml.soap.SOAPEnvelope;
import javax.xml.soap.SOAPException;
import javax.xml.soap.SOAPHeader;
import javax.xml.soap.SOAPHeaderElement;
import javax.xml.soap.SOAPMessage;
import javax.xml.soap.SOAPPart;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import org.springframework.ws.WebServiceMessage;
import org.springframework.ws.client.core.WebServiceMessageCallback;
import org.springframework.ws.client.core.support.WebServiceGatewaySupport;
import org.springframework.ws.soap.saaj.SaajSoapMessage;
import org.springframework.ws.soap.security.xwss.XwsSecuritySecurementException;
import org.springframework.xml.transform.StringResult;
import org.upyaya.sample.echo.domain.schema.EchoRequest;
import org.upyaya.sample.echo.domain.schema.EchoResponse;
import org.upyaya.sample.echo.domain.schema.EchoType;
import org.upyaya.sample.echo.domain.schema.ReturnType;

import com.sun.xml.wss.ProcessingContext;
import com.sun.xml.wss.XWSSProcessor;
import com.sun.xml.wss.XWSSProcessorFactory;
import com.sun.xml.wss.XWSSecurityException;

public class EchoClient extends WebServiceGatewaySupport {

private Resource request;

private static XWSSProcessor cprocessor;

private static ProcessingContext context;

public void setRequest(Resource request) {
this.request = request;
}

public void echo() throws IOException {
// Source requestSource = new ResourceSource(request);
StringResult result = new StringResult();
// getWebServiceTemplate().marshalSendAndReceive("http://localhost:8080/echoservice/",
// requestPayload);
// getWebServiceTemplate().sendSourceAndReceiveToResult("http://localhost:8080/echoservice/",
// requestPayload, responseResult)
/*
* getWebServiceTemplate().sendSourceAndReceiveToResult(requestSource,
* new WebServiceMessageCallback() { public void
* doWithMessage(WebServiceMessage message) throws IOException { // We
* can cast safley to SaajSoapMessage SaajSoapMessage saajSoapMessage =
* (SaajSoapMessage) message; SOAPMessage saajMessage = saajSoapMessage
* .getSaajMessage(); try { context.setSOAPMessage(saajMessage);
* SOAPMessage securedMessage = cprocessor
* .secureOutboundMessage(context);
* saajSoapMessage.setSaajMessage(securedMessage);
* } catch (XWSSecurityException e) { throw new
* XwsSecuritySecurementException(e .getMessage()); }
* }
* }, result);
*/
// getWebServiceTemplate().marshalSendAndReceive(requestPayload,
// requestCallback)
EchoRequest request = new EchoRequest();
EchoType echoType = new EchoType();
echoType.setName("Teng");
request.setEcho(echoType);
EchoResponse response = (EchoResponse)getWebServiceTemplate().marshalSendAndReceive(request);
/* EchoResponse response = (EchoResponse) getWebServiceTemplate()
.marshalSendAndReceive(request,
new WebServiceMessageCallback() {
public void doWithMessage(WebServiceMessage message)
throws IOException {
// We can cast safley to SaajSoapMessage
SaajSoapMessage saajSoapMessage = (SaajSoapMessage) message;
SOAPMessage saajMessage = saajSoapMessage
.getSaajMessage();
SOAPPart soapPart = saajMessage.getSOAPPart();
try {
SOAPEnvelope soapEnvelope = soapPart
.getEnvelope();
SOAPHeader soapHeader = soapEnvelope
.getHeader();
Name headerElementName = soapEnvelope
.createName(
"Security",
"wsse",
"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd");

// Add "Security" soapHeaderElement to soapHeader
SOAPHeaderElement soapHeaderElement = soapHeader
.addHeaderElement(headerElementName);

// This may be important for some portals!
soapHeaderElement.setActor(null);

// Add usernameToken to "Security" soapHeaderElement
SOAPElement usernameTokenSOAPElement = soapHeaderElement
.addChildElement("UsernameToken");

// Add username to usernameToken
SOAPElement userNameSOAPElement = usernameTokenSOAPElement
.addChildElement("Username");

userNameSOAPElement
.addTextNode("upyaya");

// Add password to usernameToken
SOAPElement passwordSOAPElement = usernameTokenSOAPElement
.addChildElement("Password");

passwordSOAPElement
.addTextNode("123456");

SOAPElement passwordTextSOAPElement = usernameTokenSOAPElement
.addChildElement("PasswordType");

passwordTextSOAPElement
.addTextNode("http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText");
System.out.println("here");
} catch (SOAPException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
context.setSOAPMessage(saajMessage);
SOAPMessage securedMessage = cprocessor
.secureOutboundMessage(context);
saajSoapMessage
.setSaajMessage(securedMessage);

} catch (XWSSecurityException e) {
throw new XwsSecuritySecurementException(e
.getMessage());
}

}

});*/
System.out.println("just a stop");
ReturnType returnType = response.getEchoResponse();
System.out.println(returnType.getMessage());
System.out.println(result);
}

public static void main(String[] args) throws IOException,
XWSSecurityException {
ApplicationContext applicationContext = new FileSystemXmlApplicationContext(
"applicationContext-client.xml");

// xwss

/* Resource xwssConfig = new ClassPathResource("securityPolicy-client.xml");
if (!xwssConfig.exists()) {
throw new FileNotFoundException();
}

XWSSProcessorFactory factory = XWSSProcessorFactory.newInstance();
cprocessor = factory.createProcessorForSecurityConfiguration(xwssConfig
.getInputStream(), null);
context = new ProcessingContext();*/

EchoClient echoClient = (EchoClient) applicationContext
.getBean("echoClient");
echoClient.echo();
}

}



//
// This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, v2.1-b02-fcs
// See <a href="http://java.sun.com/xml/jaxb">http://java.sun.com/xml/jaxb</a>
// Any modifications to this file will be lost upon recompilation of the source schema.
// Generated on: 2007.12.22 at 03:39:50 PM CST
//


package org.upyaya.sample.echo.domain.schema;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlType;


/**
* <p>Java class for ReturnType complex type.
*
* <p>The following schema fragment specifies the expected content contained within this class.
*
* <pre>
* <complexType name="ReturnType">
* <complexContent>
* <restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
* <sequence>
* <element name="Message" type="{http://www.w3.org/2001/XMLSchema}string"/>
* </sequence>
* </restriction>
* </complexContent>
* </complexType>
* </pre>
*
*
*/
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "ReturnType", propOrder = {
"message"
})
public class ReturnType {

@XmlElement(name = "Message", required = true)
protected String message;

/**
* Gets the value of the message property.
*
* @return
* possible object is
* {@link String }
*
*/
public String getMessage() {
return message;
}

/**
* Sets the value of the message property.
*
* @param value
* allowed object is
* {@link String }
*
*/
public void setMessage(String value) {
this.message = value;
}

}



//
// This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, v2.1-b02-fcs
// See <a href="http://java.sun.com/xml/jaxb">http://java.sun.com/xml/jaxb</a>
// Any modifications to this file will be lost upon recompilation of the source schema.
// Generated on: 2007.12.22 at 03:39:50 PM CST
//

@javax.xml.bind.annotation.XmlSchema(namespace = "http://www.upyaya.org/echo/schemas", elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED)
package org.upyaya.sample.echo.domain.schema;



//
// This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, v2.1-b02-fcs
// See <a href="http://java.sun.com/xml/jaxb">http://java.sun.com/xml/jaxb</a>
// Any modifications to this file will be lost upon recompilation of the source schema.
// Generated on: 2007.12.22 at 03:39:50 PM CST
//


package org.upyaya.sample.echo.domain.schema;

import javax.xml.bind.annotation.XmlRegistry;


/**
* This object contains factory methods for each
* Java content interface and Java element interface
* generated in the org.upyaya.sample.echo.domain.schema package.
* <p>An ObjectFactory allows you to programatically
* construct new instances of the Java representation
* for XML content. The Java representation of XML
* content can consist of schema derived interfaces
* and classes representing the binding of schema
* type definitions, element declarations and model
* groups. Factory methods for each of these are
* provided in this class.
*
*/
@XmlRegistry
public class ObjectFactory {


/**
* Create a new ObjectFactory that can be used to create new instances of schema derived classes for package: org.upyaya.sample.echo.domain.schema
*
*/
public ObjectFactory() {
}

/**
* Create an instance of {@link EchoRequest }
*
*/
public EchoRequest createEchoRequest() {
return new EchoRequest();
}

/**
* Create an instance of {@link EchoResponse }
*
*/
public EchoResponse createEchoResponse() {
return new EchoResponse();
}

/**
* Create an instance of {@link ReturnType }
*
*/
public ReturnType createReturnType() {
return new ReturnType();
}

/**
* Create an instance of {@link EchoType }
*
*/
public EchoType createEchoType() {
return new EchoType();
}

}


echoservice-servlet.XML
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<bean id="messageDispatcher"
class="org.springframework.ws.soap.server.SoapMessageDispatcher">
<description>
The MessageDispatcher is responsible for routing messages to
endpoints.
</description>
<property name="endpointMappings">
<list>
<ref local="payloadMapping" />
</list>
</property>
<property name="endpointExceptionResolvers">
<list>
<ref local="endpointExceptionResolver" />
</list>
</property>
</bean>

<bean id="payloadMapping"
class="org.springframework.ws.server.endpoint.mapping.PayloadRootQNameEndpointMapping">
<description>
This endpoint mapping uses the qualified name of the payload
(body contents) to determine the endpoint for an incoming
message. The name GetFlightsRequest with namespace
http://www.springframework.org/spring-ws/samples/airline is
mapped to the getFlightsEndpoint. Additionally, messages are
logged using the logging interceptor.
</description>
<property name="mappings">
<props>
<prop
key="{http://www.upyaya.org/echo/schemas}EchoRequest">
echoEndpoint
</prop>
</props>
</property>
<property name="interceptors">
<list>
<ref local="loggingInterceptor" />
<ref local="validatingInterceptor" />
<!--ref local="wsSecurityInterceptor" /-->
</list>
</property>
</bean>

<bean id="validatingInterceptor"
class="org.springframework.ws.soap.server.endpoint.interceptor.PayloadValidatingInterceptor">
<description>
This interceptor validates both incoming and outgoing
message contents according to the 'airline.xsd' XML Schema
file.
</description>
<property name="schema" value="/WEB-INF/echo.xsd" />
<property name="validateRequest" value="true" />
<property name="validateResponse" value="true" />
</bean>

<bean id="loggingInterceptor"
class="org.springframework.ws.server.endpoint.interceptor.PayloadLoggingInterceptor">
<description>
This interceptor logs the message payload.
</description>
</bean>

<bean id="passwordValidationHandler"
class="org.springframework.ws.soap.security.xwss.callback.SimplePasswordValidationCallbackHandler">
<property name="users">
<props>
<prop key="upyaya">123456</prop>
</props>
</property>
</bean>

<bean id="wsSecurityInterceptor"
class="org.springframework.ws.soap.security.xwss.XwsSecurityInterceptor">
<property name="policyConfiguration" value="classpath:securityPolicy.xml"/>
<property name="callbackHandlers">
<list>
<ref bean="passwordValidationHandler"/>
</list>
</property>
</bean>

<bean id="echoEndpoint"
class="org.upyaya.sample.echo.endpoint.EchoMasharlingEndpoint">
<property name="echoService">
<ref bean="echoService" />
</property>
<property name="marshaller" ref="jaxbMarshaller" />
<property name="unmarshaller" ref="jaxbMarshaller" />
</bean>

<bean id="echo"
class="org.springframework.ws.wsdl.wsdl11.DynamicWsdl11Definition">
<property name="builder">
<bean
class="org.springframework.ws.wsdl.wsdl11.builder.XsdBasedSoap11Wsdl4jDefinitionBuilder">
<property name="schema" value="/WEB-INF/echo.xsd" />
<property name="portTypeName" value="Echo" />
<property name="locationUri"
value="http://localhost:8080/echoservice/" />
<property name="targetNamespace"
value="http://www.upyaya.org/echo/schemas/ws" />
<property name="prefix" value="tns" />
<!-- property name="schemaPrefix" value="ec"/-->
</bean>
</property>
</bean>

<bean id="jaxbMarshaller"
class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
<description>
The validating JAXB Marshaller is used by the
getFlightsEndpoint to unmarshal XML to objects and
vice-versa.
</description>
<property name="contextPath"
value="org.upyaya.sample.echo.domain.schema" />
</bean>

<bean id="endpointExceptionResolver"
class="org.springframework.ws.soap.server.endpoint.SoapFaultMappingExceptionResolver">
<description>
This exception resolver maps exceptions to SOAP Faults. Both
UnmarshallingException and ValidationFailureException are
mapped to a SOAP Fault with a "Sender" fault code. All other
exceptions are mapped to a "Receiver" error code, the
default.
</description>
<property name="defaultFault">
<value>RECEIVER,Server error</value>
</property>
<property name="exceptionMappings">
<props>
<prop
key="org.springframework.oxm.UnmarshallingException">
SENDER,Invalid request
</prop>
<prop
key="org.springframework.oxm.ValidationFailureException">
SENDER,Invalid request
</prop>
</props>
</property>
</bean>
</beans>


package org.upyaya.sample.echo.endpoint;

import org.springframework.ws.server.endpoint.AbstractMarshallingPayloadEndpoint;
import org.upyaya.sample.echo.domain.schema.EchoRequest;
import org.upyaya.sample.echo.domain.schema.EchoResponse;
import org.upyaya.sample.echo.domain.schema.EchoType;
import org.upyaya.sample.echo.domain.schema.ReturnType;
import org.upyaya.sample.echo.service.EchoService;

public class EchoMasharlingEndpoint extends AbstractMarshallingPayloadEndpoint {
private EchoService echoService;

public EchoService getEchoService() {
return echoService;
}

public void setEchoService(EchoService echoService) {
this.echoService = echoService;
}

protected Object invokeInternal(Object request) throws Exception {
EchoRequest echoRequest = (EchoRequest)request;
EchoType echoType = echoRequest.getEcho();
System.out.println("-------here----------");
System.out.println(echoType.getName());
String returnMessage = echoService.echo(echoType.getName());

EchoResponse response = new EchoResponse();
ReturnType returnType = new ReturnType();
returnType.setMessage(returnMessage);
response.setEchoResponse(returnType);
return response;
}
}




//
// This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, v2.1-b02-fcs
// See <a href="http://java.sun.com/xml/jaxb">http://java.sun.com/xml/jaxb</a>
// Any modifications to this file will be lost upon recompilation of the source schema.
// Generated on: 2007.12.22 at 03:39:50 PM CST
//


package org.upyaya.sample.echo.domain.schema;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;


/**
* <p>Java class for anonymous complex type.
*
* <p>The following schema fragment specifies the expected content contained within this class.
*
* <pre>
* <complexType>
* <complexContent>
* <restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
* <all>
* <element name="Echo" type="{http://www.upyaya.org/echo/schemas}EchoType"/>
* </all>
* </restriction>
* </complexContent>
* </complexType>
* </pre>
*
*
*/
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {

})
@XmlRootElement(name = "EchoRequest")
public class EchoRequest {

@XmlElement(name = "Echo", required = true)
protected EchoType echo;

/**
* Gets the value of the echo property.
*
* @return
* possible object is
* {@link EchoType }
*
*/
public EchoType getEcho() {
return echo;
}

/**
* Sets the value of the echo property.
*
* @param value
* allowed object is
* {@link EchoType }
*
*/
public void setEcho(EchoType value) {
this.echo = value;
}

}



[code="java"]

//
// This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, v2.1-b02-fcs
// See <a href="http://java.sun.com/xml/jaxb">http://java.sun.com/xml/jaxb</a>
// Any modifications to this file will be lost upon recompilation of the source schema.
// Generated on: 2007.12.22 at 03:39:50 PM CST
//


package org.upyaya.sample.echo.domain.schema;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;


/**
* <p>Java class for anonymous complex type.
*
* <p>The following schema fragment specifies the expected content contained within this class.
*
* <pre>
* <complexType>
* <complexContent>
* <restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
* <all>
* <element name="EchoResponse" type="{http://www.upyaya.org/echo/schemas}ReturnType"/>
* </all>
* </restriction>
* </complexContent>
* </complexType>
* </pre>
*
*
*/
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {

})
@XmlRootElement(name = "EchoResponse")
public class EchoResponse {

@XmlElement(name = "EchoResponse", required = true)
protected ReturnType echoResponse;

/**
* Gets the value of the echoResponse property.
*
* @return
* possible object is
* {@link ReturnType }
*
*/
public ReturnType getEchoResponse() {
return echoResponse;
}

/**
* Sets the value of the echoResponse property.
*
* @param value
* allowed object is
* {@link ReturnType }
*
*/
public void setEchoResponse(ReturnType value) {
this.echoResponse = value;
}

}

http://blog.csdn.net/amethystic/article/details/4158258
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值