001    /**
002     * Copyright (c) 2000-2010 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.HttpHeaders;
020    import com.liferay.portal.kernel.util.ContentTypes;
021    import com.liferay.portal.kernel.util.Http;
022    import com.liferay.portal.kernel.util.HttpUtil;
023    import com.liferay.portal.kernel.util.ReleaseInfo;
024    import com.liferay.portal.kernel.util.StringBundler;
025    import com.liferay.portal.kernel.util.StringPool;
026    import com.liferay.portal.kernel.xmlrpc.Fault;
027    import com.liferay.portal.kernel.xmlrpc.Response;
028    import com.liferay.portal.kernel.xmlrpc.Success;
029    import com.liferay.portal.kernel.xmlrpc.XmlRpc;
030    import com.liferay.portal.kernel.xmlrpc.XmlRpcException;
031    
032    /**
033     * @author Alexander Chow
034     * @author Brian Wing Shun Chan
035     */
036    public class XmlRpcImpl implements XmlRpc {
037    
038            public Fault createFault(int code, String description) {
039                    return new FaultImpl(code, description);
040            }
041    
042            public Success createSuccess(String description) {
043                    return new SuccessImpl(description);
044            }
045    
046            public Response executeMethod(
047                            String url, String methodName, Object[] arguments)
048                    throws XmlRpcException {
049    
050                    try {
051                            return doExecuteMethod(url, methodName, arguments);
052                    }
053                    catch (Exception e) {
054                            throw new XmlRpcException(e);
055                    }
056            }
057    
058            protected Response doExecuteMethod(
059                            String url, String methodName, Object[] arguments)
060                    throws Exception {
061    
062                    if (_log.isDebugEnabled()) {
063                            StringBundler sb = new StringBundler();
064    
065                            sb.append("XML-RPC invoking " + methodName + " ");
066    
067                            if (arguments != null) {
068                                    for (int i = 0; i < arguments.length; i++) {
069                                            sb.append(arguments[i]);
070    
071                                            if (i < arguments.length - 1) {
072                                                    sb.append(", ");
073                                            }
074                                    }
075                            }
076    
077                            _log.debug(sb.toString());
078                    }
079    
080                    String requestXML = XmlRpcParser.buildMethod(methodName, arguments);
081    
082                    Http.Options options = new Http.Options();
083    
084                    options.addHeader(HttpHeaders.USER_AGENT, ReleaseInfo.getServerInfo());
085                    options.setBody(requestXML, ContentTypes.TEXT_XML, StringPool.UTF8);
086                    options.setLocation(url);
087                    options.setPost(true);
088    
089                    String responseXML = HttpUtil.URLtoString(options);
090    
091                    return XmlRpcParser.parseResponse(responseXML);
092            }
093    
094            private static Log _log = LogFactoryUtil.getLog(XmlRpcImpl.class);
095    
096    }