Search in sources :

Example 31 with SIPRequest

use of gov.nist.javax.sip.message.SIPRequest in project XobotOS by xamarin.

the class SIPDialog method createRequest.

/**
     * The method that actually does the work of creating a request.
     * 
     * @param method
     * @param response
     * @return
     * @throws SipException
     */
private Request createRequest(String method, SIPResponse sipResponse) throws SipException {
    if (method == null || sipResponse == null)
        throw new NullPointerException("null argument");
    if (method.equals(Request.CANCEL))
        throw new SipException("Dialog.createRequest(): Invalid request");
    if (this.getState() == null || (this.getState().getValue() == TERMINATED_STATE && !method.equalsIgnoreCase(Request.BYE)) || (this.isServer() && this.getState().getValue() == EARLY_STATE && method.equalsIgnoreCase(Request.BYE)))
        throw new SipException("Dialog  " + getDialogId() + " not yet established or terminated " + this.getState());
    SipUri sipUri = null;
    if (this.getRemoteTarget() != null)
        sipUri = (SipUri) this.getRemoteTarget().getURI().clone();
    else {
        sipUri = (SipUri) this.getRemoteParty().getURI().clone();
        sipUri.clearUriParms();
    }
    CSeq cseq = new CSeq();
    try {
        cseq.setMethod(method);
        cseq.setSeqNumber(this.getLocalSeqNumber());
    } catch (Exception ex) {
        if (sipStack.isLoggingEnabled())
            sipStack.getStackLogger().logError("Unexpected error");
        InternalErrorHandler.handleException(ex);
    }
    /*
         * Add a via header for the outbound request based on the transport of the message
         * processor.
         */
    ListeningPointImpl lp = (ListeningPointImpl) this.sipProvider.getListeningPoint(sipResponse.getTopmostVia().getTransport());
    if (lp == null) {
        if (sipStack.isLoggingEnabled())
            sipStack.getStackLogger().logError("Cannot find listening point for transport " + sipResponse.getTopmostVia().getTransport());
        throw new SipException("Cannot find listening point for transport " + sipResponse.getTopmostVia().getTransport());
    }
    Via via = lp.getViaHeader();
    From from = new From();
    from.setAddress(this.localParty);
    To to = new To();
    to.setAddress(this.remoteParty);
    SIPRequest sipRequest = sipResponse.createRequest(sipUri, via, cseq, from, to);
    if (SIPRequest.isTargetRefresh(method)) {
        ContactHeader contactHeader = ((ListeningPointImpl) this.sipProvider.getListeningPoint(lp.getTransport())).createContactHeader();
        ((SipURI) contactHeader.getAddress().getURI()).setSecure(this.isSecure());
        sipRequest.setHeader(contactHeader);
    }
    try {
        /*
             * Guess of local sequence number - this is being re-set when the request is actually
             * dispatched
             */
        cseq = (CSeq) sipRequest.getCSeq();
        cseq.setSeqNumber(this.localSequenceNumber + 1);
    } catch (InvalidArgumentException ex) {
        InternalErrorHandler.handleException(ex);
    }
    if (method.equals(Request.SUBSCRIBE)) {
        if (eventHeader != null)
            sipRequest.addHeader(eventHeader);
    }
    try {
        if (this.getLocalTag() != null) {
            from.setTag(this.getLocalTag());
        } else {
            from.removeTag();
        }
        if (this.getRemoteTag() != null) {
            to.setTag(this.getRemoteTag());
        } else {
            to.removeTag();
        }
    } catch (ParseException ex) {
        InternalErrorHandler.handleException(ex);
    }
    // get the route list from the dialog.
    this.updateRequest(sipRequest);
    return sipRequest;
}
Also used : ContactHeader(javax.sip.header.ContactHeader) ListeningPointImpl(gov.nist.javax.sip.ListeningPointImpl) CSeq(gov.nist.javax.sip.header.CSeq) From(gov.nist.javax.sip.header.From) SipUri(gov.nist.javax.sip.address.SipUri) SipURI(javax.sip.address.SipURI) SIPRequest(gov.nist.javax.sip.message.SIPRequest) DialogDoesNotExistException(javax.sip.DialogDoesNotExistException) InvalidArgumentException(javax.sip.InvalidArgumentException) ParseException(java.text.ParseException) ObjectInUseException(javax.sip.ObjectInUseException) SipException(javax.sip.SipException) IOException(java.io.IOException) TransactionDoesNotExistException(javax.sip.TransactionDoesNotExistException) Via(gov.nist.javax.sip.header.Via) InvalidArgumentException(javax.sip.InvalidArgumentException) To(gov.nist.javax.sip.header.To) ParseException(java.text.ParseException) SipException(javax.sip.SipException)

Example 32 with SIPRequest

use of gov.nist.javax.sip.message.SIPRequest in project XobotOS by xamarin.

the class SIPDialog method createReliableProvisionalResponse.

/*
     * (non-Javadoc)
     * 
     * @see javax.sip.Dialog#createReliableProvisionalResponse(int)
     */
public Response createReliableProvisionalResponse(int statusCode) throws InvalidArgumentException, SipException {
    if (!(firstTransactionIsServerTransaction)) {
        throw new SipException("Not a Server Dialog!");
    }
    /*
         * A UAS MUST NOT attempt to send a 100 (Trying) response reliably. Only provisional
         * responses numbered 101 to 199 may be sent reliably. If the request did not include
         * either a Supported or Require header field indicating this feature, the UAS MUST NOT
         * send the provisional response reliably.
         */
    if (statusCode <= 100 || statusCode > 199)
        throw new InvalidArgumentException("Bad status code ");
    SIPRequest request = this.originalRequest;
    if (!request.getMethod().equals(Request.INVITE))
        throw new SipException("Bad method");
    ListIterator<SIPHeader> list = request.getHeaders(SupportedHeader.NAME);
    if (list == null || !optionPresent(list, "100rel")) {
        list = request.getHeaders(RequireHeader.NAME);
        if (list == null || !optionPresent(list, "100rel")) {
            throw new SipException("No Supported/Require 100rel header in the request");
        }
    }
    SIPResponse response = request.createResponse(statusCode);
    /*
         * The provisional response to be sent reliably is constructed by the UAS core according
         * to the procedures of Section 8.2.6 of RFC 3261. In addition, it MUST contain a Require
         * header field containing the option tag 100rel, and MUST include an RSeq header field.
         * The value of the header field for the first reliable provisional response in a
         * transaction MUST be between 1 and 2**31 - 1. It is RECOMMENDED that it be chosen
         * uniformly in this range. The RSeq numbering space is within a single transaction. This
         * means that provisional responses for different requests MAY use the same values for the
         * RSeq number.
         */
    Require require = new Require();
    try {
        require.setOptionTag("100rel");
    } catch (Exception ex) {
        InternalErrorHandler.handleException(ex);
    }
    response.addHeader(require);
    RSeq rseq = new RSeq();
    /*
         * set an arbitrary sequence number. This is actually set when the response is sent out
         */
    rseq.setSeqNumber(1L);
    /*
         * Copy the record route headers from the request to the response ( Issue 160 ). Note that
         * other 1xx headers do not get their Record Route headers copied over but reliable
         * provisional responses do. See RFC 3262 Table 2.
         */
    RecordRouteList rrl = request.getRecordRouteHeaders();
    if (rrl != null) {
        RecordRouteList rrlclone = (RecordRouteList) rrl.clone();
        response.setHeader(rrlclone);
    }
    return response;
}
Also used : SIPResponse(gov.nist.javax.sip.message.SIPResponse) Require(gov.nist.javax.sip.header.Require) InvalidArgumentException(javax.sip.InvalidArgumentException) SIPHeader(gov.nist.javax.sip.header.SIPHeader) RecordRouteList(gov.nist.javax.sip.header.RecordRouteList) RSeq(gov.nist.javax.sip.header.RSeq) SipException(javax.sip.SipException) SIPRequest(gov.nist.javax.sip.message.SIPRequest) DialogDoesNotExistException(javax.sip.DialogDoesNotExistException) InvalidArgumentException(javax.sip.InvalidArgumentException) ParseException(java.text.ParseException) ObjectInUseException(javax.sip.ObjectInUseException) SipException(javax.sip.SipException) IOException(java.io.IOException) TransactionDoesNotExistException(javax.sip.TransactionDoesNotExistException)

Aggregations

SIPRequest (gov.nist.javax.sip.message.SIPRequest)32 IOException (java.io.IOException)17 ParseException (java.text.ParseException)17 SipException (javax.sip.SipException)16 InvalidArgumentException (javax.sip.InvalidArgumentException)10 SIPResponse (gov.nist.javax.sip.message.SIPResponse)9 Via (gov.nist.javax.sip.header.Via)8 ObjectInUseException (javax.sip.ObjectInUseException)8 SIPClientTransaction (gov.nist.javax.sip.stack.SIPClientTransaction)6 SIPDialog (gov.nist.javax.sip.stack.SIPDialog)6 DialogDoesNotExistException (javax.sip.DialogDoesNotExistException)6 TransactionDoesNotExistException (javax.sip.TransactionDoesNotExistException)6 Hop (javax.sip.address.Hop)6 SipURI (javax.sip.address.SipURI)4 ListeningPointImpl (gov.nist.javax.sip.ListeningPointImpl)3 From (gov.nist.javax.sip.header.From)3 Route (gov.nist.javax.sip.header.Route)3 To (gov.nist.javax.sip.header.To)3 SIPServerTransaction (gov.nist.javax.sip.stack.SIPServerTransaction)3 SIPTransaction (gov.nist.javax.sip.stack.SIPTransaction)3