FileDocCategorySizeDatePackage
SipHelper.javaAPI DocAndroid 5.1 API21311Thu Mar 12 22:22:52 GMT 2015com.android.server.sip

SipHelper

public class SipHelper extends Object
Helper class for holding SIP stack related classes and for various low-level SIP tasks like sending messages.

Fields Summary
private static final String
TAG
private static final boolean
DBG
private static final boolean
DBG_PING
private javax.sip.SipStack
mSipStack
private javax.sip.SipProvider
mSipProvider
private javax.sip.address.AddressFactory
mAddressFactory
private javax.sip.header.HeaderFactory
mHeaderFactory
private javax.sip.message.MessageFactory
mMessageFactory
Constructors Summary
public SipHelper(javax.sip.SipStack sipStack, javax.sip.SipProvider sipProvider)


        
              
        mSipStack = sipStack;
        mSipProvider = sipProvider;

        SipFactory sipFactory = SipFactory.getInstance();
        mAddressFactory = sipFactory.createAddressFactory();
        mHeaderFactory = sipFactory.createHeaderFactory();
        mMessageFactory = sipFactory.createMessageFactory();
    
Methods Summary
private javax.sip.header.CSeqHeadercreateCSeqHeader(java.lang.String method)

        long sequence = (long) (Math.random() * 10000);
        return mHeaderFactory.createCSeqHeader(sequence, method);
    
private javax.sip.header.CallIdHeadercreateCallIdHeader()

        return mSipProvider.getNewCallId();
    
private javax.sip.header.ContactHeadercreateContactHeader(android.net.sip.SipProfile profile)

        return createContactHeader(profile, null, 0);
    
private javax.sip.header.ContactHeadercreateContactHeader(android.net.sip.SipProfile profile, java.lang.String ip, int port)

        SipURI contactURI = (ip == null)
                ? createSipUri(profile.getUserName(), profile.getProtocol(),
                        getListeningPoint())
                : createSipUri(profile.getUserName(), profile.getProtocol(),
                        ip, port);

        Address contactAddress = mAddressFactory.createAddress(contactURI);
        contactAddress.setDisplayName(profile.getDisplayName());

        return mHeaderFactory.createContactHeader(contactAddress);
    
private javax.sip.header.FromHeadercreateFromHeader(android.net.sip.SipProfile profile, java.lang.String tag)

        return mHeaderFactory.createFromHeader(profile.getSipAddress(), tag);
    
private javax.sip.header.MaxForwardsHeadercreateMaxForwardsHeader()

        return mHeaderFactory.createMaxForwardsHeader(70);
    
private javax.sip.header.MaxForwardsHeadercreateMaxForwardsHeader(int max)

        return mHeaderFactory.createMaxForwardsHeader(max);
    
private javax.sip.message.RequestcreateRequest(java.lang.String requestType, android.net.sip.SipProfile userProfile, java.lang.String tag)

        FromHeader fromHeader = createFromHeader(userProfile, tag);
        ToHeader toHeader = createToHeader(userProfile);

        String replaceStr = Pattern.quote(userProfile.getUserName() + "@");
        SipURI requestURI = mAddressFactory.createSipURI(
                userProfile.getUriString().replaceFirst(replaceStr, ""));

        List<ViaHeader> viaHeaders = createViaHeaders();
        CallIdHeader callIdHeader = createCallIdHeader();
        CSeqHeader cSeqHeader = createCSeqHeader(requestType);
        MaxForwardsHeader maxForwards = createMaxForwardsHeader();
        Request request = mMessageFactory.createRequest(requestURI,
                requestType, callIdHeader, cSeqHeader, fromHeader,
                toHeader, viaHeaders, maxForwards);
        Header userAgentHeader = mHeaderFactory.createHeader("User-Agent",
                "SIPAUA/0.1.001");
        request.addHeader(userAgentHeader);
        return request;
    
private javax.sip.message.RequestcreateRequest(java.lang.String requestType, android.net.sip.SipProfile caller, android.net.sip.SipProfile callee, java.lang.String tag)

        FromHeader fromHeader = createFromHeader(caller, tag);
        ToHeader toHeader = createToHeader(callee);
        SipURI requestURI = callee.getUri();
        List<ViaHeader> viaHeaders = createViaHeaders();
        CallIdHeader callIdHeader = createCallIdHeader();
        CSeqHeader cSeqHeader = createCSeqHeader(requestType);
        MaxForwardsHeader maxForwards = createMaxForwardsHeader();

        Request request = mMessageFactory.createRequest(requestURI,
                requestType, callIdHeader, cSeqHeader, fromHeader,
                toHeader, viaHeaders, maxForwards);

        request.addHeader(createContactHeader(caller));
        return request;
    
private javax.sip.address.SipURIcreateSipUri(java.lang.String username, java.lang.String transport, javax.sip.ListeningPoint lp)

        return createSipUri(username, transport, lp.getIPAddress(), lp.getPort());
    
private javax.sip.address.SipURIcreateSipUri(java.lang.String username, java.lang.String transport, java.lang.String ip, int port)

        SipURI uri = mAddressFactory.createSipURI(username, ip);
        try {
            uri.setPort(port);
            uri.setTransportParam(transport);
        } catch (InvalidArgumentException e) {
            throw new RuntimeException(e);
        }
        return uri;
    
private javax.sip.header.ToHeadercreateToHeader(android.net.sip.SipProfile profile)

        return createToHeader(profile, null);
    
private javax.sip.header.ToHeadercreateToHeader(android.net.sip.SipProfile profile, java.lang.String tag)

        return mHeaderFactory.createToHeader(profile.getSipAddress(), tag);
    
private java.util.ListcreateViaHeaders()

        List<ViaHeader> viaHeaders = new ArrayList<ViaHeader>(1);
        ListeningPoint lp = getListeningPoint();
        ViaHeader viaHeader = mHeaderFactory.createViaHeader(lp.getIPAddress(),
                lp.getPort(), lp.getTransport(), null);
        viaHeader.setRPort();
        viaHeaders.add(viaHeader);
        return viaHeaders;
    
private javax.sip.header.ContactHeadercreateWildcardContactHeader()

        ContactHeader contactHeader  = mHeaderFactory.createContactHeader();
        contactHeader.setWildCard();
        return contactHeader;
    
public static java.lang.StringgetCallId(java.util.EventObject event)

        if (event == null) return null;
        if (event instanceof RequestEvent) {
            return getCallId(((RequestEvent) event).getRequest());
        } else if (event instanceof ResponseEvent) {
            return getCallId(((ResponseEvent) event).getResponse());
        } else if (event instanceof DialogTerminatedEvent) {
            Dialog dialog = ((DialogTerminatedEvent) event).getDialog();
            return getCallId(((DialogTerminatedEvent) event).getDialog());
        } else if (event instanceof TransactionTerminatedEvent) {
            TransactionTerminatedEvent e = (TransactionTerminatedEvent) event;
            return getCallId(e.isServerTransaction()
                    ? e.getServerTransaction()
                    : e.getClientTransaction());
        } else {
            Object source = event.getSource();
            if (source instanceof Transaction) {
                return getCallId(((Transaction) source));
            } else if (source instanceof Dialog) {
                return getCallId((Dialog) source);
            }
        }
        return "";
    
public static java.lang.StringgetCallId(javax.sip.Transaction transaction)

        return ((transaction != null) ? getCallId(transaction.getRequest())
                                      : "");
    
private static java.lang.StringgetCallId(javax.sip.message.Message message)

        CallIdHeader callIdHeader =
                (CallIdHeader) message.getHeader(CallIdHeader.NAME);
        return callIdHeader.getCallId();
    
private static java.lang.StringgetCallId(javax.sip.Dialog dialog)

        return dialog.getCallId().getCallId();
    
private javax.sip.ListeningPointgetListeningPoint()

        ListeningPoint lp = mSipProvider.getListeningPoint(ListeningPoint.UDP);
        if (lp == null) lp = mSipProvider.getListeningPoint(ListeningPoint.TCP);
        if (lp == null) {
            ListeningPoint[] lps = mSipProvider.getListeningPoints();
            if ((lps != null) && (lps.length > 0)) lp = lps[0];
        }
        if (lp == null) {
            throw new SipException("no listening point is available");
        }
        return lp;
    
public javax.sip.ServerTransactiongetServerTransaction(javax.sip.RequestEvent event)

        ServerTransaction transaction = event.getServerTransaction();
        if (transaction == null) {
            Request request = event.getRequest();
            return mSipProvider.getNewServerTransaction(request);
        } else {
            return transaction;
        }
    
public javax.sip.ClientTransactionhandleChallenge(javax.sip.ResponseEvent responseEvent, gov.nist.javax.sip.clientauthutils.AccountManager accountManager)

        AuthenticationHelper authenticationHelper =
                ((SipStackExt) mSipStack).getAuthenticationHelper(
                        accountManager, mHeaderFactory);
        ClientTransaction tid = responseEvent.getClientTransaction();
        ClientTransaction ct = authenticationHelper.handleChallenge(
                responseEvent.getResponse(), tid, mSipProvider, 5);
        if (DBG) log("send request with challenge response: "
                + ct.getRequest());
        ct.sendRequest();
        return ct;
    
private voidlog(java.lang.String s)

        Rlog.d(TAG, s);
    
public voidsendBye(javax.sip.Dialog dialog)

        Request byeRequest = dialog.createRequest(Request.BYE);
        if (DBG) log("send BYE: " + byeRequest);
        dialog.sendRequest(mSipProvider.getNewClientTransaction(byeRequest));
    
public voidsendCancel(javax.sip.ClientTransaction inviteTransaction)

        Request cancelRequest = inviteTransaction.createCancel();
        if (DBG) log("send CANCEL: " + cancelRequest);
        mSipProvider.getNewClientTransaction(cancelRequest).sendRequest();
    
public javax.sip.ClientTransactionsendInvite(android.net.sip.SipProfile caller, android.net.sip.SipProfile callee, java.lang.String sessionDescription, java.lang.String tag, gov.nist.javax.sip.header.extensions.ReferredByHeader referredBy, java.lang.String replaces)

        try {
            Request request = createRequest(Request.INVITE, caller, callee, tag);
            if (referredBy != null) request.addHeader(referredBy);
            if (replaces != null) {
                request.addHeader(mHeaderFactory.createHeader(
                        ReplacesHeader.NAME, replaces));
            }
            request.setContent(sessionDescription,
                    mHeaderFactory.createContentTypeHeader(
                            "application", "sdp"));
            ClientTransaction clientTransaction =
                    mSipProvider.getNewClientTransaction(request);
            if (DBG) log("send INVITE: " + request);
            clientTransaction.sendRequest();
            return clientTransaction;
        } catch (ParseException e) {
            throw new SipException("sendInvite()", e);
        }
    
public voidsendInviteAck(javax.sip.ResponseEvent event, javax.sip.Dialog dialog)

param
event the INVITE ACK request event

        Response response = event.getResponse();
        long cseq = ((CSeqHeader) response.getHeader(CSeqHeader.NAME))
                .getSeqNumber();
        Request ack = dialog.createAck(cseq);
        if (DBG) log("send ACK: " + ack);
        dialog.sendAck(ack);
    
public voidsendInviteBusyHere(javax.sip.RequestEvent event, javax.sip.ServerTransaction inviteTransaction)

        try {
            Request request = event.getRequest();
            Response response = mMessageFactory.createResponse(
                    Response.BUSY_HERE, request);

            if (inviteTransaction == null) {
                inviteTransaction = getServerTransaction(event);
            }

            if (inviteTransaction.getState() != TransactionState.COMPLETED) {
                if (DBG) log("send BUSY HERE: " + response);
                inviteTransaction.sendResponse(response);
            }
        } catch (ParseException e) {
            throw new SipException("sendInviteBusyHere()", e);
        }
    
public javax.sip.ServerTransactionsendInviteOk(javax.sip.RequestEvent event, android.net.sip.SipProfile localProfile, java.lang.String sessionDescription, javax.sip.ServerTransaction inviteTransaction, java.lang.String externalIp, int externalPort)

param
event the INVITE request event

        try {
            Request request = event.getRequest();
            Response response = mMessageFactory.createResponse(Response.OK,
                    request);
            response.addHeader(createContactHeader(localProfile, externalIp,
                    externalPort));
            response.setContent(sessionDescription,
                    mHeaderFactory.createContentTypeHeader(
                            "application", "sdp"));

            if (inviteTransaction == null) {
                inviteTransaction = getServerTransaction(event);
            }

            if (inviteTransaction.getState() != TransactionState.COMPLETED) {
                if (DBG) log("send OK: " + response);
                inviteTransaction.sendResponse(response);
            }

            return inviteTransaction;
        } catch (ParseException e) {
            throw new SipException("sendInviteOk()", e);
        }
    
public voidsendInviteRequestTerminated(javax.sip.message.Request inviteRequest, javax.sip.ServerTransaction inviteTransaction)

        try {
            Response response = mMessageFactory.createResponse(
                    Response.REQUEST_TERMINATED, inviteRequest);
            if (DBG) log("send response: " + response);
            inviteTransaction.sendResponse(response);
        } catch (ParseException e) {
            throw new SipException("sendInviteRequestTerminated()", e);
        }
    
public javax.sip.ClientTransactionsendOptions(android.net.sip.SipProfile caller, android.net.sip.SipProfile callee, java.lang.String tag)

        try {
            Request request = (caller == callee)
                    ? createRequest(Request.OPTIONS, caller, tag)
                    : createRequest(Request.OPTIONS, caller, callee, tag);

            ClientTransaction clientTransaction =
                    mSipProvider.getNewClientTransaction(request);
            clientTransaction.sendRequest();
            return clientTransaction;
        } catch (Exception e) {
            throw new SipException("sendOptions()", e);
        }
    
public voidsendReferNotify(javax.sip.Dialog dialog, java.lang.String content)

        try {
            Request request = dialog.createRequest(Request.NOTIFY);
            request.addHeader(mHeaderFactory.createSubscriptionStateHeader(
                    "active;expires=60"));
            // set content here
            request.setContent(content,
                    mHeaderFactory.createContentTypeHeader(
                            "message", "sipfrag"));
            request.addHeader(mHeaderFactory.createEventHeader(
                    ReferencesHeader.REFER));
            if (DBG) log("send NOTIFY: " + request);
            dialog.sendRequest(mSipProvider.getNewClientTransaction(request));
        } catch (ParseException e) {
            throw new SipException("sendReferNotify()", e);
        }
    
public javax.sip.ClientTransactionsendRegister(android.net.sip.SipProfile userProfile, java.lang.String tag, int expiry)

        try {
            Request request = createRequest(Request.REGISTER, userProfile, tag);
            if (expiry == 0) {
                // remove all previous registrations by wildcard
                // rfc3261#section-10.2.2
                request.addHeader(createWildcardContactHeader());
            } else {
                request.addHeader(createContactHeader(userProfile));
            }
            request.addHeader(mHeaderFactory.createExpiresHeader(expiry));

            ClientTransaction clientTransaction =
                    mSipProvider.getNewClientTransaction(request);
            clientTransaction.sendRequest();
            return clientTransaction;
        } catch (ParseException e) {
            throw new SipException("sendRegister()", e);
        }
    
public javax.sip.ClientTransactionsendReinvite(javax.sip.Dialog dialog, java.lang.String sessionDescription)

        try {
            Request request = dialog.createRequest(Request.INVITE);
            request.setContent(sessionDescription,
                    mHeaderFactory.createContentTypeHeader(
                            "application", "sdp"));

            // Adding rport argument in the request could fix some SIP servers
            // in resolving the initiator's NAT port mapping for relaying the
            // response message from the other end.

            ViaHeader viaHeader = (ViaHeader) request.getHeader(ViaHeader.NAME);
            if (viaHeader != null) viaHeader.setRPort();

            ClientTransaction clientTransaction =
                    mSipProvider.getNewClientTransaction(request);
            if (DBG) log("send RE-INVITE: " + request);
            dialog.sendRequest(clientTransaction);
            return clientTransaction;
        } catch (ParseException e) {
            throw new SipException("sendReinvite()", e);
        }
    
public voidsendResponse(javax.sip.RequestEvent event, int responseCode)

        try {
            Request request = event.getRequest();
            Response response = mMessageFactory.createResponse(
                    responseCode, request);
            if (DBG && (!Request.OPTIONS.equals(request.getMethod())
                    || DBG_PING)) {
                log("send response: " + response);
            }
            getServerTransaction(event).sendResponse(response);
        } catch (ParseException e) {
            throw new SipException("sendResponse()", e);
        }
    
public javax.sip.ServerTransactionsendRinging(javax.sip.RequestEvent event, java.lang.String tag)

param
event the INVITE request event

        try {
            Request request = event.getRequest();
            ServerTransaction transaction = getServerTransaction(event);

            Response response = mMessageFactory.createResponse(Response.RINGING,
                    request);

            ToHeader toHeader = (ToHeader) response.getHeader(ToHeader.NAME);
            toHeader.setTag(tag);
            response.addHeader(toHeader);
            if (DBG) log("send RINGING: " + response);
            transaction.sendResponse(response);
            return transaction;
        } catch (ParseException e) {
            throw new SipException("sendRinging()", e);
        }