Search in sources :

Example 1 with ContentLength

use of gov.nist.javax.sip.header.ContentLength 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 ContentLength

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

the class SIPMessage method removeHeader.

/**
     * Remove all headers given its name.
     * 
     * @param headerName is the name of the header to remove.
     */
public void removeHeader(String headerName) {
    if (headerName == null)
        throw new NullPointerException("null arg");
    String headerNameLowerCase = SIPHeaderNamesCache.toLowerCase(headerName);
    SIPHeader removed = (SIPHeader) nameTable.remove(headerNameLowerCase);
    // nothing to do then we are done.
    if (removed == null)
        return;
    // Remove the fast accessor fields.
    if (removed instanceof From) {
        this.fromHeader = null;
    } else if (removed instanceof To) {
        this.toHeader = null;
    } else if (removed instanceof CSeq) {
        this.cSeqHeader = null;
    } else if (removed instanceof CallID) {
        this.callIdHeader = null;
    } else if (removed instanceof MaxForwards) {
        this.maxForwardsHeader = null;
    } else if (removed instanceof ContentLength) {
        this.contentLengthHeader = null;
    }
    Iterator<SIPHeader> li = this.headers.iterator();
    while (li.hasNext()) {
        SIPHeader sipHeader = (SIPHeader) li.next();
        if (sipHeader.getName().equalsIgnoreCase(headerNameLowerCase))
            li.remove();
    }
}
Also used : 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 3 with ContentLength

use of gov.nist.javax.sip.header.ContentLength 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 4 with ContentLength

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

the class MessageChannel method createBadReqRes.

/**
     * Creates a response to a bad request (ie one that causes a ParseException)
     * 
     * @param badReq
     * @return message bytes, null if unable to formulate response
     */
protected final String createBadReqRes(String badReq, ParseException pe) {
    StringBuffer buf = new StringBuffer(512);
    buf.append("SIP/2.0 400 Bad Request (" + pe.getLocalizedMessage() + ')');
    // We need the following headers: all Vias, CSeq, Call-ID, From, To
    if (!copyViaHeaders(badReq, buf))
        return null;
    if (!copyHeader(CSeqHeader.NAME, badReq, buf))
        return null;
    if (!copyHeader(CallIdHeader.NAME, badReq, buf))
        return null;
    if (!copyHeader(FromHeader.NAME, badReq, buf))
        return null;
    if (!copyHeader(ToHeader.NAME, badReq, buf))
        return null;
    // Should add a to-tag if not already present...
    int toStart = buf.indexOf(ToHeader.NAME);
    if (toStart != -1 && buf.indexOf("tag", toStart) == -1) {
        buf.append(";tag=badreq");
    }
    // Let's add a Server header too..
    ServerHeader s = MessageFactoryImpl.getDefaultServerHeader();
    if (s != null) {
        buf.append("\r\n" + s.toString());
    }
    int clength = badReq.length();
    if (!(this instanceof UDPMessageChannel) || clength + buf.length() + ContentTypeHeader.NAME.length() + ": message/sipfrag\r\n".length() + ContentLengthHeader.NAME.length() < 1300) {
        /*
             * Check to see we are within one UDP packet.
             */
        ContentTypeHeader cth = new ContentType("message", "sipfrag");
        buf.append("\r\n" + cth.toString());
        ContentLength clengthHeader = new ContentLength(clength);
        buf.append("\r\n" + clengthHeader.toString());
        buf.append("\r\n\r\n" + badReq);
    } else {
        ContentLength clengthHeader = new ContentLength(0);
        buf.append("\r\n" + clengthHeader.toString());
    }
    return buf.toString();
}
Also used : ContentType(gov.nist.javax.sip.header.ContentType) ContentTypeHeader(javax.sip.header.ContentTypeHeader) ServerHeader(javax.sip.header.ServerHeader) ContentLength(gov.nist.javax.sip.header.ContentLength)

Example 5 with ContentLength

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

the class SIPMessage method encode.

/**
     * Encode this message as a string. This is more efficient when the payload is a string
     * (rather than a binary array of bytes). If the payload cannot be encoded as a UTF-8 string
     * then it is simply ignored (will not appear in the encoded message).
     * 
     * @return The Canonical String representation of the message (including the canonical string
     *         representation of the SDP payload if it exists).
     */
public String encode() {
    StringBuffer encoding = new StringBuffer();
    Iterator<SIPHeader> it = this.headers.iterator();
    while (it.hasNext()) {
        SIPHeader siphdr = (SIPHeader) it.next();
        if (!(siphdr instanceof ContentLength))
            encoding.append(siphdr.encode());
    }
    // recognized are passed through unchanged.
    for (String unrecognized : this.unrecognizedHeaders) {
        encoding.append(unrecognized).append(NEWLINE);
    }
    encoding.append(contentLengthHeader.encode()).append(NEWLINE);
    if (this.messageContentObject != null) {
        String mbody = this.getContent().toString();
        encoding.append(mbody);
    } else if (this.messageContent != null || this.messageContentBytes != null) {
        String content = null;
        try {
            if (messageContent != null)
                content = messageContent;
            else {
                // JvB: Check for 'charset' parameter which overrides the default UTF-8
                content = new String(messageContentBytes, getCharset());
            }
        } catch (UnsupportedEncodingException ex) {
            InternalErrorHandler.handleException(ex);
        }
        encoding.append(content);
    }
    return encoding.toString();
}
Also used : SIPHeader(gov.nist.javax.sip.header.SIPHeader) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ContentLength(gov.nist.javax.sip.header.ContentLength)

Aggregations

ContentLength (gov.nist.javax.sip.header.ContentLength)9 SIPHeader (gov.nist.javax.sip.header.SIPHeader)8 CSeq (gov.nist.javax.sip.header.CSeq)4 From (gov.nist.javax.sip.header.From)4 MaxForwards (gov.nist.javax.sip.header.MaxForwards)4 To (gov.nist.javax.sip.header.To)4 CallID (gov.nist.javax.sip.header.CallID)3 InReplyTo (gov.nist.javax.sip.header.InReplyTo)3 SIPHeaderList (gov.nist.javax.sip.header.SIPHeaderList)3 UnsupportedEncodingException (java.io.UnsupportedEncodingException)3 ContentType (gov.nist.javax.sip.header.ContentType)2 ParseException (java.text.ParseException)2 ServerHeader (javax.sip.header.ServerHeader)2 ContactList (gov.nist.javax.sip.header.ContactList)1 ReasonList (gov.nist.javax.sip.header.ReasonList)1 RecordRouteList (gov.nist.javax.sip.header.RecordRouteList)1 RequireList (gov.nist.javax.sip.header.RequireList)1 ViaList (gov.nist.javax.sip.header.ViaList)1 SessionExpires (gov.nist.javax.sip.header.extensions.SessionExpires)1 Iterator (java.util.Iterator)1