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

MCR-3339 Ensure usage of MCRResourceResolver with custom PMD rule #2424

Open
wants to merge 1 commit into
base: 2024.06.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public static String getVersion() {
return VERSION;
}

@SuppressWarnings("PMD.MCR.ResourceResolver")
private static Properties loadVersionProperties() {
Properties props = new Properties();
URL gitPropURL = MCRCoreVersion.class.getResource("/org/mycore/git.properties");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
* @since 2013.12
* @see MCRRuntimeComponentDetector
*/
@SuppressWarnings("PMD.MCR.ResourceResolver")
public class MCRComponent implements Comparable<MCRComponent> {

private static final Logger LOGGER = LogManager.getLogger();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
* @author Robert Stephan
* @since 2013.12
*/
@SuppressWarnings("PMD.MCR.ResourceResolver")
public class MCRConfigurationInputStream extends InputStream {

private static final String MYCORE_PROPERTIES = "mycore.properties";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
* @author Thomas Scheffler (yagee)
* @since 2013.12
*/
@SuppressWarnings("PMD.MCR.ResourceResolver")
public class MCRDefaultConfigurationLoader implements MCRConfigurationLoader {

MCRProperties properties;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
* @author Thomas Scheffler (yagee)
* @since 2013.12
*/
@SuppressWarnings("PMD.MCR.ResourceResolver")
public class MCRRuntimeComponentDetector {

private static final Logger LOGGER = LogManager.getLogger();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
/**
* @author Thomas Scheffler (yagee)
*/
@SuppressWarnings("PMD.MCR.ResourceResolver")
public class MCRServletContainerInitializer implements ServletContainerInitializer {

/* (non-Javadoc)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
* @author Thomas Scheffler (yagee)
* @since 2013.10
*/
@SuppressWarnings("PMD.MCR.ResourceResolver")
public final class MCREntityResolver implements EntityResolver2, LSResourceResolver {

private static final Logger LOGGER = LogManager.getLogger();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
*
* @author Frank Lützenkirchen
*/
@SuppressWarnings("PMD.MCR.ResourceResolver")
public final class MCRXSLInfoServlet extends MCRServlet {

@Serial
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -451,16 +451,16 @@ private void updateConditionsValueAttribute(Element rootbool) {
}

private Document loadDefaultAclDocument() throws IOException, JDOMException {
String resourcetype = "/editor_default_acls_" + id.getTypeId() + ".xml";
String resourcebase = "/editor_default_acls_" + id.getBase() + ".xml";
InputStream aclxml = MCREditorOutValidator.class.getResourceAsStream(resourcebase);
String resourcetype = "editor_default_acls_" + id.getTypeId() + ".xml";
String resourcebase = "editor_default_acls_" + id.getBase() + ".xml";
InputStream aclxml = MCRResourceHelper.getResourceAsStream(resourcebase);
if (aclxml == null) {
aclxml = MCREditorOutValidator.class.getResourceAsStream(resourcetype);
aclxml = MCRResourceHelper.getResourceAsStream(resourcetype);
if (aclxml == null) {
LOGGER.warn("Can't find default object ACL file {} or {}",
() -> resourcebase.substring(1), () -> resourcetype.substring(1));
String resource = "/editor_default_acls.xml"; // fallback
aclxml = MCREditorOutValidator.class.getResourceAsStream(resource);
resourcebase, resourcetype);
String resource = "editor_default_acls.xml"; // fallback
aclxml = MCRResourceHelper.getResourceAsStream(resource);
if (aclxml == null) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@
import org.jdom2.input.SAXBuilder;
import org.jdom2.output.Format;
import org.jdom2.output.XMLOutputter;
import org.mycore.common.MCRClassTools;
import org.mycore.common.config.MCRComponent;
import org.mycore.common.config.MCRConfiguration2;
import org.mycore.common.config.MCRConfigurationDir;
Expand All @@ -54,6 +53,7 @@
import org.mycore.common.xml.MCRXMLParserFactory;
import org.mycore.frontend.cli.annotation.MCRCommand;
import org.mycore.frontend.cli.annotation.MCRCommandGroup;
import org.mycore.resource.MCRResourceHelper;

/**
* This class contains the basic commands for MyCoRe Command Line and WebCLI.
Expand Down Expand Up @@ -224,7 +224,6 @@ private static boolean createDirectory(File directory) {
}

private static void createSampleConfigFile(String path) throws IOException {
ClassLoader classLoader = MCRClassTools.getClassLoader();
Path configurationDirectory = MCRConfigurationDir.getConfigurationDirectory().toPath();
Path targetFile = configurationDirectory.resolve(path);
if (Files.exists(targetFile)) {
Expand All @@ -237,7 +236,7 @@ private static void createSampleConfigFile(String path) throws IOException {
throw new IOException("Could not create directory for file: " + targetFile);
}
}
try (InputStream templateResource = classLoader.getResourceAsStream("configdir.template/" + path)) {
try (InputStream templateResource = MCRResourceHelper.getResourceAsStream("configdir.template/" + path)) {
if (templateResource == null) {
throw new IOException("Could not find template for " + path);
}
Expand Down Expand Up @@ -301,6 +300,7 @@ private static boolean updatePersistenceH2JdbcUrl(Document persistenceDoc) {
* @return true, if the mappings changed
* @throws IOException
*/
@SuppressWarnings("PMD.MCR.ResourceResolver")
private static boolean updatePersistenceMappings(Document persistenceDoc) throws IOException {
Namespace nsPersistence = persistenceDoc.getRootElement().getNamespace();
Element ePersistenceUnit = persistenceDoc.getRootElement().getChild("persistence-unit", nsPersistence);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@

import org.apache.commons.text.StringSubstitutor;
import org.jdom2.Element;
import org.mycore.common.MCRClassTools;
import org.mycore.common.MCRSession;
import org.mycore.common.MCRSessionMgr;
import org.mycore.common.MCRSystemUserInformation;
Expand All @@ -51,6 +50,7 @@
import org.mycore.common.content.MCRJDOMContent;
import org.mycore.common.events.MCRStartupHandler;
import org.mycore.common.xml.MCRURIResolver;
import org.mycore.resource.MCRResourceHelper;

/**
* The main class implementing the MyCoRe command line interface. With the
Expand Down Expand Up @@ -334,7 +334,7 @@ public static List<String> readCommandsFile(String file) throws IOException {
}

public static List<String> readCommandsRessource(String resource) throws IOException {
final URL resourceURL = MCRClassTools.getClassLoader().getResource(resource);
final URL resourceURL = MCRResourceHelper.getResourceUrl(resource);
if (resourceURL == null) {
throw new IOException("Resource URL is null!");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ private void redirectToCreateObject(HttpServletRequest request, HttpServletRespo
sb.append('-').append(layout);
}
String baseName = sb.toString();
String form = MCRPersistenceHelper.getWebPage(getServletContext(), baseName + ".xed", baseName + ".xml");
String form = MCRPersistenceHelper.getWebPage(baseName + ".xed", baseName + ".xml");
Properties params = new Properties();
params.put("cancelUrl", MCRPersistenceHelper.getCancelUrl(request));
params.put("mcrid", objectID.toString());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

import java.io.IOException;
import java.io.StringWriter;
import java.net.MalformedURLException;
import java.util.Map;
import java.util.Properties;
import java.util.stream.Collectors;
Expand All @@ -37,6 +36,7 @@
import org.mycore.datamodel.metadata.validator.MCREditorOutValidator;
import org.mycore.frontend.MCRFrontendUtil;
import org.mycore.frontend.servlets.MCRServlet;
import org.mycore.resource.MCRResourceHelper;

import jakarta.servlet.ServletContext;
import jakarta.servlet.ServletException;
Expand Down Expand Up @@ -130,18 +130,17 @@ static MCRObject getMCRObject(Document doc) throws JDOMException, IOException, M
return new MCRObject(validMyCoReObject);
}

protected static String getWebPage(ServletContext context, String modernPage, String deprecatedPage)
throws ServletException {
try {
if (context.getResource("/" + modernPage) == null
&& context.getResource("/" + deprecatedPage) != null) {
LogManager.getLogger()
.warn("Could not find {} in webapp root, using deprecated {} instead.", modernPage,
deprecatedPage);
return deprecatedPage;
}
} catch (MalformedURLException e) {
throw new ServletException(e);
@Deprecated
protected static String getWebPage(ServletContext context, String modernPage, String deprecatedPage) {
return getWebPage(modernPage, deprecatedPage);
}

protected static String getWebPage(String modernPage, String deprecatedPage) {
if (MCRResourceHelper.getWebResourceUrl(modernPage) == null
&& MCRResourceHelper.getWebResourceUrl(deprecatedPage) != null) {
LOGGER.warn("Could not find {} in webapp root, using deprecated {} instead.",
modernPage, deprecatedPage);
return deprecatedPage;
}
return modernPage; //even if it does not exist: nice 404 helps the user
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ abstract class MCRPersistenceServlet extends MCRServlet {
public void init() throws ServletException {
super.init();
String configuredPage = MCRConfiguration2.getStringOrThrow("MCR.FileUpload.WebPage");
uploadPage = MCRPersistenceHelper.getWebPage(getServletContext(), configuredPage, "fileupload_commit.xml");
uploadPage = MCRPersistenceHelper.getWebPage(configuredPage, "fileupload_commit.xml");
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ private void redirectToUpdateDerivate(HttpServletRequest request, HttpServletRes
Properties params = new Properties();
params.put("sourceUri", "xslStyle:mycorederivate-editor:mcrobject:" + derivateID);
params.put("cancelUrl", MCRPersistenceHelper.getCancelUrl(request));
String page = MCRPersistenceHelper.getWebPage(getServletContext(), "editor_form_derivate.xed",
String page = MCRPersistenceHelper.getWebPage("editor_form_derivate.xed",
"editor_form_derivate.xml");
String redirectURL = MCRFrontendUtil.getBaseURL() + page;
response
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
* [...].Class=org.mycore.resource.locator.MCRClassLoaderResourceLocator
* </pre>
*/
@SuppressWarnings("PMD.MCR.ResourceResolver")
@MCRConfigurationProxy(proxyClass = MCRClassLoaderResourceLocator.Factory.class)
public class MCRClassLoaderResourceLocator extends MCRResourceLocatorBase {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
* [...].Class=org.mycore.resource.locator.MCRServletContextResourceLocator
* </pre>
*/
@SuppressWarnings("PMD.MCR.ResourceResolver")
@MCRConfigurationProxy(proxyClass = MCRServletContextResourceLocator.Factory.class)
public class MCRServletContextResourceLocator extends MCRResourceLocatorBase {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
* [...].Coverage=Lorem ipsum dolor sit amet
* </pre>
*/
@SuppressWarnings("PMD.MCR.ResourceResolver")
@MCRConfigurationProxy(proxyClass = MCRClassLoaderResourceProvider.Factory.class)
public class MCRClassLoaderResourceProvider extends MCRResourceProviderBase {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,7 @@ static boolean isArray(String masked) {
return false;
}

@SuppressWarnings("PMD.MCR.ResourceResolver")
static Properties loadProperties() {
Properties deprecatedMapping = new Properties();
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import org.jdom2.Text;
import org.jdom2.input.SAXBuilder;
import org.mycore.frontend.servlets.MCRServlet;
import org.mycore.resource.MCRResourceHelper;
import org.xml.sax.InputSource;

/**
Expand All @@ -56,6 +57,7 @@
* @author shermann
* @author Matthias Eichner
*/
@SuppressWarnings("PMD.MCR.ResourceResolver")
public class MyCoReWebPageProvider {

/** German language key */
Expand Down Expand Up @@ -112,7 +114,7 @@ public Element addSection(String title, String xmlAsString, String lang) throws
SAXBuilder saxBuilder = new SAXBuilder();
saxBuilder.setEntityResolver((publicId, systemId) -> {
String resource = systemId.substring(systemId.lastIndexOf('/'));
InputStream is = getClass().getResourceAsStream(resource);
InputStream is = MCRResourceHelper.getResourceAsStream(resource);
if (is == null) {
throw new IOException(new FileNotFoundException("Unable to locate resource " + resource));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public class MCRFoFormatterFOP implements MCRFoFormatterInterface {
final ResourceResolver resolver = new ResourceResolver() {
@Override
public OutputStream getOutputStream(URI uri) throws IOException {
URL url = MCRURIResolver.getServletContext().getResource(uri.toString());
URL url = MCRResourceHelper.getResourceUrl(uri.toString());
return url.openConnection().getOutputStream();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import org.jdom2.input.SAXBuilder;
import org.jdom2.output.XMLOutputter;
import org.mycore.access.MCRAccessManager;
import org.mycore.common.MCRClassTools;
import org.mycore.common.MCRException;
import org.mycore.common.config.MCRConfiguration2;
import org.mycore.datamodel.metadata.MCRMetadataManager;
Expand All @@ -44,6 +43,7 @@
import org.mycore.mets.model.converter.MCRXMLSimpleModelConverter;
import org.mycore.mets.model.simple.MCRMetsSimpleModel;
import org.mycore.mets.tools.MCRMetsLock;
import org.mycore.resource.MCRResourceHelper;

import jakarta.ws.rs.Consumes;
import jakarta.ws.rs.DELETE;
Expand Down Expand Up @@ -72,7 +72,7 @@ public String startEditor(@PathParam(DERIVATE_ID) String derivateId) {
checkDerivateExists(derivateIdObject);
checkDerivateAccess(derivateIdObject, MCRAccessManager.PERMISSION_WRITE);

try (InputStream resourceAsStream = MCRClassTools.getClassLoader().getResourceAsStream("mets-editor.html")) {
try (InputStream resourceAsStream = MCRResourceHelper.getResourceAsStream("mets-editor.html")) {
String htmlTemplate = new String(resourceAsStream.readAllBytes(), StandardCharsets.UTF_8);
// add additional javascript code
String js = MCRConfiguration2.getString("MCR.Mets.Editor.additional.javascript").orElse(null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.jdom2.Element;
import org.jdom2.input.SAXBuilder;
import org.mycore.common.config.MCRConfiguration2;
import org.mycore.resource.MCRResourceHelper;

/**
* Interface to the command line utility <a href="https://pandoc.org">Pandoc</a>,
Expand All @@ -45,7 +46,7 @@ public class MCRPandocAPI {
private static final int TIMEOUT = MCRConfiguration2.getInt("MCR.Pandoc.Timeout").orElse(5);

private static final String LUA_PATH = MCRConfiguration2.getString("MCR.Pandoc.LuaPath")
.orElse(Thread.currentThread().getContextClassLoader().getResource("lua").getPath() + "?.lua");
.orElse(MCRResourceHelper.getResourceUrl("lua").getPath() + "?.lua");

private enum Action {
READER("Reader"), WRITER("Writer");
Expand Down Expand Up @@ -122,7 +123,7 @@ private static String convertFormatToPath(Action action, String format) {
String path = MCRConfiguration2.getString("MCR.Pandoc." + action + "." + format + ".Path").orElse("");
if (!property.isEmpty()) {
if (path.isEmpty()) {
return Thread.currentThread().getContextClassLoader().getResource(property).getFile();
return MCRResourceHelper.getResourceUrl(property).getFile();
} else {
return Paths.get(path).resolve(property).toString();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
import org.mycore.pi.doi.crossref.MCRCrossrefUtil;
import org.mycore.pi.exceptions.MCRIdentifierUnresolvableException;
import org.mycore.pi.exceptions.MCRPersistentIdentifierException;
import org.mycore.resource.MCRResourceHelper;
import org.xml.sax.SAXException;

@MCRCommandGroup(name = "DOI Commands")
Expand Down Expand Up @@ -303,7 +304,7 @@ public static void validateCrossrefDocument(String mycoreIDString, String transf
final Schema schema;
try {
SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
URL localSchemaURL = Thread.currentThread().getContextClassLoader().getResource(CROSSREF_SCHEMA_PATH);
URL localSchemaURL = MCRResourceHelper.getResourceUrl(CROSSREF_SCHEMA_PATH);
if (localSchemaURL == null) {
LOGGER.error(CROSSREF_SCHEMA_PATH + " was not found!");
return;
Expand Down
13 changes: 13 additions & 0 deletions mycore-tei/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,22 @@
<artifactId>mycore-tei</artifactId>
<name>MyCoRe TEI</name>
<dependencies>
<dependency>
<groupId>jakarta.servlet</groupId>
<artifactId>jakarta.servlet-api</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
<dependency>
<groupId>org.mycore</groupId>
<artifactId>mycore-base</artifactId>
</dependency>
<dependency>
<groupId>org.mycore</groupId>
<artifactId>mycore-base</artifactId>
<type>test-jar</type>
</dependency>
</dependencies>
</project>
Loading