Skip to content

Commit

Permalink
Merge pull request #2672 from pks-1981/accessControl
Browse files Browse the repository at this point in the history
Remove deprecated java.security.AccessControl
  • Loading branch information
dieppa authored Apr 9, 2024
2 parents 993aec2 + 0c54825 commit e7cf364
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 59 deletions.
50 changes: 5 additions & 45 deletions src/main/java/com/github/tomakehurst/wiremock/common/xml/Xml.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,17 @@
import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;
import java.security.AccessController;
import java.security.PrivilegedAction;
import javax.xml.XMLConstants;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.xpath.XPathFactory;
import org.custommonkey.xmlunit.XMLUnit;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.xml.sax.EntityResolver;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
Expand All @@ -54,28 +50,18 @@ public static void optimizeFactoriesLoading() {
String transformerFactoryImpl = TransformerFactory.newInstance().getClass().getName();
String xPathFactoryImpl = XPathFactory.newInstance().getClass().getName();

setProperty(TransformerFactory.class.getName(), transformerFactoryImpl);
setProperty(
System.setProperty(TransformerFactory.class.getName(), transformerFactoryImpl);
System.setProperty(
XPathFactory.DEFAULT_PROPERTY_NAME + ":" + XPathFactory.DEFAULT_OBJECT_MODEL_URI,
xPathFactoryImpl);

XMLUnit.setTransformerFactory(transformerFactoryImpl);
XMLUnit.setXPathFactory(xPathFactoryImpl);
} catch (Throwable ignored) {
} catch (Exception ignored) {
// Since this is just an optimisation, if an exception is thrown we do nothing and carry on
}
}

private static String setProperty(final String name, final String value) {
return AccessController.doPrivileged(
new PrivilegedAction<String>() {
@Override
public String run() {
return System.setProperty(name, value);
}
});
}

public static String prettyPrint(String xml) {
try {
return prettyPrint(read(xml));
Expand Down Expand Up @@ -131,31 +117,6 @@ public static Document read(String xml) {
}
}

public static String toStringValue(Node node) {
switch (node.getNodeType()) {
case Node.TEXT_NODE:
case Node.ATTRIBUTE_NODE:
return node.getTextContent();
case Node.ELEMENT_NODE:
return render(node);
default:
return node.toString();
}
}

private static String render(Node node) {
try {
StringWriter sw = new StringWriter();
Transformer transformer = createTransformerFactory().newTransformer();
transformer.setOutputProperty(OMIT_XML_DECLARATION, "yes");
transformer.setOutputProperty(INDENT, "yes");
transformer.transform(new DOMSource(node), new StreamResult(sw));
return sw.toString();
} catch (TransformerException e) {
return throwUnchecked(e, String.class);
}
}

public static XmlDocument parse(String xml) {
try {
InputSource source = new InputSource(new StringReader(xml));
Expand Down Expand Up @@ -213,14 +174,13 @@ private static class SkipResolvingEntitiesDocumentBuilderFactory extends Documen
});

@Override
public DocumentBuilder newDocumentBuilder() throws ParserConfigurationException {
public DocumentBuilder newDocumentBuilder() {
return DB_CACHE.get();
}

private static class ResolveToEmptyString implements EntityResolver {
@Override
public InputSource resolveEntity(String publicId, String systemId)
throws SAXException, IOException {
public InputSource resolveEntity(String publicId, String systemId) {
return new InputSource(new StringReader(""));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

import com.github.jknack.handlebars.Options;
import com.github.tomakehurst.wiremock.extension.responsetemplating.SystemKeyAuthoriser;
import java.security.AccessControlException;

public class SystemValueHelper extends HandlebarsHelper<Object> {

Expand All @@ -43,20 +42,15 @@ public String apply(Object context, Options options) {

String rawValue = "";

try {
switch (type) {
case "ENVIRONMENT":
rawValue = getSystemEnvironment(key, defaultValue);
break;
case "PROPERTY":
rawValue = getSystemProperties(key, defaultValue);
break;
}
return rawValue;

} catch (AccessControlException e) {
return this.handleError("Access to " + key + " is denied");
switch (type) {
case "ENVIRONMENT":
rawValue = getSystemEnvironment(key, defaultValue);
break;
case "PROPERTY":
rawValue = getSystemProperties(key, defaultValue);
break;
}
return rawValue;
}

private String getSystemEnvironment(final String key, final String defaultValue) {
Expand Down

0 comments on commit e7cf364

Please sign in to comment.