001    /**
002     * Copyright (c) 2000-2013 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    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            @Override
040            public Fault createFault(int code, String description) {
041                    return new FaultImpl(code, description);
042            }
043    
044            @Override
045            public Success createSuccess(String description) {
046                    return new SuccessImpl(description);
047            }
048    
049            @Override
050            public Response executeMethod(
051                            String url, String methodName, Object[] arguments)
052                    throws XmlRpcException {
053    
054                    try {
055                            return doExecuteMethod(url, methodName, arguments);
056                    }
057                    catch (Exception e) {
058                            throw new XmlRpcException(e);
059                    }
060            }
061    
062            protected Response doExecuteMethod(
063                            String url, String methodName, Object[] arguments)
064                    throws Exception {
065    
066                    if (_log.isDebugEnabled()) {
067                            StringBundler sb = new StringBundler();
068    
069                            sb.append("XML-RPC invoking ");
070                            sb.append(methodName);
071                            sb.append(" ");
072    
073                            if (arguments != null) {
074                                    for (int i = 0; i < arguments.length; i++) {
075                                            sb.append(arguments[i]);
076    
077                                            if (i < (arguments.length - 1)) {
078                                                    sb.append(", ");
079                                            }
080                                    }
081                            }
082    
083                            _log.debug(sb.toString());
084                    }
085    
086                    String requestXML = XmlRpcParser.buildMethod(methodName, arguments);
087    
088                    Http.Options options = new Http.Options();
089    
090                    if (_HTTP_HEADER_VERSION_VERBOSITY_DEFAULT) {
091                    }
092                    else if (_HTTP_HEADER_VERSION_VERBOSITY_PARTIAL) {
093                            options.addHeader(HttpHeaders.USER_AGENT, ReleaseInfo.getName());
094                    }
095                    else {
096                            options.addHeader(
097                                    HttpHeaders.USER_AGENT, ReleaseInfo.getServerInfo());
098                    }
099    
100                    options.setBody(requestXML, ContentTypes.TEXT_XML, StringPool.UTF8);
101                    options.setLocation(url);
102                    options.setPost(true);
103    
104                    String responseXML = HttpUtil.URLtoString(options);
105    
106                    return XmlRpcParser.parseResponse(responseXML);
107            }
108    
109            private static final boolean _HTTP_HEADER_VERSION_VERBOSITY_DEFAULT =
110                    PropsValues.HTTP_HEADER_VERSION_VERBOSITY.equalsIgnoreCase(
111                            ReleaseInfo.getName());
112    
113            private static final boolean _HTTP_HEADER_VERSION_VERBOSITY_PARTIAL =
114                    PropsValues.HTTP_HEADER_VERSION_VERBOSITY.equalsIgnoreCase("partial");
115    
116            private static Log _log = LogFactoryUtil.getLog(XmlRpcImpl.class);
117    
118    }