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 new file mode 100644 index 00000000..7b457aaf --- /dev/null +++ b/core/src/main/java/org/springframework/ws/transport/http/XsdSchemaHandlerAdapter.java @@ -0,0 +1,94 @@ +/* + * 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.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import javax.xml.transform.Source; +import javax.xml.transform.Transformer; +import javax.xml.transform.dom.DOMSource; +import javax.xml.transform.stream.StreamResult; + +import org.w3c.dom.Document; + +import org.springframework.util.StringUtils; +import org.springframework.web.servlet.HandlerAdapter; +import org.springframework.web.servlet.ModelAndView; +import org.springframework.xml.transform.TransformerObjectSupport; +import org.springframework.xml.transform.TraxUtils; +import org.springframework.xml.xsd.XsdSchema; + +/** + * @author Arjen Poutsma + * @since 1.5.3 + */ +public class XsdSchemaHandlerAdapter extends TransformerObjectSupport implements HandlerAdapter { + + private static final String CONTENT_TYPE = "text/xml"; + + public boolean supports(Object handler) { + return handler instanceof XsdSchema; + } + + 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; + } + + public ModelAndView handle(HttpServletRequest request, HttpServletResponse response, Object handler) + throws Exception { + if (HttpTransportConstants.METHOD_GET.equals(request.getMethod())) { + response.setContentType(CONTENT_TYPE); + Transformer transformer = createTransformer(); + XsdSchema schema = (XsdSchema) handler; + Source schemaSource = schema.getSource(); + StreamResult responseResult = new StreamResult(response.getOutputStream()); + transformer.transform(schemaSource, responseResult); + } + else { + response.setStatus(HttpServletResponse.SC_METHOD_NOT_ALLOWED); + } + return null; + } +} \ No newline at end of file diff --git a/core/src/test/java/org/springframework/ws/transport/http/XsdSchemaHandlerAdapterTest.java b/core/src/test/java/org/springframework/ws/transport/http/XsdSchemaHandlerAdapterTest.java new file mode 100644 index 00000000..b3432a1d --- /dev/null +++ b/core/src/test/java/org/springframework/ws/transport/http/XsdSchemaHandlerAdapterTest.java @@ -0,0 +1,67 @@ +/* + * 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 javax.servlet.http.HttpServletResponse; + +import org.custommonkey.xmlunit.XMLTestCase; + +import org.springframework.core.io.ClassPathResource; +import org.springframework.core.io.Resource; +import org.springframework.mock.web.MockHttpServletRequest; +import org.springframework.mock.web.MockHttpServletResponse; +import org.springframework.util.FileCopyUtils; +import org.springframework.xml.xsd.SimpleXsdSchema; + +public class XsdSchemaHandlerAdapterTest extends XMLTestCase { + + private XsdSchemaHandlerAdapter adapter; + + private MockHttpServletRequest request; + + private MockHttpServletResponse response; + + protected void setUp() throws Exception { + adapter = new XsdSchemaHandlerAdapter(); + request = new MockHttpServletRequest(); + response = new MockHttpServletResponse(); + } + + public void testGetLastModified() throws Exception { + Resource single = new ClassPathResource("single.xsd", getClass()); + SimpleXsdSchema schema = new SimpleXsdSchema(single); + schema.afterPropertiesSet(); + long lastModified = single.getFile().lastModified(); + assertEquals("Invalid last modified", lastModified, adapter.getLastModified(null, schema)); + } + + public void testHandleGet() throws Exception { + request.setMethod(HttpTransportConstants.METHOD_GET); + Resource single = new ClassPathResource("single.xsd", getClass()); + SimpleXsdSchema schema = new SimpleXsdSchema(single); + schema.afterPropertiesSet(); + adapter.handle(request, response, schema); + String expected = new String(FileCopyUtils.copyToByteArray(single.getFile())); + assertXMLEqual(expected, response.getContentAsString()); + } + + public void testHandleNonGet() throws Exception { + request.setMethod(HttpTransportConstants.METHOD_POST); + adapter.handle(request, response, null); + assertEquals("METHOD_NOT_ALLOWED expected", HttpServletResponse.SC_METHOD_NOT_ALLOWED, response.getStatus()); + } +} \ No newline at end of file diff --git a/core/src/test/resources/org/springframework/ws/transport/http/single.xsd b/core/src/test/resources/org/springframework/ws/transport/http/single.xsd new file mode 100644 index 00000000..df338465 --- /dev/null +++ b/core/src/test/resources/org/springframework/ws/transport/http/single.xsd @@ -0,0 +1,35 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file