/*
 * 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.dynamo.xmlrpc;

import java.io.*;
import com.silverwrist.util.*;
import com.silverwrist.dynamo.HttpStatusCode;
import com.silverwrist.dynamo.except.*;
import com.silverwrist.dynamo.iface.*;

public class FaultCode extends Throwable implements SelfRenderable
{
  /*--------------------------------------------------------------------------------
   * Static data members
   *--------------------------------------------------------------------------------
   */

  public static final int SYSTEM_FIRST = -32768;
  public static final int SYSTEM_LAST = -32000;

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

  protected int m_code;

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

  public FaultCode()
  {
    super();
    m_code = 0;

  } // end constructor

  public FaultCode(int code)
  {
    super();
    m_code = (isSystemFaultCode(code) ? 0 : code);

  } // end constructor

  public FaultCode(String message)
  {
    super(message);
    m_code = 0;

  } // end constructor

  public FaultCode(int code, String message)
  {
    super(message);
    m_code = (isSystemFaultCode(code) ? 0 : code);

  } // end constructor

  public FaultCode(Throwable inner)
  {
    super(inner.getMessage(),inner);
    if (inner instanceof FaultCode)
      m_code = ((FaultCode)inner).getCode();
    else
      m_code = 0;

  } // end constructor

  public FaultCode(int code, Throwable inner)
  {
    super(inner.getMessage(),inner);
    m_code = (isSystemFaultCode(code) ? 0 : code);

  } // end constructor

  public FaultCode(String message, Throwable inner)
  {
    super(message,inner);
    if (inner instanceof FaultCode)
      m_code = ((FaultCode)inner).getCode();
    else
      m_code = 0;

  } // end constructor

  public FaultCode(int code, String message, Throwable inner)
  {
    super(message,inner);
    m_code = (isSystemFaultCode(code) ? 0 : code);

  } // end constructor

  /*--------------------------------------------------------------------------------
   * Implementations from interface SelfRenderable
   *--------------------------------------------------------------------------------
   */

  public void render(SelfRenderControl control) throws IOException, RenderingException
  {
    // Create the rendered structure as raw text.
    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(m_code);
    b.append("</int></value>\r\n</member>\r\n<member>\r\n<name>faultString</name>\r\n<value><string>");
    b.append(StringUtils.encodeHTML(getMessage()));
    b.append("</string></value>\r\n</member>\r\n</struct></value></fault></methodResponse>\r\n");
    
    // Now render the binary equivalent of this structure.
    byte[] data = null;
    try
    { // get a complete rendering of this data
      data = b.toString().getBytes("UTF-8");

    } // end try
    catch (UnsupportedEncodingException e)
    { // WTF?  shouldn't happen
      data = b.toString().getBytes();

    } // end catch

    // send out the bytes as an XML message body
    control.status(HttpStatusCode.S_OK);
    BinaryRenderControl bctrl = control.getBinaryRender();
    bctrl.setContentType("text/xml; charset=UTF-8");
    bctrl.setContentLength(data.length);
    OutputStream stm = bctrl.getStream();
    stm.write(data);
    stm.flush();
    
  } // end render

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

  public static final boolean isSystemFaultCode(int code)
  {
    return ((code>=SYSTEM_FIRST) && (code<=SYSTEM_LAST));

  } // end isSystemFaultCode

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

  public int getCode()
  {
    return m_code;

  } // end getCode

} // end class FaultCode
