Search in sources :

Example 21 with InvalidParserConfigurationException

use of nl.basjes.parse.useragent.analyze.InvalidParserConfigurationException in project yauaa by nielsbasjes.

the class AbstractUserAgentAnalyzerDirect method initializeMatchers.

public synchronized void initializeMatchers() {
    if (matchersHaveBeenInitialized) {
        return;
    }
    LOG.info("Initializing Analyzer data structures");
    if (allMatchers.isEmpty()) {
        throw new InvalidParserConfigurationException("No matchers were loaded at all.");
    }
    long start = System.nanoTime();
    allMatchers.forEach(Matcher::initialize);
    long stop = System.nanoTime();
    matchersHaveBeenInitialized = true;
    LOG.info("Built in {} msec : Hashmap {}, Ranges map:{}", (stop - start) / 1000000, informMatcherActions.size(), informMatcherActionRanges.size());
    for (Matcher matcher : allMatchers) {
        if (matcher.getActionsThatRequireInput() == 0) {
            zeroInputMatchers.add(matcher);
        }
    }
    // Reset all Matchers
    for (Matcher matcher : allMatchers) {
        matcher.reset();
    }
    touchedMatchers = new MatcherList(32);
}
Also used : MatcherList(nl.basjes.parse.useragent.analyze.MatcherList) InvalidParserConfigurationException(nl.basjes.parse.useragent.analyze.InvalidParserConfigurationException) Matcher(nl.basjes.parse.useragent.analyze.Matcher)

Example 22 with InvalidParserConfigurationException

use of nl.basjes.parse.useragent.analyze.InvalidParserConfigurationException in project yauaa by nielsbasjes.

the class ConfigLoader method loadYaml.

private synchronized void loadYaml(Yaml yaml, InputStream yamlStream, String filename) {
    Node loadedYaml;
    try {
        loadedYaml = yaml.compose(new UnicodeReader(yamlStream));
    } catch (Exception e) {
        throw new InvalidParserConfigurationException("Parse error in the file " + filename + ": " + e.getMessage(), e);
    }
    if (loadedYaml == null) {
        LOG.warn("The file {} is empty", filename);
        return;
    }
    // Get and check top level config
    requireNodeInstanceOf(MappingNode.class, loadedYaml, filename, "File must be a Map");
    MappingNode rootNode = (MappingNode) loadedYaml;
    NodeTuple configNodeTuple = null;
    for (NodeTuple tuple : rootNode.getValue()) {
        String name = getKeyAsString(tuple, filename);
        if ("config".equals(name)) {
            configNodeTuple = tuple;
            break;
        }
        if ("version".equals(name)) {
            // Check the version information from the Yaml files
            assertSameVersion(tuple, filename);
            return;
        }
    }
    require(configNodeTuple != null, loadedYaml, filename, "The top level entry MUST be 'config'.");
    SequenceNode configNode = getValueAsSequenceNode(configNodeTuple, filename);
    List<Node> configList = configNode.getValue();
    for (Node configEntry : configList) {
        requireNodeInstanceOf(MappingNode.class, configEntry, filename, "The entry MUST be a mapping");
        NodeTuple entry = getExactlyOneNodeTuple((MappingNode) configEntry, filename);
        MappingNode actualEntry = getValueAsMappingNode(entry, filename);
        String entryType = getKeyAsString(entry, filename);
        switch(entryType) {
            case "lookup":
                loadYamlLookup(actualEntry, filename);
                break;
            case "set":
                loadYamlLookupSets(actualEntry, filename);
                break;
            case "matcher":
                loadYamlMatcher(actualEntry, filename);
                break;
            case "test":
                if (keepTests) {
                    loadYamlTestcase(actualEntry, filename);
                }
                break;
            default:
                throw new InvalidParserConfigurationException("Yaml config.(" + filename + ":" + actualEntry.getStartMark().getLine() + "): " + "Found unexpected config entry: " + entryType + ", allowed are 'lookup', 'set', 'matcher' and 'test'");
        }
    }
}
Also used : InvalidParserConfigurationException(nl.basjes.parse.useragent.analyze.InvalidParserConfigurationException) YamlUtils.getValueAsMappingNode(nl.basjes.parse.useragent.utils.YamlUtils.getValueAsMappingNode) MappingNode(org.yaml.snakeyaml.nodes.MappingNode) SequenceNode(org.yaml.snakeyaml.nodes.SequenceNode) YamlUtils.getValueAsMappingNode(nl.basjes.parse.useragent.utils.YamlUtils.getValueAsMappingNode) MappingNode(org.yaml.snakeyaml.nodes.MappingNode) Node(org.yaml.snakeyaml.nodes.Node) YamlUtils.getValueAsSequenceNode(nl.basjes.parse.useragent.utils.YamlUtils.getValueAsSequenceNode) UnicodeReader(org.yaml.snakeyaml.reader.UnicodeReader) YamlUtils.getValueAsString(nl.basjes.parse.useragent.utils.YamlUtils.getValueAsString) YamlUtils.getKeyAsString(nl.basjes.parse.useragent.utils.YamlUtils.getKeyAsString) YamlUtils.getExactlyOneNodeTuple(nl.basjes.parse.useragent.utils.YamlUtils.getExactlyOneNodeTuple) NodeTuple(org.yaml.snakeyaml.nodes.NodeTuple) InvalidParserConfigurationException(nl.basjes.parse.useragent.analyze.InvalidParserConfigurationException) IOException(java.io.IOException) SequenceNode(org.yaml.snakeyaml.nodes.SequenceNode) YamlUtils.getValueAsSequenceNode(nl.basjes.parse.useragent.utils.YamlUtils.getValueAsSequenceNode)

Example 23 with InvalidParserConfigurationException

use of nl.basjes.parse.useragent.analyze.InvalidParserConfigurationException in project yauaa by nielsbasjes.

the class ConfigLoader method loadResources.

// ------------------------------------------
public void loadResources(String resourceString, boolean showLoadMessages, boolean areOptional) {
    long startFiles = System.nanoTime();
    final boolean loadingDefaultResources = DEFAULT_RESOURCES.equals(resourceString);
    Map<String, Resource> resources = findAllResources(resourceString, showLoadMessages, areOptional, loadingDefaultResources);
    doingOnlyASingleTest = false;
    int maxFilenameLength = 0;
    // Just trying to open a stream for one of the resources is enough to see if we can continue.
    if (!resources.isEmpty()) {
        Resource resource = resources.entrySet().iterator().next().getValue();
        try (InputStream ignored = resource.getInputStream()) {
            // Just seeing if opening this stream triggers an error.
            // Having a useless statement that references the 'ignored' to avoid checkstyle and compilation warnings.
            LOG.debug("Opening the resource worked. {}", ignored);
        } catch (IOException e) {
            LOG.error("Cannot load the resources (usually classloading problem).");
            LOG.error("- Resource   : {}", resource);
            LOG.error("- Filename   : {}", resource.getFilename());
            LOG.error("- Description: {}", resource.getDescription());
            if (loadingDefaultResources) {
                LOG.warn("Falling back to the built in list of resources");
                resources.clear();
            } else {
                LOG.error("FATAL: Unable to load the specified resources for {}", resourceString);
                throw new InvalidParserConfigurationException("Error reading resources (" + resourceString + "): " + e.getMessage(), e);
            }
        }
    }
    if (resources.isEmpty()) {
        if (areOptional) {
            LOG.warn("NO optional resources were loaded from expression: {}", resourceString);
        } else {
            LOG.error("NO config files were found matching this expression: {}", resourceString);
            if (loadingDefaultResources) {
                LOG.warn("Unable to load the default resources, usually caused by classloader problems.");
                LOG.warn("Retrying with built in list.");
                PackagedRules.getRuleFileNames().forEach(s -> loadResources(s, false, false));
            } else {
                LOG.warn("If you are using wildcards in your expression then try explicitly naming all yamls files explicitly.");
                throw new InvalidParserConfigurationException("There were no resources found for the expression: " + resourceString);
            }
        }
        return;
    }
    // We need to determine if we are trying to load the yaml files TWICE.
    // This can happen if the library is loaded twice (perhaps even two different versions).
    Set<String> resourceBasenames = resources.keySet().stream().map(k -> k.replaceAll("^.*/", "")).collect(Collectors.toSet());
    Set<String> alreadyLoadedResourceBasenames = yamlRules.keySet().stream().map(k -> k.replaceAll("^.*/", "")).collect(Collectors.toSet());
    alreadyLoadedResourceBasenames.retainAll(resourceBasenames);
    if (!alreadyLoadedResourceBasenames.isEmpty()) {
        LOG.error("Trying to load these {} resources for the second time: {}", alreadyLoadedResourceBasenames.size(), alreadyLoadedResourceBasenames);
        throw new InvalidParserConfigurationException("Trying to load " + alreadyLoadedResourceBasenames.size() + " resources for the second time");
    }
    for (Map.Entry<String, Resource> resourceEntry : resources.entrySet()) {
        try {
            Resource resource = resourceEntry.getValue();
            String filename = resource.getFilename();
            if (filename != null) {
                maxFilenameLength = Math.max(maxFilenameLength, filename.length());
                String yamlString;
                try (InputStreamReader inputStreamReader = new InputStreamReader(resource.getInputStream(), StandardCharsets.UTF_8)) {
                    try (BufferedReader bufferedReader = new BufferedReader(inputStreamReader)) {
                        yamlString = bufferedReader.lines().collect(Collectors.joining("\n"));
                    }
                }
                yamlRules.put(filename, yamlString);
            }
        } catch (IOException e) {
            throw new InvalidParserConfigurationException("Error reading resources: " + e.getMessage(), e);
        }
    }
    long stopFiles = System.nanoTime();
    try (Formatter msg = new Formatter(Locale.ENGLISH)) {
        msg.format("- Loaded %2d files in %4d ms using expression: %s", resources.size(), (stopFiles - startFiles) / 1000000, resourceString);
        LOG.info("{}", msg);
    }
}
Also used : UnicodeReader(org.yaml.snakeyaml.reader.UnicodeReader) YamlUtils.getValueAsString(nl.basjes.parse.useragent.utils.YamlUtils.getValueAsString) SequenceNode(org.yaml.snakeyaml.nodes.SequenceNode) REQUIRE(nl.basjes.parse.useragent.config.MatcherConfig.ConfigLine.Type.REQUIRE) ConfigLine(nl.basjes.parse.useragent.config.MatcherConfig.ConfigLine) YamlUtils(nl.basjes.parse.useragent.utils.YamlUtils) YamlUtils.getValueAsMappingNode(nl.basjes.parse.useragent.utils.YamlUtils.getValueAsMappingNode) HashMap(java.util.HashMap) YauaaVersion.assertSameVersion(nl.basjes.parse.useragent.utils.YauaaVersion.assertSameVersion) MappingNode(org.yaml.snakeyaml.nodes.MappingNode) Yaml(org.yaml.snakeyaml.Yaml) ArrayList(java.util.ArrayList) Formatter(java.util.Formatter) PathMatchingResourcePatternResolver(org.springframework.core.io.support.PathMatchingResourcePatternResolver) Node(org.yaml.snakeyaml.nodes.Node) LinkedHashMap(java.util.LinkedHashMap) YamlUtils.getKeyAsString(nl.basjes.parse.useragent.utils.YamlUtils.getKeyAsString) InvalidParserConfigurationException(nl.basjes.parse.useragent.analyze.InvalidParserConfigurationException) ByteArrayInputStream(java.io.ByteArrayInputStream) Locale(java.util.Locale) Map(java.util.Map) EXTRACT(nl.basjes.parse.useragent.config.MatcherConfig.ConfigLine.Type.EXTRACT) LinkedHashSet(java.util.LinkedHashSet) Resource(org.springframework.core.io.Resource) PackagedRules(nl.basjes.parse.useragent.PackagedRules) UTF_8(java.nio.charset.StandardCharsets.UTF_8) LoaderOptions(org.yaml.snakeyaml.LoaderOptions) Set(java.util.Set) IOException(java.io.IOException) FAIL_IF_FOUND(nl.basjes.parse.useragent.config.MatcherConfig.ConfigLine.Type.FAIL_IF_FOUND) YamlUtils.getStringValues(nl.basjes.parse.useragent.utils.YamlUtils.getStringValues) InputStreamReader(java.io.InputStreamReader) Collectors(java.util.stream.Collectors) YamlUtils.getExactlyOneNodeTuple(nl.basjes.parse.useragent.utils.YamlUtils.getExactlyOneNodeTuple) StandardCharsets(java.nio.charset.StandardCharsets) NodeTuple(org.yaml.snakeyaml.nodes.NodeTuple) List(java.util.List) Logger(org.apache.logging.log4j.Logger) TreeMap(java.util.TreeMap) YamlUtils.require(nl.basjes.parse.useragent.utils.YamlUtils.require) YamlUtils.getValueAsSequenceNode(nl.basjes.parse.useragent.utils.YamlUtils.getValueAsSequenceNode) BufferedReader(java.io.BufferedReader) YamlUtils.requireNodeInstanceOf(nl.basjes.parse.useragent.utils.YamlUtils.requireNodeInstanceOf) LogManager(org.apache.logging.log4j.LogManager) Collections(java.util.Collections) InputStream(java.io.InputStream) VARIABLE(nl.basjes.parse.useragent.config.MatcherConfig.ConfigLine.Type.VARIABLE) InvalidParserConfigurationException(nl.basjes.parse.useragent.analyze.InvalidParserConfigurationException) InputStreamReader(java.io.InputStreamReader) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Formatter(java.util.Formatter) Resource(org.springframework.core.io.Resource) YamlUtils.getValueAsString(nl.basjes.parse.useragent.utils.YamlUtils.getValueAsString) YamlUtils.getKeyAsString(nl.basjes.parse.useragent.utils.YamlUtils.getKeyAsString) IOException(java.io.IOException) BufferedReader(java.io.BufferedReader) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) TreeMap(java.util.TreeMap)

Example 24 with InvalidParserConfigurationException

use of nl.basjes.parse.useragent.analyze.InvalidParserConfigurationException in project yauaa by nielsbasjes.

the class TestErrorHandling method runTest.

private void runTest(String resourceString, Matcher<String> expectedMessage) {
    InvalidParserConfigurationException exception = assertThrows(InvalidParserConfigurationException.class, () -> {
        UserAgentAnalyzerTester uaa = UserAgentAnalyzerTester.newBuilder().dropDefaultResources().keepTests().addResources(resourceString).build();
        assertTrue(uaa.runTests(false, false));
    });
    assertTrue(expectedMessage.matches(exception.getMessage()), "Bad message:" + exception.getMessage());
}
Also used : InvalidParserConfigurationException(nl.basjes.parse.useragent.analyze.InvalidParserConfigurationException) UserAgentAnalyzerTester(nl.basjes.parse.useragent.debug.UserAgentAnalyzerTester)

Example 25 with InvalidParserConfigurationException

use of nl.basjes.parse.useragent.analyze.InvalidParserConfigurationException in project yauaa by nielsbasjes.

the class TestAnnotationSystemAnonymous method testMissingAnnotations.

// ----------------------------------------------------------------
@Test
void testMissingAnnotations() {
    InvalidParserConfigurationException exception = assertThrows(InvalidParserConfigurationException.class, () -> record = new MyErrorMapper() {

        // Called via the annotation
        @SuppressWarnings("unused")
        public void setWasNotAnnotated(TestRecord testRecord, String value) {
            fail("May NEVER call this method");
        }
    }.enrich(record));
    assertEquals("You MUST specify at least 1 field to extract.", exception.getMessage());
}
Also used : InvalidParserConfigurationException(nl.basjes.parse.useragent.analyze.InvalidParserConfigurationException) Test(org.junit.jupiter.api.Test)

Aggregations

InvalidParserConfigurationException (nl.basjes.parse.useragent.analyze.InvalidParserConfigurationException)35 Test (org.junit.jupiter.api.Test)24 YamlUtils.getKeyAsString (nl.basjes.parse.useragent.utils.YamlUtils.getKeyAsString)5 YamlUtils.getValueAsString (nl.basjes.parse.useragent.utils.YamlUtils.getValueAsString)5 YamlUtils.getExactlyOneNodeTuple (nl.basjes.parse.useragent.utils.YamlUtils.getExactlyOneNodeTuple)4 NodeTuple (org.yaml.snakeyaml.nodes.NodeTuple)4 IOException (java.io.IOException)3 HashMap (java.util.HashMap)3 LinkedHashMap (java.util.LinkedHashMap)3 ArrayList (java.util.ArrayList)2 LinkedHashSet (java.util.LinkedHashSet)2 Map (java.util.Map)2 TreeMap (java.util.TreeMap)2 Matcher (nl.basjes.parse.useragent.analyze.Matcher)2 ConfigLine (nl.basjes.parse.useragent.config.MatcherConfig.ConfigLine)2 YamlUtils.getValueAsMappingNode (nl.basjes.parse.useragent.utils.YamlUtils.getValueAsMappingNode)2 YamlUtils.getValueAsSequenceNode (nl.basjes.parse.useragent.utils.YamlUtils.getValueAsSequenceNode)2 MappingNode (org.yaml.snakeyaml.nodes.MappingNode)2 Node (org.yaml.snakeyaml.nodes.Node)2 SequenceNode (org.yaml.snakeyaml.nodes.SequenceNode)2