001    /**
002     * Copyright (c) 2000-2012 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 java.util.HashMap;
036    import java.util.Map;
037    
038    import javax.servlet.http.HttpServlet;
039    import javax.servlet.http.HttpServletRequest;
040    import javax.servlet.http.HttpServletResponse;
041    
042    /**
043     * @author Alexander Chow
044     * @author Brian Wing Shun Chan
045     */
046    public class XmlRpcServlet extends HttpServlet {
047    
048            public static void registerMethod(Method method) {
049                    if (method == null) {
050                            return;
051                    }
052    
053                    String token = method.getToken();
054                    String methodName = method.getMethodName();
055    
056                    Map<String, Method> tokenMethods = _methodRegistry.get(token);
057    
058                    if (tokenMethods == null) {
059                            tokenMethods = new HashMap<String, Method>();
060    
061                            _methodRegistry.put(token, tokenMethods);
062                    }
063    
064                    Method registeredMethod = tokenMethods.get(methodName);
065    
066                    if (registeredMethod != null) {
067                            _log.error(
068                                    "There is already an XML-RPC method registered with name " +
069                                            methodName + " at " + token);
070                    }
071                    else {
072                            tokenMethods.put(methodName, method);
073                    }
074            }
075    
076            public static void unregisterMethod(Method method) {
077                    if (method == null) {
078                            return;
079                    }
080    
081                    String token = method.getToken();
082                    String methodName = method.getMethodName();
083    
084                    Map<String, Method> tokenMethods = _methodRegistry.get(token);
085    
086                    if (tokenMethods == null) {
087                            return;
088                    }
089    
090                    tokenMethods.remove(methodName);
091    
092                    if (tokenMethods.isEmpty()) {
093                            _methodRegistry.remove(token);
094                    }
095            }
096    
097            @Override
098            protected void doPost(
099                    HttpServletRequest request, HttpServletResponse response) {
100    
101                    Response xmlRpcResponse = null;
102    
103                    try {
104                            long companyId = PortalInstances.getCompanyId(request);
105    
106                            String token = getToken(request);
107    
108                            InputStream is = request.getInputStream();
109    
110                            String xml = StringUtil.read(is);
111    
112                            Tuple methodTuple = XmlRpcParser.parseMethod(xml);
113    
114                            String methodName = (String)methodTuple.getObject(0);
115                            Object[] args = (Object[])methodTuple.getObject(1);
116    
117                            xmlRpcResponse = invokeMethod(companyId, token, methodName, args);
118                    }
119                    catch (IOException ioe) {
120                            xmlRpcResponse = XmlRpcUtil.createFault(
121                                    XmlRpcConstants.NOT_WELL_FORMED, "XML is not well formed");
122    
123                            if (_log.isDebugEnabled()) {
124                                    _log.debug(ioe, ioe);
125                            }
126                    }
127                    catch (XmlRpcException xmlrpce) {
128                            _log.error(xmlrpce, xmlrpce);
129                    }
130    
131                    if (xmlRpcResponse == null) {
132                            xmlRpcResponse = XmlRpcUtil.createFault(
133                                    XmlRpcConstants.SYSTEM_ERROR, "Unknown error occurred");
134                    }
135    
136                    response.setCharacterEncoding(StringPool.UTF8);
137                    response.setContentType(ContentTypes.TEXT_XML);
138                    response.setStatus(HttpServletResponse.SC_OK);
139    
140                    try {
141                            ServletResponseUtil.write(response, xmlRpcResponse.toXml());
142                    }
143                    catch (Exception e) {
144                            if (_log.isWarnEnabled()) {
145                                    _log.warn(e, e);
146                            }
147    
148                            response.setStatus(HttpServletResponse.SC_PRECONDITION_FAILED);
149                    }
150            }
151    
152            protected Method getMethod(String token, String methodName) {
153                    Method method = null;
154    
155                    Map<String, Method> tokenMethods = _methodRegistry.get(token);
156    
157                    if (tokenMethods != null) {
158                            method = tokenMethods.get(methodName);
159                    }
160    
161                    return method;
162            }
163    
164            protected String getToken(HttpServletRequest request) {
165                    String token = request.getPathInfo();
166    
167                    return HttpUtil.fixPath(token);
168            }
169    
170            protected Response invokeMethod(
171                            long companyId, String token, String methodName, Object[] arguments)
172                    throws XmlRpcException {
173    
174                    Method method = getMethod(token, methodName);
175    
176                    if (method == null) {
177                            return XmlRpcUtil.createFault(
178                                    XmlRpcConstants.REQUESTED_METHOD_NOT_FOUND,
179                                    "Requested method not found");
180                    }
181    
182                    if (!method.setArguments(arguments)) {
183                            return XmlRpcUtil.createFault(
184                                    XmlRpcConstants.INVALID_METHOD_PARAMETERS,
185                                    "Method arguments are invalid");
186                    }
187    
188                    return method.execute(companyId);
189            }
190    
191            private static Log _log = LogFactoryUtil.getLog(XmlRpcServlet.class);
192    
193            private static Map<String, Map<String, Method>> _methodRegistry =
194                    new HashMap<String, Map<String, Method>>();
195    
196    }