Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update skin.xsl so that it works better in nojs mode. #7015

Merged
merged 8 commits into from
Jul 21, 2023
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 @@ -520,7 +522,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