<commit_before>package org.openspaces.core.space;

import com.gigaspaces.internal.utils.StringUtils;
import com.j_spaces.core.IJSpace;
import com.j_spaces.kernel.Environment;
import net.jini.core.discovery.LookupLocator;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.eclipse.jetty.server.Connector;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.nio.SelectChannelConnector;
import org.eclipse.jetty.servlet.FilterHolder;
import org.eclipse.jetty.webapp.WebAppContext;
import org.openspaces.core.GigaSpace;
import org.openspaces.core.cluster.ClusterInfo;
import org.openspaces.core.cluster.ClusterInfoAware;
import org.openspaces.pu.container.CannotCreateContainerException;
import org.openspaces.pu.container.jee.JeeServiceDetails;
import org.openspaces.pu.container.jee.JeeType;
import org.openspaces.pu.container.jee.stats.RequestStatisticsFilter;
import org.openspaces.pu.service.ServiceDetails;
import org.openspaces.pu.service.ServiceDetailsProvider;
import org.openspaces.pu.service.ServiceMonitors;
import org.openspaces.pu.service.ServiceMonitorsProvider;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;

import javax.servlet.DispatcherType;
import java.net.Inet4Address;
import java.net.UnknownHostException;
import java.util.EnumSet;
import java.util.Properties;

/**
 *
 * @since 10.1.0
 * @author yohana
 */
public class RestBean implements InitializingBean, ClusterInfoAware, DisposableBean, ServiceDetailsProvider, ServiceMonitorsProvider {
    protected Log logger = LogFactory.getLog(getClass());

    private Server server;

    private GigaSpace gigaspace;

    private String spaceName;

    private String groups;

    private String locators;

    private String port;

    int jettyPort;

    private FilterHolder filterHolder;

    private ClusterInfo clusterInfo;

    private WebAppContext webAppContext;

    private boolean jettyStarted = false;

    private Properties properties;

    @Override
    public void destroy() {
        if (jettyStarted) {
            logger.info("Stopping rest service");
            try {
                webAppContext.stop();
            } catch (Exception e) {
                logger.error("Unable to stop web context", e);
            }
            try {
                server.stop();
            } catch (Exception e) {
                logger.error("Unable to stop rest service", e);
            }

            server.destroy();
        }
    }

    public void setPort(String port) {
        this.port = port;
    }

    public void setGigaSpace(GigaSpace gigaSpace) {
        this.gigaspace = gigaSpace;
    }

    public void setSpaceName(String spaceName) {
        this.spaceName = spaceName;
    }

    public void setGroups(String groups) {
        this.groups = groups;
    }

    public void setLocators(String locators) {
        this.locators = locators;
    }

    public void setProperties(Properties properties) {
        this.properties= properties;
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        int runningNumber = clusterInfo.getRunningNumber();
        try {
            jettyPort = Integer.valueOf(this.port);
        } catch (NumberFormatException e) {
            throw new CannotCreateContainerException("Port should be number");
        }
        jettyPort += runningNumber;
        server = new Server();
        server.setStopAtShutdown(true);
        server.setGracefulShutdown(1000);

        SelectChannelConnector connector = new SelectChannelConnector();
        connector.setPort(jettyPort);
        server.setConnectors(new Connector[]{connector});

        String ispaceName, igroups, ilocators;

        if (gigaspace == null && spaceName == null) {
            throw new CannotCreateContainerException("Either giga-space or space-name attribute should be specified.");
        }
        if (gigaspace != null && spaceName != null) {
            throw new CannotCreateContainerException("Either giga-space or space-name attribute can be specified but not both.");
        }

        if (spaceName != null) {
            ispaceName = spaceName;
            //TODO validate groups and locators ?
            igroups = (groups == null ? null : groups);
            ilocators = (locators == null ? null : locators);
        } else {
            IJSpace space = gigaspace.getSpace();
            ispaceName = space.getName();
            String[] lookupgroups = space.getFinderURL().getLookupGroups();
            if (lookupgroups == null || lookupgroups.length == 0) {
                igroups = null;
            } else {
                igroups = StringUtils.join(lookupgroups, ",", 0, lookupgroups.length);
            }

            LookupLocator[] lookuplocators = space.getFinderURL().getLookupLocators();
            if (lookuplocators == null || lookuplocators.length == 0) {
                ilocators = null;
            } else {
                ilocators = "";
                for (int i=0 ; i<lookuplocators.length;i++) {
                    ilocators+= lookuplocators[i].getHost()+":"+lookuplocators[i].getPort();
                    if (i != (lookuplocators.length - 1)) {
                        ilocators += ",";
                    }
                }
            }
        }

        logger.info("Starting REST service on port [" + jettyPort + "]");
        webAppContext = new WebAppContext();
        filterHolder = new FilterHolder(RequestStatisticsFilter.class);
        webAppContext.addFilter(filterHolder, "/", EnumSet.of(DispatcherType.INCLUDE, DispatcherType.REQUEST));
        webAppContext.setContextPath("/");
        webAppContext.setWar(Environment.getHomeDirectory() + "/lib/platform/rest/RESTData.war");
        webAppContext.setInitParameter("port", port);
        webAppContext.setInitParameter("spaceName", ispaceName);
        if (igroups != null && !igroups.equalsIgnoreCase("null")) {
            logger.debug("Applying groups "+igroups);
            webAppContext.setInitParameter("lookupGroups", igroups);
        }<commit_msg>GS-12126 Fixed statistics problem: changed filter regex to include all paths.<commit_after>package org.openspaces.core.space;

import com.gigaspaces.internal.utils.StringUtils;
import com.j_spaces.core.IJSpace;
import com.j_spaces.kernel.Environment;
import net.jini.core.discovery.LookupLocator;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.eclipse.jetty.server.Connector;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.nio.SelectChannelConnector;
import org.eclipse.jetty.servlet.FilterHolder;
import org.eclipse.jetty.webapp.WebAppContext;
import org.openspaces.core.GigaSpace;
import org.openspaces.core.cluster.ClusterInfo;
import org.openspaces.core.cluster.ClusterInfoAware;
import org.openspaces.pu.container.CannotCreateContainerException;
import org.openspaces.pu.container.jee.JeeServiceDetails;
import org.openspaces.pu.container.jee.JeeType;
import org.openspaces.pu.container.jee.stats.RequestStatisticsFilter;
import org.openspaces.pu.service.ServiceDetails;
import org.openspaces.pu.service.ServiceDetailsProvider;
import org.openspaces.pu.service.ServiceMonitors;
import org.openspaces.pu.service.ServiceMonitorsProvider;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;

import javax.servlet.DispatcherType;
import java.net.Inet4Address;
import java.net.UnknownHostException;
import java.util.EnumSet;
import java.util.Properties;

/**
 *
 * @since 10.1.0
 * @author yohana
 */
public class RestBean implements InitializingBean, ClusterInfoAware, DisposableBean, ServiceDetailsProvider, ServiceMonitorsProvider {
    protected Log logger = LogFactory.getLog(getClass());

    private Server server;

    private GigaSpace gigaspace;

    private String spaceName;

    private String groups;

    private String locators;

    private String port;

    int jettyPort;

    private FilterHolder filterHolder;

    private ClusterInfo clusterInfo;

    private WebAppContext webAppContext;

    private boolean jettyStarted = false;

    private Properties properties;

    @Override
    public void destroy() {
        if (jettyStarted) {
            logger.info("Stopping rest service");
            try {
                webAppContext.stop();
            } catch (Exception e) {
                logger.error("Unable to stop web context", e);
            }
            try {
                server.stop();
            } catch (Exception e) {
                logger.error("Unable to stop rest service", e);
            }

            server.destroy();
        }
    }

    public void setPort(String port) {
        this.port = port;
    }

    public void setGigaSpace(GigaSpace gigaSpace) {
        this.gigaspace = gigaSpace;
    }

    public void setSpaceName(String spaceName) {
        this.spaceName = spaceName;
    }

    public void setGroups(String groups) {
        this.groups = groups;
    }

    public void setLocators(String locators) {
        this.locators = locators;
    }

    public void setProperties(Properties properties) {
        this.properties= properties;
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        int runningNumber = clusterInfo.getRunningNumber();
        try {
            jettyPort = Integer.valueOf(this.port);
        } catch (NumberFormatException e) {
            throw new CannotCreateContainerException("Port should be number");
        }
        jettyPort += runningNumber;
        server = new Server();
        server.setStopAtShutdown(true);
        server.setGracefulShutdown(1000);

        SelectChannelConnector connector = new SelectChannelConnector();
        connector.setPort(jettyPort);
        server.setConnectors(new Connector[]{connector});

        String ispaceName, igroups, ilocators;

        if (gigaspace == null && spaceName == null) {
            throw new CannotCreateContainerException("Either giga-space or space-name attribute should be specified.");
        }
        if (gigaspace != null && spaceName != null) {
            throw new CannotCreateContainerException("Either giga-space or space-name attribute can be specified but not both.");
        }

        if (spaceName != null) {
            ispaceName = spaceName;
            //TODO validate groups and locators ?
            igroups = (groups == null ? null : groups);
            ilocators = (locators == null ? null : locators);
        } else {
            IJSpace space = gigaspace.getSpace();
            ispaceName = space.getName();
            String[] lookupgroups = space.getFinderURL().getLookupGroups();
            if (lookupgroups == null || lookupgroups.length == 0) {
                igroups = null;
            } else {
                igroups = StringUtils.join(lookupgroups, ",", 0, lookupgroups.length);
            }

            LookupLocator[] lookuplocators = space.getFinderURL().getLookupLocators();
            if (lookuplocators == null || lookuplocators.length == 0) {
                ilocators = null;
            } else {
                ilocators = "";
                for (int i=0 ; i<lookuplocators.length;i++) {
                    ilocators+= lookuplocators[i].getHost()+":"+lookuplocators[i].getPort();
                    if (i != (lookuplocators.length - 1)) {
                        ilocators += ",";
                    }
                }
            }
        }

        logger.info("Starting REST service on port [" + jettyPort + "]");
        webAppContext = new WebAppContext();
        filterHolder = new FilterHolder(RequestStatisticsFilter.class);
        webAppContext.addFilter(filterHolder, "/*", EnumSet.of(DispatcherType.INCLUDE, DispatcherType.REQUEST));
        webAppContext.setContextPath("/");
        webAppContext.setWar(Environment.getHomeDirectory() + "/lib/platform/rest/RESTData.war");
        webAppContext.setInitParameter("port", port);
        webAppContext.setInitParameter("spaceName", ispaceName);
        if (igroups != null && !igroups.equalsIgnoreCase("null")) {
            logger.debug("Applying groups "+igroups);
            webAppContext.setInitParameter("lookupGroups", igroups);
        }
{'fraction_non_alphanumeric': 0.10104444984211805, 'fraction_numerical': 0.002671848433325237, 'mean_word_length': 2.67728490622209, 'pattern_counts': {'":': 2, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '1884646', 'n_tokens_mistral': 3412, 'n_tokens_neox': 3272, 'n_words': 706}