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;
 
 
 import java.util.List;
 
Message Factory implementation

Author(s):
M. Ranganathan
Olivier Deruelle
Version:
1.2 $Revision: 1.23 $ $Date: 2009/09/08 01:58:40 $
Since:
1.1
 
 @SuppressWarnings("unchecked")
 public class MessageFactoryImpl implements MessageFactoryMessageFactoryExt {
 
     private boolean testing = false;
     
     private boolean strict  = true;
 
     private static String defaultContentEncodingCharset = "UTF-8";
 
 
     /*
      * The UserAgent header to include for all requests created from this message factory.
      */
     private static UserAgentHeader userAgent;
 
     /*
      * The Server header to include
      */
     private static ServerHeader server;
     
     
     public void setStrict(boolean strict) {
         this. = strict;
     }



    
This is for testing -- allows you to generate invalid requests
 
     public void setTest(boolean flag) {
         this. = flag;
     }

    
Creates a new instance of MessageFactoryImpl
 
     public MessageFactoryImpl() {
     }

    
Creates a new Request message of type specified by the method paramater, containing the URI of the Request, the mandatory headers of the message with a body in the form of a Java object and the body content type.

Parameters:
requestURI - the new URI object of the requestURI value of this Message.
method - the new string of the method value of this Message.
callId - the new CallIdHeader object of the callId value of this Message.
cSeq - the new CSeqHeader object of the cSeq value of this Message.
from - the new FromHeader object of the from value of this Message.
to - the new ToHeader object of the to value of this Message.
via - the new List object of the ViaHeaders of this Message.
content - the new Object of the body content value of this Message.
contentType - the new ContentTypeHeader object of the content type value of this Message.
Throws:
java.text.ParseException which signals that an error has been reached unexpectedly while parsing the method or the body.
    public Request createRequest(javax.sip.address.URI requestURI,
            String methodCallIdHeader callIdCSeqHeader cSeq,
            FromHeader fromToHeader toList via,
            MaxForwardsHeader maxForwardsContentTypeHeader contentType,
            Object contentthrows ParseException {
        if (requestURI == null || method == null || callId == null
                || cSeq == null || from == null || to == null || via == null
                || maxForwards == null || content == null
                || contentType == null)
            throw new NullPointerException("Null parameters");
        SIPRequest sipRequest = new SIPRequest();
        sipRequest.setRequestURI(requestURI);
        sipRequest.setMethod(method);
        sipRequest.setCallId(callId);
        sipRequest.setCSeq(cSeq);
        sipRequest.setFrom(from);
        sipRequest.setTo(to);
        sipRequest.setVia(via);
        sipRequest.setMaxForwards(maxForwards);
        sipRequest.setContent(contentcontentType);
        if (  != null ) {
            sipRequest.setHeader();
        }
        return sipRequest;
    }

    
Creates a new Request message of type specified by the method paramater, containing the URI of the Request, the mandatory headers of the message with a body in the form of a byte array and body content type.

Parameters:
requestURI - the new URI object of the requestURI value of this Message.
method - the new string of the method value of this Message.
callId - the new CallIdHeader object of the callId value of this Message.
cSeq - the new CSeqHeader object of the cSeq value of this Message.
from - the new FromHeader object of the from value of this Message.
to - the new ToHeader object of the to value of this Message.
via - the new List object of the ViaHeaders of this Message.
content - the new byte array of the body content value of this Message.
contentType - the new ContentTypeHeader object of the content type value of this Message.
Throws:
java.text.ParseException which signals that an error has been reached unexpectedly while parsing the method or the body.
    public Request createRequest(URI requestURIString method,
            CallIdHeader callIdCSeqHeader cSeqFromHeader fromToHeader to,
            List viaMaxForwardsHeader maxForwardsbyte[] content,
            ContentTypeHeader contentTypethrows ParseException {
        if (requestURI == null || method == null || callId == null
                || cSeq == null || from == null || to == null || via == null
                || maxForwards == null || content == null
                || contentType == null)
            throw new ParseException(
                    "JAIN-SIP Exception, some parameters are missing"
                            + ", unable to create the request", 0);
        SIPRequest sipRequest = new SIPRequest();
        sipRequest.setRequestURI(requestURI);
        sipRequest.setMethod(method);
        sipRequest.setCallId(callId);
        sipRequest.setCSeq(cSeq);
        sipRequest.setFrom(from);
        sipRequest.setTo(to);
        sipRequest.setVia(via);
        sipRequest.setMaxForwards(maxForwards);
        sipRequest.setHeader((ContentTypecontentType);
        sipRequest.setMessageContent(content);
        if (  != null ) {
            sipRequest.setHeader();
        }
        return sipRequest;
    }

    
Creates a new Request message of type specified by the method paramater, containing the URI of the Request, the mandatory headers of the message. This new Request does not contain a body.

Parameters:
requestURI - the new URI object of the requestURI value of this Message.
method - the new string of the method value of this Message.
callId - the new CallIdHeader object of the callId value of this Message.
cSeq - the new CSeqHeader object of the cSeq value of this Message.
from - the new FromHeader object of the from value of this Message.
to - the new ToHeader object of the to value of this Message.
via - the new List object of the ViaHeaders of this Message.
Throws:
java.text.ParseException which signals that an error has been reached unexpectedly while parsing the method.
    public Request createRequest(URI requestURIString method,
            CallIdHeader callIdCSeqHeader cSeqFromHeader fromToHeader to,
            List viaMaxForwardsHeader maxForwardsthrows ParseException {
        if (requestURI == null || method == null || callId == null
                || cSeq == null || from == null || to == null || via == null
                || maxForwards == null)
            throw new ParseException(
                    "JAIN-SIP Exception, some parameters are missing"
                            + ", unable to create the request", 0);
        SIPRequest sipRequest = new SIPRequest();
        sipRequest.setRequestURI(requestURI);
        sipRequest.setMethod(method);
        sipRequest.setCallId(callId);
        sipRequest.setCSeq(cSeq);
        sipRequest.setFrom(from);
        sipRequest.setTo(to);
        sipRequest.setVia(via);
        sipRequest.setMaxForwards(maxForwards);
        if ( != null) {
            sipRequest.setHeader();
        }
        return sipRequest;
    }
    // Standard Response Creation methods

    
Creates a new Response message of type specified by the statusCode paramater, containing the mandatory headers of the message with a body in the form of a Java object and the body content type.

Parameters:
statusCode - the new integer of the statusCode value of this Message.
callId - the new CallIdHeader object of the callId value of this Message.
cSeq - the new CSeqHeader object of the cSeq value of this Message.
from - the new FromHeader object of the from value of this Message.
to - the new ToHeader object of the to value of this Message.
via - the new List object of the ViaHeaders of this Message.
content - the new Object of the body content value of this Message.
contentType - the new ContentTypeHeader object of the content type value of this Message.
Throws:
java.text.ParseException which signals that an error has been reached unexpectedly while parsing the statusCode or the body.
    public Response createResponse(int statusCodeCallIdHeader callId,
            CSeqHeader cSeqFromHeader fromToHeader toList via,
            MaxForwardsHeader maxForwardsObject content,
            ContentTypeHeader contentTypethrows ParseException {
        if (callId == null || cSeq == null || from == null || to == null
                || via == null || maxForwards == null || content == null
                || contentType == null)
            throw new NullPointerException(" unable to create the response");
        SIPResponse sipResponse = new SIPResponse();
        StatusLine statusLine = new StatusLine();
        statusLine.setStatusCode(statusCode);
        String reasonPhrase = SIPResponse.getReasonPhrase(statusCode);
        //if (reasonPhrase == null)
        //  throw new ParseException(statusCode + " Unkown  ", 0);
        statusLine.setReasonPhrase(reasonPhrase);
        sipResponse.setStatusLine(statusLine);
        sipResponse.setCallId(callId);
        sipResponse.setCSeq(cSeq);
        sipResponse.setFrom(from);
        sipResponse.setTo(to);
        sipResponse.setVia(via);
        sipResponse.setMaxForwards(maxForwards);
        sipResponse.setContent(contentcontentType);
        if ( != null) {
            sipResponse.setHeader();
        }
        return sipResponse;
    }

    
Creates a new Response message of type specified by the statusCode paramater, containing the mandatory headers of the message with a body in the form of a byte array and the body content type.

Parameters:
statusCode - the new integer of the statusCode value of this Message.
callId - the new CallIdHeader object of the callId value of this Message.
cSeq - the new CSeqHeader object of the cSeq value of this Message.
from - the new FromHeader object of the from value of this Message.
to - the new ToHeader object of the to value of this Message.
via - the new List object of the ViaHeaders of this Message.
content - the new byte array of the body content value of this Message.
contentType - the new ContentTypeHeader object of the content type value of this Message.
Throws:
java.text.ParseException which signals that an error has been reached unexpectedly while parsing the statusCode or the body.
    public Response createResponse(int statusCodeCallIdHeader callId,
            CSeqHeader cSeqFromHeader fromToHeader toList via,
            MaxForwardsHeader maxForwardsbyte[] content,
            ContentTypeHeader contentTypethrows ParseException {
        if (callId == null || cSeq == null || from == null || to == null
                || via == null || maxForwards == null || content == null
                || contentType == null)
            throw new NullPointerException("Null params ");
        SIPResponse sipResponse = new SIPResponse();
        sipResponse.setStatusCode(statusCode);
        sipResponse.setCallId(callId);
        sipResponse.setCSeq(cSeq);
        sipResponse.setFrom(from);
        sipResponse.setTo(to);
        sipResponse.setVia(via);
        sipResponse.setMaxForwards(maxForwards);
        sipResponse.setHeader((ContentTypecontentType);
        sipResponse.setMessageContent(content);
        if ( != null) {
            sipResponse.setHeader();
        }
        return sipResponse;
    }

    
Creates a new Response message of type specified by the statusCode paramater, containing the mandatory headers of the message. This new Response does not contain a body.

Parameters:
statusCode - the new integer of the statusCode value of this Message.
callId - the new CallIdHeader object of the callId value of this Message.
cSeq - the new CSeqHeader object of the cSeq value of this Message.
from - the new FromHeader object of the from value of this Message.
to - the new ToHeader object of the to value of this Message.
via - the new List object of the ViaHeaders of this Message.
Throws:
java.text.ParseException which signals that an error has been reached unexpectedly while parsing the statusCode.
    public Response createResponse(int statusCodeCallIdHeader callId,
            CSeqHeader cSeqFromHeader fromToHeader toList via,
            MaxForwardsHeader maxForwardsthrows ParseException {
        if (callId == null || cSeq == null || from == null || to == null
                || via == null || maxForwards == null)
            throw new ParseException(
                    "JAIN-SIP Exception, some parameters are missing"
                            + ", unable to create the response", 0);
        SIPResponse sipResponse = new SIPResponse();
        sipResponse.setStatusCode(statusCode);
        sipResponse.setCallId(callId);
        sipResponse.setCSeq(cSeq);
        sipResponse.setFrom(from);
        sipResponse.setTo(to);
        sipResponse.setVia(via);
        sipResponse.setMaxForwards(maxForwards);
        if ( != null) {
            sipResponse.setHeader();
        }
        return sipResponse;
    }
    // Response Creation methods based on a Request

    
Creates a new Response message of type specified by the statusCode paramater, based on a specific Request with a new body in the form of a Java object and the body content type.

Parameters:
statusCode - the new integer of the statusCode value of this Message.
request - the received Reqest object upon which to base the Response.
content - the new Object of the body content value of this Message.
contentType - the new ContentTypeHeader object of the content type value of this Message.
Throws:
java.text.ParseException which signals that an error has been reached unexpectedly while parsing the statusCode or the body.
    public Response createResponse(int statusCodeRequest request,
            ContentTypeHeader contentTypeObject content)
            throws ParseException {
        if (request == null || content == null || contentType == null)
            throw new NullPointerException("null parameters");
        SIPRequest sipRequest = (SIPRequestrequest;
        SIPResponse sipResponse = sipRequest.createResponse(statusCode);
        sipResponse.setContent(contentcontentType);
        if ( != null) {
            sipResponse.setHeader();
        }
        return sipResponse;
    }

    
Creates a new Response message of type specified by the statusCode paramater, based on a specific Request with a new body in the form of a byte array and the body content type.

Parameters:
statusCode - the new integer of the statusCode value of this Message.
request - the received Reqest object upon which to base the Response.
content - the new byte array of the body content value of this Message.
contentType - the new ContentTypeHeader object of the content type value of this Message.
Throws:
java.text.ParseException which signals that an error has been reached unexpectedly while parsing the statusCode or the body.
    public Response createResponse(int statusCodeRequest request,
            ContentTypeHeader contentTypebyte[] content)
            throws ParseException {
        if (request == null || content == null || contentType == null)
            throw new NullPointerException("null Parameters");
        SIPRequest sipRequest = (SIPRequestrequest;
        SIPResponse sipResponse = sipRequest.createResponse(statusCode);
        sipResponse.setHeader((ContentTypecontentType);
        sipResponse.setMessageContent(content);
        if ( != null) {
            sipResponse.setHeader();
        }
        return sipResponse;
    }

    
Creates a new Response message of type specified by the statusCode paramater, based on a specific Request message. This new Response does not contain a body.

Parameters:
statusCode - the new integer of the statusCode value of this Message.
request - the received Reqest object upon which to base the Response.
Throws:
java.text.ParseException which signals that an error has been reached unexpectedly while parsing the statusCode.
    public Response createResponse(int statusCodeRequest request)
            throws ParseException {
        if (request == null)
            throw new NullPointerException("null parameters");
        // if (LogWriter.needsLogging)
        // LogWriter.logMessage("createResponse " + request);
        SIPRequest sipRequest = (SIPRequestrequest;
        SIPResponse sipResponse = sipRequest.createResponse(statusCode);
        // Remove the content from the message (Bug report from
        // Antonis Karydas.
        sipResponse.removeContent();
        sipResponse.removeHeader(.);
        if ( != null) {
            sipResponse.setHeader();
        }
        return sipResponse;
    }

    
Creates a new Request message of type specified by the method paramater, containing the URI of the Request, the mandatory headers of the message with a body in the form of a byte array and body content type.

Parameters:
requestURI - the new URI object of the requestURI value of this Message.
method - the new string of the method value of this Message.
callId - the new CallIdHeader object of the callId value of this Message.
cSeq - the new CSeqHeader object of the cSeq value of this Message.
from - the new FromHeader object of the from value of this Message.
to - the new ToHeader object of the to value of this Message.
via - the new List object of the ViaHeaders of this Message.
contentType - the new ContentTypeHeader object of the content type value of this Message.
content - the new byte array of the body content value of this Message.
Throws:
java.text.ParseException which signals that an error has been reached unexpectedly while parsing the method or the body.
    public Request createRequest(javax.sip.address.URI requestURI,
            String methodCallIdHeader callIdCSeqHeader cSeq,
            FromHeader fromToHeader toList via,
            MaxForwardsHeader maxForwardsContentTypeHeader contentType,
            byte[] contentthrows ParseException {
        if (requestURI == null || method == null || callId == null
                || cSeq == null || from == null || to == null || via == null
                || maxForwards == null || content == null
                || contentType == null)
            throw new NullPointerException("missing parameters");
        SIPRequest sipRequest = new SIPRequest();
        sipRequest.setRequestURI(requestURI);
        sipRequest.setMethod(method);
        sipRequest.setCallId(callId);
        sipRequest.setCSeq(cSeq);
        sipRequest.setFrom(from);
        sipRequest.setTo(to);
        sipRequest.setVia(via);
        sipRequest.setMaxForwards(maxForwards);
        sipRequest.setContent(contentcontentType);
        if ( != null) {
            sipRequest.setHeader();
        }
        return sipRequest;
    }

    
Creates a new Response message of type specified by the statusCode paramater, containing the mandatory headers of the message with a body in the form of a Java object and the body content type.

Parameters:
statusCode the new integer of the statusCode value of this Message.
callId the new CallIdHeader object of the callId value of this Message.
cSeq the new CSeqHeader object of the cSeq value of this Message.
from the new FromHeader object of the from value of this Message.
to the new ToHeader object of the to value of this Message.
via the new List object of the ViaHeaders of this Message.
contentType the new ContentTypeHeader object of the content type value of this Message.
content the new Object of the body content value of this Message.
Throws:
java.text.ParseException which signals that an error has been reached unexpectedly while parsing the statusCode or the body.
    public Response createResponse(int statusCodeCallIdHeader callId,
            CSeqHeader cSeqFromHeader fromToHeader toList via,
            MaxForwardsHeader maxForwardsContentTypeHeader contentType,
            Object contentthrows ParseException {
        if (callId == null || cSeq == null || from == null || to == null
                || via == null || maxForwards == null || content == null
                || contentType == null)
            throw new NullPointerException("missing parameters");
        SIPResponse sipResponse = new SIPResponse();
        StatusLine statusLine = new StatusLine();
        statusLine.setStatusCode(statusCode);
        String reason = SIPResponse.getReasonPhrase(statusCode);
        if (reason == null)
            throw new ParseException(statusCode + " Unknown", 0);
        statusLine.setReasonPhrase(reason);
        sipResponse.setStatusLine(statusLine);
        sipResponse.setCallId(callId);
        sipResponse.setCSeq(cSeq);
        sipResponse.setFrom(from);
        sipResponse.setTo(to);
        sipResponse.setVia(via);
        sipResponse.setContent(contentcontentType);
        if (  != null) {
            sipResponse.setHeader();
        }
        return sipResponse;
    }

    
Creates a new Response message of type specified by the statusCode paramater, containing the mandatory headers of the message with a body in the form of a byte array and the body content type.

Parameters:
statusCode the new integer of the statusCode value of this Message.
callId the new CallIdHeader object of the callId value of this Message.
cSeq the new CSeqHeader object of the cSeq value of this Message.
from the new FromHeader object of the from value of this Message.
to the new ToHeader object of the to value of this Message.
via the new List object of the ViaHeaders of this Message.
contentType the new ContentTypeHeader object of the content type value of this Message.
content the new byte array of the body content value of this Message.
Throws:
java.text.ParseException which signals that an error has been reached unexpectedly while parsing the statusCode or the body.
    public Response createResponse(int statusCodeCallIdHeader callId,
            CSeqHeader cSeqFromHeader fromToHeader toList via,
            MaxForwardsHeader maxForwardsContentTypeHeader contentType,
            byte[] contentthrows ParseException {
        if (callId == null || cSeq == null || from == null || to == null
                || via == null || maxForwards == null || content == null
                || contentType == null)
            throw new NullPointerException("missing parameters");
        SIPResponse sipResponse = new SIPResponse();
        StatusLine statusLine = new StatusLine();
        statusLine.setStatusCode(statusCode);
        String reason = SIPResponse.getReasonPhrase(statusCode);
        if (reason == null)
            throw new ParseException(statusCode + " : Unknown", 0);
        statusLine.setReasonPhrase(reason);
        sipResponse.setStatusLine(statusLine);
        sipResponse.setCallId(callId);
        sipResponse.setCSeq(cSeq);
        sipResponse.setFrom(from);
        sipResponse.setTo(to);
        sipResponse.setVia(via);
        sipResponse.setContent(contentcontentType);
        if (  != null) {
            sipResponse.setHeader();
        }
        return sipResponse;
    }

    
Create a request from a string. Conveniance method for UACs that want to create an outgoing request from a string. Only the headers of the request should be included in the String that is supplied to this method.

Parameters:
requestString -- string from which to create the message null string returns an empty message.
    public javax.sip.message.Request createRequest(String requestString)
            throws java.text.ParseException {
        if (requestString == null || requestString.equals("")) {
            SIPRequest retval = new SIPRequest();
            retval.setNullRequest();
            return retval;
        }
        StringMsgParser smp = new StringMsgParser();
        smp.setStrict(this.);
        /*
         * This allows you to catch parse exceptions and create invalid messages
         * if you want.
         */
        ParseExceptionListener parseExceptionListener = new ParseExceptionListener() {
            public void handleException(ParseException ex,
                    SIPMessage sipMessageClass headerClass,
                    String headerTextString messageText)
                    throws ParseException {
                // Rethrow the error for the essential headers. Otherwise bad
                // headers are simply
                // recorded in the message.
                if () {
                    if (headerClass == From.class || headerClass == To.class
                            || headerClass == CallID.class
                            || headerClass == MaxForwards.class
                            || headerClass == Via.class
                            || headerClass == RequestLine.class
                            || headerClass == StatusLine.class
                            || headerClass == CSeq.class)
                        throw ex;
                    sipMessage.addUnparsed(headerText);
                }
            }
        };
        if (this.)
            smp.setParseExceptionListener(parseExceptionListener);
        SIPMessage sipMessage = smp.parseSIPMessage(requestString);
        if (!(sipMessage instanceof SIPRequest))
            throw new ParseException(requestString, 0);
        return (SIPRequestsipMessage;
    }

    
Create a response from a string

Parameters:
responseString -- string from which to create the message null string returns an empty message.
    public Response createResponse(String responseString)
            throws java.text.ParseException {
        if (responseString == null)
            return new SIPResponse();
        StringMsgParser smp = new StringMsgParser();
        SIPMessage sipMessage = smp.parseSIPMessage(responseString);
        if (!(sipMessage instanceof SIPResponse))
            throw new ParseException(responseString, 0);
        return (SIPResponsesipMessage;
    }

    
Set the common UserAgent header for all requests created from this message factory. This header is applied to all Messages created from this Factory object except those that take String for an argument and create Message from the given String.

Parameters:
userAgent -- the user agent header to set.
Since:
2.0
    public void setDefaultUserAgentHeader(UserAgentHeader userAgent) {
        . = userAgent;
    }

    
Set the common Server header for all responses created from this message factory. This header is applied to all Messages created from this Factory object except those that take String for an argument and create Message from the given String.

Parameters:
userAgent -- the user agent header to set.
Since:
2.0
    public void setDefaultServerHeader(ServerHeader server) {
        . = server;
    }
    
Get the default common UserAgentHeader.

Returns:
the user agent header.
Since:
2.0
    public static UserAgentHeader getDefaultUserAgentHeader() {
        return ;
    }


    
Get the default common server header.

Returns:
the server header.
    public static ServerHeader getDefaultServerHeader() {
        return ;
    }


    
Set default charset used for encoding String content.

Parameters:
charset
    public  void setDefaultContentEncodingCharset(String charsetthrows NullPointerException,
        if (charset == null ) throw new NullPointerException ("Null argument!");
    }
    public static String getDefaultContentEncodingCharset() {
    }
    
            String[] contentType,
            String[] contentSubtype
            String[] contentBody) {
        String boundary = multipartMimeCth.getParameter("boundary");
        MultipartMimeContentImpl retval = new MultipartMimeContentImpl(multipartMimeCth);
        for (int i = 0 ;  i < contentType.lengthi++ ) {
            ContentTypeHeader cth = new ContentType(contentType[i],contentSubtype[i]);
            ContentImpl contentImpl  = new ContentImpl(contentBody[i],boundary);
            contentImpl.setContentTypeHeader(cth);
            retval.add(contentImpl);
        }
        return retval;
    }
New to GrepCode? Check out our FAQ X