1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.xmlrpc;
16  
17  import com.liferay.portal.kernel.log.Log;
18  import com.liferay.portal.kernel.log.LogFactoryUtil;
19  import com.liferay.portal.kernel.servlet.HttpHeaders;
20  import com.liferay.portal.kernel.util.ContentTypes;
21  import com.liferay.portal.kernel.util.Http;
22  import com.liferay.portal.kernel.util.HttpUtil;
23  import com.liferay.portal.kernel.util.ReleaseInfo;
24  import com.liferay.portal.kernel.util.StringBundler;
25  import com.liferay.portal.kernel.util.StringPool;
26  import com.liferay.portal.kernel.xmlrpc.Fault;
27  import com.liferay.portal.kernel.xmlrpc.Response;
28  import com.liferay.portal.kernel.xmlrpc.Success;
29  import com.liferay.portal.kernel.xmlrpc.XmlRpc;
30  import com.liferay.portal.kernel.xmlrpc.XmlRpcException;
31  
32  /**
33   * <a href="XmlRpcImpl.java.html"><b><i>View Source</i></b></a>
34   *
35   * @author Alexander Chow
36   * @author Brian Wing Shun Chan
37   */
38  public class XmlRpcImpl implements XmlRpc {
39  
40      public Fault createFault(int code, String description) {
41          return new FaultImpl(code, description);
42      }
43  
44      public Success createSuccess(String description) {
45          return new SuccessImpl(description);
46      }
47  
48      public Response executeMethod(
49              String url, String methodName, Object[] arguments)
50          throws XmlRpcException {
51  
52          try {
53              return doExecuteMethod(url, methodName, arguments);
54          }
55          catch (Exception e) {
56              throw new XmlRpcException(e);
57          }
58      }
59  
60      protected Response doExecuteMethod(
61              String url, String methodName, Object[] arguments)
62          throws Exception {
63  
64          if (_log.isDebugEnabled()) {
65              StringBundler sb = new StringBundler();
66  
67              sb.append("XML-RPC invoking " + methodName + " ");
68  
69              if (arguments != null) {
70                  for (int i = 0; i < arguments.length; i++) {
71                      sb.append(arguments[i]);
72  
73                      if (i < arguments.length - 1) {
74                          sb.append(", ");
75                      }
76                  }
77              }
78  
79              _log.debug(sb.toString());
80          }
81  
82          String requestXML = XmlRpcParser.buildMethod(methodName, arguments);
83  
84          Http.Options options = new Http.Options();
85  
86          options.addHeader(HttpHeaders.USER_AGENT, ReleaseInfo.getServerInfo());
87          options.setBody(requestXML, ContentTypes.TEXT_XML, StringPool.UTF8);
88          options.setLocation(url);
89          options.setPost(true);
90  
91          String responseXML = HttpUtil.URLtoString(options);
92  
93          return XmlRpcParser.parseResponse(responseXML);
94      }
95  
96      private static Log _log = LogFactoryUtil.getLog(XmlRpcImpl.class);
97  
98  }