Working on SWS-346
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
|
||||
targetNamespace="http://www.springframework.org/spring-ws/single/schema"
|
||||
xmlns="http://www.springframework.org/spring-ws/single/schema" elementFormDefault="qualified"
|
||||
attributeFormDefault="unqualified">
|
||||
|
||||
<xsd:simpleType name="customType">
|
||||
<xsd:restriction base="xsd:string"/>
|
||||
</xsd:simpleType>
|
||||
|
||||
<xsd:element name="GetOrderRequest">
|
||||
<xsd:complexType>
|
||||
<xsd:sequence>
|
||||
<xsd:element name="child" type="xsd:string"/>
|
||||
</xsd:sequence>
|
||||
</xsd:complexType>
|
||||
</xsd:element>
|
||||
|
||||
<xsd:element name="GetOrderResponse">
|
||||
<xsd:complexType>
|
||||
<xsd:sequence>
|
||||
<xsd:element name="child" type="xsd:string"/>
|
||||
</xsd:sequence>
|
||||
</xsd:complexType>
|
||||
</xsd:element>
|
||||
|
||||
<xsd:element name="GetOrderFault">
|
||||
<xsd:complexType>
|
||||
<xsd:sequence>
|
||||
<xsd:element name="child" type="xsd:string"/>
|
||||
</xsd:sequence>
|
||||
</xsd:complexType>
|
||||
</xsd:element>
|
||||
|
||||
</xsd:schema>
|
||||
Reference in New Issue
Block a user