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 " + methodName + " ");
070    
071                            if (arguments != null) {
072                                    for (int i = 0; i < arguments.length; i++) {
073                                            sb.append(arguments[i]);
074    
075                                            if (i < (arguments.length - 1)) {
076                                                    sb.append(", ");
077                                            }
078                                    }
079                            }
080    
081                            _log.debug(sb.toString());
082                    }
083    
084                    String requestXML = XmlRpcParser.buildMethod(methodName, arguments);
085    
086                    Http.Options options = new Http.Options();
087    
088                    if (_HTTP_HEADER_VERSION_VERBOSITY_DEFAULT) {
089                    }
090                    else if (_HTTP_HEADER_VERSION_VERBOSITY_PARTIAL) {
091                            options.addHeader(HttpHeaders.USER_AGENT, ReleaseInfo.getName());
092                    }
093                    else {
094                            options.addHeader(
095                                    HttpHeaders.USER_AGENT, ReleaseInfo.getServerInfo());
096                    }
097    
098                    options.setBody(requestXML, ContentTypes.TEXT_XML, StringPool.UTF8);
099                    options.setLocation(url);
100                    options.setPost(true);
101    
102                    String responseXML = HttpUtil.URLtoString(options);
103    
104                    return XmlRpcParser.parseResponse(responseXML);
105            }
106    
107            private static final boolean _HTTP_HEADER_VERSION_VERBOSITY_DEFAULT =
108                    PropsValues.HTTP_HEADER_VERSION_VERBOSITY.equalsIgnoreCase(
109                            ReleaseInfo.getName());
110    
111            private static final boolean _HTTP_HEADER_VERSION_VERBOSITY_PARTIAL =
112                    PropsValues.HTTP_HEADER_VERSION_VERBOSITY.equalsIgnoreCase("partial");
113    
114            private static Log _log = LogFactoryUtil.getLog(XmlRpcImpl.class);
115    
116    }