From 8e1ec3191603b894c76596029d50022d302158ab Mon Sep 17 00:00:00 2001 From: Gustavo Martin Morcuende Date: Wed, 6 Jan 2016 02:43:32 +0100 Subject: [PATCH] cxf jaxb2: custom header interceptor --- cxf/web-services-spring-cxf-server/pom.xml | 8 ++++ .../endpoints/CustomHeaderInterceptor.java | 50 ++++++++++++++++++++++ .../resources/spring-configuration/ws/soap-ws.xml | 3 ++ jaxb2/web-services-spring-jaxb2-server/pom.xml | 8 ++++ .../endpoints/CustomHeaderInterceptor.java | 50 ++++++++++++++++++++++ .../resources/spring-configuration/ws/soap-ws.xml | 3 ++ 6 files changed, 122 insertions(+) create mode 100644 cxf/web-services-spring-cxf-server/src/main/java/de/spring/webservices/endpoints/CustomHeaderInterceptor.java create mode 100644 jaxb2/web-services-spring-jaxb2-server/src/main/java/de/spring/webservices/endpoints/CustomHeaderInterceptor.java diff --git a/cxf/web-services-spring-cxf-server/pom.xml b/cxf/web-services-spring-cxf-server/pom.xml index 1038489..6807bad 100644 --- a/cxf/web-services-spring-cxf-server/pom.xml +++ b/cxf/web-services-spring-cxf-server/pom.xml @@ -57,6 +57,14 @@ 0.9.4 + + + javax.servlet + javax.servlet-api + 4.0.0-b01 + provided + + junit diff --git a/cxf/web-services-spring-cxf-server/src/main/java/de/spring/webservices/endpoints/CustomHeaderInterceptor.java b/cxf/web-services-spring-cxf-server/src/main/java/de/spring/webservices/endpoints/CustomHeaderInterceptor.java new file mode 100644 index 0000000..e1d5302 --- /dev/null +++ b/cxf/web-services-spring-cxf-server/src/main/java/de/spring/webservices/endpoints/CustomHeaderInterceptor.java @@ -0,0 +1,50 @@ +package de.spring.webservices.endpoints; + +import javax.servlet.http.HttpServletRequest; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.util.StringUtils; +import org.springframework.ws.context.MessageContext; +import org.springframework.ws.server.EndpointInterceptor; +import org.springframework.ws.transport.context.TransportContext; +import org.springframework.ws.transport.context.TransportContextHolder; +import org.springframework.ws.transport.http.HttpServletConnection; + +public class CustomHeaderInterceptor implements EndpointInterceptor { + private static final Logger LOGGER = LoggerFactory.getLogger(CustomHeaderInterceptor.class); + private static final String MY_CUSTOM_HEADER = "MY_CUSTOM_HEADER"; + + @Override + public boolean handleRequest(MessageContext messageContext, Object endpoint) throws Exception { + TransportContext transport = TransportContextHolder.getTransportContext(); + + if (transport != null) { + HttpServletConnection connection = (HttpServletConnection) transport.getConnection(); + HttpServletRequest headers = connection.getHttpServletRequest(); + String headerValue = headers.getHeader(MY_CUSTOM_HEADER); + + if (!StringUtils.isEmpty(headerValue)) { + LOGGER.info("Custom header value: " + headerValue); + } + } + + return true; + } + + @Override + public boolean handleResponse(MessageContext messageContext, Object endpoint) throws Exception { + return true; + } + + @Override + public boolean handleFault(MessageContext messageContext, Object endpoint) throws Exception { + return true; + } + + @Override + public void afterCompletion(MessageContext messageContext, Object endpoint, Exception ex) throws Exception { + + } + +} diff --git a/cxf/web-services-spring-cxf-server/src/main/resources/spring-configuration/ws/soap-ws.xml b/cxf/web-services-spring-cxf-server/src/main/resources/spring-configuration/ws/soap-ws.xml index 08eea0a..cf3e7d7 100644 --- a/cxf/web-services-spring-cxf-server/src/main/resources/spring-configuration/ws/soap-ws.xml +++ b/cxf/web-services-spring-cxf-server/src/main/resources/spring-configuration/ws/soap-ws.xml @@ -160,6 +160,9 @@ + + + diff --git a/jaxb2/web-services-spring-jaxb2-server/pom.xml b/jaxb2/web-services-spring-jaxb2-server/pom.xml index fd5de7c..a969b08 100644 --- a/jaxb2/web-services-spring-jaxb2-server/pom.xml +++ b/jaxb2/web-services-spring-jaxb2-server/pom.xml @@ -48,6 +48,14 @@ wsdl4j + + + javax.servlet + javax.servlet-api + 4.0.0-b01 + provided + + junit diff --git a/jaxb2/web-services-spring-jaxb2-server/src/main/java/de/spring/webservices/endpoints/CustomHeaderInterceptor.java b/jaxb2/web-services-spring-jaxb2-server/src/main/java/de/spring/webservices/endpoints/CustomHeaderInterceptor.java new file mode 100644 index 0000000..e1d5302 --- /dev/null +++ b/jaxb2/web-services-spring-jaxb2-server/src/main/java/de/spring/webservices/endpoints/CustomHeaderInterceptor.java @@ -0,0 +1,50 @@ +package de.spring.webservices.endpoints; + +import javax.servlet.http.HttpServletRequest; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.util.StringUtils; +import org.springframework.ws.context.MessageContext; +import org.springframework.ws.server.EndpointInterceptor; +import org.springframework.ws.transport.context.TransportContext; +import org.springframework.ws.transport.context.TransportContextHolder; +import org.springframework.ws.transport.http.HttpServletConnection; + +public class CustomHeaderInterceptor implements EndpointInterceptor { + private static final Logger LOGGER = LoggerFactory.getLogger(CustomHeaderInterceptor.class); + private static final String MY_CUSTOM_HEADER = "MY_CUSTOM_HEADER"; + + @Override + public boolean handleRequest(MessageContext messageContext, Object endpoint) throws Exception { + TransportContext transport = TransportContextHolder.getTransportContext(); + + if (transport != null) { + HttpServletConnection connection = (HttpServletConnection) transport.getConnection(); + HttpServletRequest headers = connection.getHttpServletRequest(); + String headerValue = headers.getHeader(MY_CUSTOM_HEADER); + + if (!StringUtils.isEmpty(headerValue)) { + LOGGER.info("Custom header value: " + headerValue); + } + } + + return true; + } + + @Override + public boolean handleResponse(MessageContext messageContext, Object endpoint) throws Exception { + return true; + } + + @Override + public boolean handleFault(MessageContext messageContext, Object endpoint) throws Exception { + return true; + } + + @Override + public void afterCompletion(MessageContext messageContext, Object endpoint, Exception ex) throws Exception { + + } + +} diff --git a/jaxb2/web-services-spring-jaxb2-server/src/main/resources/spring-configuration/ws/soap-ws.xml b/jaxb2/web-services-spring-jaxb2-server/src/main/resources/spring-configuration/ws/soap-ws.xml index f1099f2..40f2901 100644 --- a/jaxb2/web-services-spring-jaxb2-server/src/main/resources/spring-configuration/ws/soap-ws.xml +++ b/jaxb2/web-services-spring-jaxb2-server/src/main/resources/spring-configuration/ws/soap-ws.xml @@ -160,6 +160,9 @@ + + + -- 2.1.4