001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.xmlrpc;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.kernel.servlet.ServletResponseUtil;
020    import com.liferay.portal.kernel.util.ContentTypes;
021    import com.liferay.portal.kernel.util.HttpUtil;
022    import com.liferay.portal.kernel.util.StringPool;
023    import com.liferay.portal.kernel.util.StringUtil;
024    import com.liferay.portal.kernel.util.Tuple;
025    import com.liferay.portal.kernel.xmlrpc.Method;
026    import com.liferay.portal.kernel.xmlrpc.Response;
027    import com.liferay.portal.kernel.xmlrpc.XmlRpcConstants;
028    import com.liferay.portal.kernel.xmlrpc.XmlRpcException;
029    import com.liferay.portal.kernel.xmlrpc.XmlRpcUtil;
030    import com.liferay.portal.util.PortalInstances;
031    
032    import java.io.IOException;
033    import java.io.InputStream;
034    
035    import javax.servlet.http.HttpServlet;
036    import javax.servlet.http.HttpServletRequest;
037    import javax.servlet.http.HttpServletResponse;
038    
039    /**
040     * @author Alexander Chow
041     * @author Brian Wing Shun Chan
042     */
043    public class XmlRpcServlet extends HttpServlet {
044    
045            @Override
046            protected void doPost(
047                    HttpServletRequest request, HttpServletResponse response) {
048    
049                    Response xmlRpcResponse = null;
050    
051                    try {
052                            long companyId = PortalInstances.getCompanyId(request);
053    
054                            String token = getToken(request);
055    
056                            InputStream is = request.getInputStream();
057    
058                            String xml = StringUtil.read(is);
059    
060                            Tuple methodTuple = XmlRpcParser.parseMethod(xml);
061    
062                            String methodName = (String)methodTuple.getObject(0);
063                            Object[] args = (Object[])methodTuple.getObject(1);
064    
065                            xmlRpcResponse = invokeMethod(companyId, token, methodName, args);
066                    }
067                    catch (IOException ioe) {
068                            xmlRpcResponse = XmlRpcUtil.createFault(
069                                    XmlRpcConstants.NOT_WELL_FORMED, "XML is not well formed");
070    
071                            if (_log.isDebugEnabled()) {
072                                    _log.debug(ioe, ioe);
073                            }
074                    }
075                    catch (XmlRpcException xmlrpce) {
076                            _log.error(xmlrpce, xmlrpce);
077                    }
078    
079                    if (xmlRpcResponse == null) {
080                            xmlRpcResponse = XmlRpcUtil.createFault(
081                                    XmlRpcConstants.SYSTEM_ERROR, "Unknown error occurred");
082                    }
083    
084                    response.setCharacterEncoding(StringPool.UTF8);
085                    response.setContentType(ContentTypes.TEXT_XML);
086                    response.setStatus(HttpServletResponse.SC_OK);
087    
088                    try {
089                            ServletResponseUtil.write(response, xmlRpcResponse.toXml());
090                    }
091                    catch (Exception e) {
092                            if (_log.isWarnEnabled()) {
093                                    _log.warn(e, e);
094                            }
095    
096                            response.setStatus(HttpServletResponse.SC_PRECONDITION_FAILED);
097                    }
098            }
099    
100            protected String getToken(HttpServletRequest request) {
101                    String token = request.getPathInfo();
102    
103                    return HttpUtil.fixPath(token);
104            }
105    
106            protected Response invokeMethod(
107                            long companyId, String token, String methodName, Object[] arguments)
108                    throws XmlRpcException {
109    
110                    Method method = XmlRpcMethodUtil.getMethod(token, methodName);
111    
112                    if (method == null) {
113                            return XmlRpcUtil.createFault(
114                                    XmlRpcConstants.REQUESTED_METHOD_NOT_FOUND,
115                                    "Requested method not found");
116                    }
117    
118                    if (!method.setArguments(arguments)) {
119                            return XmlRpcUtil.createFault(
120                                    XmlRpcConstants.INVALID_METHOD_PARAMETERS,
121                                    "Method arguments are invalid");
122                    }
123    
124                    return method.execute(companyId);
125            }
126    
127            private static final Log _log = LogFactoryUtil.getLog(XmlRpcServlet.class);
128    
129    }