Skip to content

Commit

Permalink
Merge pull request #987 from mbarto/geos6947_ftl_environment
Browse files Browse the repository at this point in the history
GEOS-6947: added support for custom variables from various sources to HT...
  • Loading branch information
mbarto committed Mar 26, 2015
2 parents 581010e + 3d0d1b7 commit b7f9390
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 5 deletions.
26 changes: 25 additions & 1 deletion doc/en/user/source/tutorials/freemarker.rst
Expand Up @@ -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)**

Expand Down Expand Up @@ -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}



Expand Down
@@ -0,0 +1,28 @@
/* (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;
}



}
@@ -1,4 +1,4 @@
/* (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.
Expand Down Expand Up @@ -64,7 +64,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);
Expand Down
@@ -1,4 +1,4 @@
/* (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.
Expand Down Expand Up @@ -44,6 +44,7 @@
import org.junit.Test;

import com.mockrunner.mock.web.MockHttpServletResponse;
import com.mockrunner.mock.web.MockServletContext;

public class HTMLFeatureInfoOutputFormatTest extends WMSTestSupport {
private HTMLFeatureInfoOutputFormat outputFormat;
Expand All @@ -56,10 +57,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()) {
Expand All @@ -68,7 +72,7 @@ public void setUp() throws URISyntaxException, IOException {
public Object findTemplateSource(String path) throws IOException {
String templatePath;
if (path.toLowerCase().contains("content")) {
templatePath = "test_content.ftl";
templatePath = currentTemplate;

} else {
templatePath = "empty.ftl";
Expand Down Expand Up @@ -135,6 +139,24 @@ public void testRequestParametersAreEvaluatedInTemplate() throws IOException {
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.
Expand Down
@@ -0,0 +1 @@
${environment.TEST_PROPERTY},${environment.TEST_INIT_PARAM}

0 comments on commit b7f9390

Please sign in to comment.