<commit_before>/*
 * This file is part of Dependency-Track.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * Copyright (c) <NAME>. All Rights Reserved.
 */
package org.owasp.dependencytrack.tasks;

import alpine.Config;
import alpine.event.framework.Event;
import alpine.event.framework.SingleThreadedEventService;
import alpine.event.framework.Subscriber;
import alpine.logging.Logger;
import alpine.persistence.PaginatedResult;
import alpine.resources.AlpineRequest;
import alpine.resources.OrderDirection;
import alpine.resources.Pagination;
import org.owasp.dependencycheck.agent.DependencyCheckScanAgent;
import org.owasp.dependencycheck.exception.ScanAgentException;
import org.owasp.dependencycheck.reporting.ReportGenerator;
import org.owasp.dependencytrack.event.DependencyCheckEvent;
import org.owasp.dependencytrack.event.MetricsUpdateEvent;
import org.owasp.dependencytrack.exception.ParseException;
import org.owasp.dependencytrack.model.Component;
import org.owasp.dependencytrack.model.Vulnerability;
import org.owasp.dependencytrack.parser.dependencycheck.DependencyCheckParser;
import org.owasp.dependencytrack.parser.dependencycheck.model.Analysis;
import org.owasp.dependencytrack.parser.dependencycheck.util.ModelConverter;
import org.owasp.dependencytrack.persistence.QueryManager;
import java.io.File;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.List;

import static org.owasp.dependencytrack.tasks.NistMirrorTask.NVD_MIRROR_DIR;

/**
 * Subscriber task that performs a Dependency-Check analysis or update.
 *
 * @author <NAME>
 * @since 3.0.0
 */
public class DependencyCheckTask implements Subscriber {

    private static final Logger LOGGER = Logger.getLogger(DependencyCheckTask.class);
    private static final String DC_ROOT_DIR = Config.getInstance().getDataDirectorty().getAbsolutePath() + File.separator + "dependency-check";
    private static final String DC_DATA_DIR = DC_ROOT_DIR + File.separator + "data";
    private static final String DC_REPORT_DIR = DC_ROOT_DIR + File.separator + "reports";
    private static final String DC_REPORT_FILE = DC_REPORT_DIR + File.separator + "dependency-check-report.xml";
    private static final String DC_GLOBAL_SUPPRESSION = DC_ROOT_DIR + File.separator + "suppressions.xml";

    /**
     * {@inheritDoc}
     */
    public void inform(Event e) {
        if (e instanceof DependencyCheckEvent) {
            final DependencyCheckEvent event = (DependencyCheckEvent) e;
            if (DependencyCheckEvent.Action.ANALYZE == event.getAction()) {
                performAnalysis(event);
            } else if (DependencyCheckEvent.Action.UPDATE_ONLY == event.getAction()) {
                performUpdateOnly();
            }
        }
    }

    /**
     * Perfors a Dependency-Check analysis.
     * @param event a DependencyCheckEvent
     */
    private void performAnalysis(DependencyCheckEvent event) {
        LOGGER.info("Executing Dependency-Check analysis task");
        if (event.analyzePortfolio()) {
            LOGGER.info("Analyzing portfolio");
            final AlpineRequest alpineRequest = new AlpineRequest(
                    null,
                    new Pagination(Pagination.Strategy.OFFSET, 0, 1000),
                    null,
                    "id",
                    OrderDirection.ASCENDING
            );

            try (QueryManager qm = new QueryManager(alpineRequest)) {
                final long total = qm.getCount(Component.class);
                long count = 0;
                while (count < total) {
                    final PaginatedResult result = qm.getComponents();
                    analyze(result.getList(Component.class));
                    count += result.getObjects().size();
                    qm.advancePagination();
                }
            }
        } else {
            analyze(event.getComponents());
        }
    }

    /**
     * Performs an update of the Dependeny-Check data directory only.
     */<commit_msg>javadoc spelling<commit_after>/*
 * This file is part of Dependency-Track.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * Copyright (c) <NAME>. All Rights Reserved.
 */
package org.owasp.dependencytrack.tasks;

import alpine.Config;
import alpine.event.framework.Event;
import alpine.event.framework.SingleThreadedEventService;
import alpine.event.framework.Subscriber;
import alpine.logging.Logger;
import alpine.persistence.PaginatedResult;
import alpine.resources.AlpineRequest;
import alpine.resources.OrderDirection;
import alpine.resources.Pagination;
import org.owasp.dependencycheck.agent.DependencyCheckScanAgent;
import org.owasp.dependencycheck.exception.ScanAgentException;
import org.owasp.dependencycheck.reporting.ReportGenerator;
import org.owasp.dependencytrack.event.DependencyCheckEvent;
import org.owasp.dependencytrack.event.MetricsUpdateEvent;
import org.owasp.dependencytrack.exception.ParseException;
import org.owasp.dependencytrack.model.Component;
import org.owasp.dependencytrack.model.Vulnerability;
import org.owasp.dependencytrack.parser.dependencycheck.DependencyCheckParser;
import org.owasp.dependencytrack.parser.dependencycheck.model.Analysis;
import org.owasp.dependencytrack.parser.dependencycheck.util.ModelConverter;
import org.owasp.dependencytrack.persistence.QueryManager;
import java.io.File;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.List;

import static org.owasp.dependencytrack.tasks.NistMirrorTask.NVD_MIRROR_DIR;

/**
 * Subscriber task that performs a Dependency-Check analysis or update.
 *
 * @author <NAME>
 * @since 3.0.0
 */
public class DependencyCheckTask implements Subscriber {

    private static final Logger LOGGER = Logger.getLogger(DependencyCheckTask.class);
    private static final String DC_ROOT_DIR = Config.getInstance().getDataDirectorty().getAbsolutePath() + File.separator + "dependency-check";
    private static final String DC_DATA_DIR = DC_ROOT_DIR + File.separator + "data";
    private static final String DC_REPORT_DIR = DC_ROOT_DIR + File.separator + "reports";
    private static final String DC_REPORT_FILE = DC_REPORT_DIR + File.separator + "dependency-check-report.xml";
    private static final String DC_GLOBAL_SUPPRESSION = DC_ROOT_DIR + File.separator + "suppressions.xml";

    /**
     * {@inheritDoc}
     */
    public void inform(Event e) {
        if (e instanceof DependencyCheckEvent) {
            final DependencyCheckEvent event = (DependencyCheckEvent) e;
            if (DependencyCheckEvent.Action.ANALYZE == event.getAction()) {
                performAnalysis(event);
            } else if (DependencyCheckEvent.Action.UPDATE_ONLY == event.getAction()) {
                performUpdateOnly();
            }
        }
    }

    /**
     * Performs a Dependency-Check analysis.
     * @param event a DependencyCheckEvent
     */
    private void performAnalysis(DependencyCheckEvent event) {
        LOGGER.info("Executing Dependency-Check analysis task");
        if (event.analyzePortfolio()) {
            LOGGER.info("Analyzing portfolio");
            final AlpineRequest alpineRequest = new AlpineRequest(
                    null,
                    new Pagination(Pagination.Strategy.OFFSET, 0, 1000),
                    null,
                    "id",
                    OrderDirection.ASCENDING
            );

            try (QueryManager qm = new QueryManager(alpineRequest)) {
                final long total = qm.getCount(Component.class);
                long count = 0;
                while (count < total) {
                    final PaginatedResult result = qm.getComponents();
                    analyze(result.getList(Component.class));
                    count += result.getObjects().size();
                    qm.advancePagination();
                }
            }
        } else {
            analyze(event.getComponents());
        }
    }

    /**
     * Performs an update of the Dependency-Check data directory only.
     */
{'fraction_non_alphanumeric': 0.08935087913303108, 'fraction_numerical': 0.0028751520513104058, 'mean_word_length': 3.103448275862069, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 4}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2654712', 'n_tokens_mistral': 2488, 'n_tokens_neox': 2349, 'n_words': 601}