Skip to content

Commit

Permalink
#1899: INSPIRE Pre-defined ATOM feeds
Browse files Browse the repository at this point in the history
  • Loading branch information
etj committed Feb 17, 2017
1 parent 9077143 commit bfb98ad
Show file tree
Hide file tree
Showing 7 changed files with 1,646 additions and 18 deletions.
15 changes: 14 additions & 1 deletion core/src/main/java/org/fao/geonet/util/XslUtil.java
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-2017 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 @@ -651,4 +651,17 @@ public static String encodeForURL(String str) {
return str;
}
}

public static String getMax(Object values) {
String[] strings = values.toString().split(" ");
String max = "";

for (int i = 0; i < strings.length; i++) {
String val = strings[i];
if(val.compareTo(max) > 0) {
max = val;
}
}
return max;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//=============================================================================
//=== Copyright (C) 2001-2010 Food and Agriculture Organization of the
//=== Copyright (C) 2001-2017 Food and Agriculture Organization of the
//=== United Nations (FAO-UN), United Nations World Food Programme (WFP)
//=== and United Nations Environment Programme (UNEP)
//===
Expand All @@ -22,30 +22,37 @@
//==============================================================================
package org.fao.geonet.inspireatom.util;

import java.net.URL;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import jeeves.constants.Jeeves;
import jeeves.server.ServiceConfig;
import jeeves.server.context.ServiceContext;

import org.apache.commons.lang.StringUtils;
import org.fao.geonet.GeonetContext;
import org.fao.geonet.constants.Geonet;
import org.fao.geonet.domain.Metadata;
import org.fao.geonet.exceptions.MetadataNotFoundEx;
import org.fao.geonet.kernel.DataManager;
import org.fao.geonet.kernel.search.LuceneSearcher;
import org.fao.geonet.kernel.search.MetaSearcher;
import org.fao.geonet.kernel.search.SearchManager;
import org.fao.geonet.kernel.search.SearcherType;
import org.fao.geonet.kernel.setting.SettingManager;
import org.fao.geonet.lib.Lib;
import org.fao.geonet.utils.GeonetHttpRequestFactory;
import org.fao.geonet.utils.Xml;
import org.fao.geonet.utils.XmlRequest;
import org.apache.commons.lang.StringUtils;

import org.fao.geonet.kernel.DataManager;
import org.fao.geonet.kernel.setting.SettingManager;
import org.fao.geonet.lib.Lib;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.Namespace;

import java.net.URL;
import java.util.*;

/**
* Utility class for INSPIRE Atom.
Expand All @@ -69,6 +76,9 @@ public class InspireAtomUtil {
**/
private static final String EXTRACT_ATOM_FEED = "extract-atom-feed.xsl";

/** Xslt process to get the atom feed link from the metadata. **/
private static final String TRANSFORM_MD_TO_ATOM_FEED = "inspire-atom-feed.xsl";

/**
* Issue an http request to retrieve the remote Atom feed document.
*
Expand Down Expand Up @@ -162,7 +172,7 @@ public static List<String> extractRelatedDatasetsIdentifiers(final String schema
java.nio.file.Path styleSheet = dataManager.getSchemaDir(schema).resolve(EXTRACT_DATASETS);

List<Element> datasetsEl = Xml.transform(md, styleSheet).getChildren();
List<String> datasets = new ArrayList<String>();
List<String> datasets = new ArrayList<>();

//--- needed to detach md from the document
md.detach();
Expand Down Expand Up @@ -190,7 +200,7 @@ public static Map<String, String> retrieveServiceMetadataWithAtomFeed(final Data
final String atomProtocol)
throws Exception {

List<Metadata> iso19139MetadataList = new ArrayList<Metadata>();
List<Metadata> iso19139MetadataList = new ArrayList<>();
iso19139MetadataList.add(iso19139Metadata);

return retrieveServiceMetadataWithAtomFeeds(dataManager, iso19139MetadataList, atomProtocol);
Expand Down Expand Up @@ -243,7 +253,7 @@ public static String extractAtomFeedUrl(final String schema,
throws Exception {
java.nio.file.Path styleSheet = dataManager.getSchemaDir(schema).resolve(EXTRACT_ATOM_FEED);

Map<String, Object> params = new HashMap<String, Object>();
Map<String, Object> params = new HashMap<>();
params.put("atomProtocol", atomProtocol);

String atomFeed = Xml.transform(md, styleSheet, params).getText().trim();
Expand All @@ -269,10 +279,10 @@ public static List<Metadata> searchMetadataByType(ServiceContext context,
searcher.search(context, request, new ServiceConfig());

Map<Integer, Metadata> allMdInfo = ((LuceneSearcher) searcher).getAllMdInfo(context, searcher.getSize());
return new ArrayList<Metadata>(allMdInfo.values());
return new ArrayList<>(allMdInfo.values());
} catch (Exception ex) {
ex.printStackTrace();
return new ArrayList<Metadata>();
return new ArrayList<>();
} finally {
if (searcher != null) searcher.close();
}
Expand Down Expand Up @@ -305,4 +315,100 @@ public static String retrieveDatasetUuidFromIdentifier(ServiceContext context,

return uuid;
}

public static Element prepareServiceFeedEltBeforeTransform(final String schema,
final Element md,
final DataManager dataManager)
throws Exception {

List<String> datasetsUuids = extractRelatedDatasetsIdentifiers(schema, md, dataManager);
Element root = new Element("root");
Element serviceElt = new Element("service");
Element datasetElt = new Element("datasets");

root.addContent(serviceElt);
md.addContent(datasetElt);

for(String uuid : datasetsUuids) {
String id = dataManager.getMetadataId(uuid);

This comment has been minimized.

Copy link
@fxprunayre

fxprunayre Nov 26, 2019

Member

That is not super safe - you are not checking that the dataset attached to the service is published to current user.

if (StringUtils.isEmpty(id))
throw new MetadataNotFoundEx(uuid);
Element ds = dataManager.getMetadata(id);
datasetElt.addContent(ds);
}
serviceElt.addContent(md);

return root;
}

public static Element prepareDatasetFeedEltBeforeTransform(
final Element md,
final String serviceMdUuid)
throws Exception {

Document doc = new Document(new Element("root"));
doc.getRootElement().addContent(new Element("dataset").addContent(md));
doc.getRootElement().addContent(new Element("serviceIdentifier").setText(serviceMdUuid));

return doc.getRootElement();
}


public static String convertIso19119ToAtomFeed(final String schema,
final Element md,
final DataManager dataManager,
final boolean isLocal) throws Exception {

java.nio.file.Path styleSheet = dataManager
.getSchemaDir(schema)
.resolve("convert/ATOM/")
.resolve(TRANSFORM_MD_TO_ATOM_FEED);

Map<String,Object> params = new HashMap<>();
params.put("isLocal", isLocal);

Element atomFeed = Xml.transform(md, styleSheet, params);
md.detach();
return Xml.getString(atomFeed);

}

/**
* Converts a dataset MD into an INSPIRE atom feed.
*
* @param schema the target schema (mainly iso19139)
* @param md The document on which the XSL should be applied, the following format should be followed:
*
* <root>
* <dataset>
* <gmd:MD_Metadata />
* </dataset>
* <serviceIdentifier>[Service Metadata UUID]</serviceIdentifier>
* ...
* </root>
*
* @param dataManager
* @param params extra parameters to pass to the XSL transformation, see inspire-atom-feed.xsl for the details:
* <xsl:param name="isLocal" select="true()" />
* <xsl:param name="serviceFeedTitle" select="string('The parent service feed')" />
*
* @return the ATOM feed as a JDOM element.
* @throws Exception
*
* See InspireAtomUtilTest.testLocalDatasetTransform() for an example of calling this method.
*/
public static Element convertDatasetMdToAtom(final String schema, final Element md, final DataManager dataManager,
Map<String,Object> params) throws Exception {

Path styleSheet = getAtomFeedXSLStylesheet(schema, dataManager);
return Xml.transform(md, styleSheet, params);
}

private static Path getAtomFeedXSLStylesheet(final String schema, final DataManager dataManager) {
return dataManager
.getSchemaDir(schema)
.resolve("convert/ATOM/")
.resolve(TRANSFORM_MD_TO_ATOM_FEED);
}
}

Loading

0 comments on commit bfb98ad

Please sign in to comment.