Skip to content

Commit

Permalink
Created preview header integration test and started extracting template
Browse files Browse the repository at this point in the history
  • Loading branch information
turingtestfail committed Jun 6, 2024
1 parent 605d1f0 commit 23cfdde
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedHashMap;
Expand Down Expand Up @@ -71,14 +72,20 @@
import org.geoserver.ows.util.ResponseUtils;
import org.geoserver.platform.ServiceException;
import org.geoserver.wms.GetMapRequest;
import org.geoserver.wms.MapLayerInfo;
import org.geoserver.wms.WMS;
import org.geoserver.wms.WMSInfo;
import org.geoserver.wms.WMSMapContent;
import org.geoserver.wms.capabilities.CapabilityUtil;
import org.geoserver.wms.featureinfo.FeatureTemplate;
import org.geotools.api.feature.simple.SimpleFeature;
import org.geotools.api.feature.simple.SimpleFeatureType;
import org.geotools.api.feature.type.FeatureType;
import org.geotools.api.referencing.FactoryException;
import org.geotools.api.referencing.crs.CoordinateReferenceSystem;
import org.geotools.api.referencing.operation.TransformException;
import org.geotools.api.style.Style;
import org.geotools.feature.simple.SimpleFeatureImpl;
import org.geotools.geometry.jts.ReferencedEnvelope;
import org.geotools.referencing.CRS;
import org.geotools.referencing.crs.DefaultGeographicCRS;
Expand Down Expand Up @@ -153,6 +160,8 @@ public class MapMLDocumentBuilder {

private Boolean isMultiExtent = MAPML_MULTILAYER_AS_MULTIEXTENT_DEFAULT;

private FeatureTemplate featureTemplate = new FeatureTemplate();

static {
PREVIEW_TCRS_MAP.put("OSMTILE", new TiledCRS("OSMTILE"));
PREVIEW_TCRS_MAP.put("CBMTILE", new TiledCRS("CBMTILE"));
Expand Down Expand Up @@ -1723,6 +1732,7 @@ public String getMapMLHTMLDocument() {
"/mapml/viewer/widget/mapml-viewer.js",
null,
URLMangler.URLType.RESOURCE);
List<String> headerContent = getTemplates("mapml-preview-head.ftl");
StringBuilder sb = new StringBuilder();
sb.append("<!DOCTYPE html>\n")
.append("<html>\n")
Expand Down Expand Up @@ -1781,6 +1791,42 @@ public String getMapMLHTMLDocument() {
return sb.toString();
}

private List<String> getTemplates(String templateName) {
List<String> templates = new ArrayList<>();
SimpleFeatureType featureType = null;
try {
for (MapLayerInfo mapLayerInfo : mapContent.getRequest().getLayers()) {
if (mapLayerInfo.getFeature() != null
&& mapLayerInfo.getFeature().getFeatureType() != null
&& mapLayerInfo.getFeature().getFeatureType()
instanceof SimpleFeatureType) {
featureType = (SimpleFeatureType) mapLayerInfo.getFeature().getFeatureType();
if (!featureTemplate.isTemplateEmpty(
featureType, templateName, FeatureTemplate.class, "0\n")) {
// no feature is passed in so none is needed for this template
SimpleFeature feature =
new SimpleFeatureImpl(
new ArrayList<>(
Collections.nCopies(
featureType.getAttributeCount(), "")),
featureType,
null);
templates.add(
featureTemplate.template(
feature, templateName, FeatureTemplate.class));
}
}
}
} catch (IOException e) {
LOGGER.fine(
"Template not found: "
+ templateName
+ " for schema: "
+ featureType.getTypeName());
}
return templates;
}

/** Builds the GetMap backlink to get MapML */
private String buildGetMap(
String layer,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import static org.junit.Assert.fail;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;
Expand All @@ -40,10 +41,14 @@
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.namespace.QName;

import org.apache.commons.io.FileUtils;
import org.custommonkey.xmlunit.XMLAssert;
import org.custommonkey.xmlunit.XMLUnit;
import org.custommonkey.xmlunit.XpathEngine;
import org.geoserver.catalog.Catalog;
import org.geoserver.catalog.CatalogBuilder;
import org.geoserver.catalog.FeatureTypeInfo;
import org.geoserver.catalog.LayerGroupInfo;
import org.geoserver.catalog.LayerInfo;
import org.geoserver.catalog.MetadataMap;
Expand Down Expand Up @@ -1574,6 +1579,42 @@ public void testHTMLWorkspaceQualified() throws Exception {
assertThat(layerSrc, containsString("LAYERS=Lakes"));
}

@Test
public void testPreviewHeadTemplate() throws Exception {
File template = null;
try {
String layerId = getLayerId(MockData.LAKES);
FeatureTypeInfo resource =
getCatalog().getResourceByName(layerId, FeatureTypeInfo.class);
File parent = getDataDirectory().get(resource).dir();
template = new File(parent, "mapml-preview-head.ftl");
FileUtils.write(template, "<link rel=\"stylesheet\" href=\"mystyle.css\">", "UTF-8");

String path =
"cite/wms?LAYERS=Lakes"
+ "&STYLES=&FORMAT="
+ MapMLConstants.MAPML_HTML_MIME_TYPE
+ "&SERVICE=WMS&VERSION=1.3.0"
+ "&REQUEST=GetMap"
+ "&SRS=epsg:3857"
+ "&BBOX=-13885038,2870337,-7455049,6338174"
+ "&WIDTH=150"
+ "&HEIGHT=150"
+ "&format_options="
+ MapMLConstants.MAPML_WMS_MIME_TYPE_OPTION
+ ":image/png";
Document doc = getAsJSoup(path);
Element layer = doc.select("mapml-viewer > layer-").first();
String layerSrc = layer.attr("src");
assertThat(layerSrc, startsWith("http://localhost:8080/geoserver/cite/wms?"));
assertThat(layerSrc, containsString("LAYERS=Lakes"));
} finally {
if (template != null) {
template.delete();
}
}
}

@Test
public void testInvalidProjectionHTML() throws Exception {
String path =
Expand Down

0 comments on commit 23cfdde

Please sign in to comment.