Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
  * Conditions Of Use
  *
  * This software was developed by employees of the National Institute of
  * Standards and Technology (NIST), an agency of the Federal Government.
  * Pursuant to title 15 Untied States Code Section 105, works of NIST
  * employees are not subject to copyright protection in the United States
  * and are considered to be in the public domain.  As a result, a formal
  * license is not needed to use the software.
 *
 * This software is provided by NIST as a service and is expressly
 * provided "AS IS."  NIST MAKES NO WARRANTY OF ANY KIND, EXPRESS, IMPLIED
 * OR STATUTORY, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTY OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT
 * AND DATA ACCURACY.  NIST does not warrant or make any representations
 * regarding the use of the software or the results thereof, including but
 * not limited to the correctness, accuracy, reliability or usefulness of
 * the software.
 *
 * Permission to use this software is contingent upon your acceptance
 * of the terms of this agreement
 *
 * .
 *
 */
Product of NIST/ITL Advanced Networking Technologies Division (ANTD) * /
 
 package gov.nist.javax.sip.message;
 
 
 
SIP Response structure.

Author(s):
M. Ranganathan
Version:
1.2 $Revision: 1.31 $ $Date: 2010-03-15 17:01:22 $
Since:
1.1
 
 public class SIPResponse
     extends SIPMessage
     implements javax.sip.message.ResponseResponseExt {
     protected StatusLine statusLine;
    
     public static String getReasonPhrase(int rc) {
         String retval = null;
         switch (rc) {
 
             case  :
                 retval = "Trying";
                 break;
 
             case  :
                 retval = "Ringing";
                 break;
 
             case  :
                 retval = "Call is being forwarded";
                 break;
 
             case  :
                 retval = "Queued";
                 break;
 
             case  :
                 retval = "Session progress";
                 break;
 
            case  :
                retval = "OK";
                break;
            case  :
                retval = "Accepted";
                break;
            case  :
                retval = "Multiple choices";
                break;
            case  :
                retval = "Moved permanently";
                break;
            case  :
                retval = "Moved Temporarily";
                break;
            case  :
                retval = "Use proxy";
                break;
            case  :
                retval = "Alternative service";
                break;
            case  :
                retval = "Bad request";
                break;
            case  :
                retval = "Unauthorized";
                break;
            case  :
                retval = "Payment required";
                break;
            case  :
                retval = "Forbidden";
                break;
            case  :
                retval = "Not found";
                break;
            case  :
                retval = "Method not allowed";
                break;
            case  :
                retval = "Not acceptable";
                break;
            case  :
                retval = "Proxy Authentication required";
                break;
            case  :
                retval = "Request timeout";
                break;
            case  :
                retval = "Gone";
                break;
            case  :
                retval = "Temporarily Unavailable";
                break;
            case  :
                retval = "Request entity too large";
                break;
            case  :
                retval = "Request-URI too large";
                break;
            case  :
                retval = "Unsupported media type";
                break;
            case  :
                retval = "Unsupported URI Scheme";
                break;
            case  :
                retval = "Bad extension";
                break;
            case  :
                retval = "Etension Required";
                break;
            case  :
                retval = "Interval too brief";
                break;
            case  :
                retval = "Call leg/Transaction does not exist";
                break;
            case  :
                retval = "Loop detected";
                break;
            case  :
                retval = "Too many hops";
                break;
            case  :
                retval = "Address incomplete";
                break;
            case  :
                retval = "Ambiguous";
                break;
            case  :
                retval = "Busy here";
                break;
            case  :
                retval = "Request Terminated";
                break;
            //Issue 168, Typo fix reported by fre on the retval
            case  :
                retval = "Not Acceptable here";
                break;
            case  :
                retval = "Bad Event";
                break;
            case  :
                retval = "Request Pending";
                break;
            case  :
                retval = "Server Internal Error";
                break;
            case  :
                retval = "Undecipherable";
                break;
            case  :
                retval = "Not implemented";
                break;
            case  :
                retval = "Bad gateway";
                break;
            case  :
                retval = "Service unavailable";
                break;
            case  :
                retval = "Gateway timeout";
                break;
            case  :
                retval = "SIP version not supported";
                break;
            case  :
                retval = "Message Too Large";
                break;
            case  :
                retval = "Busy everywhere";
                break;
            case  :
                retval = "Decline";
                break;
            case  :
                retval = "Does not exist anywhere";
                break;
            case  :
                retval = "Session Not acceptable";
                break;
            case :
                retval = "Conditional request failed";
                break;
            default :
                retval = "Unknown Status";
        }
        return retval;
    }

    
set the status code.

Parameters:
statusCode is the status code to set.
Throws:
IlegalArgumentException if invalid status code.
    public void setStatusCode(int statusCodethrows ParseException {
      // RFC3261 defines statuscode as 3DIGIT, 606 is the highest officially
      // defined code but extensions may add others (in theory up to 999,
      // but in practice up to 699 since the 6xx range is defined as 'final error')
        if (statusCode < 100 || statusCode > 699)
            throw new ParseException("bad status code", 0);
        if (this. == null)
            this. = new StatusLine();
        this..setStatusCode(statusCode);
    }

    
Get the status line of the response.

Returns:
StatusLine
    public StatusLine getStatusLine() {
        return ;
    }

    
Get the staus code (conveniance function).

Returns:
the status code of the status line.
    public int getStatusCode() {
        return .getStatusCode();
    }

    
Set the reason phrase.

Parameters:
reasonPhrase the reason phrase.
Throws:
java.lang.IllegalArgumentException if null string
    public void setReasonPhrase(String reasonPhrase) {
        if (reasonPhrase == null)
            throw new IllegalArgumentException("Bad reason phrase");
        if (this. == null)
            this. = new StatusLine();
        this..setReasonPhrase(reasonPhrase);
    }

    
Get the reason phrase.

Returns:
the reason phrase.
    public String getReasonPhrase() {
        if ( == null || .getReasonPhrase() == null)
            return "";
        else
            return .getReasonPhrase();
    }

    
Return true if the response is a final response.

Parameters:
rc is the return code.
Returns:
true if the parameter is between the range 200 and 700.
    public static boolean isFinalResponse(int rc) {
        return rc >= 200 && rc < 700;
    }

    
Is this a final response?

Returns:
true if this is a final response.
    public boolean isFinalResponse() {
        return isFinalResponse(.getStatusCode());
    }

    
Set the status line field.

Parameters:
sl Status line to set.
    public void setStatusLine(StatusLine sl) {
         = sl;
    }

    
Constructor.
    public SIPResponse() {
        super();
    }
    
Print formatting function. Indent and parenthesize for pretty printing. Note -- use the encode method for formatting the message. Hack here to XMLize.

Returns:
a string for pretty printing.
    public String debugDump() {
        String superstring = super.debugDump();
         = "";
        sprint(SIPResponse.class.getCanonicalName());
        sprint("{");
        if ( != null) {
            sprint(.debugDump());
        }
        sprint(superstring);
        sprint("}");
        return ;
    }

    
Check the response structure. Must have from, to CSEQ and VIA headers.
    public void checkHeaders() throws ParseException {
        if (getCSeq() == null) {
            throw new ParseException(." Is missing ", 0);
        }
        if (getTo() == null) {
            throw new ParseException(." Is missing ", 0);
        }
        if (getFrom() == null) {
            throw new ParseException(." Is missing ", 0);
        }
        if (getViaHeaders() == null) {
            throw new ParseException(." Is missing ", 0);
        }
        if (getCallId() == null) {
            throw new ParseException(. + " Is missing ", 0);
        }
        if (getStatusCode() > 699) {
            throw new ParseException("Unknown error code!" + getStatusCode(), 0);
        }
    }

    
Encode the SIP Request as a string.

Returns:
The string encoded canonical form of the message.
    public String encode() {
        String retval;
        if ( != null)
            retval = .encode() + super.encode();
        else
            retval = super.encode();
        return retval ;
    }

    
Encode the message except for the body.

Returns:
The string except for the body.
    public String encodeMessage() {
        String retval;
        if ( != null)
            retval = .encode() + super.encodeSIPHeaders();
        else
            retval = super.encodeSIPHeaders();
        return retval ;
    }



    
Get this message as a list of encoded strings.

Returns:
LinkedList containing encoded strings for each header in the message.
        LinkedList retval = super.getMessageAsEncodedStrings();
        if ( != null)
            retval.addFirst(.encode());
        return retval;
    }

    
Make a clone (deep copy) of this object.

Returns:
a deep copy of this object.
    public Object clone() {
        SIPResponse retval = (SIPResponsesuper.clone();
        if (this. != null)
            retval.statusLine = (StatusLinethis..clone();
        return retval;
    }


    
Compare for equality.

Parameters:
other other object to compare with.
    public boolean equals(Object other) {
        if (!this.getClass().equals(other.getClass()))
            return false;
        SIPResponse that = (SIPResponseother;
        return .equals(that.statusLine) && super.equals(other);
    }

    
Match with a template.

Parameters:
matchObj template object to match ourselves with (null in any position in the template object matches wildcard)
    public boolean match(Object matchObj) {
        if (matchObj == null)
            return true;
        else if (!matchObj.getClass().equals(this.getClass())) {
            return false;
        } else if (matchObj == this)
            return true;
        SIPResponse that = (SIPResponsematchObj;
        StatusLine rline = that.statusLine;
        if (this. == null && rline != null)
            return false;
        else if (this. == rline)
            return super.match(matchObj);
        else {
            return .match(that.statusLine) && super.match(matchObj);
        }
    }

    
Encode this into a byte array. This is used when the body has been set as a binary array and you want to encode the body as a byte array for transmission.

Returns:
a byte array containing the SIPRequest encoded as a byte array.
    public byte[] encodeAsBytesString transport ) {
        byte[] slbytes = null;
        if ( != null) {
            try {
                slbytes = .encode().getBytes("UTF-8");
            } catch (UnsupportedEncodingException ex) {
                InternalErrorHandler.handleException(ex);
            }
        }
        byte[] superbytes = super.encodeAsBytestransport );
        byte[] retval = new byte[slbytes.length + superbytes.length];
        System.arraycopy(slbytes, 0, retval, 0, slbytes.length);
        System.arraycopy(superbytes, 0, retvalslbytes.length,
                superbytes.length);
        return retval;
    }



    
Get a dialog identifier. Generates a string that can be used as a dialog identifier.

Parameters:
isServer is set to true if this is the UAS and set to false if this is the UAC
    public String getDialogId(boolean isServer) {
        CallID cid = (CallIDthis.getCallId();
        From from = (Fromthis.getFrom();
        To to = (Tothis.getTo();
        StringBuffer retval = new StringBuffer(cid.getCallId());
        if (!isServer) {
            //retval.append(COLON).append(from.getUserAtHostPort());
            if (from.getTag() != null) {
                retval.append();
                retval.append(from.getTag());
            }
            //retval.append(COLON).append(to.getUserAtHostPort());
            if (to.getTag() != null) {
                retval.append();
                retval.append(to.getTag());
            }
        } else {
            //retval.append(COLON).append(to.getUserAtHostPort());
            if (to.getTag() != null) {
                retval.append();
                retval.append(to.getTag());
            }
            //retval.append(COLON).append(from.getUserAtHostPort());
            if (from.getTag() != null) {
                retval.append();
                retval.append(from.getTag());
            }
        }
        return retval.toString().toLowerCase();
    }
    public String getDialogId(boolean isServerString toTag) {
        CallID cid = (CallIDthis.getCallId();
        From from = (Fromthis.getFrom();
        StringBuffer retval = new StringBuffer(cid.getCallId());
        if (!isServer) {
            //retval.append(COLON).append(from.getUserAtHostPort());
            if (from.getTag() != null) {
                retval.append();
                retval.append(from.getTag());
            }
            //retval.append(COLON).append(to.getUserAtHostPort());
            if (toTag != null) {
                retval.append();
                retval.append(toTag);
            }
        } else {
            //retval.append(COLON).append(to.getUserAtHostPort());
            if (toTag != null) {
                retval.append();
                retval.append(toTag);
            }
            //retval.append(COLON).append(from.getUserAtHostPort());
            if (from.getTag() != null) {
                retval.append();
                retval.append(from.getTag());
            }
        }
        return retval.toString().toLowerCase();
    }

    
Sets the Via branch for CANCEL or ACK requests

Parameters:
via
method
Throws:
java.text.ParseException
    private final void setBranchVia viaString method ) {
        String branch;
        if (method.equals. ) ) {
            if (.getStatusCode() >= 300 ) {
                branch = getTopmostVia().getBranch();   // non-2xx ACK uses same branch
            } else {
                branch = Utils.getInstance().generateBranchId();    // 2xx ACK gets new branch
            }
        } else if (method.equals. )) {
            branch = getTopmostVia().getBranch();   // CANCEL uses same branch
        } else return;
        try {
            via.setBranchbranch );
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }


    
Get the encoded first line.

Returns:
the status line encoded.
    public String getFirstLine() {
        if (this. == null)
            return null;
        else
            return this..encode();
    }
    public void setSIPVersion(String sipVersion) {
        this..setSipVersion(sipVersion);
    }
    public String getSIPVersion() {
        return this..getSipVersion();
    }
    public String toString() {
        if ( == nullreturn  "";
        else return .encode() + super.encode();
    }

    
Generate a request from a response.

Parameters:
requestURI -- the request URI to assign to the request.
via -- the Via header to assign to the request
cseq -- the CSeq header to assign to the request
from -- the From header to assign to the request
to -- the To header to assign to the request
Returns:
-- the newly generated sip request.
    public SIPRequest createRequest(SipUri requestURIVia viaCSeq cseqFrom fromTo to) {
        SIPRequest newRequest = new SIPRequest();
        String method = cseq.getMethod();
        newRequest.setMethod(method);
        newRequest.setRequestURI(requestURI);
        this.setBranchviamethod );
        newRequest.setHeader(via);
        newRequest.setHeader(cseq);
        Iterator headerIterator = getHeaders();
        while (headerIterator.hasNext()) {
            SIPHeader nextHeader = (SIPHeaderheaderIterator.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
                || nextHeader instanceof ContactList    // JvB: added
                || nextHeader instanceof ContentLength
                || nextHeader instanceof ServerHeader
                || nextHeader instanceof ReasonHeader
                || nextHeader instanceof SessionExpires
                || nextHeader instanceof ReasonList) {
                continue;
            }
            if (nextHeader instanceof To)
                nextHeader = (SIPHeaderto;
            else if (nextHeader instanceof From)
                nextHeader = (SIPHeaderfrom;
            try {
                newRequest.attachHeader(nextHeaderfalse);
            } catch (SIPDuplicateHeaderException e) {
                //Should not happen!
                e.printStackTrace();
            }
        }
        try {
          // JvB: all requests need a Max-Forwards
          newRequest.attachHeadernew MaxForwards(70), false);
        } catch (Exception d) {
        }
        if (MessageFactoryImpl.getDefaultUserAgentHeader() != null ) {
            newRequest.setHeader(MessageFactoryImpl.getDefaultUserAgentHeader());
        }
        return newRequest;
    }
   
    
New to GrepCode? Check out our FAQ X