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    import com.liferay.portal.util.PortalUtil;
032    
033    import java.io.IOException;
034    import java.io.InputStream;
035    
036    import javax.servlet.ServletException;
037    import javax.servlet.http.HttpServlet;
038    import javax.servlet.http.HttpServletRequest;
039    import javax.servlet.http.HttpServletResponse;
040    
041    /**
042     * @author Alexander Chow
043     * @author Brian Wing Shun Chan
044     */
045    public class XmlRpcServlet extends HttpServlet {
046    
047            @Override
048            protected void doGet(
049                            HttpServletRequest request, HttpServletResponse response)
050                    throws IOException, ServletException {
051    
052                    PortalUtil.sendError(
053                            HttpServletResponse.SC_NOT_FOUND,
054                            new IllegalArgumentException("The GET method is not supported"),
055                            request, response);
056            }
057    
058            @Override
059            protected void doPost(
060                    HttpServletRequest request, HttpServletResponse response) {
061    
062                    Response xmlRpcResponse = null;
063    
064                    try {
065                            long companyId = PortalInstances.getCompanyId(request);
066    
067                            String token = getToken(request);
068    
069                            InputStream is = request.getInputStream();
070    
071                            String xml = StringUtil.read(is);
072    
073                            Tuple methodTuple = XmlRpcParser.parseMethod(xml);
074    
075                            String methodName = (String)methodTuple.getObject(0);
076                            Object[] args = (Object[])methodTuple.getObject(1);
077    
078                            xmlRpcResponse = invokeMethod(companyId, token, methodName, args);
079                    }
080                    catch (IOException ioe) {
081                            xmlRpcResponse = XmlRpcUtil.createFault(
082                                    XmlRpcConstants.NOT_WELL_FORMED, "XML is not well formed");
083    
084                            if (_log.isDebugEnabled()) {
085                                    _log.debug(ioe, ioe);
086                            }
087                    }
088                    catch (XmlRpcException xmlrpce) {
089                            _log.error(xmlrpce, xmlrpce);
090                    }
091    
092                    if (xmlRpcResponse == null) {
093                            xmlRpcResponse = XmlRpcUtil.createFault(
094                                    XmlRpcConstants.SYSTEM_ERROR, "Unknown error occurred");
095                    }
096    
097                    response.setCharacterEncoding(StringPool.UTF8);
098                    response.setContentType(ContentTypes.TEXT_XML);
099                    response.setStatus(HttpServletResponse.SC_OK);
100    
101                    try {
102                            ServletResponseUtil.write(response, xmlRpcResponse.toXml());
103                    }
104                    catch (Exception e) {
105                            if (_log.isWarnEnabled()) {
106                                    _log.warn(e, e);
107                            }
108    
109                            response.setStatus(HttpServletResponse.SC_PRECONDITION_FAILED);
110                    }
111            }
112    
113            protected String getToken(HttpServletRequest request) {
114                    String token = request.getPathInfo();
115    
116                    return HttpUtil.fixPath(token);
117            }
118    
119            protected Response invokeMethod(
120                            long companyId, String token, String methodName, Object[] arguments)
121                    throws XmlRpcException {
122    
123                    Method method = XmlRpcMethodUtil.getMethod(token, methodName);
124    
125                    if (method == null) {
126                            return XmlRpcUtil.createFault(
127                                    XmlRpcConstants.REQUESTED_METHOD_NOT_FOUND,
128                                    "Requested method not found");
129                    }
130    
131                    if (!method.setArguments(arguments)) {
132                            return XmlRpcUtil.createFault(
133                                    XmlRpcConstants.INVALID_METHOD_PARAMETERS,
134                                    "Method arguments are invalid");
135                    }
136    
137                    return method.execute(companyId);
138            }
139    
140            private static final Log _log = LogFactoryUtil.getLog(XmlRpcServlet.class);
141    
142    }