/*
 * The contents of this file are subject to the Mozilla Public License Version 1.1
 * (the "License"); you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at <http://www.mozilla.org/MPL/>.
 * 
 * Software distributed under the License is distributed on an "AS IS" basis, WITHOUT
 * WARRANTY OF ANY KIND, either express or implied. See the License for the specific
 * language governing rights and limitations under the License.
 * 
 * The Original Code is the Venice Web Communities System.
 * 
 * The Initial Developer of the Original Code is Eric J. Bowersox <erbo@silcom.com>,
 * for Silverwrist Design Studios.  Portions created by Eric J. Bowersox are
 * Copyright (C) 2002 Eric J. Bowersox/Silverwrist Design Studios.  All Rights Reserved.
 * 
 * Contributor(s): 
 */
package com.silverwrist.venice.ui.rpc;

import java.io.IOException;
import com.silverwrist.util.StringUtil;
import com.silverwrist.venice.except.SupplyFaultCode;
import com.silverwrist.venice.ui.*;
import com.silverwrist.venice.ui.helpers.ThrowableContent;

public class XmlRpcFault extends ThrowableContent implements ContentExecute
{
  /*--------------------------------------------------------------------------------
   * Static data members
   *--------------------------------------------------------------------------------
   */

  // Some "standard" XML-RPC error codes.
  public static final int INVALID_REQUEST = -32600;
  public static final int METHOD_NOT_FOUND = -32601;
  public static final int INVALID_PARAMS = -32602;
  public static final int INTERNAL_ERROR = -32603;
  public static final int APPLICATION_ERROR = -32500;
  public static final int SERVER_ERROR = -32400;
  public static final int TRANSPORT_ERROR = -32300;
  public static final int DEFAULT_ERROR = 0;

  /*--------------------------------------------------------------------------------
   * Attributes
   *--------------------------------------------------------------------------------
   */

  private int fault_code = DEFAULT_ERROR;

  /*--------------------------------------------------------------------------------
   * Constructors
   *--------------------------------------------------------------------------------
   */

  public XmlRpcFault(String msg)
  {
    super(msg);

  } // end constructor

  public XmlRpcFault(int code, String msg)
  {
    super(msg);
    fault_code = code;

  } // end constructor

  public XmlRpcFault(Throwable t)
  {
    super(t);
    if (t instanceof SupplyFaultCode)
    { // retrieve the fault code
      SupplyFaultCode sfc = (SupplyFaultCode)t;
      fault_code = sfc.getFaultCode();

    } // end if

  } // end constructor

  public XmlRpcFault(int code, Throwable t)
  {
    super(t);
    fault_code = code;

  } // end constructor

  public XmlRpcFault(String msg, Throwable t)
  {
    super(msg,t);
    if (t instanceof SupplyFaultCode)
    { // retrieve the fault code
      SupplyFaultCode sfc = (SupplyFaultCode)t;
      fault_code = sfc.getFaultCode();

    } // end if

  } // end constructor

  public XmlRpcFault(int code, String msg, Throwable t)
  {
    super(msg,t);
    fault_code = code;

  } // end constructor

  /*--------------------------------------------------------------------------------
   * Implementations from interface ContentExecute
   *--------------------------------------------------------------------------------
   */

  public void execute(RequestExec req) throws IOException
  {
    StringBuffer b =
        new StringBuffer("<?xml version=\"1.0\"?>\r\n<methodResponse><fault><value><struct>\r\n<member>\r\n"
			 + "<name>faultCode</name>\r\n<value><int>");
    b.append(fault_code);
    b.append("</int></value>\r\n</member>\r\n<member>\r\n<name>faultString</name>\r\n<value><string>");
    b.append(StringUtil.encodeHTML(getMessage()));
    b.append("</string></value>\r\n</member>\r\n</struct></value></fault></methodResponse>\r\n");
    byte[] raw = b.toString().getBytes("UTF-8");
    req.sendBinary("text/xml",raw);

  } // end execute

  /*--------------------------------------------------------------------------------
   * External operations
   *--------------------------------------------------------------------------------
   */

  public final int getFaultCode()
  {
    return fault_code;

  } // end getFaultCode

} // end class XmlRpcFault
