Package org.exist.xmlrpc

Source Code of org.exist.xmlrpc.RpcServlet$XmldbRequestProcessorFactoryFactory

/*
*  eXist Open Source Native XML Database
*  Copyright (C) 2001-07 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 library; if not, write to the Free Software
*  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*
* $Id$
*/
package org.exist.xmlrpc;

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.server.XmlRpcNoSuchHandlerException;
import org.apache.xmlrpc.webserver.XmlRpcServlet;
import org.exist.EXistException;
import org.exist.http.Descriptor;
import org.exist.http.servlets.HttpServletRequestWrapper;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class RpcServlet extends XmlRpcServlet {

  private static final long serialVersionUID = -1003413291835771186L;

    private static boolean useDefaultUser = true;

    public static boolean isUseDefaultUser() {
        return useDefaultUser;
    }

    public static void setUseDefaultUser(boolean useDefaultUser) {
        RpcServlet.useDefaultUser = useDefaultUser;
    }

  @Override
    public void doPost(HttpServletRequest request, 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("Problem during XmlRpc execution", e);
            throw new ServletException("An unknown error occurred: " + e.getMessage(), e);
        }
    }

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

    private static class XmldbRequestProcessorFactoryFactory extends RequestProcessorFactoryFactory.RequestSpecificProcessorFactoryFactory {

        RequestProcessorFactory instance = null;

        public RequestProcessorFactory getRequestProcessorFactory(Class pClass) throws XmlRpcException {
            try {
                if (instance == null)
                    {instance = new XmldbRequestProcessorFactory("exist", useDefaultUser);}
                return instance;
            } catch (final EXistException e) {
                throw new XmlRpcException("Failed to initialize XMLRPC interface: " + e.getMessage(), e);
            }
        }
    }

    private static class DefaultHandlerMapping extends AbstractReflectiveHandlerMapping {

        private DefaultHandlerMapping() throws XmlRpcException {
        }

        public void loadDefault(Class<?> clazz) throws XmlRpcException {
            registerPublicMethods("Default", clazz);
        }

        public XmlRpcHandler getHandler(String pHandlerName) throws XmlRpcNoSuchHandlerException, XmlRpcException {
            if (pHandlerName.indexOf('.') < 0)
                {pHandlerName = "Default." + pHandlerName;}
            return super.getHandler(pHandlerName);
        }
    }
}
TOP

Related Classes of org.exist.xmlrpc.RpcServlet$XmldbRequestProcessorFactoryFactory

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.