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 @@ -429,15 +429,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) {}
String locServ = getBaseURL() + nodeId + "/";
return locServ;
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 @@ -509,7 +511,19 @@ public static Node downloadJsonAsXML(String url) {
return null;
}

/**
/**
* Check is user is currently 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 @@ -25,13 +25,21 @@

import org.fao.geonet.ApplicationContextHolder;
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.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 +59,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 +115,33 @@ 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()) {
// Skip keys that are not alpha numeric only including "." - otherwhise chars like : ? +... cause problem when creating element.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you indicate some translation including such values in the keys?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated comments to show some sample keys that cause issues.

if (entry.getKey().matches("[a-zA-Z0-9\\.]+")) {
try {
translations.addContent(new Element(entry.getKey()).setText(entry.getValue()));
} catch (Exception e) {
System.out.println("Failed to add translation key for \"" + entry.getKey() + "\"=\"" + entry.getValue() + "\". " + e.getMessage());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be changed to log the exception.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed to log the exception

}
}
}

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