Skip to content
This repository was archived by the owner on Sep 16, 2024. It is now read-only.
Merged
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 @@ -15,8 +15,6 @@
*/
package com.marklogic.client.ext.file;

import java.util.Properties;

/**
* Looks for a special file in each directory - defaults to collections.properties - that contains properties where the
* key is the name of a file in the directory, and the value is a comma-delimited list of collections to load the file
Expand All @@ -34,18 +32,8 @@ public CollectionsFileDocumentFileProcessor(String propertiesFilename) {
super(propertiesFilename);
}

@Override
protected void processProperties(DocumentFile documentFile, Properties properties) {
String name = documentFile.getFile().getName();
if (properties.containsKey(name)) {
String value = getPropertyValue(properties, name);
documentFile.getDocumentMetadata().withCollections(value.split(delimiter));
}

if (properties.containsKey(WILDCARD_KEY)) {
String value = getPropertyValue(properties, WILDCARD_KEY);
documentFile.getDocumentMetadata().withCollections(value.split(delimiter));
}
protected void applyPropertyMatch(DocumentFile documentFile, String pattern, String value) {
documentFile.getDocumentMetadata().withCollections(value.split(delimiter));
}

public String getDelimiter() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@

import com.marklogic.client.ext.util.DocumentPermissionsParser;

import java.nio.file.FileSystems;
import java.nio.file.Path;
import java.nio.file.PathMatcher;
import java.util.Enumeration;
import java.util.Properties;

/**
Expand Down Expand Up @@ -46,26 +50,11 @@ public PermissionsFileDocumentFileProcessor(String propertiesFilename, DocumentP
this.documentPermissionsParser = documentPermissionsParser;
}

@Override
protected void processProperties(DocumentFile documentFile, Properties properties) {
String name = documentFile.getFile().getName();
if (properties.containsKey(name)) {
String value = getPropertyValue(properties, name);
if (documentPermissionsParser != null) {
documentPermissionsParser.parsePermissions(value, documentFile.getDocumentMetadata().getPermissions());
} else {
documentFile.getDocumentMetadata().getPermissions().addFromDelimitedString(value);
}

}

if (properties.containsKey(WILDCARD_KEY)) {
String value = getPropertyValue(properties, WILDCARD_KEY);
if (documentPermissionsParser != null) {
documentPermissionsParser.parsePermissions(value, documentFile.getDocumentMetadata().getPermissions());
} else {
documentFile.getDocumentMetadata().getPermissions().addFromDelimitedString(value);
}
protected void applyPropertyMatch(DocumentFile documentFile, String pattern, String value) {
if (documentPermissionsParser != null) {
documentPermissionsParser.parsePermissions(value, documentFile.getDocumentMetadata().getPermissions());
} else {
documentFile.getDocumentMetadata().getPermissions().addFromDelimitedString(value);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@
import java.io.FileFilter;
import java.io.FileReader;
import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.Path;
import java.nio.file.PathMatcher;
import java.util.Enumeration;
import java.util.Properties;

/**
Expand All @@ -31,8 +35,6 @@
public abstract class PropertiesDrivenDocumentFileProcessor extends LoggingObject
implements DocumentFileProcessor, FileFilter, SupportsTokenReplacer {

protected final static String WILDCARD_KEY = "*";

private final String propertiesFilename;

private Properties properties;
Expand Down Expand Up @@ -62,17 +64,47 @@ public DocumentFile processDocumentFile(DocumentFile documentFile) {
return documentFile;
}

protected abstract void processProperties(DocumentFile documentFile, Properties properties);
/**
* New in 4.7.0 - each pattern in the properties object is now assumed to be a glob pattern; this retains backwards
* compatibility with the previous approach of only supporting "*" and exact filename matches. When a property
* is found to match the given file, then a subclass method is invoked to determine what to do with the value
* associated with the property.
*
* @param documentFile
* @param properties
*/
private void processProperties(DocumentFile documentFile, Properties properties) {
final Path filename = documentFile.getFile().toPath().getFileName();
Enumeration patterns = properties.propertyNames();
while (patterns.hasMoreElements()) {
String pattern = (String) patterns.nextElement();
PathMatcher matcher = FileSystems.getDefault().getPathMatcher("glob:" + pattern);
if (matcher.matches(filename)) {
String value = getPropertyValue(properties, pattern);
this.applyPropertyMatch(documentFile, pattern, value);
}
}
}

protected Properties loadProperties(File propertiesFile) throws IOException {
/**
* Subclasses must implement this to determine how to apply the value of a matching property to the given
* {@code DocumentFile}.
*
* @param documentFile
* @param pattern
* @param value
*/
protected abstract void applyPropertyMatch(DocumentFile documentFile, String pattern, String value);

protected final Properties loadProperties(File propertiesFile) throws IOException {
properties = new Properties();
try (FileReader reader = new FileReader(propertiesFile)) {
properties.load(reader);
return properties;
}
}

protected String getPropertyValue(Properties properties, String propertyName) {
private String getPropertyValue(Properties properties, String propertyName) {
if (properties == null || propertyName == null) {
return null;
}
Expand All @@ -89,10 +121,6 @@ public void setTokenReplacer(TokenReplacer tokenReplacer) {
this.tokenReplacer = tokenReplacer;
}

protected TokenReplacer getTokenReplacer() {
return tokenReplacer;
}

protected void setProperties(Properties properties) {
this.properties = properties;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package com.marklogic.client.ext.file;

import com.marklogic.client.ext.tokenreplacer.DefaultTokenReplacer;
import com.marklogic.client.io.DocumentMetadataHandle;
import org.junit.jupiter.api.Test;

import java.io.File;
Expand All @@ -37,15 +38,30 @@ public void wildcard() throws IOException {

DocumentFile file = new DocumentFile("/test.json", new File(testDir, "test.json"));
processor.processDocumentFile(file);
assertTrue(file.getDocumentMetadata().getCollections().contains("json-data"));
assertFalse(file.getDocumentMetadata().getCollections().contains("xml-data"));
assertTrue(file.getDocumentMetadata().getCollections().contains("global"));
DocumentMetadataHandle.DocumentCollections collections = file.getDocumentMetadata().getCollections();
assertEquals(3, collections.size(), "test.json should have the *, test.json, and *.json rules applied for it.");
assertTrue(collections.contains("json-data"));
assertTrue(collections.contains("json-data-wildcard"));
assertFalse(collections.contains("xml-data"));
assertTrue(collections.contains("global"));

file = new DocumentFile("/test-1.json", new File(testDir, "test-1.json"));
processor.processDocumentFile(file);
collections = file.getDocumentMetadata().getCollections();
assertEquals(2, collections.size(), "test-1.json should have the * and *.json rules applied for it.");
assertFalse(collections.contains("json-data"));
assertTrue(collections.contains("json-data-wildcard"));
assertFalse(collections.contains("xml-data"));
assertTrue(collections.contains("global"));

file = new DocumentFile("/test.xml", new File(testDir, "test.xml"));
processor.processDocumentFile(file);
assertFalse(file.getDocumentMetadata().getCollections().contains("json-data"));
assertTrue(file.getDocumentMetadata().getCollections().contains("xml-data"));
assertTrue(file.getDocumentMetadata().getCollections().contains("global"));
collections = file.getDocumentMetadata().getCollections();
assertEquals(2, collections.size(), "test.xml should have the * and test.xml rules applied for it.");
assertFalse(collections.contains("json-data"));
assertFalse(collections.contains("json-data-wildcard"));
assertTrue(collections.contains("xml-data"));
assertTrue(collections.contains("global"));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,25 @@ public void wildcard() throws IOException {
DocumentFile file = new DocumentFile("/test.json", new File(testDir, "test.json"));
processor.processDocumentFile(file);
DocumentMetadataHandle.DocumentPermissions permissions = file.getDocumentMetadata().getPermissions();
assertEquals(3, permissions.size(), "test.json should have the *, test.json, and *.json rules applied to it");
assertTrue(permissions.get("manage-user").contains(DocumentMetadataHandle.Capability.READ));
assertTrue(permissions.get("manage-user").contains(DocumentMetadataHandle.Capability.UPDATE));
assertTrue(permissions.get("manage-admin").contains(DocumentMetadataHandle.Capability.UPDATE));
assertNull(permissions.get("qconsole-user"));
assertTrue(permissions.get("qconsole-user").contains(DocumentMetadataHandle.Capability.READ));

file = new DocumentFile("/test-1.json", new File(testDir, "test-1.json"));
processor.processDocumentFile(file);
permissions = file.getDocumentMetadata().getPermissions();
assertEquals(2, permissions.size(), "test-1.json should have the * and *.json rules applied to it");
assertTrue(permissions.get("manage-user").contains(DocumentMetadataHandle.Capability.READ));
assertFalse(permissions.get("manage-user").contains(DocumentMetadataHandle.Capability.UPDATE));
assertNull(permissions.get("manage-admin"));
assertTrue(permissions.get("qconsole-user").contains(DocumentMetadataHandle.Capability.READ));

file = new DocumentFile("/test.xml", new File(testDir, "test.xml"));
processor.processDocumentFile(file);
permissions = file.getDocumentMetadata().getPermissions();
assertEquals(2, permissions.size(), "test.xml should have the * and test.xml rules applied to it");
assertTrue(permissions.get("manage-user").contains(DocumentMetadataHandle.Capability.READ));
assertFalse(permissions.get("manage-user").contains(DocumentMetadataHandle.Capability.UPDATE));
assertNull(permissions.get("manage-admin"));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
*=global
test.json=json-data
*.json=json-data-wildcard
test.xml=xml-data
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
*=manage-user,read
test.json=manage-user,update,manage-admin,update
*.json=qconsole-user,read
test.xml=qconsole-user,update
3 changes: 3 additions & 0 deletions src/test/resources/process-files/wildcard-test/test-1.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"test": "test-1"
}