|
06.07.2013, 22:52:19
#38323010
Ссылка:
Ссылка на сообщение:
Ссылка с названием темы:
Ссылка на профиль пользователя:
|
|
|
Участник
Сообщения: 3 596
Рейтинг:
0
/ 0
|
|
|
|
abc_da,
Буквально неделю назад делал для spring-ws, выкладываю код - может поможет.
1. 2. 3. 4. 5. 6. 7. 8. 9. 10.
public class AIAControllerSpecParent{
@Before
public void setupData() {
MockWebService.mockFactory();
MockWebService.map(UsageEligibilityRequest.class, "discovery/aia/services/mock/xml/common/UsageEligibilityResponse.xml");
MockWebService.map(GetUsageRequest.class, "discovery/aia/services/mock/xml/common/GetUsageResponse.xml");
MockWebService.map(RetrieveTenantIdRequest.class, "discovery/aia/services/mock/xml/common/RetrieveTenant.xml");
}
1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. 33. 34. 35. 36. 37. 38. 39. 40. 41. 42. 43. 44. 45. 46. 47. 48. 49.
import java.lang.reflect.ParameterizedType;
import java.util.HashMap;
import java.util.Map;
import org.springframework.ws.client.core.WebServiceTemplate;
@SuppressWarnings("rawtypes")
public class MockWebService {
private static Map<Class<?>, String> responseMapping = new HashMap<Class<?>, String>();
private static Map<Class<?>, MockWebServiceConnectionHandler> handlersMapping = new HashMap<Class<?>, MockWebServiceConnectionHandler>();
public static void map(Class<?> payloadClass, String resourcePath) {
responseMapping.put(payloadClass, resourcePath);
}
public static void addHandler(MockWebServiceConnectionHandler handler) {
ParameterizedType type =(ParameterizedType) handler.getClass().getGenericSuperclass();
Class<?> commandClass = (Class<?>) type.getActualTypeArguments()[0];
handlersMapping.put(commandClass, handler);
}
public static String getResourcePath(Class<?> payloadClass) {
return responseMapping.get(payloadClass);
}
@SuppressWarnings("unchecked")
public static <T> MockWebServiceConnectionHandler<T> getHandler(Class<T> payloadClass) {
return handlersMapping.get(payloadClass);
}
public static void mockFactory() {
clearMapping();
Map<String, WebServiceTemplate> all = WebServiceTemplateFactory.getContext().getBeansOfType(WebServiceTemplate.class);
for (WebServiceTemplate t : all.values()) {
t.setMessageSender(new FileWebServiceMessageSender(t));
}
}
static void clearMapping() {
responseMapping.clear();
handlersMapping.clear();
}
}
1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23.
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.ws.client.core.WebServiceTemplate;
public class WebServiceTemplateFactory {
final ClassPathXmlApplicationContext context;
private WebServiceTemplateFactory() {
context = new ClassPathXmlApplicationContext("integration-service-context.xml");
}
private static final WebServiceTemplateFactory instance = new WebServiceTemplateFactory();
public static WebServiceTemplate getTemplate(String bean) {
return (WebServiceTemplate)WebServiceTemplateFactory.instance.context.getBean(bean);
}
public static ApplicationContext getContext() {
return instance.context;
}
}
1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18.
import java.io.IOException;
public abstract class MockWebServiceConnectionHandler<T> {
public void send(T t) throws IOException {
}
public String getReponsePath(T t) {
return null;
}
protected void refuseConnection() throws IOException {
throw new IOException("Connection refused");
}
}
1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. 33. 34. 35. 36. 37. 38. 39. 40. 41. 42. 43. 44. 45. 46. 47. 48. 49. 50. 51. 52. 53. 54. 55. 56. 57. 58. 59. 60. 61. 62. 63. 64. 65. 66. 67. 68. 69. 70. 71. 72. 73. 74. 75. 76. 77. 78. 79. 80. 81. 82. 83. 84. 85. 86. 87.
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import org.springframework.oxm.Unmarshaller;
import org.springframework.ws.WebServiceMessage;
import org.springframework.ws.WebServiceMessageFactory;
import org.springframework.ws.client.core.WebServiceTemplate;
import org.springframework.ws.soap.saaj.SaajSoapMessageFactory;
import org.springframework.ws.transport.WebServiceConnection;
import org.springframework.ws.transport.WebServiceMessageSender;
public class FileWebServiceMessageSender implements WebServiceMessageSender {
static final SaajSoapMessageFactory FACTORY = new SaajSoapMessageFactory();
static {
FACTORY.afterPropertiesSet();
}
Unmarshaller unmarshaller;
public FileWebServiceMessageSender(WebServiceTemplate t) {
unmarshaller = t.getUnmarshaller();
}
public WebServiceConnection createConnection(final URI uri) throws IOException {
return new WebServiceConnection() {
Object request = null;
@SuppressWarnings("rawtypes")
MockWebServiceConnectionHandler handler = null;
@SuppressWarnings({ "unchecked"})
public void send(WebServiceMessage message) throws IOException {
request = unmarshaller.unmarshal(message.getPayloadSource());
handler = MockWebService.getHandler(request.getClass());
if (handler != null) {
handler.send(request);
}
}
@SuppressWarnings("unchecked")
public WebServiceMessage receive(WebServiceMessageFactory messageFactory) throws IOException {
String responsePath = MockWebService.getResourcePath(request.getClass());
if (handler != null && handler.getReponsePath(request) != null) {
responsePath = handler.getReponsePath(request);
}
if (responsePath == null) {
throw new NullPointerException("Response path is not specified for request " + request.getClass());
}
return FACTORY.createWebServiceMessage(getClass().getClassLoader().getResourceAsStream(responsePath));
}
public boolean hasError() throws IOException {
return false;
}
public URI getUri() throws URISyntaxException {
return null;
}
public String getErrorMessage() throws IOException {
return null;
}
public void close() throws IOException {
}
};
}
public boolean supports(URI uri) {
return true;
}
}
1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30.
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:ehcache="http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="wsMessageSender"
class="org.springframework.ws.transport.http.HttpComponentsMessageSender">
<property name="connectionTimeout" value="60000" />
<property name="readTimeout" value="120000" />
<property name="maxTotalConnections" value="10000" />
</bean>
<!-- Usage service -->
<bean id="usageServiceMarshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
<property name="contextPath" value="za.co.discovery.vitality.usageservice" />
</bean>
<bean id="usageServiceTemplate" class="org.springframework.ws.client.core.WebServiceTemplate">
<property name="marshaller" ref="usageServiceMarshaller" />
<property name="unmarshaller" ref="usageServiceMarshaller" />
<property name="defaultUri" value="${usage.service.URL}" />
<property name="messageSender" ref="wsMessageSender" />
</bean>
</beans>
|
|
|