Skip to content

Commit 15b836f

Browse files
MajoBergermilanmajchrak
authored andcommitted
prevent empty argument errors (#655)
Some xsl crosswalks call functions that expect parameters, but for various reasons, parameters are empty. That is now checked and logged.
1 parent 3242faf commit 15b836f

4 files changed

Lines changed: 61 additions & 10 deletions

File tree

dspace-oai/src/main/java/org/dspace/xoai/services/impl/resources/functions/LogMissingFn.java

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import net.sf.saxon.s9api.SequenceType;
2121
import net.sf.saxon.s9api.XdmAtomicValue;
2222
import net.sf.saxon.s9api.XdmValue;
23+
import org.apache.logging.log4j.Logger;
2324
import org.bouncycastle.util.Arrays;
2425
import org.dspace.utils.XslLogUtil;
2526

@@ -30,6 +31,7 @@
3031
*/
3132
public class LogMissingFn implements ExtensionFunction {
3233

34+
private static final Logger log = org.apache.logging.log4j.LogManager.getLogger(LogMissingFn.class);
3335
@Override
3436
public QName getName() {
3537
return new QName(BASE, "logMissing");
@@ -53,8 +55,27 @@ public XdmValue call(XdmValue[] xdmValues) throws SaxonApiException {
5355
if (Objects.isNull(xdmValues) || Arrays.isNullOrContainsNull(xdmValues)) {
5456
return new XdmAtomicValue("");
5557
}
56-
return new XdmAtomicValue(checks(XslLogUtil.logMissing(xdmValues[0].itemAt(0).getStringValue(),
57-
xdmValues[1].itemAt(0).getStringValue())));
58+
59+
String val0;
60+
try {
61+
val0 = xdmValues[0].itemAt(0).getStringValue();
62+
} catch (Exception e) {
63+
// e.g. when no parameter is passed and xdmValues[0] ends with index error
64+
log.warn("Empty value to call of function LogMissingFn in the first argument");
65+
val0 = "";
66+
}
67+
68+
String val1;
69+
try {
70+
val1 = xdmValues[1].itemAt(0).getStringValue();
71+
} catch (Exception e) {
72+
// e.g. when no parameter is passed and xdmValues[0] ends with index error
73+
log.warn("Empty value to call of function LogMissingFn in the second argument");
74+
val1 = "";
75+
}
76+
77+
78+
return new XdmAtomicValue(checks(XslLogUtil.logMissing(val0,val1)));
5879
}
5980

6081
private String checks(String got) {

dspace-oai/src/main/java/org/dspace/xoai/services/impl/resources/functions/NodeListXslFunction.java

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99
package org.dspace.xoai.services.impl.resources.functions;
1010

11-
import static org.apache.logging.log4j.LogManager.getLogger;
1211
import static org.dspace.xoai.services.impl.resources.functions.StringXSLFunction.BASE;
1312

1413
import java.util.Objects;
@@ -38,12 +37,10 @@
3837
*/
3938
public abstract class NodeListXslFunction implements ExtensionFunction {
4039

40+
private static final Logger log = org.apache.logging.log4j.LogManager.getLogger(NodeListXslFunction.class);
4141
protected abstract String getFnName();
4242

4343
protected abstract NodeList getNodeList(String param);
44-
45-
private static final Logger log = getLogger(NodeListXslFunction.class);
46-
4744
@Override
4845
final public QName getName() {
4946
return new QName(BASE, getFnName());
@@ -58,7 +55,7 @@ final public SequenceType getResultType() {
5855
final public SequenceType[] getArgumentTypes() {
5956
return new SequenceType[]{
6057
SequenceType.makeSequenceType(
61-
ItemType.STRING, OccurrenceIndicator.ONE)};
58+
ItemType.STRING, OccurrenceIndicator.ZERO_OR_MORE)};
6259
}
6360

6461
@Override
@@ -67,7 +64,17 @@ final public XdmValue call(XdmValue[] xdmValues) throws SaxonApiException {
6764
return new XdmAtomicValue("");
6865
}
6966

70-
NodeList nodeList = getNodeList(xdmValues[0].itemAt(0).getStringValue());
67+
String val;
68+
try {
69+
val = xdmValues[0].itemAt(0).getStringValue();
70+
} catch (Exception e) {
71+
// e.g. when no parameter is passed and xdmValues[0] ends with index error
72+
log.warn("Empty value in call of function of NodeListXslFunction type");
73+
val = "";
74+
}
75+
76+
77+
NodeList nodeList = getNodeList(val);
7178
Node oneNode = nodeList.item(0);
7279

7380
DocumentBuilder db = new Processor(false).newDocumentBuilder();

dspace-oai/src/main/java/org/dspace/xoai/services/impl/resources/functions/NodeXslFunction.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import net.sf.saxon.s9api.SequenceType;
2626
import net.sf.saxon.s9api.XdmAtomicValue;
2727
import net.sf.saxon.s9api.XdmValue;
28+
import org.apache.logging.log4j.Logger;
2829
import org.bouncycastle.util.Arrays;
2930
import org.w3c.dom.Node;
3031

@@ -35,6 +36,7 @@
3536
*/
3637
public abstract class NodeXslFunction implements ExtensionFunction {
3738

39+
private static final Logger log = org.apache.logging.log4j.LogManager.getLogger(NodeXslFunction.class);
3840
protected abstract String getFnName();
3941

4042
protected abstract Node getNode(String param);
@@ -61,7 +63,16 @@ final public XdmValue call(XdmValue[] xdmValues) throws SaxonApiException {
6163
if (Objects.isNull(xdmValues) || Arrays.isNullOrContainsNull(xdmValues)) {
6264
return new XdmAtomicValue("");
6365
}
64-
Node node = getNode(xdmValues[0].itemAt(0).getStringValue());
66+
String val;
67+
try {
68+
val = xdmValues[0].itemAt(0).getStringValue();
69+
} catch (Exception e) {
70+
// e.g. when no parameter is passed and xdmValues[0] ends with index error
71+
log.warn("Empty value in call of function of NodeXslFunction type");
72+
val = "";
73+
}
74+
75+
Node node = getNode(val);
6576
if (Objects.isNull(node)) {
6677
try {
6778
node = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();

dspace-oai/src/main/java/org/dspace/xoai/services/impl/resources/functions/StringXSLFunction.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import net.sf.saxon.s9api.SequenceType;
1919
import net.sf.saxon.s9api.XdmAtomicValue;
2020
import net.sf.saxon.s9api.XdmValue;
21+
import org.apache.logging.log4j.Logger;
2122
import org.bouncycastle.util.Arrays;
2223

2324

@@ -28,6 +29,7 @@
2829
*/
2930
public abstract class StringXSLFunction implements ExtensionFunction {
3031

32+
private static final Logger log = org.apache.logging.log4j.LogManager.getLogger(StringXSLFunction.class);
3133
public static final String BASE = "http://custom.crosswalk.functions";
3234

3335
protected String uncertainString(Object val) {
@@ -63,7 +65,17 @@ final public XdmValue call(XdmValue[] xdmValues) throws SaxonApiException {
6365
if (Objects.isNull(xdmValues) || Arrays.isNullOrContainsNull(xdmValues)) {
6466
return new XdmAtomicValue("");
6567
}
66-
return new XdmAtomicValue(checks(getStringResult(xdmValues[0].itemAt(0).getStringValue())));
68+
69+
String val;
70+
try {
71+
val = xdmValues[0].itemAt(0).getStringValue();
72+
} catch (Exception e) {
73+
// e.g. when no parameter is passed and xdmValues[0] ends with index error
74+
log.warn("Empty value in call of function of StringXslFunction type");
75+
val = "";
76+
}
77+
78+
return new XdmAtomicValue(checks(getStringResult(val)));
6779
}
6880

6981
private String checks(String got) {

0 commit comments

Comments
 (0)