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.util.StringBundler;
018    import com.liferay.portal.kernel.xmlrpc.Fault;
019    import com.liferay.portal.kernel.xmlrpc.XmlRpcException;
020    
021    /**
022     * @author Alexander Chow
023     * @author Brian Wing Shun Chan
024     */
025    public class FaultImpl implements Fault {
026    
027            public FaultImpl(int code, String description) {
028                    _code = code;
029                    _description = description;
030            }
031    
032            @Override
033            public int getCode() {
034                    return _code;
035            }
036    
037            @Override
038            public String getDescription() {
039                    return _description;
040            }
041    
042            @Override
043            public String toString() {
044                    return "XML-RPC fault " + _code + " " + _description;
045            }
046    
047            @Override
048            public String toXml() throws XmlRpcException {
049                    StringBundler sb = new StringBundler(17);
050    
051                    sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
052    
053                    sb.append("<methodResponse>");
054                    sb.append("<fault>");
055                    sb.append("<value>");
056                    sb.append("<struct>");
057                    sb.append("<member>");
058                    sb.append("<name>faultCode</name>");
059                    sb.append(XmlRpcParser.wrapValue(_code));
060                    sb.append("</member>");
061                    sb.append("<member>");
062                    sb.append("<name>faultString</name>");
063                    sb.append(XmlRpcParser.wrapValue(_description));
064                    sb.append("</member>");
065                    sb.append("</struct>");
066                    sb.append("</value>");
067                    sb.append("</fault>");
068                    sb.append("</methodResponse>");
069    
070                    return sb.toString();
071            }
072    
073            private int _code;
074            private String _description;
075    
076    }