Commit a78ab8a3 authored by Mauro Bartolomeoli's avatar Mauro Bartolomeoli
Browse files

GEOS-6947: added support for custom variables from various sources to HTML...

GEOS-6947: added support for custom variables from various sources to HTML GetFeatureInfo ftl templates
parent 92e43380
......@@ -38,7 +38,7 @@ The data model is a sort of a tree, where each element has a name and a type. Be
* map: a key/value map, that you usually access using the dot notation, as in ``${myMap.myKey``}, and can be nested;
* listMap: a special construct that is, at the same time, a Map, and a list of the values.
Here are the three data models (as you can see there are redundancies, in particular in attributes, we chose this approach to make template building easier):
Here are the data models (as you can see there are redundancies, in particular in attributes, we chose this approach to make template building easier):
**FeatureType (map)**
......@@ -85,6 +85,30 @@ Here are the three data models (as you can see there are redundancies, in partic
* type (FeatureType, see above)
**request (map)**
Contains the GetFeatureInfo request parameters and related values.
**environment (map)**
Allows accessing several environment variables, in particular those defined in:
* JVM system properties
* OS environment variables
* web.xml context parameters
Examples
``````````````````
**request**
* ${request.LAYERS}
* ${request.ENV.PROPERTY}
**environment**
* ${environment.GEOSERVER_DATA_DIR}
* ${environment.WEB_SITE_URL}
......
/* (c) 2015 Open Source Geospatial Foundation - all rights reserved
* This code is licensed under the GPL 2.0 license, available at the root
* application directory.
*/
package org.geoserver.wms.featureinfo;
import org.geoserver.platform.GeoServerExtensions;
import freemarker.template.SimpleScalar;
import freemarker.template.TemplateHashModel;
import freemarker.template.TemplateModel;
import freemarker.template.TemplateModelException;
public class EnvironmentVariablesTemplateModel implements TemplateHashModel {
@Override
public TemplateModel get(String propertyName) throws TemplateModelException {
return new SimpleScalar(GeoServerExtensions.getProperty(propertyName));
}
@Override
public boolean isEmpty() throws TemplateModelException {
return false;
}
}
/* (c) 2014 Open Source Geospatial Foundation - all rights reserved
/* (c) 2014 - 2015 Open Source Geospatial Foundation - all rights reserved
* (c) 2001 - 2013 OpenPlans
* This code is licensed under the GPL 2.0 license, available at the root
* application directory.
......@@ -65,7 +65,8 @@ public class HTMLFeatureInfoOutputFormat extends GetFeatureInfoOutputFormat {
public TemplateModel wrap(Object object) throws TemplateModelException {
if (object instanceof FeatureCollection) {
SimpleHash map = (SimpleHash) super.wrap(object);
map.put("request", Dispatcher.REQUEST.get().getKvp());
map.put("request", Dispatcher.REQUEST.get().getKvp());
map.put("environment", new EnvironmentVariablesTemplateModel());
return map;
}
return super.wrap(object);
......
/* (c) 2014 Open Source Geospatial Foundation - all rights reserved
/* (c) 2014 - 2015 Open Source Geospatial Foundation - all rights reserved
* (c) 2001 - 2013 OpenPlans
* This code is licensed under the GPL 2.0 license, available at the root
* application directory.
......@@ -38,6 +38,9 @@ import org.geoserver.wms.WMSTestSupport;
import org.junit.Before;
import org.junit.Test;
import com.mockrunner.mock.web.MockHttpServletResponse;
import com.mockrunner.mock.web.MockServletContext;
public class HTMLFeatureInfoOutputFormatTest extends WMSTestSupport {
private HTMLFeatureInfoOutputFormat outputFormat;
......@@ -49,10 +52,13 @@ public class HTMLFeatureInfoOutputFormatTest extends WMSTestSupport {
private static final String templateFolder = "/org/geoserver/wms/featureinfo/";
private String currentTemplate;
@Before
public void setUp() throws URISyntaxException, IOException {
outputFormat = new HTMLFeatureInfoOutputFormat(getWMS());
currentTemplate = "test_content.ftl";
// configure template loader
GeoServerTemplateLoader templateLoader = new GeoServerTemplateLoader(
this.getClass(), getDataDirectory()) {
......@@ -61,7 +67,7 @@ public class HTMLFeatureInfoOutputFormatTest extends WMSTestSupport {
public Object findTemplateSource(String path) throws IOException {
String templatePath;
if (path.toLowerCase().contains("content")) {
templatePath = "test_content.ftl";
templatePath = currentTemplate;
} else {
templatePath = "empty.ftl";
......@@ -128,6 +134,24 @@ public class HTMLFeatureInfoOutputFormatTest extends WMSTestSupport {
assertEquals("VALUE1,VALUE2,testLayer" , result);
}
@Test
public void testEnvironmentVariablesAreEvaluatedInTemplate() throws IOException {
currentTemplate = "test_env_content.ftl";
System.setProperty("TEST_PROPERTY", "MYVALUE");
MockServletContext servletContext = (MockServletContext)applicationContext.getServletContext();
servletContext.setInitParameter("TEST_INIT_PARAM", "MYPARAM");
try {
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
outputFormat.write(fcType, getFeatureInfoRequest, outStream);
String result = new String(outStream.toByteArray());
assertEquals("MYVALUE,MYPARAM" , result);
} finally {
System.clearProperty("TEST_PROPERTY");
}
}
/**
* Test that if template asks a request parameter that is not present in request
* an exception is thrown.
......
${environment.TEST_PROPERTY},${environment.TEST_INIT_PARAM}
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment