Skip to content

Commit

Permalink
[BP] Overview not shown in PDF export when the overview image is stor…
Browse files Browse the repository at this point in the history
…ed in GeoNetwork and requires authentication to access it. Fixes #7540 (#7556)
  • Loading branch information
josegar74 committed Dec 22, 2023
1 parent d24aed4 commit 06b6e9e
Showing 1 changed file with 70 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2001-2016 Food and Agriculture Organization of the
* Copyright (C) 2001-2023 Food and Agriculture Organization of the
* United Nations (FAO-UN), United Nations World Food Programme (WFP)
* and United Nations Environment Programme (UNEP)
*
Expand Down Expand Up @@ -28,11 +28,14 @@
import com.lowagie.text.Image;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;
import org.fao.geonet.ApplicationContextHolder;
import org.fao.geonet.api.ApiUtils;
import org.fao.geonet.api.records.attachments.Store;
import org.fao.geonet.api.records.extent.MapRenderer;
import org.fao.geonet.api.records.extent.MetadataExtentApi;
import org.fao.geonet.constants.Geonet;
import org.fao.geonet.constants.Params;
import org.fao.geonet.domain.MetadataResourceVisibility;
import org.fao.geonet.utils.Log;
import org.xhtmlrenderer.extend.FSImage;
import org.xhtmlrenderer.extend.ReplacedElement;
Expand Down Expand Up @@ -82,20 +85,23 @@ private static Set<String> getSupportedExts() {
if (imgFormatExts == null) {
synchronized (ImageReplacedElementFactory.class) {
if (imgFormatExts == null) {
imgFormatExts = Sets.newHashSet();
Set<String> tmpImgFormatExts = Sets.newHashSet();
for (String ext : ImageIO.getReaderFileSuffixes()) {
imgFormatExts.add(ext.toLowerCase());
tmpImgFormatExts.add(ext.toLowerCase());
}
imgFormatExts = tmpImgFormatExts;
}
}
}

return imgFormatExts;
}

static private Pattern ONE_EXTENT_API_REGEX = Pattern.compile(".*/(.*)/extents/([0-9]+)\\.png.*");
static private Pattern ALL_EXTENT_API_REGEX = Pattern.compile(".*/(.*)/extents\\.png.*");
static private final String EXTENT_XPATH = ".//*[local-name() ='extent']/*/*[local-name() = 'geographicElement']/*";
private static final Pattern ONE_EXTENT_API_REGEX = Pattern.compile(".*/(.*)/extents/(\\d+)\\.png.*");
private static final Pattern ALL_EXTENT_API_REGEX = Pattern.compile(".*/(.*)/extents\\.png.*");
private static final String EXTENT_XPATH = ".//*[local-name() ='extent']/*/*[local-name() = 'geographicElement']/*";

private static final String DEFAULT_SRS = "EPSG:4326";

@Override
public ReplacedElement createReplacedElement(LayoutContext layoutContext, BlockBox box,
Expand All @@ -109,15 +115,15 @@ public ReplacedElement createReplacedElement(LayoutContext layoutContext, BlockB
if (!"img".equals(nodeName)) {
try {
return superFactory.createReplacedElement(layoutContext, box, userAgentCallback, cssWidth, cssHeight);
} catch (Throwable e) {
} catch (Exception e) {
return new EmptyReplacedElement(cssWidth, cssHeight);
}
}


String src = element.getAttribute("src");
String baseUrlNoLang = baseURL.substring(0, baseURL.length() - 4);

boolean useExtentApi = src.startsWith(baseURL.substring(0, baseURL.length() - 4))
boolean useExtentApi = src.startsWith(baseUrlNoLang)
&& mapRenderer != null
&& (ALL_EXTENT_API_REGEX.matcher(src).matches()
|| ONE_EXTENT_API_REGEX.matcher(src).matches());
Expand All @@ -135,7 +141,7 @@ public ReplacedElement createReplacedElement(LayoutContext layoutContext, BlockB
regionId = String.format("metadata:@id%s:@xpath(%s)[%s]", ApiUtils.getInternalId(oneMatcher.group(1), true), EXTENT_XPATH, oneMatcher.group(2));
}
Map<String, String> parameters = getParams(src);
String srs = parameters.get(MetadataExtentApi.MAP_SRS_PARAM) != null ? parameters.get(MetadataExtentApi.MAP_SRS_PARAM) : "EPSG:4326";
String srs = parameters.get(MetadataExtentApi.MAP_SRS_PARAM) != null ? parameters.get(MetadataExtentApi.MAP_SRS_PARAM) : DEFAULT_SRS;
Integer width = parameters.get(MetadataExtentApi.WIDTH_PARAM) != null ? Integer.parseInt(parameters.get(MetadataExtentApi.WIDTH_PARAM)) : null;
Integer height = parameters.get(MetadataExtentApi.HEIGHT_PARAM) != null ? Integer.parseInt(parameters.get(MetadataExtentApi.HEIGHT_PARAM)) : null;
String background = parameters.get(MetadataExtentApi.BACKGROUND_PARAM);
Expand All @@ -145,20 +151,25 @@ public ReplacedElement createReplacedElement(LayoutContext layoutContext, BlockB
}
float factor = layoutContext.getDotsPerPixel();
return loadImage(layoutContext, box, userAgentCallback, cssWidth, cssHeight, new BufferedImageLoader(image), factor);
} else if (src.startsWith(baseURL + "region.getmap.png") | src.endsWith("/geom.png") && mapRenderer != null) {
} else if (src.startsWith(baseURL + "region.getmap.png") || src.endsWith("/geom.png") && mapRenderer != null) {
BufferedImage image = null;
try {
Map<String, String> parameters = getParams(src);

String id = parameters.get(Params.ID);
String srs = parameters.get(MetadataExtentApi.MAP_SRS_PARAM) != null ? parameters.get(MetadataExtentApi.MAP_SRS_PARAM) : "EPSG:4326";
String srs = parameters.get(MetadataExtentApi.MAP_SRS_PARAM) != null ? parameters.get(MetadataExtentApi.MAP_SRS_PARAM) : DEFAULT_SRS;
Integer width = parameters.get(MetadataExtentApi.WIDTH_PARAM) != null ? Integer.parseInt(parameters.get(MetadataExtentApi.WIDTH_PARAM)) : null;
Integer height = parameters.get(MetadataExtentApi.HEIGHT_PARAM) != null ? Integer.parseInt(parameters.get(MetadataExtentApi.HEIGHT_PARAM)) : null;
String background = parameters.get(MetadataExtentApi.BACKGROUND_PARAM);
String geomParam = parameters.get(MetadataExtentApi.GEOM_PARAM);
String geomType = parameters.get(MetadataExtentApi.GEOM_TYPE_PARAM) != null ? parameters.get(MetadataExtentApi.GEOM_TYPE_PARAM) : "WKT";
String geomSrs = parameters.get(MetadataExtentApi.GEOM_SRS_PARAM) != null ? parameters.get(MetadataExtentApi.GEOM_SRS_PARAM) : "EPSG:4326";
String geomSrs = parameters.get(MetadataExtentApi.GEOM_SRS_PARAM) != null ? parameters.get(MetadataExtentApi.GEOM_SRS_PARAM) : DEFAULT_SRS;

if ((width == null) && (height == null)) {
// Width or height are required. If not set the default width with the same value
// as defined in MetadataExtentApi.getOneRecordExtentAsImage
width = 300;
}
image = mapRenderer.render(
id, srs, width, height, background, geomParam, geomType, geomSrs, null, null);
} catch (Exception e) {
Expand Down Expand Up @@ -189,14 +200,29 @@ public ReplacedElement createReplacedElement(LayoutContext layoutContext, BlockB
}
float factor = layoutContext.getDotsPerPixel();
return loadImage(layoutContext, box, userAgentCallback, cssWidth, cssHeight, new UrlImageLoader(builder.toString()), factor);

} else if (src.startsWith(baseUrlNoLang) && src.contains("/attachments/")) {
// Process attachments urls to load the images from the data directory
Matcher m = Pattern.compile(baseUrlNoLang + "api/records/(.*)/attachments/(.*)$").matcher(src);
if (m.find()) {
String uuid = m.group(1);
String file = m.group(2);

float factor = layoutContext.getDotsPerPixel();
return loadImage(layoutContext, box, userAgentCallback, cssWidth, cssHeight, new DataDirectoryImageLoader(uuid, file), factor);
} else if (isSupportedImageFormat(src)) {
float factor = layoutContext.getDotsPerPixel();
return loadImage(layoutContext, box, userAgentCallback, cssWidth, cssHeight, new UrlImageLoader(src), factor);
}

} else if (isSupportedImageFormat(src)) {
float factor = layoutContext.getDotsPerPixel();
return loadImage(layoutContext, box, userAgentCallback, cssWidth, cssHeight, new UrlImageLoader(src), factor);
}

try {
return superFactory.createReplacedElement(layoutContext, box, userAgentCallback, cssWidth, cssHeight);
} catch (Throwable e) {
} catch (Exception e) {
return new EmptyReplacedElement(cssWidth, cssHeight);
}
}
Expand Down Expand Up @@ -247,7 +273,7 @@ private ReplacedElement loadImage(LayoutContext layoutContext, BlockBox box, Use

try {
return superFactory.createReplacedElement(layoutContext, box, userAgentCallback, cssWidth, cssHeight);
} catch (Throwable e2) {
} catch (Exception e2) {
return new EmptyReplacedElement(cssWidth, cssHeight);
}
}
Expand Down Expand Up @@ -296,6 +322,35 @@ public Image loadImage() throws Exception {
}
}


/**
* Class to load images from the metadata data directory.
*/
private class DataDirectoryImageLoader implements ImageLoader {
private final String uuid;
private final String file;
public DataDirectoryImageLoader(String uuid, String file) {
this.uuid = uuid;
this.file = file;
}

@Override
public Image loadImage() throws Exception {
Store store = ApplicationContextHolder.get().getBean("filesystemStore", Store.class);
BufferedImage bufferedImage;
try (Store.ResourceHolder imageFile = store.getResourceInternal(
this.uuid,
MetadataResourceVisibility.PUBLIC,
this.file, true)) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bufferedImage = ImageIO.read(imageFile.getPath().toFile());
ImageIO.write(bufferedImage, "png", baos);
return Image.getInstance(baos.toByteArray());
}
}
}


/* Define an AWT BufferedImage image loader */

private class BufferedImageLoader implements ImageLoader {
Expand Down

0 comments on commit 06b6e9e

Please sign in to comment.