diff --git a/core/src/main/java/org/springframework/ws/transport/http/LastModifiedHelper.java b/core/src/main/java/org/springframework/ws/transport/http/LastModifiedHelper.java new file mode 100644 index 00000000..7079cfb4 --- /dev/null +++ b/core/src/main/java/org/springframework/ws/transport/http/LastModifiedHelper.java @@ -0,0 +1,75 @@ +/* + * Copyright 2008 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.ws.transport.http; + +import java.io.File; +import java.net.URI; +import java.net.URISyntaxException; +import javax.xml.transform.Source; +import javax.xml.transform.dom.DOMSource; + +import org.w3c.dom.Document; + +import org.springframework.util.StringUtils; +import org.springframework.xml.transform.TraxUtils; + +/** + * Utility class that determines the last modified date of a given {@link Source}. + * + * @author Arjen Poutsma + * @since 1.5.3 + */ +class LastModifiedHelper { + + private LastModifiedHelper() { + } + + /** + * Returns the last modified date of the given {@link Source}. + * + * @param source the source + * @return the last modified date, as a long + */ + static long getLastModified(Source source) { + if (source instanceof DOMSource) { + Document document = TraxUtils.getDocument((DOMSource) source); + return document != null ? getLastModified(document.getDocumentURI()) : -1; + } + else { + return getLastModified(source.getSystemId()); + } + } + + private static long getLastModified(String systemId) { + if (StringUtils.hasText(systemId)) { + try { + URI systemIdUri = new URI(systemId); + if ("file".equals(systemIdUri.getScheme())) { + File documentFile = new File(systemIdUri); + if (documentFile.exists()) { + return documentFile.lastModified(); + } + } + } + catch (URISyntaxException e) { + // ignore + } + } + return -1; + } + +} diff --git a/core/src/main/java/org/springframework/ws/transport/http/WsdlDefinitionHandlerAdapter.java b/core/src/main/java/org/springframework/ws/transport/http/WsdlDefinitionHandlerAdapter.java index 3ae8f482..014c9b0d 100644 --- a/core/src/main/java/org/springframework/ws/transport/http/WsdlDefinitionHandlerAdapter.java +++ b/core/src/main/java/org/springframework/ws/transport/http/WsdlDefinitionHandlerAdapter.java @@ -109,7 +109,8 @@ public class WsdlDefinitionHandlerAdapter extends TransformerObjectSupport imple } public long getLastModified(HttpServletRequest request, Object handler) { - return -1; + Source definitionSource = ((WsdlDefinition) handler).getSource(); + return LastModifiedHelper.getLastModified(definitionSource); } public ModelAndView handle(HttpServletRequest request, HttpServletResponse response, Object handler) diff --git a/core/src/main/java/org/springframework/ws/transport/http/XsdSchemaHandlerAdapter.java b/core/src/main/java/org/springframework/ws/transport/http/XsdSchemaHandlerAdapter.java index 4eac58d9..afb9cb9e 100644 --- a/core/src/main/java/org/springframework/ws/transport/http/XsdSchemaHandlerAdapter.java +++ b/core/src/main/java/org/springframework/ws/transport/http/XsdSchemaHandlerAdapter.java @@ -56,31 +56,7 @@ public class XsdSchemaHandlerAdapter extends TransformerObjectSupport implements public long getLastModified(HttpServletRequest request, Object handler) { Source schemaSource = ((XsdSchema) handler).getSource(); - if (schemaSource instanceof DOMSource) { - Document document = TraxUtils.getDocument((DOMSource) schemaSource); - return document != null ? getLastModified(document.getDocumentURI()) : -1; - } - else { - return getLastModified(schemaSource.getSystemId()); - } - } - - private long getLastModified(String systemId) { - if (StringUtils.hasText(systemId)) { - try { - URI systemIdUri = new URI(systemId); - if ("file".equals(systemIdUri.getScheme())) { - File documentFile = new File(systemIdUri); - if (documentFile.exists()) { - return documentFile.lastModified(); - } - } - } - catch (URISyntaxException e) { - // ignore - } - } - return -1; + return LastModifiedHelper.getLastModified(schemaSource); } public ModelAndView handle(HttpServletRequest request, HttpServletResponse response, Object handler) diff --git a/core/src/test/java/org/springframework/ws/transport/http/LastModifiedHelperTest.java b/core/src/test/java/org/springframework/ws/transport/http/LastModifiedHelperTest.java new file mode 100644 index 00000000..1812c8a8 --- /dev/null +++ b/core/src/test/java/org/springframework/ws/transport/http/LastModifiedHelperTest.java @@ -0,0 +1,63 @@ +/* + * Copyright 2008 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.ws.transport.http; + +import java.util.Date; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.parsers.DocumentBuilder; +import javax.xml.transform.dom.DOMSource; +import javax.xml.transform.stream.StreamSource; + +import junit.framework.*; + +import org.springframework.ws.transport.http.LastModifiedHelper; +import org.springframework.core.io.Resource; +import org.springframework.core.io.ClassPathResource; +import org.springframework.xml.transform.ResourceSource; + +import org.w3c.dom.Document; + +public class LastModifiedHelperTest extends TestCase { + + private Resource resource; + + private long expected; + + protected void setUp() throws Exception { + resource = new ClassPathResource("single.xsd", getClass()); + expected = resource.lastModified(); + } + + public void testSaxSource() throws Exception { + long result = LastModifiedHelper.getLastModified(new ResourceSource(resource)); + assertEquals("Invalid last modified", expected, result); + } + + public void testDomSource() throws Exception { + DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); + documentBuilderFactory.setNamespaceAware(true); + DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder(); + Document document = documentBuilder.parse(resource.getFile()); + long result = LastModifiedHelper.getLastModified(new DOMSource(document)); + assertEquals("Invalid last modified", expected, result); + } + + public void testStreamSource() throws Exception { + long result = LastModifiedHelper.getLastModified(new StreamSource(resource.getFile())); + assertEquals("Invalid last modified", expected, result); + } +} \ No newline at end of file