<commit_before>/*
 * eXist Open Source Native XML Database
 * Copyright (C) 2001-2017 The eXist Project
 * http://exist-db.org
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */
package org.exist.xmlrpc;

import com.evolvedbinary.j8fu.lazy.AtomicLazyValE;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.xmlrpc.XmlRpcException;
import org.apache.xmlrpc.XmlRpcHandler;
import org.apache.xmlrpc.server.AbstractReflectiveHandlerMapping;
import org.apache.xmlrpc.server.RequestProcessorFactoryFactory;
import org.apache.xmlrpc.server.XmlRpcHandlerMapping;
import org.apache.xmlrpc.webserver.XmlRpcServlet;
import org.exist.EXistException;
import org.exist.http.Descriptor;
import org.exist.http.servlets.HttpServletRequestWrapper;

import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
import java.util.List;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Predicate;

import static com.evolvedbinary.j8fu.Either.*;

public class RpcServlet extends XmlRpcServlet {

	private static final long serialVersionUID = -1003413291835771186L;
    private static final Logger LOG = LogManager.getLogger(RpcServlet.class);
    private static final AtomicReference<RequestProcessorFactoryFactory> XMLDB_REQUEST_PROCESSOR_FACTORY_FACTORY = new AtomicReference<>();
    private static final boolean DEFAULT_USE_DEFAULT_USER = true;

    private boolean useDefaultUser = DEFAULT_USE_DEFAULT_USER;

    @Override
    public void init(final ServletConfig pConfig) throws ServletException {
        final String useDefaultUser = pConfig.getInitParameter("useDefaultUser");
        if(useDefaultUser != null) {
            this.useDefaultUser = Boolean.parseBoolean(useDefaultUser);
        } else {
            this.useDefaultUser = DEFAULT_USE_DEFAULT_USER;
        }

        super.init(new FilteredServletConfig(pConfig, paramName -> !"useDefaultUser".equals(paramName)));
    }

    @Override
    public void doPost(HttpServletRequest request, final HttpServletResponse response) throws IOException, ServletException {
        // Request logger

        final Descriptor descriptor = Descriptor.getDescriptorSingleton();
        if (descriptor.allowRequestLogging() && !descriptor.requestsFiltered()) {
            // Wrap HttpServletRequest, because both request Logger and xmlrpc
            // need the request InputStream, which is consumed when read.
            request =
                    new HttpServletRequestWrapper(request, /*formEncoding*/ "utf-8");
            descriptor.doLogRequestInReplayLog(request);
        }

        try {
            super.doPost(request, response);
        } catch (final Throwable e){
            LOG.error("Problem during XmlRpc execution", e);
            final String exceptionMessage;
            if (e instanceof XmlRpcException) {
                final Throwable linkedException = ((XmlRpcException)e).linkedException;
                LOG.error(linkedException.getMessage(), linkedException);
                exceptionMessage = "An error occurred: " + e.getMessage() + ": " + linkedException.getMessage();
            } else {
                exceptionMessage = "An unknown error occurred: " + e.getMessage();
            }
            throw new ServletException(exceptionMessage, e);
        }
    }

    @Override
    protected XmlRpcHandlerMapping newXmlRpcHandlerMapping() throws XmlRpcException {
        final DefaultHandlerMapping mapping = new DefaultHandlerMapping();
        mapping.setVoidMethodEnabled(true);
        mapping.setRequestProcessorFactoryFactory(
                XMLDB_REQUEST_PROCESSOR_FACTORY_FACTORY.updateAndGet(prev -> prev != null ? prev : new XmldbRequestProcessorFactoryFactory(useDefaultUser))
        );
        mapping.loadDefault(RpcConnection.class);
        return mapping;
    }
<commit_msg>[bugfix] Fix the ability to restart eXist-db using the XML-RPC interface<commit_after>/*
 * eXist Open Source Native XML Database
 * Copyright (C) 2001-2017 The eXist Project
 * http://exist-db.org
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */
package org.exist.xmlrpc;

import com.evolvedbinary.j8fu.lazy.AtomicLazyValE;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.xmlrpc.XmlRpcException;
import org.apache.xmlrpc.XmlRpcHandler;
import org.apache.xmlrpc.server.AbstractReflectiveHandlerMapping;
import org.apache.xmlrpc.server.RequestProcessorFactoryFactory;
import org.apache.xmlrpc.server.XmlRpcHandlerMapping;
import org.apache.xmlrpc.webserver.XmlRpcServlet;
import org.exist.EXistException;
import org.exist.http.Descriptor;
import org.exist.http.servlets.HttpServletRequestWrapper;

import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
import java.util.List;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Predicate;

import static com.evolvedbinary.j8fu.Either.*;

public class RpcServlet extends XmlRpcServlet {

	private static final long serialVersionUID = -1003413291835771186L;
    private static final Logger LOG = LogManager.getLogger(RpcServlet.class);
    private static final boolean DEFAULT_USE_DEFAULT_USER = true;

    private boolean useDefaultUser = DEFAULT_USE_DEFAULT_USER;

    @Override
    public void init(final ServletConfig pConfig) throws ServletException {
        final String useDefaultUser = pConfig.getInitParameter("useDefaultUser");
        if(useDefaultUser != null) {
            this.useDefaultUser = Boolean.parseBoolean(useDefaultUser);
        } else {
            this.useDefaultUser = DEFAULT_USE_DEFAULT_USER;
        }

        super.init(new FilteredServletConfig(pConfig, paramName -> !"useDefaultUser".equals(paramName)));
    }

    @Override
    public void doPost(HttpServletRequest request, final HttpServletResponse response) throws IOException, ServletException {
        // Request logger

        final Descriptor descriptor = Descriptor.getDescriptorSingleton();
        if (descriptor.allowRequestLogging() && !descriptor.requestsFiltered()) {
            // Wrap HttpServletRequest, because both request Logger and xmlrpc
            // need the request InputStream, which is consumed when read.
            request =
                    new HttpServletRequestWrapper(request, /*formEncoding*/ "utf-8");
            descriptor.doLogRequestInReplayLog(request);
        }

        try {
            super.doPost(request, response);
        } catch (final Throwable e){
            LOG.error("Problem during XmlRpc execution", e);
            final String exceptionMessage;
            if (e instanceof XmlRpcException) {
                final Throwable linkedException = ((XmlRpcException)e).linkedException;
                LOG.error(linkedException.getMessage(), linkedException);
                exceptionMessage = "An error occurred: " + e.getMessage() + ": " + linkedException.getMessage();
            } else {
                exceptionMessage = "An unknown error occurred: " + e.getMessage();
            }
            throw new ServletException(exceptionMessage, e);
        }
    }

    @Override
    protected XmlRpcHandlerMapping newXmlRpcHandlerMapping() throws XmlRpcException {
        final DefaultHandlerMapping mapping = new DefaultHandlerMapping();
        mapping.setVoidMethodEnabled(true);
        mapping.setRequestProcessorFactoryFactory(new XmldbRequestProcessorFactoryFactory(useDefaultUser));
        mapping.loadDefault(RpcConnection.class);
        return mapping;
    }

{'fraction_non_alphanumeric': 0.07883289124668436, 'fraction_numerical': 0.0093368700265252, 'mean_word_length': 3.645638245441104, 'pattern_counts': {'":': 2, '<': 5, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 16}, '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': '6328091', 'n_tokens_mistral': 2471, 'n_tokens_neox': 2388, 'n_words': 692}