Search in sources :

Example 1 with SIPHeaderList

use of gov.nist.javax.sip.header.SIPHeaderList in project XobotOS by xamarin.

the class SIPMessage method removeHeader.

/**
     * Remove a header given its name. If multiple headers of a given name are present then the
     * top flag determines which end to remove headers from.
     * 
     * @param headerName is the name of the header to remove.
     * @param top -- flag that indicates which end of header list to process.
     */
public void removeHeader(String headerName, boolean top) {
    String headerNameLowerCase = SIPHeaderNamesCache.toLowerCase(headerName);
    SIPHeader toRemove = (SIPHeader) nameTable.get(headerNameLowerCase);
    // nothing to do then we are done.
    if (toRemove == null)
        return;
    if (toRemove instanceof SIPHeaderList) {
        SIPHeaderList<?> hdrList = (SIPHeaderList<?>) toRemove;
        if (top)
            hdrList.removeFirst();
        else
            hdrList.removeLast();
        // Clean up empty list
        if (hdrList.isEmpty()) {
            Iterator<SIPHeader> li = this.headers.iterator();
            while (li.hasNext()) {
                SIPHeader sipHeader = (SIPHeader) li.next();
                if (sipHeader.getName().equalsIgnoreCase(headerNameLowerCase))
                    li.remove();
            }
            // JvB: also remove it from the nameTable! Else NPE in
            // DefaultRouter
            nameTable.remove(headerNameLowerCase);
        }
    } else {
        this.nameTable.remove(headerNameLowerCase);
        if (toRemove instanceof From) {
            this.fromHeader = null;
        } else if (toRemove instanceof To) {
            this.toHeader = null;
        } else if (toRemove instanceof CSeq) {
            this.cSeqHeader = null;
        } else if (toRemove instanceof CallID) {
            this.callIdHeader = null;
        } else if (toRemove instanceof MaxForwards) {
            this.maxForwardsHeader = null;
        } else if (toRemove instanceof ContentLength) {
            this.contentLengthHeader = null;
        }
        Iterator<SIPHeader> li = this.headers.iterator();
        while (li.hasNext()) {
            SIPHeader sipHeader = (SIPHeader) li.next();
            if (sipHeader.getName().equalsIgnoreCase(headerName))
                li.remove();
        }
    }
}
Also used : SIPHeaderList(gov.nist.javax.sip.header.SIPHeaderList) CSeq(gov.nist.javax.sip.header.CSeq) SIPHeader(gov.nist.javax.sip.header.SIPHeader) CallID(gov.nist.javax.sip.header.CallID) MaxForwards(gov.nist.javax.sip.header.MaxForwards) From(gov.nist.javax.sip.header.From) To(gov.nist.javax.sip.header.To) InReplyTo(gov.nist.javax.sip.header.InReplyTo) ContentLength(gov.nist.javax.sip.header.ContentLength)

Example 2 with SIPHeaderList

use of gov.nist.javax.sip.header.SIPHeaderList in project XobotOS by xamarin.

the class SIPMessage method attachHeader.

/**
     * Attach the header to the SIP Message structure at a specified position in its list of
     * headers.
     * 
     * @param header Header to attach.
     * @param replaceFlag If true then replace the existing header.
     * @param top Location in the header list to insert the header.
     * @exception SIPDuplicateHeaderException if the header is of a type that cannot tolerate
     *            duplicates and one of this type already exists (e.g. CSeq header).
     * @throws IndexOutOfBoundsException If the index specified is greater than the number of
     *         headers that are in this message.
     */
public void attachHeader(SIPHeader header, boolean replaceFlag, boolean top) throws SIPDuplicateHeaderException {
    if (header == null) {
        throw new NullPointerException("null header");
    }
    SIPHeader h;
    if (ListMap.hasList(header) && !SIPHeaderList.class.isAssignableFrom(header.getClass())) {
        SIPHeaderList<SIPHeader> hdrList = ListMap.getList(header);
        hdrList.add(header);
        h = hdrList;
    } else {
        h = header;
    }
    String headerNameLowerCase = SIPHeaderNamesCache.toLowerCase(h.getName());
    if (replaceFlag) {
        nameTable.remove(headerNameLowerCase);
    } else if (nameTable.containsKey(headerNameLowerCase) && !(h instanceof SIPHeaderList)) {
        if (h instanceof ContentLength) {
            try {
                ContentLength cl = (ContentLength) h;
                contentLengthHeader.setContentLength(cl.getContentLength());
            } catch (InvalidArgumentException e) {
            }
        }
        // Just ignore duplicate header.
        return;
    }
    SIPHeader originalHeader = (SIPHeader) getHeader(header.getName());
    // Delete the original header from our list structure.
    if (originalHeader != null) {
        Iterator<SIPHeader> li = headers.iterator();
        while (li.hasNext()) {
            SIPHeader next = (SIPHeader) li.next();
            if (next.equals(originalHeader)) {
                li.remove();
            }
        }
    }
    if (!nameTable.containsKey(headerNameLowerCase)) {
        nameTable.put(headerNameLowerCase, h);
        headers.add(h);
    } else {
        if (h instanceof SIPHeaderList) {
            SIPHeaderList<?> hdrlist = (SIPHeaderList<?>) nameTable.get(headerNameLowerCase);
            if (hdrlist != null)
                hdrlist.concatenate((SIPHeaderList) h, top);
            else
                nameTable.put(headerNameLowerCase, h);
        } else {
            nameTable.put(headerNameLowerCase, h);
        }
    }
    // Direct accessor fields for frequently accessed headers.
    if (h instanceof From) {
        this.fromHeader = (From) h;
    } else if (h instanceof ContentLength) {
        this.contentLengthHeader = (ContentLength) h;
    } else if (h instanceof To) {
        this.toHeader = (To) h;
    } else if (h instanceof CSeq) {
        this.cSeqHeader = (CSeq) h;
    } else if (h instanceof CallID) {
        this.callIdHeader = (CallID) h;
    } else if (h instanceof MaxForwards) {
        this.maxForwardsHeader = (MaxForwards) h;
    }
}
Also used : SIPHeaderList(gov.nist.javax.sip.header.SIPHeaderList) CSeq(gov.nist.javax.sip.header.CSeq) CallID(gov.nist.javax.sip.header.CallID) MaxForwards(gov.nist.javax.sip.header.MaxForwards) From(gov.nist.javax.sip.header.From) InvalidArgumentException(javax.sip.InvalidArgumentException) SIPHeader(gov.nist.javax.sip.header.SIPHeader) ContentLength(gov.nist.javax.sip.header.ContentLength) To(gov.nist.javax.sip.header.To) InReplyTo(gov.nist.javax.sip.header.InReplyTo)

Example 3 with SIPHeaderList

use of gov.nist.javax.sip.header.SIPHeaderList in project XobotOS by xamarin.

the class SecurityAgreeParser method parse.

public SIPHeaderList parse(SecurityAgree header) throws ParseException {
    SIPHeaderList list;
    if (header.getClass().isInstance(new SecurityClient())) {
        list = new SecurityClientList();
    } else if (header.getClass().isInstance(new SecurityServer())) {
        list = new SecurityServerList();
    } else if (header.getClass().isInstance(new SecurityVerify())) {
        list = new SecurityVerifyList();
    } else
        return null;
    // the security-mechanism:
    this.lexer.SPorHT();
    lexer.match(TokenTypes.ID);
    Token type = lexer.getNextToken();
    header.setSecurityMechanism(type.getTokenValue());
    this.lexer.SPorHT();
    char la = lexer.lookAhead(0);
    if (la == '\n') {
        list.add(header);
        return list;
    } else if (la == ';')
        this.lexer.match(';');
    this.lexer.SPorHT();
    // The parameters:
    try {
        while (lexer.lookAhead(0) != '\n') {
            this.parseParameter(header);
            this.lexer.SPorHT();
            char laInLoop = lexer.lookAhead(0);
            if (laInLoop == '\n' || laInLoop == '\0')
                break;
            else if (laInLoop == ',') {
                list.add(header);
                if (header.getClass().isInstance(new SecurityClient())) {
                    header = new SecurityClient();
                } else if (header.getClass().isInstance(new SecurityServer())) {
                    header = new SecurityServer();
                } else if (header.getClass().isInstance(new SecurityVerify())) {
                    header = new SecurityVerify();
                }
                this.lexer.match(',');
                // the security-mechanism:
                this.lexer.SPorHT();
                lexer.match(TokenTypes.ID);
                type = lexer.getNextToken();
                header.setSecurityMechanism(type.getTokenValue());
            }
            this.lexer.SPorHT();
            if (lexer.lookAhead(0) == ';')
                this.lexer.match(';');
            this.lexer.SPorHT();
        }
        list.add(header);
        return list;
    } catch (ParseException ex) {
        throw ex;
    }
}
Also used : SIPHeaderList(gov.nist.javax.sip.header.SIPHeaderList) Token(gov.nist.core.Token) ParseException(java.text.ParseException)

Example 4 with SIPHeaderList

use of gov.nist.javax.sip.header.SIPHeaderList in project XobotOS by xamarin.

the class SIPMessage method getMessageAsEncodedStrings.

/**
     * Get the headers as a linked list of encoded Strings
     * 
     * @return a linked list with each element of the list containing a string encoded header in
     *         canonical form.
     */
public LinkedList<String> getMessageAsEncodedStrings() {
    LinkedList<String> retval = new LinkedList<String>();
    Iterator<SIPHeader> li = headers.iterator();
    while (li.hasNext()) {
        SIPHeader sipHeader = (SIPHeader) li.next();
        if (sipHeader instanceof SIPHeaderList) {
            SIPHeaderList<?> shl = (SIPHeaderList<?>) sipHeader;
            retval.addAll(shl.getHeadersAsEncodedStrings());
        } else {
            retval.add(sipHeader.encode());
        }
    }
    return retval;
}
Also used : SIPHeaderList(gov.nist.javax.sip.header.SIPHeaderList) SIPHeader(gov.nist.javax.sip.header.SIPHeader) LinkedList(java.util.LinkedList)

Example 5 with SIPHeaderList

use of gov.nist.javax.sip.header.SIPHeaderList in project XobotOS by xamarin.

the class SIPMessage method setHeader.

/**
     * Attach a header (replacing the original header).
     * 
     * @param sipHeader SIPHeader that replaces a header of the same type.
     */
public void setHeader(Header sipHeader) {
    SIPHeader header = (SIPHeader) sipHeader;
    if (header == null)
        throw new IllegalArgumentException("null header!");
    try {
        if (header instanceof SIPHeaderList) {
            SIPHeaderList<?> hl = (SIPHeaderList<?>) header;
            // Ignore empty lists.
            if (hl.isEmpty())
                return;
        }
        this.removeHeader(header.getHeaderName());
        attachHeader(header, true, false);
    } catch (SIPDuplicateHeaderException ex) {
        InternalErrorHandler.handleException(ex);
    }
}
Also used : SIPHeaderList(gov.nist.javax.sip.header.SIPHeaderList) SIPHeader(gov.nist.javax.sip.header.SIPHeader)

Aggregations

SIPHeaderList (gov.nist.javax.sip.header.SIPHeaderList)6 SIPHeader (gov.nist.javax.sip.header.SIPHeader)5 ContentLength (gov.nist.javax.sip.header.ContentLength)3 CSeq (gov.nist.javax.sip.header.CSeq)2 CallID (gov.nist.javax.sip.header.CallID)2 From (gov.nist.javax.sip.header.From)2 InReplyTo (gov.nist.javax.sip.header.InReplyTo)2 MaxForwards (gov.nist.javax.sip.header.MaxForwards)2 To (gov.nist.javax.sip.header.To)2 Token (gov.nist.core.Token)1 ParseException (java.text.ParseException)1 LinkedList (java.util.LinkedList)1 InvalidArgumentException (javax.sip.InvalidArgumentException)1