Search in sources :

Example 1 with PeerUnavailableException

use of javax.sip.PeerUnavailableException in project XobotOS by xamarin.

the class SipUri method equals.

/**
     * Compare two URIs and return true if they are equal.
     * @param that the object to compare to.
     * @return true if the object is equal to this object.
     *
     * JvB: Updated to define equality in terms of API methods, according to the rules
     * in RFC3261 section 19.1.4
     *
     * Jean Deruelle: Updated to define equality of API methods, according to the rules
     * in RFC3261 section 19.1.4 convert potential ie :
     *    %HEX HEX encoding parts of the URI before comparing them
     *    transport param added in comparison
     *    header equality enforced in comparison
     *
     */
@SuppressWarnings("unchecked")
@Override
public boolean equals(Object that) {
    // Shortcut for same object
    if (that == this)
        return true;
    if (that instanceof SipURI) {
        final SipURI a = this;
        final SipURI b = (SipURI) that;
        // A SIP and SIPS URI are never equivalent
        if (a.isSecure() ^ b.isSecure())
            return false;
        // components must match; comparison of userinfo is case-sensitive
        if (a.getUser() == null ^ b.getUser() == null)
            return false;
        if (a.getUserPassword() == null ^ b.getUserPassword() == null)
            return false;
        if (a.getUser() != null && !RFC2396UrlDecoder.decode(a.getUser()).equals(RFC2396UrlDecoder.decode(b.getUser())))
            return false;
        if (a.getUserPassword() != null && !RFC2396UrlDecoder.decode(a.getUserPassword()).equals(RFC2396UrlDecoder.decode(b.getUserPassword())))
            return false;
        if (a.getHost() == null ^ b.getHost() == null)
            return false;
        if (a.getHost() != null && !a.getHost().equalsIgnoreCase(b.getHost()))
            return false;
        if (a.getPort() != b.getPort())
            return false;
        // URI parameters
        for (Iterator i = a.getParameterNames(); i.hasNext(); ) {
            String pname = (String) i.next();
            String p1 = a.getParameter(pname);
            String p2 = b.getParameter(pname);
            // those present in both must match (case-insensitive)
            if (p1 != null && p2 != null && !RFC2396UrlDecoder.decode(p1).equalsIgnoreCase(RFC2396UrlDecoder.decode(p2)))
                return false;
        }
        // transport, user, ttl or method must match when present in either
        if (a.getTransportParam() == null ^ b.getTransportParam() == null)
            return false;
        if (a.getUserParam() == null ^ b.getUserParam() == null)
            return false;
        if (a.getTTLParam() == -1 ^ b.getTTLParam() == -1)
            return false;
        if (a.getMethodParam() == null ^ b.getMethodParam() == null)
            return false;
        if (a.getMAddrParam() == null ^ b.getMAddrParam() == null)
            return false;
        // Headers: must match according to their definition.
        if (a.getHeaderNames().hasNext() && !b.getHeaderNames().hasNext())
            return false;
        if (!a.getHeaderNames().hasNext() && b.getHeaderNames().hasNext())
            return false;
        if (a.getHeaderNames().hasNext() && b.getHeaderNames().hasNext()) {
            HeaderFactory headerFactory = null;
            try {
                headerFactory = SipFactory.getInstance().createHeaderFactory();
            } catch (PeerUnavailableException e) {
                Debug.logError("Cannot get the header factory to parse the header of the sip uris to compare", e);
                return false;
            }
            for (Iterator i = a.getHeaderNames(); i.hasNext(); ) {
                String hname = (String) i.next();
                String h1 = a.getHeader(hname);
                String h2 = b.getHeader(hname);
                if (h1 == null && h2 != null)
                    return false;
                if (h2 == null && h1 != null)
                    return false;
                // The following check should not be needed but we add it for findbugs.
                if (h1 == null && h2 == null)
                    continue;
                try {
                    Header header1 = headerFactory.createHeader(hname, RFC2396UrlDecoder.decode(h1));
                    Header header2 = headerFactory.createHeader(hname, RFC2396UrlDecoder.decode(h2));
                    // those present in both must match according to the equals method of the corresponding header
                    if (!header1.equals(header2))
                        return false;
                } catch (ParseException e) {
                    Debug.logError("Cannot parse one of the header of the sip uris to compare " + a + " " + b, e);
                    return false;
                }
            }
        }
        // Finally, we can conclude that they are indeed equal
        return true;
    }
    return false;
}
Also used : PeerUnavailableException(javax.sip.PeerUnavailableException) Header(javax.sip.header.Header) ParseException(java.text.ParseException) SipURI(javax.sip.address.SipURI) HeaderFactory(javax.sip.header.HeaderFactory)

Aggregations

ParseException (java.text.ParseException)1 PeerUnavailableException (javax.sip.PeerUnavailableException)1 SipURI (javax.sip.address.SipURI)1 Header (javax.sip.header.Header)1 HeaderFactory (javax.sip.header.HeaderFactory)1