Skip to content

Commit

Permalink
[BP] Update skin.xsl so that it works better in nojs mode. (#7015)
Browse files Browse the repository at this point in the history
* Update skin so that it works better in nojs mode.
- Added missing translation
- Check authentication before displaying login link
- Use node configurations
- Fix issue with base url for url like /geonetwork/srv/api/records/{uuid}?language=eng

* Update web/src/main/webapp/xslt/skin/default/skin.xsl

Co-authored-by: Jose García <josegar74@gmail.com>

* Added logging and sample data that causes issues.

* If no configuration has been setup then /root/request/ui will be null. In this case use defaults identified from CatController.js - defaultConfig

* Update typo in comments

---------

Co-authored-by: Jose García <josegar74@gmail.com>
Co-authored-by: Ian <ian.allen@dfo.mpo.gc.ca>
  • Loading branch information
3 people committed Jul 21, 2023
1 parent a033ef0 commit 855438e
Show file tree
Hide file tree
Showing 5 changed files with 264 additions and 109 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -431,14 +431,23 @@ String getSiteURL(String language) {
public
@Nonnull
String getNodeURL() {
return getBaseURL() + getNodeId() + "/";
}

/**
* Return node id - i.e. srv
*/
public
@Nonnull
String getNodeId() {
String nodeId = NodeInfo.DEFAULT_NODE;
try {
NodeInfo node = ApplicationContextHolder.get().getBean(NodeInfo.class);
if (node != null) {
nodeId = node.getId();
}
} catch (Exception e) {}
return getBaseURL() + nodeId + "/";
return nodeId;
}
/**
* Return complete node URL eg. http://localhost:8080/geonetwork/
Expand Down
18 changes: 16 additions & 2 deletions core/src/main/java/org/fao/geonet/util/XslUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,13 @@
import org.owasp.esapi.errors.EncodingException;
import org.owasp.esapi.reference.DefaultEncoder;
import org.springframework.beans.factory.BeanCreationException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.BeanFactoryAnnotationUtils;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.http.HttpStatus;
import org.springframework.security.authentication.AnonymousAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.w3c.dom.Node;
import org.xml.sax.SAXException;

Expand Down Expand Up @@ -533,7 +535,19 @@ public static Node downloadJsonAsXML(String url) {
return null;
}

/**
/**
* Check if user is authenticated.
*/
public static boolean isAuthenticated() throws Exception {
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
if (authentication == null || AnonymousAuthenticationToken.class.isAssignableFrom(authentication.getClass())) {
return false;
}
return authentication.isAuthenticated();
}


/**
* Check if security provider require login form
*/
public static boolean isDisableLoginForm() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,24 @@
package org.fao.geonet.api.records.formatters;

import org.fao.geonet.ApplicationContextHolder;
import org.fao.geonet.constants.Geonet;
import org.fao.geonet.kernel.SchemaManager;
import org.fao.geonet.kernel.search.JSONLocCacheLoader;
import org.fao.geonet.kernel.setting.SettingManager;
import org.fao.geonet.utils.Log;
import org.fao.geonet.utils.Xml;
import org.jdom.Element;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.stereotype.Component;

import java.util.*;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import static org.fao.geonet.api.records.formatters.SchemaLocalizations.loadSchemaLocalizations;

Expand All @@ -51,6 +61,16 @@
@Component
public class XsltFormatter implements FormatterImpl {

private final Map<String, Element> translationElements = new HashMap<>();

private final ConfigurableApplicationContext configurableApplicationContext;

@Autowired
public XsltFormatter(ConfigurableApplicationContext configurableApplicationContext) {
this.configurableApplicationContext = configurableApplicationContext;
}


/**
* @param schema Use all to return all schemas translations
* @param schemasToLoadList
Expand Down Expand Up @@ -97,16 +117,41 @@ public String format(FormatterParams fparams) throws Exception {

root.addContent(new Element("lang").setText(fparams.context.getLanguage()));
root.addContent(new Element("url").setText(fparams.url));
// FIXME: This is a hack to mimic what Jeeves service are doing.
// Some XSLT are used by both formatters and Jeeves and Spring MVC services
Element translations = new Element("translations");

if (!translationElements.containsKey(fparams.context.getLanguage())) {
// Get translation keys and add them to the cache
Element translations = new Element("translations");
Map<String, String> translationMap = new JSONLocCacheLoader(configurableApplicationContext, fparams.context.getLanguage()).call();
for (Map.Entry<String, String> entry : translationMap.entrySet()) {
// Attempt to only use name that are valid element names.
// https://www.w3.org/TR/REC-xml/#NT-Name
// https://www.w3.org/TR/REC-xml/#NT-NameStartChar
// Skip keys that are not alphanumeric only including "." - otherwise certain chars like ':?+...' can cause problem when creating element as they are invalid element names
// i.e. some properties look like the following
// "cron-0 0 12 * * ?": "Fire at 12pm (noon) every day"
// "system/feedback"="Feedback"

if (entry.getKey().matches("[a-zA-Z0-9\\.]+")) {
try {
translations.addContent(new Element(entry.getKey()).setText(entry.getValue()));
} catch (Exception e) {
// If errors are generated here then it may mean that the regular expression needs to be updated.
Log.error(Geonet.GEONETWORK, "Failed to add translation key for \"" + entry.getKey() + "\"=\"" + entry.getValue() + "\". " + e.getMessage());
}
}
}

translationElements.put(fparams.context.getLanguage(), translations);
}
root.addContent((Element)translationElements.get(fparams.context.getLanguage()).clone());
Element gui = new Element("gui");
String baseUrl = settingManager.getBaseURL();
String context = baseUrl.replace(settingManager.getServerURL(), "");
gui.addContent(new Element("url").setText(
context.substring(0, context.length() - 1)
));
gui.addContent(new Element("nodeUrl").setText(settingManager.getNodeURL()));
gui.addContent(new Element("nodeId").setText(settingManager.getNodeId()));
gui.addContent(new Element("baseUrl").setText(baseUrl));
gui.addContent(new Element("serverUrl").setText(settingManager.getServerURL()));
gui.addContent(new Element("language").setText(fparams.context.getLanguage()));
Expand Down
1 change: 1 addition & 0 deletions web/src/main/webapp/xslt/common/render-html.xsl
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
<title><xsl:value-of select="if($title != '')
then $title
else /root/gui/systemConfig/settings/system/site/name"/></title>
<base href="{$nodeUrl}eng/catalog.search"/>
<meta charset="utf-8"/>

<xsl:copy-of select="$meta"/>
Expand Down
Loading

0 comments on commit 855438e

Please sign in to comment.