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