geosolutions-it / geoserver

Official GeoServer repository
http://geoserver.org
Other
10 stars 7 forks source link

Support for ENV variable in freemarker templates #200

Open tdipisa opened 4 years ago

tdipisa commented 4 years ago

It would be useful to have support for the ENV variable in GeoServer Freemarker templates. For example in GetFeatureInfo requests, where the locale sent by the client as a query parameter in the URL could be used.

http://mydomain/geoserver/wms?service=WMS&version=1.1.1&request=GetFeatureInfo&exceptions=application/json&id=layer_name_propr__3&layers=layer_name&query_layers=layer_name&x=51&y=51&height=101&width=101&srs=EPSG:3857&bbox=1262445.155988286,5859892.496590748,1262475.3127846017,5859922.653387064&feature_count=10&info_format=text/html&ENV=mapstore_language:it

With this support available in GeoServer, the corresponding part in the FTL model could be:

<#assign locale = request.ENV.mapstore_language/>

or something similar.

Related to https://github.com/geosolutions-it/austrocontrol-C125/issues/170

nmco commented 4 years ago

Client and project:

AUSTROCONTROL
C125-2020-AUSTROCONTROL-Map2Imp

@fernandor777 looks like this should already be supported as per this documentation, specially this example: ${request.ENV.PROPERTY}.

Can you double check if this is supported, and if yes what's the syntax? Otherwise, please prepare a plan to implement it and estimate it.

fernandor777 commented 4 years ago

An update about ENV variables support on GetFeatureInfo Freemarker templates:

Using the ENV definition on the URL:

&ENV=mapstore_language:it_IT

The variable usage in the template should be:

${request.ENV.MAPSTORE_LANGUAGE}

Example content template definition (data/templates/content.ftl):

<#assign locale="${request.ENV.MAPSTORE_LANGUAGE}">
<#setting locale="${request.ENV.MAPSTORE_LANGUAGE}">

<p>${request.ENV.MAPSTORE_LANGUAGE}</p>
<p>${locale}</p>

<ul>
<#list features as feature>
  <li><b>Type: ${type.name}</b> (id: <em>${feature.fid}</em>):
  <ul>
  <#list feature.attributes as attribute>
    <#if !attribute.isGeometry>
      <li>${attribute.name}: ${attribute.value}</li>
    </#if>
  </#list>
  </ul>
  </li>
</#list>
</ul>

Example output: Screenshot from 2020-07-27 10-23-23

Checking the internal freemarker locale object

Using the following template update for checking the internal Freemarker locale object state:

<#setting locale="${request.ENV.MAPSTORE_LANGUAGE}">

The current locale is: ${.locale} <br/>
Current language is:  ${.lang} <br/>

<ul>
<#list features as feature>
  <li><b>Type: ${type.name}</b> (id: <em>${feature.fid}</em>):
  <ul>
  <#list feature.attributes as attribute>
    <#if !attribute.isGeometry>
      <li>${attribute.name}: ${attribute.value}</li>
    </#if>
  </#list>
  </ul>
  </li>
</#list>
</ul>

We got this output: Screenshot from 2020-07-27 10-34-31

nmco commented 4 years ago

@tdipisa let's know if you need more details.

tdipisa commented 4 years ago

@fernandor777 @nmco ok thank you. Therefore it is already supported. Did I understand correctly?

nmco commented 4 years ago

Yes @tdipisa, that say @fernandor777 when you have 5 minutes can you do a PR adding a more meaningful example to the documentation?

tdipisa commented 4 years ago

@nmco @fernandor777 one more question please: is this available also on 2.16.2?

fernandor777 commented 4 years ago

@tdipisa yes, I tested it with GeoServer 2.16.2 and it is supported.

tdipisa commented 4 years ago

@fernandor777 ok thank you so much.