001
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.util.ContentTypes;
020 import com.liferay.portal.kernel.util.HttpUtil;
021 import com.liferay.portal.kernel.util.StringPool;
022 import com.liferay.portal.kernel.util.StringUtil;
023 import com.liferay.portal.kernel.util.Tuple;
024 import com.liferay.portal.kernel.xmlrpc.Method;
025 import com.liferay.portal.kernel.xmlrpc.Response;
026 import com.liferay.portal.kernel.xmlrpc.XmlRpcConstants;
027 import com.liferay.portal.kernel.xmlrpc.XmlRpcException;
028 import com.liferay.portal.kernel.xmlrpc.XmlRpcUtil;
029 import com.liferay.portal.util.PortalInstances;
030 import com.liferay.util.servlet.ServletResponseUtil;
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
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 protected void doPost(
098 HttpServletRequest request, HttpServletResponse response) {
099
100 Response xmlRpcResponse = null;
101
102 try {
103 long companyId = PortalInstances.getCompanyId(request);
104
105 String token = getToken(request);
106
107 InputStream is = request.getInputStream();
108
109 String xml = StringUtil.read(is);
110
111 Tuple methodTuple = XmlRpcParser.parseMethod(xml);
112
113 String methodName = (String)methodTuple.getObject(0);
114 Object[] args = (Object[])methodTuple.getObject(1);
115
116 xmlRpcResponse = invokeMethod(companyId, token, methodName, args);
117 }
118 catch (IOException ioe) {
119 xmlRpcResponse = XmlRpcUtil.createFault(
120 XmlRpcConstants.NOT_WELL_FORMED, "XML is not well formed");
121
122 if (_log.isDebugEnabled()) {
123 _log.debug(ioe, ioe);
124 }
125 }
126 catch (XmlRpcException xmlrpce) {
127 _log.error(xmlrpce, xmlrpce);
128 }
129
130 if (xmlRpcResponse == null) {
131 xmlRpcResponse = XmlRpcUtil.createFault(
132 XmlRpcConstants.SYSTEM_ERROR, "Unknown error occurred");
133 }
134
135 response.setCharacterEncoding(StringPool.UTF8);
136 response.setContentType(ContentTypes.TEXT_XML);
137 response.setStatus(HttpServletResponse.SC_OK);
138
139 try {
140 ServletResponseUtil.write(response, xmlRpcResponse.toXml());
141 }
142 catch (Exception e) {
143 if (_log.isWarnEnabled()) {
144 _log.warn(e, e);
145 }
146
147 response.setStatus(HttpServletResponse.SC_PRECONDITION_FAILED);
148 }
149 }
150
151 protected Method getMethod(String token, String methodName) {
152 Method method = null;
153
154 Map<String, Method> tokenMethods = _methodRegistry.get(token);
155
156 if (tokenMethods != null) {
157 method = tokenMethods.get(methodName);
158 }
159
160 return method;
161 }
162
163 protected String getToken(HttpServletRequest request) {
164 String token = request.getPathInfo();
165
166 return HttpUtil.fixPath(token);
167 }
168
169 protected Response invokeMethod(
170 long companyId, String token, String methodName, Object[] arguments)
171 throws XmlRpcException {
172
173 Method method = getMethod(token, methodName);
174
175 if (method == null) {
176 return XmlRpcUtil.createFault(
177 XmlRpcConstants.REQUESTED_METHOD_NOT_FOUND,
178 "Requested method not found");
179 }
180
181 if (!method.setArguments(arguments)) {
182 return XmlRpcUtil.createFault(
183 XmlRpcConstants.INVALID_METHOD_PARAMETERS,
184 "Method arguments are invalid");
185 }
186
187 return method.execute(companyId);
188 }
189
190 private static Log _log = LogFactoryUtil.getLog(XmlRpcServlet.class);
191
192 private static Map<String, Map<String, Method>> _methodRegistry =
193 new HashMap<String, Map<String, Method>>();
194
195 }