Author: klape
Date: 2011-04-18 11:50:03 -0400 (Mon, 18 Apr 2011)
New Revision: 14157
Modified:
stack/native/trunk/modules/core/src/main/java/org/jboss/ws/core/soap/SOAPBodyElementDoc.java
stack/native/trunk/modules/core/src/main/java/org/jboss/ws/extensions/validation/SchemaExtractor.java
stack/native/trunk/modules/core/src/main/java/org/jboss/ws/extensions/validation/SchemaValidationHelper.java
Log:
[JBWS-2235] initial code change for this jira
Modified:
stack/native/trunk/modules/core/src/main/java/org/jboss/ws/core/soap/SOAPBodyElementDoc.java
===================================================================
---
stack/native/trunk/modules/core/src/main/java/org/jboss/ws/core/soap/SOAPBodyElementDoc.java 2011-04-18
15:02:02 UTC (rev 14156)
+++
stack/native/trunk/modules/core/src/main/java/org/jboss/ws/core/soap/SOAPBodyElementDoc.java 2011-04-18
15:50:03 UTC (rev 14157)
@@ -21,6 +21,7 @@
*/
package org.jboss.ws.core.soap;
+import java.io.InputStream;
import java.net.URL;
import javax.xml.namespace.QName;
@@ -102,6 +103,7 @@
EndpointMetaData epMetaData = msgContext.getEndpointMetaData();
feature = epMetaData.getFeature(SchemaValidationFeature.class);
URL xsdURL = feature.getSchemaLocation() != null ? new
URL(feature.getSchemaLocation()) : null;
+ InputStream[] xsdStreams = null;
if (xsdURL == null)
{
URL wsdlURL = epMetaData.getServiceMetaData().getWsdlFileOrLocation();
@@ -111,7 +113,7 @@
}
else
{
- xsdURL = schemaExtractor.getSchemaUrl(wsdlURL);
+ xsdStreams = schemaExtractor.getSchemas(wsdlURL);
}
}
if (xsdURL != null)
@@ -120,6 +122,12 @@
Element xmlDOM = DOMUtils.sourceToElement(source);
new
SchemaValidationHelper(xsdURL).setErrorHandler(errorHandler).validateDocument(xmlDOM);
}
+ else //xsdStreams != null
+ {
+ ErrorHandler errorHandler = feature.getErrorHandler();
+ Element xmlDOM = DOMUtils.sourceToElement(source);
+ new
SchemaValidationHelper(xsdStreams).setErrorHandler(errorHandler).validateDocument(xmlDOM);
+ }
}
catch (RuntimeException rte)
{
@@ -129,10 +137,6 @@
{
WSException.rethrow(ex);
}
- finally
- {
- schemaExtractor.close();
- }
}
private boolean isValidationEnabled()
Modified:
stack/native/trunk/modules/core/src/main/java/org/jboss/ws/extensions/validation/SchemaExtractor.java
===================================================================
---
stack/native/trunk/modules/core/src/main/java/org/jboss/ws/extensions/validation/SchemaExtractor.java 2011-04-18
15:02:02 UTC (rev 14156)
+++
stack/native/trunk/modules/core/src/main/java/org/jboss/ws/extensions/validation/SchemaExtractor.java 2011-04-18
15:50:03 UTC (rev 14157)
@@ -25,8 +25,15 @@
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
+import java.io.ByteArrayOutputStream;
+import java.io.ByteArrayInputStream;
+import java.io.InputStream;
+import java.io.FileInputStream;
import java.net.URL;
import java.util.List;
+import java.util.ArrayList;
+import java.util.regex.Pattern;
+import java.util.regex.Matcher;
import javax.xml.namespace.QName;
@@ -48,9 +55,15 @@
private static Logger log = Logger.getLogger(SchemaExtractor.class);
private File xsdFile;
+ private String path;
- public URL getSchemaUrl(URL wsdlURL) throws IOException
+ public InputStream[] getSchemas(URL wsdlURL) throws IOException
{
+ //Get the path to the WSDL
+ Pattern p = Pattern.compile("[a-zA-Z]+\\.[a-zA-Z]+$");
+ Matcher m = p.matcher(wsdlURL.getFile());
+ path = m.replaceFirst("");
+
// parse the wsdl
Element root = DOMUtils.parse(wsdlURL.openStream());
@@ -77,25 +90,64 @@
}
Element schemaElement = schemaElements.get(0);
- File tmpdir = IOUtils.createTempDirectory();
- xsdFile = File.createTempFile("jbossws_schema", ".xsd",
tmpdir);
- xsdFile.deleteOnExit();
+ List<InputStream> streams = new ArrayList<InputStream>();
- OutputStreamWriter outwr = new OutputStreamWriter(new FileOutputStream(xsdFile));
+ pullImportedSchemas(schemaElement, streams);
+
+ //Add the WSDL schema to the schema array
+ ByteArrayOutputStream outStream = new ByteArrayOutputStream();
+ OutputStreamWriter outwr = new OutputStreamWriter( outStream );
DOMWriter domWriter = new DOMWriter(outwr);
domWriter.setPrettyprint(true);
domWriter.print(schemaElement);
- outwr.close();
- return xsdFile.toURL();
+ streams.add(new ByteArrayInputStream(outStream.toByteArray()));
+
+ return streams.toArray(new InputStream[streams.size()]);
}
-
- public void close()
+
+ private void pullImportedSchemas(Element schemaElement, List<InputStream>
streams)
{
- if (xsdFile != null)
+ QName importQName = new QName( "http://www.w3.org/2001/XMLSchema",
"import" );
+ List<Element> importElements = DOMUtils.getChildElementsAsList(
schemaElement, importQName );
+
+ ArrayList<String> schemaLocations = new ArrayList<String>();
+ for( Element importElement : importElements )
{
- xsdFile.delete();
- xsdFile = null;
+ String schemaLocation = importElement.getAttribute( "schemaLocation"
);
+ schemaLocations.add( schemaLocation );
}
+
+ ByteArrayOutputStream outStream = null;
+
+ for( int i=0; i < schemaLocations.size(); i++ )
+ {
+ String schemaLocation = schemaLocations.get( i );
+
+ try
+ {
+ FileInputStream in = new FileInputStream( path + schemaLocation );
+ outStream = new ByteArrayOutputStream();
+
+ int bt = 0;
+ while(( bt = in.read() ) != -1 )
+ {
+ outStream.write( (byte)bt );
+ }
+
+ InputStream inputStream = new ByteArrayInputStream(outStream.toByteArray());
+ inputStream.mark(0);
+
+ Element root = DOMUtils.parse(inputStream);
+ pullImportedSchemas(root, streams);
+
+ inputStream.reset();
+ streams.add(inputStream);
+ }
+ catch(IOException ioe)
+ {
+ log.warn("Error obtaining schema: " + path + schemaLocation);
+ }
+ }
}
}
Modified:
stack/native/trunk/modules/core/src/main/java/org/jboss/ws/extensions/validation/SchemaValidationHelper.java
===================================================================
---
stack/native/trunk/modules/core/src/main/java/org/jboss/ws/extensions/validation/SchemaValidationHelper.java 2011-04-18
15:02:02 UTC (rev 14156)
+++
stack/native/trunk/modules/core/src/main/java/org/jboss/ws/extensions/validation/SchemaValidationHelper.java 2011-04-18
15:50:03 UTC (rev 14157)
@@ -30,6 +30,7 @@
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
+import org.jboss.logging.Logger;
import org.jboss.wsf.common.DOMWriter;
import org.w3c.dom.Element;
import org.xml.sax.ErrorHandler;
@@ -44,13 +45,21 @@
public class SchemaValidationHelper
{
private URL xsdURL;
+ private InputStream[] xsdStreams;
private ErrorHandler errorHandler = new StrictlyValidErrorHandler();
+
+ private static Logger log = Logger.getLogger(SchemaValidationHelper.class);
public SchemaValidationHelper(URL xsdURL)
{
this.xsdURL = xsdURL;
}
+ public SchemaValidationHelper(InputStream[] xsdStreams)
+ {
+ this.xsdStreams = xsdStreams;
+ }
+
public SchemaValidationHelper setErrorHandler(ErrorHandler errorHandler)
{
this.errorHandler = errorHandler;
@@ -79,14 +88,23 @@
DocumentBuilder builder = getDocumentBuilder();
builder.parse(inxml);
}
-
+
private DocumentBuilder getDocumentBuilder() throws ParserConfigurationException
{
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setValidating(true);
factory.setNamespaceAware(true);
factory.setAttribute("http://java.sun.com/xml/jaxp/properties/schema...;,
"http://www.w3.org/2001/XMLSchema");
-
factory.setAttribute("http://java.sun.com/xml/jaxp/properties/schema...;,
xsdURL.toExternalForm());
+
+ if(xsdStreams != null)
+ {
+
factory.setAttribute("http://java.sun.com/xml/jaxp/properties/schema...;,
xsdStreams);
+ }
+ else //use xsdURL
+ {
+
factory.setAttribute("http://java.sun.com/xml/jaxp/properties/schema...;,
xsdURL.toExternalForm());
+ }
+
factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
DocumentBuilder builder = factory.newDocumentBuilder();
builder.setErrorHandler(errorHandler);