Search in sources :

Example 6 with ContentLength

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

the class SIPMessage method encodeAsBytes.

/**
     * Encode the message as a byte array. Use this when the message payload is a binary byte
     * array.
     * 
     * @return The Canonical byte array representation of the message (including the canonical
     *         byte array representation of the SDP payload if it exists all in one contiguous
     *         byte array).
     */
public byte[] encodeAsBytes(String transport) {
    if (this instanceof SIPRequest && ((SIPRequest) this).isNullRequest()) {
        return "\r\n\r\n".getBytes();
    }
    // JvB: added to fix case where application provides the wrong transport
    // in the topmost Via header
    ViaHeader topVia = (ViaHeader) this.getHeader(ViaHeader.NAME);
    try {
        topVia.setTransport(transport);
    } catch (ParseException e) {
        InternalErrorHandler.handleException(e);
    }
    StringBuffer encoding = new StringBuffer();
    synchronized (this.headers) {
        Iterator<SIPHeader> it = this.headers.iterator();
        while (it.hasNext()) {
            SIPHeader siphdr = (SIPHeader) it.next();
            if (!(siphdr instanceof ContentLength))
                siphdr.encode(encoding);
        }
    }
    contentLengthHeader.encode(encoding);
    encoding.append(NEWLINE);
    byte[] retval = null;
    byte[] content = this.getRawContent();
    if (content != null) {
        // Append the content
        byte[] msgarray = null;
        try {
            msgarray = encoding.toString().getBytes(getCharset());
        } catch (UnsupportedEncodingException ex) {
            InternalErrorHandler.handleException(ex);
        }
        retval = new byte[msgarray.length + content.length];
        System.arraycopy(msgarray, 0, retval, 0, msgarray.length);
        System.arraycopy(content, 0, retval, msgarray.length, content.length);
    } else {
        try {
            retval = encoding.toString().getBytes(getCharset());
        } catch (UnsupportedEncodingException ex) {
            InternalErrorHandler.handleException(ex);
        }
    }
    return retval;
}
Also used : ViaHeader(javax.sip.header.ViaHeader) SIPHeader(gov.nist.javax.sip.header.SIPHeader) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ParseException(java.text.ParseException) ContentLength(gov.nist.javax.sip.header.ContentLength)

Example 7 with ContentLength

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

the class SIPMessage method encodeSIPHeaders.

/**
     * Encode only the message and exclude the contents (for debugging);
     * 
     * @return a string with all the headers encoded.
     */
protected String encodeSIPHeaders() {
    StringBuffer encoding = new StringBuffer();
    Iterator<SIPHeader> it = this.headers.iterator();
    while (it.hasNext()) {
        SIPHeader siphdr = (SIPHeader) it.next();
        if (!(siphdr instanceof ContentLength))
            siphdr.encode(encoding);
    }
    return contentLengthHeader.encode(encoding).append(NEWLINE).toString();
}
Also used : SIPHeader(gov.nist.javax.sip.header.SIPHeader) ContentLength(gov.nist.javax.sip.header.ContentLength)

Example 8 with ContentLength

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

the class SIPResponse method createRequest.

/**
     * Generate a request from a response.
     *
     * @param requestURI -- the request URI to assign to the request.
     * @param via -- the Via header to assign to the request
     * @param cseq -- the CSeq header to assign to the request
     * @param from -- the From header to assign to the request
     * @param to -- the To header to assign to the request
     * @return -- the newly generated sip request.
     */
public SIPRequest createRequest(SipUri requestURI, Via via, CSeq cseq, From from, To to) {
    SIPRequest newRequest = new SIPRequest();
    String method = cseq.getMethod();
    newRequest.setMethod(method);
    newRequest.setRequestURI(requestURI);
    this.setBranch(via, method);
    newRequest.setHeader(via);
    newRequest.setHeader(cseq);
    Iterator headerIterator = getHeaders();
    while (headerIterator.hasNext()) {
        SIPHeader nextHeader = (SIPHeader) headerIterator.next();
        // Some headers do not belong in a Request ....
        if (SIPMessage.isResponseHeader(nextHeader) || nextHeader instanceof ViaList || nextHeader instanceof CSeq || nextHeader instanceof ContentType || nextHeader instanceof ContentLength || nextHeader instanceof RecordRouteList || nextHeader instanceof RequireList || // JvB: added
        nextHeader instanceof ContactList || nextHeader instanceof ContentLength || nextHeader instanceof ServerHeader || nextHeader instanceof ReasonHeader || nextHeader instanceof SessionExpires || nextHeader instanceof ReasonList) {
            continue;
        }
        if (nextHeader instanceof To)
            nextHeader = (SIPHeader) to;
        else if (nextHeader instanceof From)
            nextHeader = (SIPHeader) from;
        try {
            newRequest.attachHeader(nextHeader, false);
        } catch (SIPDuplicateHeaderException e) {
            //Should not happen!
            e.printStackTrace();
        }
    }
    try {
        // JvB: all requests need a Max-Forwards
        newRequest.attachHeader(new MaxForwards(70), false);
    } catch (Exception d) {
    }
    if (MessageFactoryImpl.getDefaultUserAgentHeader() != null) {
        newRequest.setHeader(MessageFactoryImpl.getDefaultUserAgentHeader());
    }
    return newRequest;
}
Also used : RequireList(gov.nist.javax.sip.header.RequireList) SessionExpires(gov.nist.javax.sip.header.extensions.SessionExpires) ReasonList(gov.nist.javax.sip.header.ReasonList) CSeq(gov.nist.javax.sip.header.CSeq) ContentType(gov.nist.javax.sip.header.ContentType) MaxForwards(gov.nist.javax.sip.header.MaxForwards) ContactList(gov.nist.javax.sip.header.ContactList) From(gov.nist.javax.sip.header.From) ReasonHeader(javax.sip.header.ReasonHeader) ParseException(java.text.ParseException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ViaList(gov.nist.javax.sip.header.ViaList) SIPHeader(gov.nist.javax.sip.header.SIPHeader) Iterator(java.util.Iterator) RecordRouteList(gov.nist.javax.sip.header.RecordRouteList) ServerHeader(javax.sip.header.ServerHeader) ContentLength(gov.nist.javax.sip.header.ContentLength) To(gov.nist.javax.sip.header.To)

Example 9 with ContentLength

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

the class SIPMessage method match.

/**
     * Template match for SIP messages. The matchObj is a SIPMessage template to match against.
     * This method allows you to do pattern matching with incoming SIP messages. Null matches wild
     * card.
     * 
     * @param other is the match template to match against.
     * @return true if a match occured and false otherwise.
     */
public boolean match(Object other) {
    if (other == null)
        return true;
    if (!other.getClass().equals(this.getClass()))
        return false;
    SIPMessage matchObj = (SIPMessage) other;
    Iterator<SIPHeader> li = matchObj.getHeaders();
    while (li.hasNext()) {
        SIPHeader hisHeaders = (SIPHeader) li.next();
        List<SIPHeader> myHeaders = this.getHeaderList(hisHeaders.getHeaderName());
        // Could not find a header to match his header.
        if (myHeaders == null || myHeaders.size() == 0)
            return false;
        if (hisHeaders instanceof SIPHeaderList) {
            ListIterator<?> outerIterator = ((SIPHeaderList<?>) hisHeaders).listIterator();
            while (outerIterator.hasNext()) {
                SIPHeader hisHeader = (SIPHeader) outerIterator.next();
                if (hisHeader instanceof ContentLength)
                    continue;
                ListIterator<?> innerIterator = myHeaders.listIterator();
                boolean found = false;
                while (innerIterator.hasNext()) {
                    SIPHeader myHeader = (SIPHeader) innerIterator.next();
                    if (myHeader.match(hisHeader)) {
                        found = true;
                        break;
                    }
                }
                if (!found)
                    return false;
            }
        } else {
            SIPHeader hisHeader = hisHeaders;
            ListIterator<SIPHeader> innerIterator = myHeaders.listIterator();
            boolean found = false;
            while (innerIterator.hasNext()) {
                SIPHeader myHeader = (SIPHeader) innerIterator.next();
                if (myHeader.match(hisHeader)) {
                    found = true;
                    break;
                }
            }
            if (!found)
                return false;
        }
    }
    return true;
}
Also used : SIPHeaderList(gov.nist.javax.sip.header.SIPHeaderList) SIPHeader(gov.nist.javax.sip.header.SIPHeader) 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