cxf jaxb2: custom header interceptor
authorGustavo Martin Morcuende <gu.martinm@gmail.com>
Wed, 6 Jan 2016 01:43:32 +0000 (02:43 +0100)
committerGustavo Martin Morcuende <gu.martinm@gmail.com>
Wed, 6 Jan 2016 01:43:32 +0000 (02:43 +0100)
cxf/web-services-spring-cxf-server/pom.xml
cxf/web-services-spring-cxf-server/src/main/java/de/spring/webservices/endpoints/CustomHeaderInterceptor.java [new file with mode: 0644]
cxf/web-services-spring-cxf-server/src/main/resources/spring-configuration/ws/soap-ws.xml
jaxb2/web-services-spring-jaxb2-server/pom.xml
jaxb2/web-services-spring-jaxb2-server/src/main/java/de/spring/webservices/endpoints/CustomHeaderInterceptor.java [new file with mode: 0644]
jaxb2/web-services-spring-jaxb2-server/src/main/resources/spring-configuration/ws/soap-ws.xml

index 1038489..6807bad 100644 (file)
                        <version>0.9.4</version>
                </dependency>
 
+               <!-- Required by my CustomHeaderInterceptor -->
+               <dependency>
+                       <groupId>javax.servlet</groupId>
+                       <artifactId>javax.servlet-api</artifactId>
+                       <version>4.0.0-b01</version>
+                       <scope>provided</scope>
+               </dependency>
+
                <!-- Unitary and integration tests -->
                <dependency>
                        <groupId>junit</groupId>
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 (file)
index 0000000..e1d5302
--- /dev/null
@@ -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 {
+               
+       }
+
+}
index 08eea0a..cf3e7d7 100644 (file)
             <property name="validateRequest" value="true"/>
             <property name="validateResponse" value="true"/>
         </bean>
+
+        <!-- My custom header interceptor -->
+        <bean class="de.spring.webservices.endpoints.CustomHeaderInterceptor" />
     </sws:interceptors>
     
     
index fd5de7c..a969b08 100644 (file)
                        <artifactId>wsdl4j</artifactId>
                </dependency>
 
+        <!-- Required by my CustomHeaderInterceptor -->
+        <dependency>
+            <groupId>javax.servlet</groupId>
+            <artifactId>javax.servlet-api</artifactId>
+            <version>4.0.0-b01</version>
+            <scope>provided</scope>
+        </dependency>
+
                <!-- Unitary and integration tests -->
                <dependency>
                        <groupId>junit</groupId>
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 (file)
index 0000000..e1d5302
--- /dev/null
@@ -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 {
+               
+       }
+
+}
index f1099f2..40f2901 100644 (file)
             <property name="validateRequest" value="true"/>
             <property name="validateResponse" value="true"/>
         </bean>
+
+        <!-- My custom header interceptor -->
+        <bean class="de.spring.webservices.endpoints.CustomHeaderInterceptor" />
     </sws:interceptors>