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
This commit is contained in:
@@ -322,20 +322,6 @@ public class MessageDispatcherServlet extends FrameworkServlet {
|
||||
initStrategies(context);
|
||||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("deprecation")
|
||||
protected long getLastModified(HttpServletRequest httpServletRequest) {
|
||||
WsdlDefinition definition = getWsdlDefinition(httpServletRequest);
|
||||
if (definition != null) {
|
||||
return this.wsdlDefinitionHandlerAdapter.getLastModified(httpServletRequest, definition);
|
||||
}
|
||||
XsdSchema schema = getXsdSchema(httpServletRequest);
|
||||
if (schema != null) {
|
||||
return this.xsdSchemaHandlerAdapter.getLastModified(httpServletRequest, schema);
|
||||
}
|
||||
return this.messageReceiverHandlerAdapter.getLastModified(httpServletRequest, this.messageReceiver);
|
||||
}
|
||||
|
||||
/** Returns the {@link WebServiceMessageReceiver} used by this servlet. */
|
||||
protected WebServiceMessageReceiver getMessageReceiver() {
|
||||
return this.messageReceiver;
|
||||
|
||||
@@ -30,6 +30,7 @@ import jakarta.servlet.http.HttpServletResponse;
|
||||
import org.w3c.dom.Document;
|
||||
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
import org.springframework.web.context.request.ServletWebRequest;
|
||||
import org.springframework.web.servlet.HandlerAdapter;
|
||||
import org.springframework.web.servlet.ModelAndView;
|
||||
import org.springframework.ws.wsdl.WsdlDefinition;
|
||||
@@ -151,10 +152,12 @@ public class WsdlDefinitionHandlerAdapter extends LocationTransformerObjectSuppo
|
||||
throws Exception {
|
||||
if (HttpTransportConstants.METHOD_GET.equals(request.getMethod())) {
|
||||
WsdlDefinition definition = (WsdlDefinition) handler;
|
||||
|
||||
Transformer transformer = createTransformer();
|
||||
Source definitionSource = definition.getSource();
|
||||
|
||||
if (new ServletWebRequest(request, response)
|
||||
.checkNotModified(LastModifiedHelper.getLastModified(definitionSource))) {
|
||||
return null;
|
||||
}
|
||||
Transformer transformer = createTransformer();
|
||||
if (this.transformLocations || this.transformSchemaLocations) {
|
||||
DOMResult domResult = new DOMResult();
|
||||
transformer.transform(definitionSource, domResult);
|
||||
|
||||
@@ -30,6 +30,7 @@ import jakarta.servlet.http.HttpServletResponse;
|
||||
import org.w3c.dom.Document;
|
||||
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
import org.springframework.web.context.request.ServletWebRequest;
|
||||
import org.springframework.web.servlet.HandlerAdapter;
|
||||
import org.springframework.web.servlet.ModelAndView;
|
||||
import org.springframework.xml.xpath.XPathExpression;
|
||||
@@ -98,9 +99,12 @@ public class XsdSchemaHandlerAdapter extends LocationTransformerObjectSupport
|
||||
public ModelAndView handle(HttpServletRequest request, HttpServletResponse response, Object handler)
|
||||
throws Exception {
|
||||
if (HttpTransportConstants.METHOD_GET.equals(request.getMethod())) {
|
||||
Transformer transformer = createTransformer();
|
||||
Source schemaSource = getSchemaSource((XsdSchema) handler);
|
||||
|
||||
if (new ServletWebRequest(request, response)
|
||||
.checkNotModified(LastModifiedHelper.getLastModified(schemaSource))) {
|
||||
return null;
|
||||
}
|
||||
Transformer transformer = createTransformer();
|
||||
if (this.transformSchemaLocations) {
|
||||
DOMResult domResult = new DOMResult();
|
||||
transformer.transform(schemaSource, domResult);
|
||||
|
||||
@@ -30,8 +30,12 @@ import org.w3c.dom.Document;
|
||||
import org.xmlunit.assertj.XmlAssert;
|
||||
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.mock.web.MockHttpServletRequest;
|
||||
import org.springframework.mock.web.MockHttpServletResponse;
|
||||
import org.springframework.util.FileCopyUtils;
|
||||
import org.springframework.ws.wsdl.WsdlDefinition;
|
||||
import org.springframework.ws.wsdl.wsdl11.SimpleWsdl11Definition;
|
||||
import org.springframework.xml.DocumentBuilderFactoryUtils;
|
||||
@@ -79,6 +83,33 @@ public class WsdlDefinitionHandlerAdapterTest {
|
||||
verify(this.definitionMock);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void handleGetUpToDate() throws Exception {
|
||||
this.request.setMethod(HttpTransportConstants.METHOD_GET);
|
||||
Resource single = new ClassPathResource("echo-input.wsdl", getClass());
|
||||
long lastModified = single.getFile().lastModified();
|
||||
SimpleWsdl11Definition definition = new SimpleWsdl11Definition(single);
|
||||
definition.afterPropertiesSet();
|
||||
this.request.addHeader(HttpHeaders.IF_MODIFIED_SINCE, lastModified);
|
||||
this.adapter.handle(this.request, this.response, definition);
|
||||
assertThat(this.response.getStatus()).isEqualTo(HttpStatus.NOT_MODIFIED.value());
|
||||
assertThat(this.response.getContentLength()).isEqualTo(0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void handleGetNotUpToDate() throws Exception {
|
||||
this.request.setMethod(HttpTransportConstants.METHOD_GET);
|
||||
Resource single = new ClassPathResource("echo-input.wsdl", getClass());
|
||||
long lastModified = single.getFile().lastModified();
|
||||
SimpleWsdl11Definition definition = new SimpleWsdl11Definition(single);
|
||||
definition.afterPropertiesSet();
|
||||
this.request.addHeader(HttpHeaders.IF_MODIFIED_SINCE, lastModified - 10000);
|
||||
this.adapter.handle(this.request, this.response, definition);
|
||||
assertThat(this.response.getStatus()).isEqualTo(HttpStatus.OK.value());
|
||||
String expected = new String(FileCopyUtils.copyToByteArray(single.getFile()));
|
||||
XmlAssert.assertThat(this.response.getContentAsString()).and(expected).ignoreWhitespace().areIdentical();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void handleNonGet() throws Exception {
|
||||
|
||||
|
||||
@@ -30,6 +30,8 @@ import org.xmlunit.assertj.XmlAssert;
|
||||
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.mock.web.MockHttpServletRequest;
|
||||
import org.springframework.mock.web.MockHttpServletResponse;
|
||||
import org.springframework.util.FileCopyUtils;
|
||||
@@ -80,6 +82,33 @@ public class XsdSchemaHandlerAdapterTest {
|
||||
XmlAssert.assertThat(this.response.getContentAsString()).and(expected).ignoreWhitespace().areIdentical();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void handleGetUpToDate() throws Exception {
|
||||
this.request.setMethod(HttpTransportConstants.METHOD_GET);
|
||||
Resource single = new ClassPathResource("single.xsd", getClass());
|
||||
long lastModified = single.getFile().lastModified();
|
||||
SimpleXsdSchema schema = new SimpleXsdSchema(single);
|
||||
schema.afterPropertiesSet();
|
||||
this.request.addHeader(HttpHeaders.IF_MODIFIED_SINCE, lastModified);
|
||||
this.adapter.handle(this.request, this.response, schema);
|
||||
assertThat(this.response.getStatus()).isEqualTo(HttpStatus.NOT_MODIFIED.value());
|
||||
assertThat(this.response.getContentLength()).isEqualTo(0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void handleGetNotUpToDate() throws Exception {
|
||||
this.request.setMethod(HttpTransportConstants.METHOD_GET);
|
||||
Resource single = new ClassPathResource("single.xsd", getClass());
|
||||
long lastModified = single.getFile().lastModified();
|
||||
SimpleXsdSchema schema = new SimpleXsdSchema(single);
|
||||
schema.afterPropertiesSet();
|
||||
this.request.addHeader(HttpHeaders.IF_MODIFIED_SINCE, lastModified - 1000);
|
||||
this.adapter.handle(this.request, this.response, schema);
|
||||
assertThat(this.response.getStatus()).isEqualTo(HttpStatus.OK.value());
|
||||
String expected = new String(FileCopyUtils.copyToByteArray(single.getFile()));
|
||||
XmlAssert.assertThat(this.response.getContentAsString()).and(expected).ignoreWhitespace().areIdentical();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void handleNonGet() throws Exception {
|
||||
|
||||
|
||||
Reference in New Issue
Block a user