Exemplo n.º 1
0
/**
 * Attach a header (replacing the original header).
 * @param header SIPHeader that replaces a header of the same type.
 */
func (this *SIPMessage) SetHeader(h header.Header) (IllegalArgumentException error) {
	if h == nil {
		return errors.New("IllegalArgumentException: nil header!")
	}

	if hl, ok := h.(header.SIPHeaderLister); ok {
		// Ignore empty lists.
		if hl.Len() == 0 {
			return nil
		}
	}
	this.RemoveHeader(h.GetHeaderName())
	return this.AttachHeader3(h, true, false)
}
Exemplo n.º 2
0
/**
 * 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 index 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.
 */
func (this *SIPMessage) AttachHeader3(h header.Header, replaceFlag, top bool) error { //throws SIPDuplicateHeaderException {
	if h == nil {
		errors.New("NullPointerException: nil header")
	}

	if replaceFlag {
		delete(this.nameTable, strings.ToLower(h.GetName()))
	} else {
		if _, present := this.nameTable[strings.ToLower(h.GetName())]; present {
			if _, ok := h.(header.SIPHeaderLister); !ok {
				if cl, ok := h.(*header.ContentLength); ok {
					this.contentLengthHeader.SetContentLength(cl.GetContentLength())
				}
				// Just ignore duplicate header.
				return nil
			}
		}
	}

	originalHeader := this.GetHeader(h.GetName())

	// Delete the original sh from our list structure.
	if originalHeader != nil {
		for li := this.headers.Front(); li != nil; li = li.Next() {
			next := li.Value.(header.Header)
			if next == originalHeader {
				this.headers.Remove(li)
			}
		}
	}

	if this.GetHeader(h.GetName()) == nil {
		this.nameTable[strings.ToLower(h.GetName())] = h
		this.headers.PushBack(h)
	} else {
		if hs, ok := h.(header.SIPHeaderLister); ok {
			hdrlist := this.nameTable[strings.ToLower(h.GetName())].(header.SIPHeaderLister)
			if hdrlist != nil {
				hdrlist.Concatenate(hs, top)
			} else {
				this.nameTable[strings.ToLower(h.GetName())] = h
			}
		} else {
			this.nameTable[strings.ToLower(h.GetName())] = h
		}
	}

	// Direct accessor fields for frequently accessed headers.
	if sh, ok := h.(*header.From); ok {
		this.fromHeader = sh
	} else if sh, ok := h.(*header.ContentLength); ok {
		this.contentLengthHeader = sh
	} else if sh, ok := h.(*header.To); ok {
		this.toHeader = sh
	} else if sh, ok := h.(*header.CSeq); ok {
		this.cSeqHeader = sh
	} else if sh, ok := h.(*header.CallID); ok {
		this.callIdHeader = sh
	} else if sh, ok := h.(*header.MaxForwards); ok {
		this.maxForwardsHeader = sh
	}

	return nil
}