Search in sources :

Example 1 with CSeqHeader

use of javax.sip.header.CSeqHeader in project Openfire by igniterealtime.

the class SimpleSession method prepareRequest.

/**
     * @param requestType Type of request
	 * @param destUri    The SipURI for the destination.  Leave <code>null</code> if a loopback request (e.g. REGISTER) is being made.
	 * @param toTag      The tag for to header.  Can leave null.
	 * @param requestUri The Request URI to set in the message.  Leave null if the default destination SipURI should be used.
     * @param callId     ID of call
     * @param seqNum     Sequence number
     * @return Prepared request
	 */
private Request prepareRequest(RequestType requestType, SipURI destUri, String toTag, SipURI requestUri, String callId, long seqNum) {
    Request request = null;
    String myXMPPUsername = this.jid.getNode();
    Log.debug("SimpleSession(" + myXMPPUsername + ").prepareRequest:  Preparing request packet of type '" + requestType + "'");
    try {
        // Prepare request packet first
        request = messageFactory.createRequest(null);
        request.setMethod(requestType.toString());
    } catch (Exception e) {
        Log.debug("SimpleSession(" + myXMPPUsername + ").prepareRequest:  Exception occured when preparing request.", e);
    }
    // Prepare "From" header
    Log.debug("SimpleSession(" + myXMPPUsername + ").prepareRequest:  Preparing \"From\" header...");
    String mySipUsername = registration.getUsername();
    try {
        SipURI fromUri = addressFactory.createSipURI(mySipUsername, sipHost);
        Address fromNameAddress = addressFactory.createAddress(fromUri);
        fromNameAddress.setDisplayName(mySipUsername);
        FromHeader fromHeader = headerFactory.createFromHeader(fromNameAddress, getTag());
        // Use "set" because this header is mandatory.
        request.setHeader(fromHeader);
    } catch (Exception e) {
        Log.debug("SimpleSession(" + myXMPPUsername + ").prepareRequest:  Exception occured when preparing FromHeader.", e);
        return null;
    }
    // Prepare "To" header
    Log.debug("SimpleSession(" + myXMPPUsername + ").prepareRequest:  Preparing \"To\" header...");
    try {
        if (destUri == null)
            destUri = addressFactory.createSipURI(mySipUsername, sipHost);
        Address toNameAddress = addressFactory.createAddress(destUri);
        ToHeader toHeader = headerFactory.createToHeader(toNameAddress, toTag);
        // Use "set" because this header is mandatory.
        request.setHeader(toHeader);
    } catch (Exception e) {
        Log.debug("SimpleSession(" + myXMPPUsername + ").prepareRequest:  Exception occured when preparing ToHeader.", e);
        return null;
    }
    // Prepare "Via" header
    Log.debug("SimpleSession(" + myXMPPUsername + ").prepareRequest:  Preparing \"Via\" header...");
    try {
        ViaHeader viaHeader = headerFactory.createViaHeader(InetAddress.getLocalHost().getHostAddress(), sipPort, ListeningPoint.UDP, null);
        // Use "set" because this header is mandatory.
        request.setHeader(viaHeader);
    } catch (Exception e) {
        Log.debug("SimpleSession(" + myXMPPUsername + ").prepareRequest:  Exception occured when preparing ViaHeader.", e);
        return null;
    }
    // Prepare "CallId" header
    Log.debug("SimpleSession(" + myXMPPUsername + ").prepareRequest:  Preparing \"CallId\" header...");
    CallIdHeader callIdHeader;
    try {
        if (callId != null)
            callIdHeader = headerFactory.createCallIdHeader(callId);
        else
            callIdHeader = udpSipProvider.getNewCallId();
        // Use "set" because this header is mandatory.
        request.setHeader(callIdHeader);
    } catch (Exception e) {
        Log.debug("SimpleSession(" + myXMPPUsername + ").prepareRequest:  Exception occured when preparing CallIdHeader.", e);
        return null;
    }
    // Prepare "CSeq" header
    Log.debug("SimpleSession(" + myXMPPUsername + ").prepareRequest:  Preparing \"CSeq\" header...");
    try {
        CSeqHeader cSeqHeader = headerFactory.createCSeqHeader(seqNum, requestType.toString());
        // Use "set" because this header is mandatory.
        request.setHeader(cSeqHeader);
    } catch (Exception e) {
        Log.debug("SimpleSession(" + myXMPPUsername + ").prepareRequest:  Exception occured when preparing CSeqHeader.", e);
        return null;
    }
    // Prepare "MaxForwards" header
    Log.debug("SimpleSession(" + myXMPPUsername + ").prepareRequest:  Preparing \"MaxForwards\" header...");
    try {
        MaxForwardsHeader maxForwardsHeader = headerFactory.createMaxForwardsHeader(70);
        // Use "set" because this header is mandatory.
        request.setHeader(maxForwardsHeader);
    } catch (Exception e) {
        Log.debug("SimpleSession(" + myXMPPUsername + ").prepareRequest:  Exception occured when preparing MaxForwardsHeader.", e);
        return null;
    }
    // Setting Request URI
    Log.debug("SimpleSession(" + myXMPPUsername + ").prepareRequest:  setting request URI...");
    try {
        if (requestUri == null) {
            requestUri = (SipURI) destUri.clone();
            requestUri.setTransportParam(ListeningPoint.UDP);
        }
        request.setRequestURI(requestUri);
    } catch (Exception e) {
        Log.debug("SimpleSession(" + myXMPPUsername + ").prepareRequest:  Exception occured when setting request URI.", e);
        return null;
    }
    // Add "Contact" header
    Log.debug("SimpleSession(" + myXMPPUsername + ").prepareRequest:  Preparing \"Contact\" header...");
    try {
        SipURI contactURI = addressFactory.createSipURI(mySipUsername, InetAddress.getLocalHost().getHostAddress());
        contactURI.setPort(sipPort);
        Address contactAddress = addressFactory.createAddress(contactURI);
        contactAddress.setDisplayName(mySipUsername);
        ContactHeader contactHeader = headerFactory.createContactHeader(contactAddress);
        request.setHeader(contactHeader);
    } catch (Exception e) {
        Log.debug("SimpleSession(" + myXMPPUsername + ").prepareRequest:  Exception occured when adding ContactHeader.", e);
        return null;
    }
    return request;
}
Also used : CSeqHeader(javax.sip.header.CSeqHeader) MaxForwardsHeader(javax.sip.header.MaxForwardsHeader) ContactHeader(javax.sip.header.ContactHeader) InetAddress(java.net.InetAddress) Address(javax.sip.address.Address) ViaHeader(javax.sip.header.ViaHeader) FromHeader(javax.sip.header.FromHeader) Request(javax.sip.message.Request) ToHeader(javax.sip.header.ToHeader) CallIdHeader(javax.sip.header.CallIdHeader) SipURI(javax.sip.address.SipURI) InvalidArgumentException(javax.sip.InvalidArgumentException) ParseException(java.text.ParseException) NotFoundException(org.jivesoftware.util.NotFoundException) TooManyListenersException(java.util.TooManyListenersException) SipException(javax.sip.SipException) UserNotFoundException(org.jivesoftware.openfire.user.UserNotFoundException)

Example 2 with CSeqHeader

use of javax.sip.header.CSeqHeader in project Openfire by igniterealtime.

the class SimpleListener method processResponse.

public void processResponse(ResponseEvent responseEvent) {
    if (responseEvent.getClientTransaction() != null) {
        Log.debug("SimpleListener for " + myUsername + ":  Getting client transaction...");
        ClientTransaction clientTransaction = responseEvent.getClientTransaction();
        Dialog clientDialog = clientTransaction.getDialog();
        getSession().printDialog(clientDialog);
    }
    Log.debug("SimpleListener for " + myUsername + ":  Received a response event:  " + responseEvent.getResponse().toString());
    //		String   fromAddr = "";
    String toAddr = "";
    Response response = responseEvent.getResponse();
    //		}
    if (response.getHeader(ToHeader.NAME) != null) {
        ToHeader toHeader = (ToHeader) response.getHeader(ToHeader.NAME);
        URI toUri = toHeader.getAddress().getURI();
        if (toUri instanceof SipUri)
            toAddr = ((SipUri) toUri).getUser() + "@" + ((SipUri) toUri).getHost();
        else
            toAddr = toUri.toString();
    }
    if (response.getHeader(CSeqHeader.NAME) != null) {
        String method = ((CSeqHeader) response.getHeader(CSeqHeader.NAME)).getMethod();
        if (method.equals(Request.REGISTER)) {
            if (response.getStatusCode() / 100 == 2) {
                int expires = 0;
                if (response.getHeader(ContactHeader.NAME) != null) {
                    expires = ((ContactHeader) response.getHeader(ContactHeader.NAME)).getExpires();
                } else if (response.getHeader(ExpiresHeader.NAME) != null) {
                    expires = ((ExpiresHeader) response.getHeader(ExpiresHeader.NAME)).getExpires();
                }
                if (expires > 0) {
                    Log.debug("SimpleListener(" + myUsername + ").processResponse:  " + getSession().getRegistration().getUsername() + " log in successful!");
                    getSession().sipUserLoggedIn();
                } else {
                    if (getSession().getLoginStatus().equals(TransportLoginStatus.LOGGING_OUT)) {
                        Log.debug("SimpleListener(" + myUsername + ").processResponse:  " + getSession().getRegistration().getUsername() + " log out successful!");
                        getSession().sipUserLoggedOut();
                        getSession().removeStack();
                    }
                }
            }
        }
        if (method.equals(Request.SUBSCRIBE)) {
            if (response.getStatusCode() / 100 == 2) {
                Log.debug("SimpleListener for " + myUsername + ":  Handling SUBSCRIBE acknowledgement!!");
                int expires = 0;
                if (response.getHeader(ContactHeader.NAME) != null) {
                    expires = ((ContactHeader) response.getHeader(ContactHeader.NAME)).getExpires();
                }
                if (response.getHeader(ExpiresHeader.NAME) != null) {
                    expires = ((ExpiresHeader) response.getHeader(ExpiresHeader.NAME)).getExpires();
                }
                if (expires > 0) {
                    // Confirm subscription of roster item
                    getSession().contactSubscribed(toAddr);
                } else {
                    // Confirm unsubscription of roster item
                    getSession().contactUnsubscribed(toAddr);
                }
                Log.debug("SimpleListener for " + myUsername + ":  Handled SUBSCRIBE acknowledgement!!");
            }
        }
    }
}
Also used : Response(javax.sip.message.Response) CSeqHeader(javax.sip.header.CSeqHeader) Dialog(javax.sip.Dialog) ClientTransaction(javax.sip.ClientTransaction) ToHeader(javax.sip.header.ToHeader) ExpiresHeader(javax.sip.header.ExpiresHeader) SipUri(gov.nist.javax.sip.address.SipUri) URI(javax.sip.address.URI) SipURI(javax.sip.address.SipURI)

Example 3 with CSeqHeader

use of javax.sip.header.CSeqHeader in project camel by apache.

the class SipConfiguration method createSipRequest.

public Request createSipRequest(long sequenceNumber, String requestMethod, Object body) throws ParseException, InvalidArgumentException {
    //SipConfiguration configuration = sipPublisher.getConfiguration();
    CSeqHeader cSeqHeader = getHeaderFactory().createCSeqHeader(sequenceNumber, requestMethod);
    // Create the request.
    Request request = getMessageFactory().createRequest(getSipUri(), requestMethod, getCallIdHeader(), cSeqHeader, getFromHeader(), getToHeader(), getViaHeaders(), getMaxForwardsHeader());
    if (getEventHeader() != null) {
        request.addHeader(getEventHeader());
    }
    if (getExpiresHeader() != null) {
        request.addHeader(getExpiresHeader());
    }
    if (getContactHeader() != null) {
        request.addHeader(getContactHeader());
    }
    if (getExtensionHeader() != null) {
        request.addHeader(getExtensionHeader());
    }
    request.setContent(body, getContentTypeHeader());
    return request;
}
Also used : CSeqHeader(javax.sip.header.CSeqHeader) Request(javax.sip.message.Request)

Example 4 with CSeqHeader

use of javax.sip.header.CSeqHeader in project XobotOS by xamarin.

the class SipHelper method sendInviteAck.

/**
     * @param event the INVITE ACK request event
     */
public void sendInviteAck(ResponseEvent event, Dialog dialog) throws SipException {
    Response response = event.getResponse();
    long cseq = ((CSeqHeader) response.getHeader(CSeqHeader.NAME)).getSeqNumber();
    Request ack = dialog.createAck(cseq);
    if (DEBUG)
        Log.d(TAG, "send ACK: " + ack);
    dialog.sendAck(ack);
}
Also used : Response(javax.sip.message.Response) CSeqHeader(javax.sip.header.CSeqHeader) Request(javax.sip.message.Request)

Example 5 with CSeqHeader

use of javax.sip.header.CSeqHeader in project XobotOS by xamarin.

the class AuthenticationHelperImpl method handleChallenge.

/*
     * (non-Javadoc)
     *
     * @see gov.nist.javax.sip.clientauthutils.AuthenticationHelper#handleChallenge(javax.sip.message.Response,
     *      javax.sip.ClientTransaction, javax.sip.SipProvider)
     */
public ClientTransaction handleChallenge(Response challenge, ClientTransaction challengedTransaction, SipProvider transactionCreator, int cacheTime) throws SipException, NullPointerException {
    try {
        if (sipStack.isLoggingEnabled()) {
            sipStack.getStackLogger().logDebug("handleChallenge: " + challenge);
        }
        SIPRequest challengedRequest = ((SIPRequest) challengedTransaction.getRequest());
        Request reoriginatedRequest = null;
        /*
             * If the challenged request is part of a Dialog and the
             * Dialog is confirmed the re-originated request should be
             * generated as an in-Dialog request.
             */
        if (challengedRequest.getToTag() != null || challengedTransaction.getDialog() == null || challengedTransaction.getDialog().getState() != DialogState.CONFIRMED) {
            reoriginatedRequest = (Request) challengedRequest.clone();
        } else {
            /*
                 * Re-originate the request by consulting the dialog. In particular
                 * the route set could change between the original request and the 
                 * in-dialog challenge.
                 */
            reoriginatedRequest = challengedTransaction.getDialog().createRequest(challengedRequest.getMethod());
            Iterator<String> headerNames = challengedRequest.getHeaderNames();
            while (headerNames.hasNext()) {
                String headerName = headerNames.next();
                if (reoriginatedRequest.getHeader(headerName) != null) {
                    ListIterator<Header> iterator = reoriginatedRequest.getHeaders(headerName);
                    while (iterator.hasNext()) {
                        reoriginatedRequest.addHeader(iterator.next());
                    }
                }
            }
        }
        // remove the branch id so that we could use the request in a new
        // transaction
        removeBranchID(reoriginatedRequest);
        if (challenge == null || reoriginatedRequest == null) {
            throw new NullPointerException("A null argument was passed to handle challenge.");
        }
        ListIterator authHeaders = null;
        if (challenge.getStatusCode() == Response.UNAUTHORIZED) {
            authHeaders = challenge.getHeaders(WWWAuthenticateHeader.NAME);
        } else if (challenge.getStatusCode() == Response.PROXY_AUTHENTICATION_REQUIRED) {
            authHeaders = challenge.getHeaders(ProxyAuthenticateHeader.NAME);
        } else {
            throw new IllegalArgumentException("Unexpected status code ");
        }
        if (authHeaders == null) {
            throw new IllegalArgumentException("Could not find WWWAuthenticate or ProxyAuthenticate headers");
        }
        // Remove all authorization headers from the request (we'll re-add them
        // from cache)
        reoriginatedRequest.removeHeader(AuthorizationHeader.NAME);
        reoriginatedRequest.removeHeader(ProxyAuthorizationHeader.NAME);
        // rfc 3261 says that the cseq header should be augmented for the new
        // request. do it here so that the new dialog (created together with
        // the new client transaction) takes it into account.
        // Bug report - Fredrik Wickstrom
        CSeqHeader cSeq = (CSeqHeader) reoriginatedRequest.getHeader((CSeqHeader.NAME));
        try {
            cSeq.setSeqNumber(cSeq.getSeqNumber() + 1l);
        } catch (InvalidArgumentException ex) {
            throw new SipException("Invalid CSeq -- could not increment : " + cSeq.getSeqNumber());
        }
        /* Resolve this to the next hop based on the previous lookup. If we are not using
             * lose routing (RFC2543) then just attach hop as a maddr param.
             */
        if (challengedRequest.getRouteHeaders() == null) {
            Hop hop = ((SIPClientTransaction) challengedTransaction).getNextHop();
            SipURI sipUri = (SipURI) reoriginatedRequest.getRequestURI();
            // BEGIN android-added
            if (!hop.getHost().equalsIgnoreCase(sipUri.getHost()) && !hop.equals(sipStack.getRouter(challengedRequest).getOutboundProxy()))
                // END android-added
                sipUri.setMAddrParam(hop.getHost());
            if (hop.getPort() != -1)
                sipUri.setPort(hop.getPort());
        }
        ClientTransaction retryTran = transactionCreator.getNewClientTransaction(reoriginatedRequest);
        WWWAuthenticateHeader authHeader = null;
        SipURI requestUri = (SipURI) challengedTransaction.getRequest().getRequestURI();
        while (authHeaders.hasNext()) {
            authHeader = (WWWAuthenticateHeader) authHeaders.next();
            String realm = authHeader.getRealm();
            AuthorizationHeader authorization = null;
            String sipDomain;
            if (this.accountManager instanceof SecureAccountManager) {
                UserCredentialHash credHash = ((SecureAccountManager) this.accountManager).getCredentialHash(challengedTransaction, realm);
                URI uri = reoriginatedRequest.getRequestURI();
                sipDomain = credHash.getSipDomain();
                authorization = this.getAuthorization(reoriginatedRequest.getMethod(), uri.toString(), (reoriginatedRequest.getContent() == null) ? "" : new String(reoriginatedRequest.getRawContent()), authHeader, credHash);
            } else {
                UserCredentials userCreds = ((AccountManager) this.accountManager).getCredentials(challengedTransaction, realm);
                sipDomain = userCreds.getSipDomain();
                if (userCreds == null)
                    throw new SipException("Cannot find user creds for the given user name and realm");
                // we haven't yet authenticated this realm since we were
                // started.
                authorization = this.getAuthorization(reoriginatedRequest.getMethod(), reoriginatedRequest.getRequestURI().toString(), (reoriginatedRequest.getContent() == null) ? "" : new String(reoriginatedRequest.getRawContent()), authHeader, userCreds);
            }
            if (sipStack.isLoggingEnabled())
                sipStack.getStackLogger().logDebug("Created authorization header: " + authorization.toString());
            if (cacheTime != 0)
                cachedCredentials.cacheAuthorizationHeader(sipDomain, authorization, cacheTime);
            reoriginatedRequest.addHeader(authorization);
        }
        if (sipStack.isLoggingEnabled()) {
            sipStack.getStackLogger().logDebug("Returning authorization transaction." + retryTran);
        }
        return retryTran;
    } catch (SipException ex) {
        throw ex;
    } catch (Exception ex) {
        sipStack.getStackLogger().logError("Unexpected exception ", ex);
        throw new SipException("Unexpected exception ", ex);
    }
}
Also used : SIPClientTransaction(gov.nist.javax.sip.stack.SIPClientTransaction) SIPClientTransaction(gov.nist.javax.sip.stack.SIPClientTransaction) ClientTransaction(javax.sip.ClientTransaction) ProxyAuthorizationHeader(javax.sip.header.ProxyAuthorizationHeader) AuthorizationHeader(javax.sip.header.AuthorizationHeader) Request(javax.sip.message.Request) SIPRequest(gov.nist.javax.sip.message.SIPRequest) Hop(javax.sip.address.Hop) WWWAuthenticateHeader(javax.sip.header.WWWAuthenticateHeader) ListIterator(java.util.ListIterator) SipURI(javax.sip.address.SipURI) SIPRequest(gov.nist.javax.sip.message.SIPRequest) URI(javax.sip.address.URI) SipURI(javax.sip.address.SipURI) InvalidArgumentException(javax.sip.InvalidArgumentException) ParseException(java.text.ParseException) SipException(javax.sip.SipException) CSeqHeader(javax.sip.header.CSeqHeader) InvalidArgumentException(javax.sip.InvalidArgumentException) CSeqHeader(javax.sip.header.CSeqHeader) ProxyAuthorizationHeader(javax.sip.header.ProxyAuthorizationHeader) ProxyAuthenticateHeader(javax.sip.header.ProxyAuthenticateHeader) ViaHeader(javax.sip.header.ViaHeader) WWWAuthenticateHeader(javax.sip.header.WWWAuthenticateHeader) AuthorizationHeader(javax.sip.header.AuthorizationHeader) Header(javax.sip.header.Header) SipException(javax.sip.SipException)

Aggregations

CSeqHeader (javax.sip.header.CSeqHeader)6 Request (javax.sip.message.Request)5 SipURI (javax.sip.address.SipURI)4 ToHeader (javax.sip.header.ToHeader)3 ViaHeader (javax.sip.header.ViaHeader)3 ParseException (java.text.ParseException)2 ClientTransaction (javax.sip.ClientTransaction)2 InvalidArgumentException (javax.sip.InvalidArgumentException)2 SipException (javax.sip.SipException)2 URI (javax.sip.address.URI)2 CallIdHeader (javax.sip.header.CallIdHeader)2 FromHeader (javax.sip.header.FromHeader)2 MaxForwardsHeader (javax.sip.header.MaxForwardsHeader)2 Response (javax.sip.message.Response)2 SipUri (gov.nist.javax.sip.address.SipUri)1 SIPRequest (gov.nist.javax.sip.message.SIPRequest)1 SIPClientTransaction (gov.nist.javax.sip.stack.SIPClientTransaction)1 InetAddress (java.net.InetAddress)1 ListIterator (java.util.ListIterator)1 TooManyListenersException (java.util.TooManyListenersException)1