Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions geowebcache/core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@
<groupId>org.geotools</groupId>
<artifactId>gt-coverage</artifactId>
</dependency>
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-xml</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-collections4</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
Expand All @@ -41,19 +43,21 @@
import java.util.logging.Logger;
import java.util.stream.Collectors;
import javax.annotation.Nullable;
import javax.xml.XMLConstants;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.TransformerFactoryConfigurationError;
import javax.xml.transform.dom.DOMResult;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Validator;
import org.geotools.util.logging.Logging;
import org.geotools.xml.XMLUtils;
import org.geowebcache.GeoWebCacheEnvironment;
import org.geowebcache.GeoWebCacheException;
import org.geowebcache.GeoWebCacheExtensions;
Expand Down Expand Up @@ -117,6 +121,10 @@ public class XMLConfiguration

public static final String DEFAULT_CONFIGURATION_FILE_NAME = "geowebcache.xml";

public static final String SCHEMA_NAMESPACE_PREFIX = "http://geowebcache.org/schema/";

public static final String DEFAULT_SCHEMA_RESOURCE = "geowebcache.xsd";

private static Logger log = Logging.getLogger(XMLConfiguration.class.getName());

/** Web app context, used to look up {@link XMLConfigurationProvider}s. */
Expand Down Expand Up @@ -614,9 +622,9 @@ private synchronized void addOrReplaceGridSet(final XMLGridSet gridSet) throws I
static Node loadDocument(InputStream xmlFile) throws ConfigurationException, IOException {
Node topNode = null;
try {
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilderFactory docBuilderFactory = XMLUtils.newDocumentBuilderFactory();
docBuilderFactory.setNamespaceAware(true);
DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
DocumentBuilder docBuilder = XMLUtils.newDocumentBuilder(docBuilderFactory);
topNode = checkAndTransform(docBuilder.parse(xmlFile));
} catch (Exception e) {
throw (IOException) new IOException(e.getMessage()).initCause(e);
Expand All @@ -629,14 +637,12 @@ private static Node checkAndTransform(Document doc) throws ConfigurationExceptio
Node rootNode = doc.getDocumentElement();

// debugPrint(rootNode);

if (!rootNode.getNodeName().equals("gwcConfiguration")) {
log.config("The configuration file is of the pre 1.0 type, trying to convert.");
rootNode = applyTransform(rootNode, "geowebcache_pre10.xsl").getFirstChild();
}

// debugPrint(rootNode);

if (rootNode.getNamespaceURI().equals("http://geowebcache.org/schema/1.0.0")) {
log.config("Updating configuration from 1.0.0 to 1.0.1");
rootNode = applyTransform(rootNode, "geowebcache_100.xsl").getFirstChild();
Expand Down Expand Up @@ -716,6 +722,11 @@ private static Node checkAndTransform(Document doc) throws ConfigurationExceptio
rootNode = applyTransform(rootNode, "geowebcache_151.xsl").getFirstChild();
}

if (!rootNode.getNamespaceURI().equals("http://geowebcache.org/schema/2.0.0")) {
log.config("Updating configuration to 2.0.0");
rootNode = applyTransform(rootNode, "geowebcache_200.xsl").getFirstChild();
}

// Check again after transform
if (!rootNode.getNodeName().equals("gwcConfiguration")) {
log.log(Level.SEVERE, "Unable to parse file, expected gwcConfiguration at root after transform.");
Expand All @@ -726,8 +737,8 @@ private static Node checkAndTransform(Document doc) throws ConfigurationExceptio
validate(rootNode);
log.config("TileLayerConfiguration file validated fine.");
} catch (SAXException e) {
log.warning("GWC configuration validation error: " + e.getMessage());
log.warning(
log.fine("GWC configuration validation error: " + e.getMessage());
log.fine(
"Will try to use configuration anyway. Please check the order of declared elements against the schema.");
} catch (IOException e) {
throw new RuntimeException(e.getMessage(), e);
Expand All @@ -739,11 +750,14 @@ private static Node checkAndTransform(Document doc) throws ConfigurationExceptio

static void validate(Node rootNode) throws SAXException, IOException {
// Perform validation
// TODO dont know why this one suddenly failed to look up, revert to
// XMLConstants.W3C_XML_SCHEMA_NS_URI
SchemaFactory factory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
SchemaFactory factory = XMLUtils.newSchemaFactory(XMLConstants.W3C_XML_SCHEMA_NS_URI);
// We do not need access to external schemas
factory.setProperty(XMLConstants.ACCESS_EXTERNAL_SCHEMA, "");
factory.setProperty(XMLConstants.ACCESS_EXTERNAL_DTD, "");
try (InputStream is = XMLConfiguration.class.getResourceAsStream("geowebcache.xsd")) {

if (is == null) {
throw new IOException("Could not load schema resource 'geowebcache.xsd'");
}
Schema schema = factory.newSchema(new StreamSource(is));
Validator validator = schema.newValidator();

Expand All @@ -758,7 +772,7 @@ static String getCurrentSchemaVersion() {

Document dom;
try (InputStream is = XMLConfiguration.class.getResourceAsStream("geowebcache.xsd")) {
dom = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(is);
dom = XMLUtils.newDocumentBuilder().parse(is);
} catch (Exception e) {
throw new RuntimeException(e);
}
Expand All @@ -777,7 +791,7 @@ private static Node applyTransform(Node oldRootNode, String xslFilename) {
try (InputStream is = XMLConfiguration.class.getResourceAsStream(xslFilename)) {

try {
transformer = TransformerFactory.newInstance().newTransformer(new StreamSource(is));
transformer = XMLUtils.newTransformer(new StreamSource(is));
transformer.transform(new DOMSource(oldRootNode), result);
} catch (TransformerFactoryConfigurationError | TransformerException e) {
log.log(Level.FINE, e.getMessage(), e);
Expand Down Expand Up @@ -1462,4 +1476,17 @@ public void deinitialize() throws Exception {
this.layers = null;
this.gwcConfig = null;
}

/** Print a DOM tree to an output stream or if there is an exception while doing so, print the stack trace. */
public static void printDom(Node dom, OutputStream os) {
Transformer trans;
PrintWriter w = new PrintWriter(os);
try {
trans = XMLUtils.newTransformer(); // XMLUtils.newTransformer()
trans.transform(new DOMSource(dom), new StreamResult(new OutputStreamWriter(os)));
} catch (TransformerException e) {
w.println("An error ocurred while transforming the given DOM:");
e.printStackTrace(w);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xml="http://www.w3.org/XML/1998/namespace"
targetNamespace="http://geowebcache.org/schema/1.28.0" xmlns:gwc="http://geowebcache.org/schema/1.28.0"
elementFormDefault="qualified" version="1.28.0">
targetNamespace="http://geowebcache.org/schema/2.0.0" xmlns:gwc="http://geowebcache.org/schema/2.0.0"
elementFormDefault="qualified" version="2.0.0">

<xs:element name="gwcConfiguration">
<xs:annotation>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:gwc="http://geowebcache.org/schema/1.6.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" version="1.0" encoding="utf-8" indent="yes"/>

<xsl:template match="node()|*">
<xsl:copy>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>

<xsl:template match="/|comment()|processing-instruction()">
<xsl:copy>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>

<xsl:template match="*">
<xsl:element name="{local-name()}">
<xsl:apply-templates select="@*|node()"/>
</xsl:element>
</xsl:template>

<xsl:template match="@*">
<xsl:attribute name="{local-name()}">
<xsl:value-of select="."/>
</xsl:attribute>
</xsl:template>

<xsl:template match="gwc:keyword">
<string><xsl:apply-templates/></string>
</xsl:template>

<xsl:template match="gwc:gwcConfiguration">
<gwcConfiguration xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://geowebcache.org/schema/2.0.0"
xsi:schemaLocation="http://geowebcache.org/schema/2.0.0 http://geowebcache.org/schema/2.0.0/geowebcache.xsd">
<xsl:apply-templates/>
</gwcConfiguration>
</xsl:template>

</xsl:stylesheet>
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@

public class ServerConfigurationTest {

private static final String VERSION_PATTERN = "1(\\.\\d+)+(\\-\\w+)*";
private static final String VERSION_PATTERN = "2(\\.\\d+)+(\\-\\w+)*";

ServerConfiguration config;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.geotools.xml.XMLUtils;
import org.geowebcache.GeoWebCacheException;
import org.geowebcache.config.meta.ServiceInformation;
import org.geowebcache.filter.request.RequestFilter;
Expand All @@ -45,7 +46,9 @@ public class XMLConfigurationBackwardsCompatibilityTest {

public static final String GWC_125_CONFIG_FILE = "geowebcache_125.xml";

public static final String LATEST_FILENAME = "geowebcache_130.xml";
public static final String GWC_130_CONFIG_FILE = "geowebcache_130.xml";

public static final String LATEST_FILENAME = "geowebcache_200.xml";

@Test
public void testLoadPre10() throws Exception {
Expand Down Expand Up @@ -227,14 +230,14 @@ private XMLConfiguration loadConfig(String fileName) throws Exception {

/** Utility method to print out a dom. */
protected void print(Document dom) throws Exception {
TransformerFactory txFactory = TransformerFactory.newInstance();
TransformerFactory txFactory = XMLUtils.newTransformerFactory();
try {
txFactory.setAttribute("{http://xml.apache.org/xalan}indent-number", Integer.valueOf(2));
} catch (Exception e) {
// some
}

Transformer tx = txFactory.newTransformer();
Transformer tx = XMLUtils.newTransformer(txFactory);
tx.setOutputProperty(OutputKeys.METHOD, "xml");
tx.setOutputProperty(OutputKeys.INDENT, "yes");

Expand Down
Loading
Loading