0001: /*
0002: * Portions Copyright 2000-2007 Sun Microsystems, Inc. All Rights
0003: * Reserved. Use is subject to license terms.
0004: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
0005: *
0006: * This program is free software; you can redistribute it and/or
0007: * modify it under the terms of the GNU General Public License version
0008: * 2 only, as published by the Free Software Foundation.
0009: *
0010: * This program is distributed in the hope that it will be useful, but
0011: * WITHOUT ANY WARRANTY; without even the implied warranty of
0012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
0013: * General Public License version 2 for more details (a copy is
0014: * included at /legal/license.txt).
0015: *
0016: * You should have received a copy of the GNU General Public License
0017: * version 2 along with this work; if not, write to the Free Software
0018: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
0019: * 02110-1301 USA
0020: *
0021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
0022: * Clara, CA 95054 or visit www.sun.com if you need additional
0023: * information or have any questions.
0024: */
0025: /*
0026: */
0027: package gov.nist.siplite.address;
0028:
0029: import java.util.*;
0030:
0031: import gov.nist.core.*;
0032: import gov.nist.siplite.SIPConstants;
0033: import gov.nist.siplite.parser.Lexer;
0034:
0035: /**
0036: * The SipURI structure.
0037: *
0038: *
0039: * <a href="{@docRoot}/uncopyright.html">This code is in the public domain.</a>
0040: *
0041: */
0042: public class SipURI extends URI {
0043:
0044: /**
0045: * Authority for the uri.
0046: */
0047: protected Authority authority = null;
0048:
0049: /**
0050: * uriParms list
0051: */
0052: protected NameValueList uriParms = null;
0053:
0054: /**
0055: * qheaders list
0056: */
0057: protected NameValueList qheaders = null;
0058:
0059: /**
0060: * telephoneSubscriber field
0061: */
0062: protected TelephoneNumber telephoneSubscriber = null;
0063:
0064: /** Flag if the instance is server URI. */
0065: private boolean isServer = false;
0066:
0067: /** Flag if the instance is server shared URI. */
0068: private boolean isShared = false;
0069:
0070: /** Default constructor. */
0071: public SipURI() {
0072: scheme = SIPConstants.SCHEME_SIP;
0073: uriParms = new NameValueList("uriparms");
0074: qheaders = new NameValueList("qheaders");
0075: qheaders.setSeparator("&");
0076: }
0077:
0078: /**
0079: * Sets URI as server.
0080: */
0081: public void setServer() {
0082: isServer = true;
0083: }
0084:
0085: /**
0086: * Sets URI as shared.
0087: */
0088: public void setShared() {
0089: isShared = true;
0090: }
0091:
0092: /**
0093: * Checks if URI is shared.
0094: * @return true when URI is server else false
0095: */
0096: public boolean isServer() {
0097: return isServer;
0098: }
0099:
0100: /**
0101: * Checks if URI is shared.
0102: * @return true when URI is shared else false
0103: */
0104: public boolean isShared() {
0105: return isShared;
0106: }
0107:
0108: /**
0109: * Constructor given the scheme.
0110: * The scheme must be either Sip or Sips.
0111: * @param scheme URI protocol scheme
0112: * @exception IllegalArgumentException if scheme is no sip or sips
0113: */
0114: public void setScheme(String scheme)
0115: throws IllegalArgumentException {
0116: if (compareToIgnoreCase(scheme, SIPConstants.SCHEME_SIP) != 0
0117: && compareToIgnoreCase(scheme, SIPConstants.SCHEME_SIPS) != 0)
0118: throw new IllegalArgumentException("bad scheme " + scheme);
0119: this .scheme = scheme.toLowerCase();
0120: }
0121:
0122: /**
0123: * Clears all URI Parameters.
0124: * @since v1.0
0125: */
0126: public void clearUriParms() {
0127: uriParms = new NameValueList("uriparms");
0128: }
0129:
0130: /**
0131: * Clears the password from the user part if it exists.
0132: */
0133: public void clearPassword() {
0134: if (this .authority != null) {
0135: UserInfo userInfo = authority.getUserInfo();
0136: if (userInfo != null)
0137: userInfo.clearPassword();
0138: }
0139: }
0140:
0141: /**
0142: * Gets the authority.
0143: * @return the authority information
0144: */
0145: public Authority getAuthority() {
0146: return this .authority;
0147: }
0148:
0149: /**
0150: * Clears all Qheaders.
0151: */
0152: public void clearQheaders() {
0153: qheaders = new NameValueList("qheaders");
0154: }
0155:
0156: /**
0157: * Compares two URIs and return true if they are equal.
0158: * @param that the object to compare to.
0159: * @return true if the object is equal to this object.
0160: */
0161: public boolean equals(Object that) {
0162:
0163: if (that == null) {
0164: return false;
0165: }
0166:
0167: if (!this .getClass().equals(that.getClass())) {
0168: return false;
0169: }
0170:
0171: SipURI other = (SipURI) that;
0172:
0173: // Compare the authority portion.
0174: if (!this .authority.equals(other.authority))
0175: return false;
0176:
0177: // compare the parameter lists.
0178: // Assuming default for TRANSPORT so cannot use the method
0179: // of NameValueList to check for equality
0180: NameValueList hisParms = other.uriParms;
0181:
0182: // transport comparation
0183: String currTransport = uriParms.getValueDefault(
0184: SIPConstants.GENERAL_TRANSPORT,
0185: SIPConstants.TRANSPORT_UDP);
0186: String hisTransport = hisParms.getValueDefault(
0187: SIPConstants.GENERAL_TRANSPORT,
0188: SIPConstants.TRANSPORT_UDP);
0189: if (!currTransport.equals(hisTransport))
0190: return false;
0191:
0192: // other parameters
0193: Enumeration enumNames = uriParms.getKeys();
0194: String currKey;
0195: while (enumNames.hasMoreElements()) {
0196: currKey = (String) enumNames.nextElement();
0197: if (currKey.equals(SIPConstants.GENERAL_TRANSPORT))
0198: continue;
0199: // check that other URI contains this parameter
0200: if (!hisParms.hasNameValue(currKey))
0201: return false;
0202: // check that other URI contains the same value of this parameter
0203: if (!uriParms.getValue(currKey).equals(
0204: hisParms.getValue(currKey)))
0205: return false;
0206: }
0207:
0208: // leave headers alone - they are just a screwy way of constructing
0209: // an entire sip message header as part of a URL.
0210: return true;
0211: }
0212:
0213: /**
0214: * Constructs a URL from the parsed structure.
0215: * @return String
0216: */
0217: public String encode() {
0218: StringBuffer retval = new StringBuffer(scheme)
0219: .append(Separators.COLON);
0220: if (authority != null) {
0221: retval.append(authority.encode());
0222: }
0223:
0224: if (!uriParms.isEmpty()) {
0225: retval.append(Separators.SEMICOLON).append(
0226: uriParms.encode());
0227: }
0228:
0229: if (!qheaders.isEmpty()) {
0230: retval.append(Separators.QUESTION)
0231: .append(qheaders.encode());
0232: }
0233:
0234: return retval.toString();
0235: }
0236:
0237: /**
0238: * Returns a string representation.
0239: *
0240: * @return the String representation of this URI.
0241: */
0242: public String toString() {
0243: return this .encode();
0244: }
0245:
0246: /**
0247: * Gets user at host information.
0248: * @return user@host portion of the uri (null if none exists).
0249: */
0250: public String getUserAtHost() {
0251: if (authority == null) {
0252: return null;
0253: }
0254:
0255: String user = authority.getUserInfo().getUser();
0256: String host = authority.getHost().encode();
0257:
0258: return new StringBuffer(user).append(Separators.AT)
0259: .append(host).toString();
0260: }
0261:
0262: /**
0263: * Returns the URI part of the address (without parameters)
0264: * i.e. scheme:user@host:port.
0265: * @return URI part of the address
0266: */
0267: public String getPlainURI() {
0268: StringBuffer retval = new StringBuffer(scheme)
0269: .append(Separators.COLON);
0270: retval.append(getUserAtHostPort());
0271: return retval.toString();
0272: }
0273:
0274: /**
0275: * Gets user at host and port infromation.
0276: * instead of the current function body.
0277: * @return user@host portion of the uri (null if none exists).
0278: */
0279: public String getUserAtHostPort() {
0280: return authority.encode();
0281: }
0282:
0283: /**
0284: * Gets the parameter (do a name lookup) and return null if none exists.
0285: * @param parmname Name of the parameter to get.
0286: * @return Parameter of the given name (null if none exists).
0287: */
0288: public Object getParm(String parmname) {
0289: Object obj = uriParms.getValue(parmname);
0290: return obj;
0291: }
0292:
0293: /**
0294: * Gets the method parameter.
0295: * @return Method parameter.
0296: */
0297: public String getMethod() {
0298: return (String) getParm(SIPConstants.GENERAL_METHOD);
0299: }
0300:
0301: /**
0302: * Accessor for URI parameters
0303: * @return A name-value list containing the parameters.
0304: */
0305: public NameValueList getUriParms() {
0306: return uriParms;
0307: }
0308:
0309: /**
0310: * Removes the URI parameters.
0311: *
0312: */
0313: public void removeUriParms() {
0314: uriParms = new NameValueList();
0315: }
0316:
0317: /**
0318: * Accessor forSIPObjects
0319: * @return Get the query headers (that appear after the ? in
0320: * the URL)
0321: */
0322: public NameValueList getQheaders() {
0323: return qheaders;
0324: }
0325:
0326: /**
0327: * Gets the user parameter.
0328: * @return User parameter (user= phone or user=ip).
0329: */
0330: public String getUserType() {
0331: return (String) uriParms.getValue(SIPConstants.GENERAL_USER);
0332: }
0333:
0334: /**
0335: * Gets the password of the user.
0336: * @return User password when it embedded as part of the uri
0337: * ( a very bad idea).
0338: */
0339: public String getUserPassword() {
0340: if (authority == null)
0341: return null;
0342: return authority.getPassword();
0343: }
0344:
0345: /**
0346: * Sets the user password.
0347: * @param password - password to set.
0348: * @throws IllegalArgumentException if password contains invalid
0349: * characters
0350: */
0351: public void setUserPassword(String password)
0352: throws IllegalArgumentException {
0353: if (authority == null) {
0354: authority = new Authority();
0355: }
0356: authority.setPassword(password);
0357: }
0358:
0359: /**
0360: * Returns the stucture corresponding to the telephone number
0361: * provided that the user is a telephone subscriber.
0362: * @return TelephoneNumber part of the url (only makes sense
0363: * when user = phone is specified)
0364: */
0365: public TelephoneNumber getTelephoneSubscriber() {
0366: if (telephoneSubscriber == null) {
0367: telephoneSubscriber = new TelephoneNumber();
0368: }
0369: return telephoneSubscriber;
0370: }
0371:
0372: /**
0373: * Gets the host and port of the server.
0374: * @return get the host:port part of the url parsed into a
0375: * structure.
0376: */
0377: public HostPort getHostPort() {
0378: if (authority == null)
0379: return null;
0380: else {
0381: return authority.getHostPort();
0382: }
0383: }
0384:
0385: /**
0386: * Gets the port from the authority field.
0387: *
0388: * @return the port from the authority field.
0389: */
0390: public int getPort() {
0391: HostPort hp = this .getHostPort();
0392:
0393: if (hp == null) {
0394: return -1;
0395: }
0396:
0397: return hp.getPort();
0398: }
0399:
0400: /**
0401: * Returns default port number according to current scheme.
0402: *
0403: * @return Current scheme default port.
0404: */
0405: public int getDefaultPort() {
0406: return (scheme.equalsIgnoreCase(SIPConstants.SCHEME_SIP)) ? SIPConstants.DEFAULT_NONTLS_PORT
0407: : SIPConstants.DEFAULT_TLS_PORT;
0408: }
0409:
0410: /**
0411: * Gets the host protion of the URI.
0412: * @return the host portion of the url.
0413: */
0414: public String getHost() {
0415: Host h = authority.getHost();
0416: return (h == null) ? "" : h.encode();
0417: }
0418:
0419: /**
0420: * Returns true if the user is a telephone subscriber.
0421: * If the host is an Internet telephony
0422: * gateway, a telephone-subscriber field MAY be used instead
0423: * of a user field. The telephone-subscriber field uses the
0424: * notation of RFC 2806 [19]. Any characters of the un-escaped
0425: * "telephone-subscriber" that are not either in the set
0426: * "unreserved" or "user-unreserved" MUST be escaped. The set
0427: * of characters not reserved in the RFC 2806 description of
0428: * telephone-subscriber contains a number of characters in
0429: * various syntax elements that need to be escaped when used
0430: * in SIP URLs, for example quotation marks (%22), hash (%23),
0431: * colon (%3a), at-sign (%40) and the "unwise" characters,
0432: * i.e., punctuation of %5b and above.
0433: * <p>
0434: * The telephone number is a special case of a user name and
0435: * cannot be distinguished by a BNF. Thus, a URL parameter,
0436: * user, is added to distinguish telephone numbers from user
0437: * names.
0438: * <p>
0439: * The user parameter value "phone" indicates that the user
0440: * part contains a telephone number. Even without this
0441: * parameter, recipients of SIP URLs MAY interpret the pre-@
0442: * part as a telephone number if local restrictions on the
0443: * @return true if the user is a telephone subscriber.
0444: */
0445: public boolean isUserTelephoneSubscriber() {
0446: String usrtype = (String) uriParms.getValue(USER);
0447: if (usrtype == null) {
0448: return false;
0449: }
0450: return usrtype.equals("phone");
0451: }
0452:
0453: /**
0454: * Removes the ttl value from the parameter list if it exists.
0455: */
0456: public void removeTTL() {
0457: if (uriParms != null)
0458: uriParms.delete(TTL);
0459: }
0460:
0461: /**
0462: * Removes the maddr param if it exists.
0463: */
0464: public void removeMAddr() {
0465: if (uriParms != null)
0466: uriParms.delete(MADDR);
0467: }
0468:
0469: /**
0470: * Deletes the transport string.
0471: */
0472: public void removeTransport() {
0473: if (uriParms != null)
0474: uriParms.delete(TRANSPORT);
0475: }
0476:
0477: /**
0478: * Removes a header given its name (provided it exists).
0479: * @param name name of the header to remove.
0480: */
0481: public void removeHeader(String name) {
0482: if (qheaders != null)
0483: qheaders.delete(name);
0484: }
0485:
0486: /**
0487: * Removes all headers.
0488: */
0489: public void removeHeaders() {
0490: qheaders = new NameValueList("qheaders");
0491: }
0492:
0493: /**
0494: * Sets the user type.
0495: */
0496: public void removeUserType() {
0497: if (uriParms != null)
0498: uriParms.delete(USER);
0499: }
0500:
0501: /**
0502: * Removes the port setting.
0503: */
0504: public void removePort() {
0505: authority.removePort();
0506: }
0507:
0508: /**
0509: * Removes the Method.
0510: */
0511: public void removeMethod() {
0512: if (uriParms != null)
0513: uriParms.delete(METHOD);
0514: }
0515:
0516: /**
0517: * Sets the user of SipURI. The identifier of a particular resource at
0518: * the host being addressed. The user and the user password including the
0519: * "at" sign make up the user-info.
0520: *
0521: * @param uname the new String value of the user.
0522: * @throws IllegalArgumentException if the user name is invalid
0523: */
0524: public void setUser(String uname) throws IllegalArgumentException {
0525: if (authority == null) {
0526: authority = new Authority();
0527: }
0528:
0529: authority.setUser(uname);
0530: }
0531:
0532: /**
0533: * Removes the user.
0534: */
0535: public void removeUser() {
0536: authority.removeUserInfo();
0537: }
0538:
0539: /**
0540: * Sets the default parameters for this URI.
0541: * Do nothing if the parameter is already set to some value.
0542: * Otherwise set it to the given value.
0543: * @param name Name of the parameter to set.
0544: * @param value value of the parameter to set.
0545: */
0546: public void setDefaultParm(String name, Object value) {
0547: if (uriParms.getValue(name) == null) {
0548: NameValue nv = new NameValue(name, value);
0549: uriParms.add(nv);
0550: }
0551: }
0552:
0553: /**
0554: * Sets the authority member
0555: * @param newAuthority Authority to set.
0556: */
0557: public void setAuthority(Authority newAuthority) {
0558: authority = newAuthority;
0559: }
0560:
0561: /**
0562: * Sets the host for this URI.
0563: * @param h host to set.
0564: */
0565: public void setHost(Host h) {
0566: // authority = new Authority();
0567: if (authority == null) {
0568: authority = new Authority();
0569: }
0570: authority.setHost(h);
0571: }
0572:
0573: /**
0574: * Sets the qheaders member.
0575: * @param parms query headers to set.
0576: */
0577: public void setQheaders(NameValueList parms) {
0578: qheaders = parms;
0579: }
0580:
0581: /**
0582: * Sets the MADDR parameter.
0583: * @param mAddr Host Name to set
0584: */
0585: public void setMAddr(String mAddr) {
0586: NameValue nameValue = uriParms.getNameValue(MADDR);
0587: Host host = new Host();
0588: host.setAddress(mAddr);
0589:
0590: if (nameValue != null) {
0591: nameValue.setValue(host);
0592: } else {
0593: nameValue = new NameValue(MADDR, host);
0594: uriParms.add(nameValue);
0595: }
0596: }
0597:
0598: /**
0599: * Sets the value of the user parameter. The user URI parameter exists to
0600: * distinguish telephone numbers from user names that happen to look like
0601: * telephone numbers. This is equivalent to setParameter("user", user).
0602: *
0603: * @param usertype new value String value of the method parameter
0604: */
0605: public void setUserParam(String usertype) {
0606: uriParms.delete(USER);
0607: uriParms.add(USER, usertype);
0608: }
0609:
0610: /**
0611: * Sets the Method.
0612: * @param method method parameter
0613: */
0614: public void setMethod(String method) {
0615: uriParms.add(METHOD, method);
0616: }
0617:
0618: /**
0619: * Sets ISDN subaddress of SipURL.
0620: * @param isdnSubAddress ISDN subaddress
0621: */
0622: public void setIsdnSubAddress(String isdnSubAddress) {
0623: if (telephoneSubscriber == null)
0624: telephoneSubscriber = new TelephoneNumber();
0625: telephoneSubscriber.setIsdnSubaddress(isdnSubAddress);
0626: }
0627:
0628: /**
0629: * Sets the telephone subscriber field.
0630: * @param tel Telephone subscriber field to set.
0631: */
0632: public void setTelephoneSubscriber(TelephoneNumber tel) {
0633: telephoneSubscriber = tel;
0634: }
0635:
0636: /**
0637: * Sets the port to a given value.
0638: * @param p Port to set.
0639: */
0640: public void setPort(int p) {
0641: if (authority == null)
0642: authority = new Authority();
0643: authority.setPort(p);
0644: }
0645:
0646: /**
0647: * Boolean to check if a parameter of a given name exists.
0648: * @param name Name of the parameter to check on.
0649: * @return a boolean indicating whether the parameter exists.
0650: */
0651: public boolean hasParameter(String name) {
0652: return uriParms.getValue(name) != null;
0653: }
0654:
0655: /**
0656: * Sets the query header when provided as a name-value pair.
0657: * @param nameValue qeuery header provided as a name,value pair.
0658: */
0659: public void setQHeader(NameValue nameValue) {
0660: this .qheaders.set(nameValue);
0661: }
0662:
0663: /**
0664: * Sets the parameter as given.
0665: * @param nameValue - parameter to set.
0666: */
0667: public void setUriParameter(NameValue nameValue) {
0668: uriParms.set(nameValue);
0669: }
0670:
0671: /**
0672: * Returns true if the transport parameter is defined.
0673: * @return true if transport appears as a parameter and false otherwise.
0674: */
0675: public boolean hasTransport() {
0676: return hasParameter(TRANSPORT);
0677: }
0678:
0679: /**
0680: * Removes a parameter given its name
0681: * @param name -- name of the parameter to remove.
0682: */
0683: public void removeParameter(String name) {
0684: uriParms.delete(name);
0685: }
0686:
0687: /**
0688: * Sets the hostPort field of the embedded authority field.
0689: * @param hostPort is the hostPort to set.
0690: */
0691: public void setHostPort(HostPort hostPort) {
0692: if (this .authority == null) {
0693: this .authority = new Authority();
0694: }
0695: authority.setHostPort(hostPort);
0696: }
0697:
0698: /**
0699: * Copies the current instance.
0700: * @return copy of current instance
0701: */
0702: public Object clone() {
0703: SipURI retval = new SipURI();
0704: retval.scheme = new String(this .scheme);
0705: retval.authority = (Authority) this .authority.clone();
0706: retval.uriParms = (NameValueList) this .uriParms.clone();
0707: if (this .qheaders != null)
0708: retval.qheaders = (NameValueList) this .qheaders.clone();
0709:
0710: if (this .telephoneSubscriber != null) {
0711: retval.telephoneSubscriber = (TelephoneNumber) this .telephoneSubscriber
0712: .clone();
0713: }
0714: return retval;
0715: }
0716:
0717: /**
0718: * Returns the value of the named header, or null if it is not set.
0719: * SIP/SIPS URIs may specify headers. As an example, the URI
0720: * sip:joe@jcp.org?priority=urgent has a header "priority" whose
0721: * value is "urgent".
0722: *
0723: * @param name name of header to retrieve
0724: * @return the value of specified header
0725: */
0726: public String getHeader(String name) {
0727: return this .qheaders.getValue(name) != null ? this .qheaders
0728: .getValue(name).toString() : null;
0729:
0730: }
0731:
0732: /**
0733: * Returns an Iterator over the names (Strings) of all headers present
0734: * in this SipURI.
0735: *
0736: * @return an Iterator over all the header names
0737: */
0738: public Vector getHeaderNames() {
0739: return this .qheaders.getNames();
0740: }
0741:
0742: /**
0743: * Returns the value of the <code>lr</code> parameter, or null if this
0744: * is not set. This is equivalent to getParameter("lr").
0745: *
0746: * @return the value of the <code>lr</code> parameter
0747: */
0748: public String getLrParam() {
0749: boolean haslr = this .hasParameter(LR);
0750: return haslr ? "true" : null;
0751: }
0752:
0753: /**
0754: * Returns the value of the <code>maddr</code> parameter, or null if this
0755: * is not set. This is equivalent to getParameter("maddr").
0756: *
0757: * @return the value of the <code>maddr</code> parameter
0758: */
0759: public String getMAddrParam() {
0760: NameValue maddr = uriParms.getNameValue(MADDR);
0761: if (maddr == null)
0762: return null;
0763: String host = (String) maddr.getValue();
0764: return host;
0765: }
0766:
0767: /**
0768: * Returns the value of the <code>method</code> parameter, or null if this
0769: * is not set. This is equivalent to getParameter("method").
0770: *
0771: * @return the value of the <code>method</code> parameter
0772: */
0773: public String getMethodParam() {
0774: return this .getParameter(METHOD);
0775: }
0776:
0777: /**
0778: * Returns the value of the named parameter, or null if it is not set. A
0779: *
0780: * zero-length String indicates flag parameter.
0781: *
0782: *
0783: *
0784: * @param name name of parameter to retrieve
0785: *
0786: * @return the value of specified parameter
0787: *
0788: */
0789: public String getParameter(String name) {
0790: Object val = uriParms.getValue(name.toLowerCase());
0791: if (val == null)
0792: return null;
0793: if (val instanceof GenericObject)
0794: return ((GenericObject) val).encode();
0795: else
0796: return val.toString();
0797: }
0798:
0799: /**
0800: * Returns an Iterator over the names (Strings) of all parameters present
0801: * in this ParametersHeader.
0802: * @return an Iterator over all the parameter names
0803: *
0804: */
0805: public Vector getParameterNames() {
0806: return uriParms.getNames();
0807: }
0808:
0809: /**
0810: * Returns the value of the "ttl" parameter, or -1 if this is not set.
0811: * This method is equivalent to getParameter("ttl").
0812: *
0813: * @return the value of the <code>ttl</code> parameter
0814: */
0815: public int getTTLParam() {
0816: Integer ttl = (Integer) uriParms
0817: .getValue(SIPConstants.GENERAL_TTL);
0818: if (ttl != null)
0819: return ttl.intValue();
0820: else
0821: return -1;
0822: }
0823:
0824: /**
0825: * Returns the value of the "transport" parameter, or null if this is not
0826: * set. This is equivalent to getParameter("transport").
0827: *
0828: * @return the transport paramter of the SipURI
0829: */
0830: public String getTransportParam() {
0831: if (uriParms != null) {
0832: return (String) uriParms
0833: .getValue(SIPConstants.GENERAL_TRANSPORT);
0834: } else
0835: return null;
0836: }
0837:
0838: /**
0839: * Returns the value of the "type" parameter, or null if this is not
0840: * set. This is equivalent to getParameter("type"). The result is
0841: * unquoted
0842: *
0843: * @return the transport paramter of the SipURI
0844: */
0845: public String getTypeParam() {
0846: if (uriParms != null) {
0847: NameValue nv = uriParms
0848: .getNameValue(SIPConstants.GENERAL_TYPE);
0849: if (nv == null) {
0850: return null;
0851: }
0852: return (String) nv.getUnquotedValue();
0853: } else
0854: return null;
0855: }
0856:
0857: /**
0858: * Returns the value of the <code>userParam</code>,
0859: * or null if this is not set.
0860: * <p>
0861: * This is equivalent to getParameter("user").
0862: *
0863: * @return the value of the <code>userParam</code> of the SipURI
0864: */
0865: public String getUser() {
0866: return authority.getUser();
0867: }
0868:
0869: /**
0870: * Returns true if this SipURI is secure i.e. if this SipURI represents a
0871: * sips URI. A sip URI returns false.
0872: *
0873: * @return <code>true</code> if this SipURI represents a sips URI, and
0874: * <code>false</code> if it represents a sip URI.
0875: */
0876: public boolean isSecure() {
0877: return equalsIgnoreCase(this .getScheme(),
0878: SIPConstants.SCHEME_SIPS);
0879: }
0880:
0881: /**
0882: * This method determines if this is a URI with a scheme of "sip"
0883: * or "sips".
0884: *
0885: * @return true if the scheme is "sip" or "sips", false otherwise.
0886: */
0887: public boolean isSipURI() {
0888: return true;
0889: }
0890:
0891: /**
0892: * This method determines if this is a URI with a scheme of
0893: * "tel"
0894: *
0895: * @return true if the scheme is "tel", false otherwise.
0896: */
0897: public boolean isTelURL() {
0898: return false;
0899: }
0900:
0901: /**
0902: * Sets the value of the specified header fields to be included in a
0903: * request constructed from the URI. If the header already had a value it
0904: * will be overwritten.
0905: *
0906: * @param name - a String specifying the header name
0907: * @param value - a String specifying the header value
0908: */
0909: public void setHeader(String name, String value) {
0910: if (qheaders.getValue(name) == null) {
0911: NameValue nv = new NameValue(name, value);
0912: qheaders.add(nv);
0913: } else {
0914: NameValue nv = qheaders.getNameValue(name);
0915: nv.setValue(value);
0916: }
0917: }
0918:
0919: /**
0920: * Returns the host part of this SipURI.
0921: * @param host the host part of the URI
0922: * @throws IllegalArgumentException if the host part is formated wrong way
0923: */
0924: public void setHost(String host) throws IllegalArgumentException {
0925: Lexer lexer = new Lexer("sip_urlLexer", host);
0926: HostNameParser hnp = new HostNameParser(lexer);
0927: try {
0928: Host h = new Host(hnp.hostName());
0929: if (GenericObject
0930: .compareToIgnoreCase(h.getHostname(), host) == 0) {
0931: this .setHost(h);
0932: return;
0933: }
0934: } catch (ParseException e) {
0935: throw new IllegalArgumentException(e.getMessage());
0936: }
0937: throw new IllegalArgumentException("Illegal host name");
0938: }
0939:
0940: /**
0941: * Sets the value of the <code>lr</code> parameter of this SipURI. The lr
0942: * parameter, when present, indicates that the element responsible for
0943: * this resource implements the routing mechanisms specified in RFC 3261.
0944: * This parameter will be used in the URIs proxies place in the
0945: * Record-Route header field values, and may appear in the URIs in a
0946: * pre-existing route set.
0947: */
0948: public void setLrParam() {
0949: if (uriParms.getValue(SIPConstants.GENERAL_LR) != null)
0950: return;
0951: NameValue nv = new NameValue(SIPConstants.GENERAL_LR, null);
0952: uriParms.add(nv);
0953: }
0954:
0955: /**
0956: * Sets the value of the <code>maddr</code> parameter of this SipURI. The
0957: * maddr parameter indicates the server address to be contacted for this
0958: * user, overriding any address derived from the host field. This is
0959: * equivalent to setParameter("maddr", maddr).
0960: *
0961: * @param maddr new value of the <code>maddr</code> parameter
0962: */
0963: public void setMAddrParam(String maddr) throws ParseException {
0964: if (maddr == null)
0965: throw new NullPointerException("bad maddr");
0966: setParameter(SIPConstants.GENERAL_MADDR, maddr);
0967: }
0968:
0969: /**
0970: * Sets the value of the <code>method</code> parameter. This specifies
0971: * which SIP method to use in requests directed at this URI. This is
0972: * equivalent to setParameter("method", method).
0973: *
0974: * @param method - new value String value of the method parameter
0975: */
0976: public void setMethodParam(String method) throws ParseException {
0977: setParameter(SIPConstants.GENERAL_METHOD, method);
0978: }
0979:
0980: /**
0981: * Sets the value of the specified parameter. If the parameter already had
0982: * a value it will be overwritten. A zero-length String indicates flag
0983: * parameter.
0984: *
0985: * @param name - a String specifying the parameter name
0986: * @param value - a String specifying the parameter value
0987: * @throws ParseException which signals that an error has been reached
0988: * unexpectedly while parsing the parameter name or value.
0989: */
0990: public void setParameter(String name, String value)
0991: throws ParseException {
0992: if (name.equalsIgnoreCase(SIPConstants.GENERAL_TTL)) {
0993: try {
0994: int ttl = Integer.parseInt(value);
0995: } catch (NumberFormatException ex) {
0996: throw new ParseException("bad parameter " + value, 0);
0997: }
0998: }
0999: name = name.toLowerCase();
1000: // If the value is null the parameter is interpreted
1001: // as a parameter without value.
1002: NameValue nv = new NameValue(name, (value == null) ? "" : value);
1003: uriParms.delete(name);
1004: uriParms.add(nv);
1005: }
1006:
1007: /**
1008: * Sets the scheme of this URI to sip or sips depending on whether the
1009: * argument is true or false. The default value is false.
1010: *
1011: * @param secure - the boolean value indicating if the SipURI is secure.
1012: */
1013: public void setSecure(boolean secure) {
1014: if (secure)
1015: scheme = SIPConstants.SCHEME_SIPS;
1016: else
1017: scheme = SIPConstants.SCHEME_SIP;
1018: }
1019:
1020: /**
1021: * Sets the value of the <code>ttl</code> parameter. The ttl parameter
1022: * specifies the time-to-live value when packets are sent using UDP
1023: * multicast. This is equivalent to setParameter("ttl", ttl).
1024: *
1025: * @param ttl - new value of the <code>ttl</code> parameter
1026: */
1027: public void setTTLParam(int ttl) throws IllegalArgumentException {
1028: if (ttl <= 0)
1029: throw new IllegalArgumentException("Bad ttl value");
1030: if (uriParms != null) {
1031: uriParms.delete(SIPConstants.GENERAL_TTL);
1032: NameValue nv = new NameValue(SIPConstants.GENERAL_TTL,
1033: new Integer(ttl));
1034: uriParms.add(nv);
1035: }
1036: }
1037:
1038: /**
1039: * Sets the value of the "transport" parameter. This parameter specifies
1040: * which transport protocol to use for sending requests and responses to
1041: * this entity. The following values are defined: "udp", "tcp", "sctp",
1042: * "tls", but other values may be used also. This method is equivalent to
1043: * setParameter("transport", transport). Transport parameter constants
1044: * are defined in the {@link gov.nist.siplite.ListeningPoint}.
1045: *
1046: * @param transport - new value for the "transport" parameter
1047: * @see gov.nist.siplite.ListeningPoint
1048: */
1049: public void setTransportParam(String transport)
1050: throws ParseException {
1051: if (transport == null)
1052: throw new NullPointerException("null arg");
1053: if (equalsIgnoreCase(transport, SIPConstants.TRANSPORT_UDP)
1054: || equalsIgnoreCase(transport,
1055: SIPConstants.TRANSPORT_TCP)) {
1056: NameValue nv = new NameValue(
1057: SIPConstants.GENERAL_TRANSPORT, transport
1058: .toLowerCase());
1059: uriParms.delete(SIPConstants.GENERAL_TRANSPORT);
1060: uriParms.add(nv);
1061: } else
1062: throw new ParseException("bad transport " + transport, 0);
1063: }
1064:
1065: /**
1066: * Returns the user part of this SipURI, or null if it is not set.
1067: *
1068: * @return the user part of this SipURI
1069: */
1070: public String getUserParam() {
1071: return getParameter("user");
1072: }
1073:
1074: /**
1075: * Returns whether the the <code>lr</code> parameter is set. This is
1076: * equivalent to hasParameter("lr"). This interface has no getLrParam as
1077: * RFC3261 does not specify any values for the "lr" paramater.
1078: *
1079: * @return true if the "lr" parameter is set, false otherwise.
1080: */
1081: public boolean hasLrParam() {
1082: return uriParms.getNameValue(SIPConstants.GENERAL_LR) != null;
1083: }
1084: }
|