<commit_before>/*
 * Copyright 2014 <NAME>. (http://www.onehippo.com)
 *
 * 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.
 */

package org.onehippo.cms7.essentials.dashboard.rest;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Collections;
import java.util.List;

import javax.inject.Inject;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletResponse;

import org.codehaus.jackson.map.ObjectMapper;
import org.onehippo.cms7.essentials.dashboard.config.FilePluginService;
import org.onehippo.cms7.essentials.dashboard.config.InstallerDocument;
import org.onehippo.cms7.essentials.dashboard.config.PluginConfigService;
import org.onehippo.cms7.essentials.dashboard.config.ProjectSettingsBean;
import org.onehippo.cms7.essentials.dashboard.config.ResourcePluginService;
import org.onehippo.cms7.essentials.dashboard.ctx.DefaultPluginContext;
import org.onehippo.cms7.essentials.dashboard.ctx.PluginContext;
import org.onehippo.cms7.essentials.dashboard.event.DisplayEvent;
import org.onehippo.cms7.essentials.dashboard.model.DependencyType;
import org.onehippo.cms7.essentials.dashboard.model.EssentialsDependency;
import org.onehippo.cms7.essentials.dashboard.model.Plugin;
import org.onehippo.cms7.essentials.dashboard.model.PluginRestful;
import org.onehippo.cms7.essentials.dashboard.model.Repository;
import org.onehippo.cms7.essentials.dashboard.packaging.InstructionPackage;
import org.onehippo.cms7.essentials.dashboard.packaging.TemplateSupportInstructionPackage;
import org.onehippo.cms7.essentials.dashboard.setup.ProjectSetupPlugin;
import org.onehippo.cms7.essentials.dashboard.utils.DependencyUtils;
import org.onehippo.cms7.essentials.dashboard.utils.GlobalUtils;
import org.onehippo.cms7.essentials.dashboard.utils.ProjectUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.google.common.base.Strings;
import com.google.common.eventbus.EventBus;

/**
 * @version "$Id$"
 */
public class BaseResource {

    private static Logger log = LoggerFactory.getLogger(BaseResource.class);

    @Inject
    private AutowireCapableBeanFactory injector;

    private ApplicationContext applicationContext;



    public MessageRestful createErrorMessage(final String message, final HttpServletResponse response) {
        response.setStatus(HttpServletResponse.SC_PRECONDITION_FAILED);
        return new ErrorMessageRestful(message);
    }

    /**
     * Instantiate InstructionPackage for plugin.
     *
     * @param plugin Plugin instance
     * @return null if packageClass & packageFile are null or empty
     */
    protected InstructionPackage instructionPackageInstance(final Plugin plugin) {
        final String packageClass = plugin.getPackageClass();
        final String packageFile = plugin.getPackageFile();
        InstructionPackage instructionPackage;
        if (Strings.isNullOrEmpty(packageClass)) {
            if (Strings.isNullOrEmpty(packageFile)) {
                log.warn("Package class and Package file were not defined for plugin: {}/{}", plugin.getName(), plugin.getPluginId());
                return null;
            }
            instructionPackage = GlobalUtils.newInstance(TemplateSupportInstructionPackage.class);
            instructionPackage.setInstructionPath(packageFile);
        } else {
            instructionPackage = GlobalUtils.newInstance(packageClass);
        }
        getInjector().autowireBean(instructionPackage);
        return instructionPackage;
    }

    protected boolean isInstalled(final Plugin plugin) {
        final List<EssentialsDependency> dependencies = plugin.getDependencies();
        for (EssentialsDependency dependency : dependencies) {
            if (!DependencyUtils.hasDependency(dependency)) {
                return false;
            }
        }
        final List<Repository>  repositories = plugin.getRepositories();
        for (Repository repository : repositories) {
            if (!DependencyUtils.hasRepository(repository)) {
                return false;
            }
        }
        return true;
    }


    protected ProjectRestful getProjectRestful() {
        final PluginContext context = new DefaultPluginContext(null);
        final ProjectRestful projectRestful = new ProjectRestful();
        // inject project settings:
        try (final PluginConfigService configService = context.getConfigService()) {

            final ProjectSettingsBean document = configService.read(ProjectSettingsBean.DEFAULT_NAME, ProjectSettingsBean.class);
            if (document != null) {
                projectRestful.setNamespace(document.getProjectNamespace());
            }

        } catch (Exception e) {
            log.error("Error reading project settings", e);
        }
        return projectRestful;
    }

    protected List<PluginRestful> getPlugins(final ServletContext servletContext) {
        final InputStream stream = getClass().getResourceAsStream("/plugin_descriptor.json");
        final String json = GlobalUtils.readStreamAsText(stream);
        final ObjectMapper mapper = new ObjectMapper();
        try {

            @SuppressWarnings("unchecked")
            final RestfulList<PluginRestful> restfulList = mapper.readValue(json, RestfulList.class);

            postProcessPlugins(restfulList, servletContext);

            return restfulList.getItems();
        } catch (IOException e) {
            log.error("Error parsing plugins", e);
        }
        return Collections.emptyList();
    }

    /**
     * Post-process the list of available plugins to provide additional, project-specific information to the front-end.
     *
     * @param plugins list of plugins.
     */
    protected void postProcessPlugins(final RestfulList<PluginRestful> plugins, final ServletContext servletContext) {
        final PluginContext context = getContext(servletContext);

        for (PluginRestful plugin : plugins.getItems()) {

            // Populate the "needsInstallation" flag based on other plugin descriptor fields.
            if ("plugins".equals(plugin.getType())) {
                final List<EssentialsDependency> deps = plugin.getDependencies();
                boolean needsInstallation = false;
                for (EssentialsDependency dep : deps) {
                    final DependencyType depType = dep.getDependencyType();
                    if (depType == DependencyType.ESSENTIALS) {
                        plugin.setNeedsInstallation(!ProjectUtils.isInstalled(depType, dep.createMavenDependency()));
                        break;
                    }
                }
            }

            populateInstallState(plugin, context);
        }
    }

    protected void populateInstallState(final PluginRestful plugin, final PluginContext context) {
        boolean boarding = false;
        boolean onBoard = false;
        boolean installing = false;
        boolean installed = false;

        try (PluginConfigService service = new FilePluginService(context)) {
            final InstallerDocument document = service.read(plugin.getPluginId(), InstallerDocument.class);
            if (document != null) {
                boarding = document.getDateInstalled() != null;
                installing = document.getDateAdded() != null;
            }
        } catch (Exception e) {
            log.error("Error reading settings for plugin {} from file", plugin.getPluginId(), e);
        }

        try (PluginConfigService service = new ResourcePluginService(context)) {
            final InstallerDocument document = service.read(plugin.getPluginId(), InstallerDocument.class);
            if (document != null) {
                onBoard = document.getDateInstalled() != null;
                installed = document.getDateAdded() != null;
            }
        } catch (Exception e) {
            log.error("Error reading settings for plugin {} from resource", plugin.getPluginId(), e);<commit_msg>ESSENTIALS-229: Remove "overruled" code.<commit_after>/*
 * Copyright 2014 <NAME>. (http://www.onehippo.com)
 *
 * 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.
 */

package org.onehippo.cms7.essentials.dashboard.rest;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Collections;
import java.util.List;

import javax.inject.Inject;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletResponse;

import org.codehaus.jackson.map.ObjectMapper;
import org.onehippo.cms7.essentials.dashboard.config.FilePluginService;
import org.onehippo.cms7.essentials.dashboard.config.InstallerDocument;
import org.onehippo.cms7.essentials.dashboard.config.PluginConfigService;
import org.onehippo.cms7.essentials.dashboard.config.ProjectSettingsBean;
import org.onehippo.cms7.essentials.dashboard.config.ResourcePluginService;
import org.onehippo.cms7.essentials.dashboard.ctx.DefaultPluginContext;
import org.onehippo.cms7.essentials.dashboard.ctx.PluginContext;
import org.onehippo.cms7.essentials.dashboard.event.DisplayEvent;
import org.onehippo.cms7.essentials.dashboard.model.DependencyType;
import org.onehippo.cms7.essentials.dashboard.model.EssentialsDependency;
import org.onehippo.cms7.essentials.dashboard.model.Plugin;
import org.onehippo.cms7.essentials.dashboard.model.PluginRestful;
import org.onehippo.cms7.essentials.dashboard.model.Repository;
import org.onehippo.cms7.essentials.dashboard.packaging.InstructionPackage;
import org.onehippo.cms7.essentials.dashboard.packaging.TemplateSupportInstructionPackage;
import org.onehippo.cms7.essentials.dashboard.setup.ProjectSetupPlugin;
import org.onehippo.cms7.essentials.dashboard.utils.DependencyUtils;
import org.onehippo.cms7.essentials.dashboard.utils.GlobalUtils;
import org.onehippo.cms7.essentials.dashboard.utils.ProjectUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.google.common.base.Strings;
import com.google.common.eventbus.EventBus;

/**
 * @version "$Id$"
 */
public class BaseResource {

    private static Logger log = LoggerFactory.getLogger(BaseResource.class);

    @Inject
    private AutowireCapableBeanFactory injector;

    private ApplicationContext applicationContext;



    public MessageRestful createErrorMessage(final String message, final HttpServletResponse response) {
        response.setStatus(HttpServletResponse.SC_PRECONDITION_FAILED);
        return new ErrorMessageRestful(message);
    }

    /**
     * Instantiate InstructionPackage for plugin.
     *
     * @param plugin Plugin instance
     * @return null if packageClass & packageFile are null or empty
     */
    protected InstructionPackage instructionPackageInstance(final Plugin plugin) {
        final String packageClass = plugin.getPackageClass();
        final String packageFile = plugin.getPackageFile();
        InstructionPackage instructionPackage;
        if (Strings.isNullOrEmpty(packageClass)) {
            if (Strings.isNullOrEmpty(packageFile)) {
                log.warn("Package class and Package file were not defined for plugin: {}/{}", plugin.getName(), plugin.getPluginId());
                return null;
            }
            instructionPackage = GlobalUtils.newInstance(TemplateSupportInstructionPackage.class);
            instructionPackage.setInstructionPath(packageFile);
        } else {
            instructionPackage = GlobalUtils.newInstance(packageClass);
        }
        getInjector().autowireBean(instructionPackage);
        return instructionPackage;
    }

    protected boolean isInstalled(final Plugin plugin) {
        final List<EssentialsDependency> dependencies = plugin.getDependencies();
        for (EssentialsDependency dependency : dependencies) {
            if (!DependencyUtils.hasDependency(dependency)) {
                return false;
            }
        }
        final List<Repository>  repositories = plugin.getRepositories();
        for (Repository repository : repositories) {
            if (!DependencyUtils.hasRepository(repository)) {
                return false;
            }
        }
        return true;
    }


    protected ProjectRestful getProjectRestful() {
        final PluginContext context = new DefaultPluginContext(null);
        final ProjectRestful projectRestful = new ProjectRestful();
        // inject project settings:
        try (final PluginConfigService configService = context.getConfigService()) {

            final ProjectSettingsBean document = configService.read(ProjectSettingsBean.DEFAULT_NAME, ProjectSettingsBean.class);
            if (document != null) {
                projectRestful.setNamespace(document.getProjectNamespace());
            }

        } catch (Exception e) {
            log.error("Error reading project settings", e);
        }
        return projectRestful;
    }

    protected List<PluginRestful> getPlugins(final ServletContext servletContext) {
        final InputStream stream = getClass().getResourceAsStream("/plugin_descriptor.json");
        final String json = GlobalUtils.readStreamAsText(stream);
        final ObjectMapper mapper = new ObjectMapper();
        try {

            @SuppressWarnings("unchecked")
            final RestfulList<PluginRestful> restfulList = mapper.readValue(json, RestfulList.class);

            postProcessPlugins(restfulList, servletContext);

            return restfulList.getItems();
        } catch (IOException e) {
            log.error("Error parsing plugins", e);
        }
        return Collections.emptyList();
    }

    /**
     * Post-process the list of available plugins to provide additional, project-specific information to the front-end.
     *
     * @param plugins list of plugins.
     */
    protected void postProcessPlugins(final RestfulList<PluginRestful> plugins, final ServletContext servletContext) {
        final PluginContext context = getContext(servletContext);

        for (PluginRestful plugin : plugins.getItems()) {
            populateInstallState(plugin, context);
        }
    }

    protected void populateInstallState(final PluginRestful plugin, final PluginContext context) {
        boolean boarding = false;
        boolean onBoard = false;
        boolean installing = false;
        boolean installed = false;

        try (PluginConfigService service = new FilePluginService(context)) {
            final InstallerDocument document = service.read(plugin.getPluginId(), InstallerDocument.class);
            if (document != null) {
                boarding = document.getDateInstalled() != null;
                installing = document.getDateAdded() != null;
            }
        } catch (Exception e) {
            log.error("Error reading settings for plugin {} from file", plugin.getPluginId(), e);
        }

        try (PluginConfigService service = new ResourcePluginService(context)) {
            final InstallerDocument document = service.read(plugin.getPluginId(), InstallerDocument.class);
            if (document != null) {
                onBoard = document.getDateInstalled() != null;
                installed = document.getDateAdded() != null;
            }
        } catch (Exception e) {
            log.error("Error reading settings for plugin {} from resource", plugin.getPluginId(), e);
{'fraction_non_alphanumeric': 0.08776356100198233, 'fraction_numerical': 0.003784465669489998, 'mean_word_length': 3.2578005115089512, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 16, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, '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': '4945790', 'n_tokens_mistral': 4277, 'n_tokens_neox': 4201, 'n_words': 977}