/*****************************************************************************
 *                                                                           *
 *         Licensed Materials - Property of IBM                              *
 *                                                                           *
 *         "Restricted Materials of IBM"                                     *
 *                                                                           *
 *         5799-QRY                                                          *
 *                                                                           *
 *         (C) Copyright IBM Corp. 1998                                      *
 *                                                                           *
 *****************************************************************************/
package com.ibm.devtools.SIPNoTE;

import java.io.*;
import java.util.*;

/**
 * This class represents the data contained in the Via: header
 * field of a SIP message.</p>
 *
 * For example:
 * <pre>
 *     Via: SIP/2.0/TCP 9.2.20.17:1107
 * </pre>
 *
 * @author Bill Nagy
 * @version 1.0
 */
public class ViaEntity
{
  /** The version of SIP used at this host. */
  String version = new String("SIP/2.0");
  /** The transport used to transmit the message from this host. */
  String transport = new String("TCP");
  /** The name of the host */
  String host;
  /** The port number needed to talk to this host */
  int port = 1107;

  /**
   * Parses the Via: data from a string, and fills in the object.
   *
   * @param viastr The string to be parsed.
   * @exception MalformedHeaderException
   *            Thrown if the Via: statement in the string is malformed.
   */
  public void fromString(String viastr)
       throws MalformedHeaderException
  {
    StringTokenizer st = new StringTokenizer(viastr);
    try 
      {
	/*Throw away the "Via:"*/
	st.nextToken();

	//fixme
	st.nextToken();
	//	version = st.nextToken();
	//	transport = st.nextToken();

	host = st.nextToken(" :");
      }
    catch (NoSuchElementException e)
      {
	throw new MalformedHeaderException("Malformed VIA: --> "+viastr);
      }

    try
      {
	port = (new Integer(st.nextToken())).intValue();
      }
    catch (NoSuchElementException e)
      {
      }
  }

  /**
   * Constructs a Via: string from the data present within the object.
   *
   * @return The string representation of this Via:.
   * @exception MalformedHeaderException
   *            Thrown if enough data is not present within the object to
   *            build the string.
   */
  public String toViaString()
       throws MalformedHeaderException
  {
    if (host == null)
      throw new MalformedHeaderException("Missing hostname");

    if (port == 0)
      return new String("Via: "+version+"/"+transport+" "+host);
    else
      return new String("Via: "+version+"/"+transport+" "+host+":"+port);
  }

  /**
   * Returns the version portion of the Via:.
   * 
   * @return The version
   */
  public String getVersion()
  {
    return version;
  }
  
  /**
   * Sets the version portion of the Via:.
   *
   * @param ver The new version.
   */
  public void setVersion(String ver)
  {
    version = ver;
    return;
  }

  /**
   * Returns the transport portion of the Via:.
   *
   * @return The transport protocol.
   */
  public String getTransport()
  {
    return transport;
  }
  
  /**
   * Sets the transport portion of the Via:.
   *
   * @param prot The new protocol.
   */
  public void setTransport(String prot)
  {
    //fixme
    //need to check to see if either UDP or TCP
    transport = prot;
    return;
  }

  /**
   * Returns the host portion of the Via:.
   * 
   * @return The host.
   */
  public String getHost()
  {
    return host;
  }
  
  /**
   * Sets the version portion of the Via:.
   * 
   * @param id The new host.
   */
  public void setHost(String id)
  {
    host = id;
    return;
  }

  /**
   * Returns the port number of the Via:.
   * 
   * @return The port portion of the Via.
   */
  public int getPort()
  {
    return port;
  }

  /**
   * Sets the port number of the Via:.
   *
   * @param num The new port number.
   */
  public void setPort(int num)
  {
    port = num;
    return;
  }

  public static void main(String argv[])
       throws Exception
  {
    String input;
    ViaEntity via = new ViaEntity();
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    
    System.out.println("Enter the via line:");
    input = br.readLine();
    via.fromString(input);
    
    System.out.println();
    System.out.println("Version: "+via.getVersion());
    System.out.println("Transport: "+via.getTransport());
    System.out.println("Host: "+via.getHost());
    System.out.println("Port: "+via.getPort());
    System.out.println(via.toViaString());
  }
}

