Skip to content

Commit ef4e79b

Browse files
committed
Stop relying on getLastModified
This commit delegates the check of validating that a request needs to be processed to the handler themselves. As a result, MessageDispatcherServlet is no longer implementing getLastModified. An advantage of this change is that the server now handles "If-Not-Modified" request headers. Closes gh-1470
1 parent 5e0897c commit ef4e79b

File tree

5 files changed

+72
-19
lines changed

5 files changed

+72
-19
lines changed

spring-ws-core/src/main/java/org/springframework/ws/transport/http/MessageDispatcherServlet.java

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -322,20 +322,6 @@ protected void onRefresh(ApplicationContext context) {
322322
initStrategies(context);
323323
}
324324

325-
@Override
326-
@SuppressWarnings("deprecation")
327-
protected long getLastModified(HttpServletRequest httpServletRequest) {
328-
WsdlDefinition definition = getWsdlDefinition(httpServletRequest);
329-
if (definition != null) {
330-
return this.wsdlDefinitionHandlerAdapter.getLastModified(httpServletRequest, definition);
331-
}
332-
XsdSchema schema = getXsdSchema(httpServletRequest);
333-
if (schema != null) {
334-
return this.xsdSchemaHandlerAdapter.getLastModified(httpServletRequest, schema);
335-
}
336-
return this.messageReceiverHandlerAdapter.getLastModified(httpServletRequest, this.messageReceiver);
337-
}
338-
339325
/** Returns the {@link WebServiceMessageReceiver} used by this servlet. */
340326
protected WebServiceMessageReceiver getMessageReceiver() {
341327
return this.messageReceiver;

spring-ws-core/src/main/java/org/springframework/ws/transport/http/WsdlDefinitionHandlerAdapter.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import org.w3c.dom.Document;
3131

3232
import org.springframework.beans.factory.InitializingBean;
33+
import org.springframework.web.context.request.ServletWebRequest;
3334
import org.springframework.web.servlet.HandlerAdapter;
3435
import org.springframework.web.servlet.ModelAndView;
3536
import org.springframework.ws.wsdl.WsdlDefinition;
@@ -151,10 +152,12 @@ public ModelAndView handle(HttpServletRequest request, HttpServletResponse respo
151152
throws Exception {
152153
if (HttpTransportConstants.METHOD_GET.equals(request.getMethod())) {
153154
WsdlDefinition definition = (WsdlDefinition) handler;
154-
155-
Transformer transformer = createTransformer();
156155
Source definitionSource = definition.getSource();
157-
156+
if (new ServletWebRequest(request, response)
157+
.checkNotModified(LastModifiedHelper.getLastModified(definitionSource))) {
158+
return null;
159+
}
160+
Transformer transformer = createTransformer();
158161
if (this.transformLocations || this.transformSchemaLocations) {
159162
DOMResult domResult = new DOMResult();
160163
transformer.transform(definitionSource, domResult);

spring-ws-core/src/main/java/org/springframework/ws/transport/http/XsdSchemaHandlerAdapter.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import org.w3c.dom.Document;
3131

3232
import org.springframework.beans.factory.InitializingBean;
33+
import org.springframework.web.context.request.ServletWebRequest;
3334
import org.springframework.web.servlet.HandlerAdapter;
3435
import org.springframework.web.servlet.ModelAndView;
3536
import org.springframework.xml.xpath.XPathExpression;
@@ -98,9 +99,12 @@ public long getLastModified(HttpServletRequest request, Object handler) {
9899
public ModelAndView handle(HttpServletRequest request, HttpServletResponse response, Object handler)
99100
throws Exception {
100101
if (HttpTransportConstants.METHOD_GET.equals(request.getMethod())) {
101-
Transformer transformer = createTransformer();
102102
Source schemaSource = getSchemaSource((XsdSchema) handler);
103-
103+
if (new ServletWebRequest(request, response)
104+
.checkNotModified(LastModifiedHelper.getLastModified(schemaSource))) {
105+
return null;
106+
}
107+
Transformer transformer = createTransformer();
104108
if (this.transformSchemaLocations) {
105109
DOMResult domResult = new DOMResult();
106110
transformer.transform(schemaSource, domResult);

spring-ws-core/src/test/java/org/springframework/ws/transport/http/WsdlDefinitionHandlerAdapterTest.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,12 @@
3030
import org.xmlunit.assertj.XmlAssert;
3131

3232
import org.springframework.core.io.ClassPathResource;
33+
import org.springframework.core.io.Resource;
34+
import org.springframework.http.HttpHeaders;
35+
import org.springframework.http.HttpStatus;
3336
import org.springframework.mock.web.MockHttpServletRequest;
3437
import org.springframework.mock.web.MockHttpServletResponse;
38+
import org.springframework.util.FileCopyUtils;
3539
import org.springframework.ws.wsdl.WsdlDefinition;
3640
import org.springframework.ws.wsdl.wsdl11.SimpleWsdl11Definition;
3741
import org.springframework.xml.DocumentBuilderFactoryUtils;
@@ -79,6 +83,33 @@ public void handleGet() throws Exception {
7983
verify(this.definitionMock);
8084
}
8185

86+
@Test
87+
public void handleGetUpToDate() throws Exception {
88+
this.request.setMethod(HttpTransportConstants.METHOD_GET);
89+
Resource single = new ClassPathResource("echo-input.wsdl", getClass());
90+
long lastModified = single.getFile().lastModified();
91+
SimpleWsdl11Definition definition = new SimpleWsdl11Definition(single);
92+
definition.afterPropertiesSet();
93+
this.request.addHeader(HttpHeaders.IF_MODIFIED_SINCE, lastModified);
94+
this.adapter.handle(this.request, this.response, definition);
95+
assertThat(this.response.getStatus()).isEqualTo(HttpStatus.NOT_MODIFIED.value());
96+
assertThat(this.response.getContentLength()).isEqualTo(0);
97+
}
98+
99+
@Test
100+
public void handleGetNotUpToDate() throws Exception {
101+
this.request.setMethod(HttpTransportConstants.METHOD_GET);
102+
Resource single = new ClassPathResource("echo-input.wsdl", getClass());
103+
long lastModified = single.getFile().lastModified();
104+
SimpleWsdl11Definition definition = new SimpleWsdl11Definition(single);
105+
definition.afterPropertiesSet();
106+
this.request.addHeader(HttpHeaders.IF_MODIFIED_SINCE, lastModified - 10000);
107+
this.adapter.handle(this.request, this.response, definition);
108+
assertThat(this.response.getStatus()).isEqualTo(HttpStatus.OK.value());
109+
String expected = new String(FileCopyUtils.copyToByteArray(single.getFile()));
110+
XmlAssert.assertThat(this.response.getContentAsString()).and(expected).ignoreWhitespace().areIdentical();
111+
}
112+
82113
@Test
83114
public void handleNonGet() throws Exception {
84115

spring-ws-core/src/test/java/org/springframework/ws/transport/http/XsdSchemaHandlerAdapterTest.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030

3131
import org.springframework.core.io.ClassPathResource;
3232
import org.springframework.core.io.Resource;
33+
import org.springframework.http.HttpHeaders;
34+
import org.springframework.http.HttpStatus;
3335
import org.springframework.mock.web.MockHttpServletRequest;
3436
import org.springframework.mock.web.MockHttpServletResponse;
3537
import org.springframework.util.FileCopyUtils;
@@ -80,6 +82,33 @@ public void handleGet() throws Exception {
8082
XmlAssert.assertThat(this.response.getContentAsString()).and(expected).ignoreWhitespace().areIdentical();
8183
}
8284

85+
@Test
86+
public void handleGetUpToDate() throws Exception {
87+
this.request.setMethod(HttpTransportConstants.METHOD_GET);
88+
Resource single = new ClassPathResource("single.xsd", getClass());
89+
long lastModified = single.getFile().lastModified();
90+
SimpleXsdSchema schema = new SimpleXsdSchema(single);
91+
schema.afterPropertiesSet();
92+
this.request.addHeader(HttpHeaders.IF_MODIFIED_SINCE, lastModified);
93+
this.adapter.handle(this.request, this.response, schema);
94+
assertThat(this.response.getStatus()).isEqualTo(HttpStatus.NOT_MODIFIED.value());
95+
assertThat(this.response.getContentLength()).isEqualTo(0);
96+
}
97+
98+
@Test
99+
public void handleGetNotUpToDate() throws Exception {
100+
this.request.setMethod(HttpTransportConstants.METHOD_GET);
101+
Resource single = new ClassPathResource("single.xsd", getClass());
102+
long lastModified = single.getFile().lastModified();
103+
SimpleXsdSchema schema = new SimpleXsdSchema(single);
104+
schema.afterPropertiesSet();
105+
this.request.addHeader(HttpHeaders.IF_MODIFIED_SINCE, lastModified - 1000);
106+
this.adapter.handle(this.request, this.response, schema);
107+
assertThat(this.response.getStatus()).isEqualTo(HttpStatus.OK.value());
108+
String expected = new String(FileCopyUtils.copyToByteArray(single.getFile()));
109+
XmlAssert.assertThat(this.response.getContentAsString()).and(expected).ignoreWhitespace().areIdentical();
110+
}
111+
83112
@Test
84113
public void handleNonGet() throws Exception {
85114

0 commit comments

Comments
 (0)