Skip to content

Commit

Permalink
Move test-specific behaviour to test side.
Browse files Browse the repository at this point in the history
  • Loading branch information
bradh committed Dec 25, 2017
1 parent fbfc3c4 commit e90db02
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,8 @@
package org.geoserver.util;

import org.geoserver.config.GeoServer;
import org.geotools.factory.GeoTools;
import org.geotools.xml.PreventLocalEntityResolver;
import org.xml.sax.EntityResolver;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

import java.io.IOException;


/**
Expand All @@ -22,82 +17,33 @@
*/
public class EntityResolverProvider {

private static EntityResolver entityResolver = PreventLocalEntityResolver.INSTANCE;

/**
* A entity resolver provider that always disabled entity resolution
* A entity resolver provider that always disables entity resolution
*/
public static final EntityResolverProvider RESOLVE_DISABLED_PROVIDER = new EntityResolverProvider(
null);

/**
* In IDEs during development GeoTools sources can be in the classpath of GeoServer tests, this
* resolver allows them to be resolved while blocking the rest
*/
public static final EntityResolver RESOLVE_DISABLED_PROVIDER_DEVMODE = new PreventLocalEntityResolver() {
@Override
public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException {
if (DEVELOPER_MODE && isLocalGeoToolsSchema(null, systemId)) {
return null;
}
private final GeoServer geoServer;

return super.resolveEntity(publicId, systemId);
}

@Override
public InputSource resolveEntity(String name, String publicId, String baseURI, String systemId) throws SAXException, IOException {
if (DEVELOPER_MODE && isLocalGeoToolsSchema(baseURI, systemId)) {
return null;
}

return super.resolveEntity(name, publicId, baseURI, systemId);
}

private boolean isLocalGeoToolsSchema(String baseURI, String systemId) {
if (systemId.startsWith("file:/")) {
return isLocalGeotoolsSchema(systemId);
} else if (!systemId.contains("://") && baseURI != null) {
// location relative to a baseURI
return isLocalGeotoolsSchema(baseURI);
}
return false;
}

private boolean isLocalGeotoolsSchema(String path) {
// Windows case insensitive filesystem work-around
path = path.toLowerCase();
// Match the GeoTools locations having schemas we resolve against
return path.matches(".*modules[\\\\/]extension[\\\\/]xsd[\\\\/].*\\.xsd") ||
path.matches(".*modules[\\\\/]ogc[\\\\/].*\\.xsd");
}
};

/**
* Set this to true to allow resolution of any XSD file
*/
public static boolean DEVELOPER_MODE = false;


private GeoServer geoServer;

public EntityResolverProvider(GeoServer geoServer) {
this.geoServer = geoServer;
}


public static void setEntityResolver(EntityResolver resolver) {
entityResolver = resolver;
}

public EntityResolver getEntityResolver() {
if (geoServer != null) {
Boolean externalEntitiesEnabled = geoServer.getGlobal().isXmlExternalEntitiesEnabled();
if (externalEntitiesEnabled != null && externalEntitiesEnabled.booleanValue()) {
if (externalEntitiesEnabled != null && externalEntitiesEnabled) {
// XML parser will try to resolve entities
return null;
}
}

if (DEVELOPER_MODE) {
return RESOLVE_DISABLED_PROVIDER_DEVMODE;
} else {
// default behaviour: entities disabled
return PreventLocalEntityResolver.INSTANCE;
}
return entityResolver;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,8 @@

import net.sf.json.JSON;
import net.sf.json.JSONSerializer;
import org.geotools.xml.PreventLocalEntityResolver;
import org.xml.sax.EntityResolver;

/**
* Base test class for GeoServer system tests that require a fully configured spring context and
Expand Down Expand Up @@ -193,6 +195,48 @@ protected SystemTestData createTestData() throws Exception {
*/
protected static DispatcherServlet dispatcher;

/**
* In IDEs during development GeoTools sources can be in the classpath of GeoServer tests, this
* resolver allows them to be resolved while blocking the rest
*/
public static final EntityResolver RESOLVE_DISABLED_PROVIDER_DEVMODE = new PreventLocalEntityResolver() {
@Override
public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException {
if (isLocalGeoToolsSchema(null, systemId)) {
return null;
}

return super.resolveEntity(publicId, systemId);
}

@Override
public InputSource resolveEntity(String name, String publicId, String baseURI, String systemId) throws SAXException, IOException {
if (isLocalGeoToolsSchema(baseURI, systemId)) {
return null;
}

return super.resolveEntity(name, publicId, baseURI, systemId);
}

private boolean isLocalGeoToolsSchema(String baseURI, String systemId) {
if (systemId.startsWith("file:/")) {
return isLocalGeotoolsSchema(systemId);
} else if (!systemId.contains("://") && baseURI != null) {
// location relative to a baseURI
return isLocalGeotoolsSchema(baseURI);
}
return false;
}

private boolean isLocalGeotoolsSchema(String path) {
// Windows case insensitive filesystem work-around
path = path.toLowerCase();
// Match the GeoTools locations having schemas we resolve against
return path.matches(".*modules[\\\\/]extension[\\\\/]xsd[\\\\/].*\\.xsd") ||
path.matches(".*modules[\\\\/]ogc[\\\\/].*\\.xsd");
}
};

protected final void setUp(SystemTestData testData) throws Exception {
// speed up xpath evaluations
try {
Expand Down Expand Up @@ -262,7 +306,7 @@ protected final void setUp(SystemTestData testData) throws Exception {
dispatcher = buildDispatcher();

// Allow resolution of XSDs from local file system
EntityResolverProvider.DEVELOPER_MODE = true;
EntityResolverProvider.setEntityResolver(RESOLVE_DISABLED_PROVIDER_DEVMODE);

onSetUp(testData);
}
Expand Down

0 comments on commit e90db02

Please sign in to comment.