001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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    import com.liferay.portal.util.PropsValues;
032    
033    /**
034     * @author Alexander Chow
035     * @author Brian Wing Shun Chan
036     */
037    public class XmlRpcImpl implements XmlRpc {
038    
039            public Fault createFault(int code, String description) {
040                    return new FaultImpl(code, description);
041            }
042    
043            public Success createSuccess(String description) {
044                    return new SuccessImpl(description);
045            }
046    
047            public Response executeMethod(
048                            String url, String methodName, Object[] arguments)
049                    throws XmlRpcException {
050    
051                    try {
052                            return doExecuteMethod(url, methodName, arguments);
053                    }
054                    catch (Exception e) {
055                            throw new XmlRpcException(e);
056                    }
057            }
058    
059            protected Response doExecuteMethod(
060                            String url, String methodName, Object[] arguments)
061                    throws Exception {
062    
063                    if (_log.isDebugEnabled()) {
064                            StringBundler sb = new StringBundler();
065    
066                            sb.append("XML-RPC invoking " + methodName + " ");
067    
068                            if (arguments != null) {
069                                    for (int i = 0; i < arguments.length; i++) {
070                                            sb.append(arguments[i]);
071    
072                                            if (i < (arguments.length - 1)) {
073                                                    sb.append(", ");
074                                            }
075                                    }
076                            }
077    
078                            _log.debug(sb.toString());
079                    }
080    
081                    String requestXML = XmlRpcParser.buildMethod(methodName, arguments);
082    
083                    Http.Options options = new Http.Options();
084    
085                    if (_HTTP_HEADER_VERSION_VERBOSITY_DEFAULT) {
086                    }
087                    else if (_HTTP_HEADER_VERSION_VERBOSITY_PARTIAL) {
088                            options.addHeader(HttpHeaders.USER_AGENT, ReleaseInfo.getName());
089                    }
090                    else {
091                            options.addHeader(
092                                    HttpHeaders.USER_AGENT, ReleaseInfo.getServerInfo());
093                    }
094    
095                    options.setBody(requestXML, ContentTypes.TEXT_XML, StringPool.UTF8);
096                    options.setLocation(url);
097                    options.setPost(true);
098    
099                    String responseXML = HttpUtil.URLtoString(options);
100    
101                    return XmlRpcParser.parseResponse(responseXML);
102            }
103    
104            private static final boolean _HTTP_HEADER_VERSION_VERBOSITY_DEFAULT =
105                    PropsValues.HTTP_HEADER_VERSION_VERBOSITY.equalsIgnoreCase(
106                            ReleaseInfo.getName());
107    
108            private static final boolean _HTTP_HEADER_VERSION_VERBOSITY_PARTIAL =
109                    PropsValues.HTTP_HEADER_VERSION_VERBOSITY.equalsIgnoreCase("partial");
110    
111            private static Log _log = LogFactoryUtil.getLog(XmlRpcImpl.class);
112    
113    }