Home | History | Annotate | Download | only in address
      1 /*
      2 * Conditions Of Use
      3 *
      4 * This software was developed by employees of the National Institute of
      5 * Standards and Technology (NIST), an agency of the Federal Government.
      6 * Pursuant to title 15 Untied States Code Section 105, works of NIST
      7 * employees are not subject to copyright protection in the United States
      8 * and are considered to be in the public domain.  As a result, a formal
      9 * license is not needed to use the software.
     10 *
     11 * This software is provided by NIST as a service and is expressly
     12 * provided "AS IS."  NIST MAKES NO WARRANTY OF ANY KIND, EXPRESS, IMPLIED
     13 * OR STATUTORY, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTY OF
     14 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT
     15 * AND DATA ACCURACY.  NIST does not warrant or make any representations
     16 * regarding the use of the software or the results thereof, including but
     17 * not limited to the correctness, accuracy, reliability or usefulness of
     18 * the software.
     19 *
     20 * Permission to use this software is contingent upon your acceptance
     21 * of the terms of this agreement
     22 *
     23 * .
     24 *
     25 */
     26 /*******************************************************************************
     27  * Product of NIST/ITL Advanced Networking Technologies Division (ANTD).        *
     28  *******************************************************************************/
     29 package gov.nist.javax.sip.address;
     30 import gov.nist.core.*;
     31 import javax.sip.address.*;
     32 
     33 /*
     34  * BUG Fix from Antonis Kadris.
     35  */
     36 /**
     37  * Address structure. Imbeds a URI and adds a display name.
     38  *
     39  *@author M. Ranganathan   <br/>
     40  *
     41  *
     42  *
     43  *@version 1.2 $Revision: 1.11 $ $Date: 2009/07/17 18:57:21 $
     44  *
     45  */
     46 public final class AddressImpl
     47     extends NetObject
     48     implements javax.sip.address.Address {
     49 
     50 
     51     private static final long serialVersionUID = 429592779568617259L;
     52 
     53     /** Constant field.
     54      */
     55     public static final int NAME_ADDR = 1;
     56 
     57     /** constant field.
     58      */
     59     public static final int ADDRESS_SPEC = 2;
     60 
     61     /** Constant field.
     62      */
     63     public static final int WILD_CARD = 3;
     64 
     65     protected int addressType;
     66 
     67     /** displayName field
     68      */
     69     protected String displayName;
     70 
     71     /** address field
     72      */
     73     protected GenericURI address;
     74 
     75     /** Match on the address only.
     76      * Dont care about the display name.
     77      */
     78 
     79     public boolean match(Object other) {
     80         // TODO -- add the matcher;
     81         if (other == null)
     82             return true;
     83         if (!(other instanceof Address))
     84             return false;
     85         else {
     86             AddressImpl that = (AddressImpl) other;
     87             if (that.getMatcher() != null)
     88                 return that.getMatcher().match(this.encode());
     89             else if (that.displayName != null && this.displayName == null)
     90                 return false;
     91             else if (that.displayName == null)
     92                 return address.match(that.address);
     93             else
     94                 return displayName.equalsIgnoreCase(that.displayName)
     95                     && address.match(that.address);
     96         }
     97 
     98     }
     99 
    100     /** Get the host port portion of the address spec.
    101      *@return host:port in a HostPort structure.
    102      */
    103     public HostPort getHostPort() {
    104         if (!(address instanceof SipUri))
    105             throw new RuntimeException("address is not a SipUri");
    106         SipUri uri = (SipUri) address;
    107         return uri.getHostPort();
    108     }
    109 
    110     /** Get the port from the imbedded URI. This assumes that a SIP URL
    111      * is encapsulated in this address object.
    112      *
    113      *@return the port from the address.
    114      *
    115      */
    116     public int getPort() {
    117         if (!(address instanceof SipUri))
    118             throw new RuntimeException("address is not a SipUri");
    119         SipUri uri = (SipUri) address;
    120         return uri.getHostPort().getPort();
    121     }
    122 
    123     /** Get the user@host:port for the address field. This assumes
    124      * that the encapsulated object is a SipUri.
    125      *
    126      *
    127      *@return string containing user@host:port.
    128      */
    129     public String getUserAtHostPort() {
    130         if (address instanceof SipUri) {
    131             SipUri uri = (SipUri) address;
    132             return uri.getUserAtHostPort();
    133         } else
    134             return address.toString();
    135     }
    136 
    137     /** Get the host name from the address.
    138      *
    139      *@return the host name.
    140      */
    141     public String getHost() {
    142         if (!(address instanceof SipUri))
    143             throw new RuntimeException("address is not a SipUri");
    144         SipUri uri = (SipUri) address;
    145         return uri.getHostPort().getHost().getHostname();
    146     }
    147 
    148     /** Remove a parameter from the address.
    149      *
    150      *@param parameterName is the name of the parameter to remove.
    151      */
    152     public void removeParameter(String parameterName) {
    153         if (!(address instanceof SipUri))
    154             throw new RuntimeException("address is not a SipUri");
    155         SipUri uri = (SipUri) address;
    156         uri.removeParameter(parameterName);
    157     }
    158 
    159     /**
    160      * Encode the address as a string and return it.
    161      * @return String canonical encoded version of this address.
    162      */
    163     public String encode() {
    164         return encode(new StringBuffer()).toString();
    165     }
    166 
    167     public StringBuffer encode(StringBuffer buffer) {
    168         if (this.addressType == WILD_CARD) {
    169             buffer.append('*');
    170         }
    171         else {
    172             if (displayName != null) {
    173                 buffer.append(DOUBLE_QUOTE)
    174                         .append(displayName)
    175                         .append(DOUBLE_QUOTE)
    176                         .append(SP);
    177             }
    178             if (address != null) {
    179                 if (addressType == NAME_ADDR || displayName != null)
    180                     buffer.append(LESS_THAN);
    181                 address.encode(buffer);
    182                 if (addressType == NAME_ADDR || displayName != null)
    183                     buffer.append(GREATER_THAN);
    184             }
    185         }
    186         return buffer;
    187     }
    188 
    189     public AddressImpl() {
    190         this.addressType = NAME_ADDR;
    191     }
    192 
    193     /**
    194      * Get the address type;
    195      * @return int
    196      */
    197     public int getAddressType() {
    198         return addressType;
    199     }
    200 
    201     /**
    202      * Set the address type. The address can be NAME_ADDR, ADDR_SPEC or
    203      * WILD_CARD
    204      *
    205      * @param atype int to set
    206      *
    207      */
    208     public void setAddressType(int atype) {
    209         addressType = atype;
    210     }
    211 
    212     /**
    213      * get the display name
    214      *
    215      * @return String
    216      *
    217      */
    218     public String getDisplayName() {
    219         return displayName;
    220     }
    221 
    222     /**
    223      * Set the displayName member
    224      *
    225      * @param displayName String to set
    226      *
    227      */
    228     public void setDisplayName(String displayName) {
    229         this.displayName = displayName;
    230         this.addressType = NAME_ADDR;
    231     }
    232 
    233     /**
    234      * Set the address field
    235      *
    236      * @param address SipUri to set
    237      *
    238      */
    239     public void setAddess(javax.sip.address.URI address) {
    240         this.address = (GenericURI) address;
    241     }
    242 
    243     /**
    244      * hashCode impelmentation
    245      *
    246      */
    247     public int hashCode() {
    248         return this.address.hashCode();
    249     }
    250 
    251     /**
    252      * Compare two address specs for equality.
    253      *
    254      * @param other Object to compare this this address
    255      *
    256      * @return boolean
    257      *
    258      */
    259     public boolean equals(Object other) {
    260 
    261         if (this==other) return true;
    262 
    263         if (other instanceof Address) {
    264             final Address o = (Address) other;
    265 
    266             // Don't compare display name (?)
    267             return this.getURI().equals( o.getURI() );
    268         }
    269         return false;
    270     }
    271 
    272     /** return true if DisplayName exist.
    273      *
    274      * @return boolean
    275      */
    276     public boolean hasDisplayName() {
    277         return (displayName != null);
    278     }
    279 
    280     /** remove the displayName field
    281      */
    282     public void removeDisplayName() {
    283         displayName = null;
    284     }
    285 
    286     /** Return true if the imbedded URI is a sip URI.
    287      *
    288      * @return true if the imbedded URI is a SIP URI.
    289      *
    290      */
    291     public boolean isSIPAddress() {
    292         return address instanceof SipUri;
    293     }
    294 
    295     /** Returns the URI address of this Address. The type of URI can be
    296      * determined by the scheme.
    297      *
    298      * @return address parmater of the Address object
    299      */
    300     public URI getURI() {
    301         return this.address;
    302     }
    303 
    304     /** This determines if this address is a wildcard address. That is
    305      * <code>Address.getAddress.getUserInfo() == *;</code>
    306      *
    307      * @return true if this name address is a wildcard, false otherwise.
    308      */
    309     public boolean isWildcard() {
    310         return this.addressType == WILD_CARD;
    311     }
    312 
    313     /** Sets the URI address of this Address. The URI can be either a
    314      * TelURL or a SipURI.
    315      *
    316      * @param address - the new URI address value of this NameAddress.
    317      */
    318     public void setURI(URI address) {
    319         this.address = (GenericURI) address;
    320     }
    321 
    322     /** Set the user name for the imbedded URI.
    323      *
    324      *@param user -- user name to set for the imbedded URI.
    325      */
    326     public void setUser(String user) {
    327         ((SipUri) this.address).setUser(user);
    328     }
    329 
    330     /** Mark this a wild card address type.
    331      * Also set the SIP URI to a special wild card address.
    332      */
    333     public void setWildCardFlag() {
    334         this.addressType = WILD_CARD;
    335         this.address = new SipUri();
    336         ((SipUri)this.address).setUser("*");
    337     }
    338 
    339     public Object clone() {
    340         AddressImpl retval = (AddressImpl) super.clone();
    341         if (this.address != null)
    342             retval.address = (GenericURI) this.address.clone();
    343         return retval;
    344     }
    345 
    346 }
    347