Search in sources :

Example 1 with ViaHeader

use of javax.sip.header.ViaHeader 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 ViaHeader

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

the class DefaultRouter method createHop.

/**
     * Utility method to create a hop from a SIP URI
     *
     * @param sipUri
     * @return
     */
private final Hop createHop(SipURI sipUri, Request request) {
    // always use TLS when secure
    String transport = sipUri.isSecure() ? SIPConstants.TLS : sipUri.getTransportParam();
    if (transport == null) {
        //@see issue 131
        ViaHeader via = (ViaHeader) request.getHeader(ViaHeader.NAME);
        transport = via.getTransport();
    }
    // sipUri.removeParameter("transport");
    int port;
    if (sipUri.getPort() != -1) {
        port = sipUri.getPort();
    } else {
        if (transport.equalsIgnoreCase(SIPConstants.TLS))
            port = 5061;
        else
            // TCP or UDP
            port = 5060;
    }
    String host = sipUri.getMAddrParam() != null ? sipUri.getMAddrParam() : sipUri.getHost();
    AddressResolver addressResolver = this.sipStack.getAddressResolver();
    return addressResolver.resolveAddress(new HopImpl(host, port, transport));
}
Also used : AddressResolver(gov.nist.core.net.AddressResolver) ViaHeader(javax.sip.header.ViaHeader)

Example 3 with ViaHeader

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

the class SIPMessage method encodeAsBytes.

/**
     * Encode the message as a byte array. Use this when the message payload is a binary byte
     * array.
     * 
     * @return The Canonical byte array representation of the message (including the canonical
     *         byte array representation of the SDP payload if it exists all in one contiguous
     *         byte array).
     */
public byte[] encodeAsBytes(String transport) {
    if (this instanceof SIPRequest && ((SIPRequest) this).isNullRequest()) {
        return "\r\n\r\n".getBytes();
    }
    // JvB: added to fix case where application provides the wrong transport
    // in the topmost Via header
    ViaHeader topVia = (ViaHeader) this.getHeader(ViaHeader.NAME);
    try {
        topVia.setTransport(transport);
    } catch (ParseException e) {
        InternalErrorHandler.handleException(e);
    }
    StringBuffer encoding = new StringBuffer();
    synchronized (this.headers) {
        Iterator<SIPHeader> it = this.headers.iterator();
        while (it.hasNext()) {
            SIPHeader siphdr = (SIPHeader) it.next();
            if (!(siphdr instanceof ContentLength))
                siphdr.encode(encoding);
        }
    }
    contentLengthHeader.encode(encoding);
    encoding.append(NEWLINE);
    byte[] retval = null;
    byte[] content = this.getRawContent();
    if (content != null) {
        // Append the content
        byte[] msgarray = null;
        try {
            msgarray = encoding.toString().getBytes(getCharset());
        } catch (UnsupportedEncodingException ex) {
            InternalErrorHandler.handleException(ex);
        }
        retval = new byte[msgarray.length + content.length];
        System.arraycopy(msgarray, 0, retval, 0, msgarray.length);
        System.arraycopy(content, 0, retval, msgarray.length, content.length);
    } else {
        try {
            retval = encoding.toString().getBytes(getCharset());
        } catch (UnsupportedEncodingException ex) {
            InternalErrorHandler.handleException(ex);
        }
    }
    return retval;
}
Also used : ViaHeader(javax.sip.header.ViaHeader) SIPHeader(gov.nist.javax.sip.header.SIPHeader) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ParseException(java.text.ParseException) ContentLength(gov.nist.javax.sip.header.ContentLength)

Example 4 with ViaHeader

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

the class AuthenticationHelperImpl method removeBranchID.

/**
     * Removes all via headers from <tt>request</tt> and replaces them with a new one, equal to
     * the one that was top most.
     *
     * @param request the Request whose branchID we'd like to remove.
     *
     */
private void removeBranchID(Request request) {
    ViaHeader viaHeader = (ViaHeader) request.getHeader(ViaHeader.NAME);
    viaHeader.removeParameter("branch");
}
Also used : ViaHeader(javax.sip.header.ViaHeader)

Example 5 with ViaHeader

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

the class SipSessionGroup method extractExternalAddress.

private void extractExternalAddress(ResponseEvent evt) {
    Response response = evt.getResponse();
    ViaHeader viaHeader = (ViaHeader) (response.getHeader(SIPHeaderNames.VIA));
    if (viaHeader == null)
        return;
    int rport = viaHeader.getRPort();
    String externalIp = viaHeader.getReceived();
    if ((rport > 0) && (externalIp != null)) {
        mExternalIp = externalIp;
        mExternalPort = rport;
        Log.d(TAG, " got external addr " + externalIp + ":" + rport + " on " + mSipStack);
    }
}
Also used : Response(javax.sip.message.Response) SIPResponse(gov.nist.javax.sip.message.SIPResponse) ViaHeader(javax.sip.header.ViaHeader) ListeningPoint(javax.sip.ListeningPoint)

Aggregations

ViaHeader (javax.sip.header.ViaHeader)40 Request (javax.sip.message.Request)24 ParseException (java.text.ParseException)23 SipURI (javax.sip.address.SipURI)23 FromHeader (javax.sip.header.FromHeader)23 ToHeader (javax.sip.header.ToHeader)23 CallIdHeader (javax.sip.header.CallIdHeader)21 ContactHeader (javax.sip.header.ContactHeader)20 MaxForwardsHeader (javax.sip.header.MaxForwardsHeader)20 SipException (javax.sip.SipException)19 InvalidArgumentException (javax.sip.InvalidArgumentException)18 CSeqHeader (javax.sip.header.CSeqHeader)18 RouteHeader (javax.sip.header.RouteHeader)15 TransactionUnavailableException (javax.sip.TransactionUnavailableException)14 Address (javax.sip.address.Address)14 ContentTypeHeader (javax.sip.header.ContentTypeHeader)14 Header (javax.sip.header.Header)14 ListeningPoint (javax.sip.ListeningPoint)13 RecordRouteHeader (javax.sip.header.RecordRouteHeader)13 ClientTransaction (javax.sip.ClientTransaction)12