#310 - Allow for topic/patterns with '#' and end of line comments
This commit is contained in:
@@ -20,6 +20,8 @@ import org.slf4j.Logger;
|
|||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
import java.text.ParseException;
|
import java.text.ParseException;
|
||||||
|
import java.util.regex.Matcher;
|
||||||
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Parses the acl configuration file. If a line starts with # it's comment. Blank lines are skipped.
|
* Parses the acl configuration file. If a line starts with # it's comment. Blank lines are skipped.
|
||||||
@@ -83,25 +85,29 @@ public final class ACLFileParser {
|
|||||||
String line;
|
String line;
|
||||||
AuthorizationsCollector collector = new AuthorizationsCollector();
|
AuthorizationsCollector collector = new AuthorizationsCollector();
|
||||||
|
|
||||||
|
Pattern emptyLine = Pattern.compile("^\\s*$");
|
||||||
|
Pattern commentLine = Pattern.compile("^#.*"); // As spec, comment lines should start with '#'
|
||||||
|
Pattern invalidCommentLine = Pattern.compile("^\\s*#.*");
|
||||||
|
Pattern endLineComment = Pattern.compile("^([\\w\\s\\/\\+]+#?)(\\s*#.*)$"); // This pattern has a dependency on filtering `commentLine`.
|
||||||
|
Matcher endLineCommentMatcher;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
while ((line = br.readLine()) != null) {
|
while ((line = br.readLine()) != null) {
|
||||||
int commentMarker = line.indexOf('#');
|
if (line.isEmpty() || emptyLine.matcher(line).matches() || commentLine.matcher(line).matches()) {
|
||||||
if (commentMarker != -1) {
|
// skip it's a black line or comment
|
||||||
if (commentMarker == 0) {
|
continue;
|
||||||
// skip its a comment
|
} else if (invalidCommentLine.matcher(line).matches()) {
|
||||||
continue;
|
// it's a malformed comment
|
||||||
} else {
|
int commentMarker = line.indexOf('#');
|
||||||
// it's a malformed comment
|
throw new ParseException(line, commentMarker);
|
||||||
throw new ParseException(line, commentMarker);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if (line.isEmpty() || line.matches("^\\s*$")) {
|
|
||||||
// skip it's a black line
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
collector.parse(line);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
endLineCommentMatcher = endLineComment.matcher(line);
|
||||||
|
if (endLineCommentMatcher.matches()) {
|
||||||
|
line = endLineCommentMatcher.group(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
collector.parse(line);
|
||||||
}
|
}
|
||||||
} catch (IOException ex) {
|
} catch (IOException ex) {
|
||||||
throw new ParseException("Failed to read", 1);
|
throw new ParseException("Failed to read", 1);
|
||||||
|
|||||||
@@ -44,9 +44,12 @@ public class ACLFileParserTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test(expected = ParseException.class)
|
@Test(expected = ParseException.class)
|
||||||
public void testParseInvalidComment() throws ParseException {
|
public void testParseInvalidPaddedComment() throws ParseException {
|
||||||
Reader conf = new StringReader(" #simple comment");
|
Reader conf = new StringReader(" #simple comment");
|
||||||
ACLFileParser.parse(conf);
|
AuthorizationsCollector authorizations = ACLFileParser.parse(conf);
|
||||||
|
|
||||||
|
// Verify
|
||||||
|
assertTrue(authorizations.isEmpty());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -59,4 +62,33 @@ public class ACLFileParserTest {
|
|||||||
assertTrue(authorizations.canWrite(new Topic("/weather/italy/anemometer"), "", ""));
|
assertTrue(authorizations.canWrite(new Topic("/weather/italy/anemometer"), "", ""));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testParseValidEndLineComment() throws ParseException {
|
||||||
|
Reader conf = new StringReader("topic /weather/italy/anemometer #simple comment");
|
||||||
|
AuthorizationsCollector authorizations = ACLFileParser.parse(conf);
|
||||||
|
|
||||||
|
// Verify
|
||||||
|
assertTrue(authorizations.canRead(new Topic("/weather/italy/anemometer"), "", ""));
|
||||||
|
assertTrue(authorizations.canWrite(new Topic("/weather/italy/anemometer"), "", ""));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testParseValidPoundTopicWithEndLineComment() throws ParseException {
|
||||||
|
Reader conf = new StringReader("topic /weather/italy/anemometer/# #simple comment");
|
||||||
|
AuthorizationsCollector authorizations = ACLFileParser.parse(conf);
|
||||||
|
|
||||||
|
// Verify
|
||||||
|
assertTrue(authorizations.canRead(new Topic("/weather/italy/anemometer/#"), "", ""));
|
||||||
|
assertTrue(authorizations.canWrite(new Topic("/weather/italy/anemometer/#"), "", ""));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testParseValidPlusTopicWithEndLineComment() throws ParseException {
|
||||||
|
Reader conf = new StringReader("topic /weather/+/anemometer #simple comment");
|
||||||
|
AuthorizationsCollector authorizations = ACLFileParser.parse(conf);
|
||||||
|
|
||||||
|
// Verify
|
||||||
|
assertTrue(authorizations.canRead(new Topic("/weather/+/anemometer"), "", ""));
|
||||||
|
assertTrue(authorizations.canWrite(new Topic("/weather/+/anemometer"), "", ""));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user