Search in sources :

Example 36 with ViaHeader

use of javax.sip.header.ViaHeader in project Spark by igniterealtime.

the class CallProcessing method sayBye.

// send message
// private void sendInfoMessage(Dialog dialog, String body)
// throws CommunicationsException {
// Request request = dialog.getFirstTransaction().getRequest();
// Request info = null;
// String contentType = "application/dtmd-relay";
// String[] contentTypeTab = contentType.split("/");
// ContentTypeHeader contentTypeHeader = null;
// try {
// info = dialog.createRequest(Request.INFO);
// try {
// contentTypeHeader = sipManCallback.headerFactory
// .createContentTypeHeader(contentTypeTab[0],
// contentTypeTab[1]);
// info.setContent(body, contentTypeHeader);
// }
// catch (ParseException ex) {
// 
// throw new CommunicationsException(
// "ContentType Header must look like type/subtype!",
// ex);
// }
// }
// catch (SipException ex) {
// 
// throw new CommunicationsException(
// "Failed to create bye request!", ex);
// }
// ClientTransaction clientTransaction = null;
// try {
// clientTransaction = sipManCallback.sipProvider
// .getNewClientTransaction(info);
// }
// catch (TransactionUnavailableException ex) {
// 
// throw new CommunicationsException(
// "Failed to construct a client transaction from the INFO request",
// ex);
// }
// try {
// dialog.sendRequest(clientTransaction);
// }
// catch (SipException ex1) {
// throw new CommunicationsException(
// "Failed to send the INFO request");
// }
// 
// } // send message
// Bye
private void sayBye(Dialog dialog) throws CommunicationsException {
    Request bye = null;
    bye = dialog.getFirstTransaction().getRequest();
    try {
        bye = dialog.createRequest(Request.BYE);
    } catch (SipException e) {
        Log.error("bye", e);
    }
    long cseq = ((CSeq) (bye.getHeader(CSeq.NAME))).getSequenceNumber() + 1;
    bye.removeHeader(CSeq.NAME);
    try {
        bye.addHeader(sipManCallback.headerFactory.createCSeqHeader(cseq, Request.BYE));
    } catch (Exception e) {
        Log.error("bye", e);
    }
    bye.removeHeader(ViaHeader.NAME);
    for (ViaHeader via : sipManCallback.getLocalViaHeaders()) bye.addHeader(via);
    ContactHeader contactHeader = sipManCallback.getContactHeader();
    bye.addHeader(contactHeader);
    AllowHeader allow = null;
    try {
        allow = sipManCallback.headerFactory.createAllowHeader("INVITE, ACK, CANCEL, OPTIONS, BYE, REFER, NOTIFY, MESSAGE, SUBSCRIBE, INFO");
        bye.addHeader(allow);
    } catch (ParseException e) {
        Log.error(e);
    }
    // Transaction
    ClientTransaction inviteTransaction;
    try {
        inviteTransaction = sipManCallback.sipProvider.getNewClientTransaction(bye);
        dialog.sendRequest(inviteTransaction);
    } catch (SipException ee) {
        Log.error("bye", ee);
    }
    return;
}
Also used : ContactHeader(javax.sip.header.ContactHeader) ViaHeader(javax.sip.header.ViaHeader) ClientTransaction(javax.sip.ClientTransaction) Request(javax.sip.message.Request) AllowHeader(javax.sip.header.AllowHeader) ParseException(java.text.ParseException) SipException(javax.sip.SipException) SipSecurityException(net.java.sipmack.sip.security.SipSecurityException) InvalidArgumentException(javax.sip.InvalidArgumentException) ParseException(java.text.ParseException) SipException(javax.sip.SipException) TransactionUnavailableException(javax.sip.TransactionUnavailableException)

Example 37 with ViaHeader

use of javax.sip.header.ViaHeader in project Spark by igniterealtime.

the class CallProcessing method invite.

// -------------------------------- User Initiated processing
// ---------------------------------
Call invite(String callee, String sdpContent) throws CommunicationsException {
    callee = callee.trim();
    // Remove excessive characters from phone numbers such as '
    // ','(',')','-'
    String excessiveChars = SIPConfig.getExcessiveURIChar();
    if (excessiveChars != null) {
        StringBuffer calleeBuff = new StringBuffer(callee);
        for (int i = 0; i < excessiveChars.length(); i++) {
            String charToDeleteStr = excessiveChars.substring(i, i + 1);
            int charIndex = -1;
            while ((charIndex = calleeBuff.indexOf(charToDeleteStr)) != -1) calleeBuff.delete(charIndex, charIndex + 1);
        }
        callee = calleeBuff.toString();
    }
    // Handle default domain name (i.e. transform 1234 -> 1234@sip.com
    String defaultDomainName = SIPConfig.getDefaultDomain();
    if (// no sip scheme
    defaultDomainName != null && !callee.trim().startsWith("tel:") && // most probably a sip uri
    callee.indexOf('@') == -1) {
        callee = callee + "@" + defaultDomainName;
    }
    // Let's be uri fault tolerant
    if (// no sip scheme
    callee.toLowerCase().indexOf("sip:") == -1 && // most probably a sip uri
    callee.indexOf('@') != -1) {
        callee = "sip:" + callee;
    }
    // Request URI
    URI requestURI;
    try {
        requestURI = sipManCallback.addressFactory.createURI(callee);
    } catch (ParseException ex) {
        throw new CommunicationsException(callee + " is not a legal SIP uri!", ex);
    }
    // Call ID
    CallIdHeader callIdHeader = sipManCallback.sipProvider.getNewCallId();
    // CSeq
    CSeqHeader cSeqHeader;
    try {
        cSeqHeader = sipManCallback.headerFactory.createCSeqHeader(1L, Request.INVITE);
    } catch (ParseException ex) {
        throw new CommunicationsException("An unexpected erro occurred while" + "constructing the CSeqHeadder", ex);
    } catch (InvalidArgumentException ex) {
        // Shouldn't happen
        throw new CommunicationsException("An unexpected erro occurred while" + "constructing the CSeqHeadder", ex);
    }
    // FromHeader
    FromHeader fromHeader = sipManCallback.getFromHeader();
    // ToHeader
    Address toAddress = sipManCallback.addressFactory.createAddress(requestURI);
    ToHeader toHeader;
    try {
        toHeader = sipManCallback.headerFactory.createToHeader(toAddress, null);
    } catch (ParseException ex) {
        // Shouldn't happen
        throw new CommunicationsException("Null is not an allowed tag for the to header!", ex);
    }
    // ViaHeaders
    ArrayList<ViaHeader> viaHeaders = sipManCallback.getLocalViaHeaders();
    // MaxForwards
    MaxForwardsHeader maxForwards = sipManCallback.getMaxForwardsHeader();
    // Contact
    ContactHeader contactHeader = sipManCallback.getContactHeader();
    Request invite = null;
    try {
        invite = sipManCallback.messageFactory.createRequest(requestURI, Request.INVITE, callIdHeader, cSeqHeader, fromHeader, toHeader, viaHeaders, maxForwards);
    } catch (ParseException ex) {
        throw new CommunicationsException("Failed to create invite Request!", ex);
    }
    // 
    invite.addHeader(contactHeader);
    AllowHeader allow = null;
    try {
        allow = sipManCallback.headerFactory.createAllowHeader("INVITE, ACK, CANCEL, OPTIONS, BYE, REFER, NOTIFY, MESSAGE, SUBSCRIBE, INFO");
        invite.addHeader(allow);
    } catch (ParseException e) {
        Log.error(e);
    }
    // Content
    ContentTypeHeader contentTypeHeader = null;
    try {
        // content type should be application/sdp (not applications)
        // reported by Oleg Shevchenko (Miratech)
        contentTypeHeader = sipManCallback.headerFactory.createContentTypeHeader("application", "sdp");
    } catch (ParseException ex) {
        // Shouldn't happen
        throw new CommunicationsException("Failed to create a content type header for the INVITE request", ex);
    }
    try {
        invite.setContent(sdpContent, contentTypeHeader);
    } catch (ParseException ex) {
        throw new CommunicationsException("Failed to parse sdp data while creating invite request!", ex);
    }
    // Transaction
    ClientTransaction inviteTransaction;
    try {
        inviteTransaction = sipManCallback.sipProvider.getNewClientTransaction(invite);
    } catch (TransactionUnavailableException ex) {
        throw new CommunicationsException("Failed to create inviteTransaction.\n" + "This is most probably a network connection error.", ex);
    }
    try {
        inviteTransaction.sendRequest();
    } catch (SipException ex) {
        throw new CommunicationsException("An error occurred while sending invite request", ex);
    }
    Call call = callDispatcher.createCall(inviteTransaction.getDialog(), invite);
    call.setState(Call.DIALING);
    return call;
}
Also used : MaxForwardsHeader(javax.sip.header.MaxForwardsHeader) ContactHeader(javax.sip.header.ContactHeader) Address(javax.sip.address.Address) FromHeader(javax.sip.header.FromHeader) ContentTypeHeader(javax.sip.header.ContentTypeHeader) ClientTransaction(javax.sip.ClientTransaction) Request(javax.sip.message.Request) URI(javax.sip.address.URI) SipURI(javax.sip.address.SipURI) CSeqHeader(javax.sip.header.CSeqHeader) InvalidArgumentException(javax.sip.InvalidArgumentException) ViaHeader(javax.sip.header.ViaHeader) ToHeader(javax.sip.header.ToHeader) CallIdHeader(javax.sip.header.CallIdHeader) TransactionUnavailableException(javax.sip.TransactionUnavailableException) ParseException(java.text.ParseException) AllowHeader(javax.sip.header.AllowHeader) SipException(javax.sip.SipException)

Example 38 with ViaHeader

use of javax.sip.header.ViaHeader in project Spark by igniterealtime.

the class CallProcessing method hold.

// Hold
public void hold(int callID, String sdpContent) throws CommunicationsException {
    Call call = callDispatcher.getCall(callID);
    Request invite = null;
    invite = call.getDialog().getFirstTransaction().getRequest();
    try {
        invite = call.getDialog().createRequest(Request.INVITE);
    } catch (SipException e) {
        Log.error("hold", e);
    }
    long cseq = ((CSeq) (invite.getHeader(CSeq.NAME))).getSequenceNumber() + 1;
    invite.removeHeader(CSeq.NAME);
    try {
        invite.addHeader(sipManCallback.headerFactory.createCSeqHeader(cseq, Request.INVITE));
    } catch (Exception e) {
        Log.error("hold", e);
    }
    invite.removeHeader(ViaHeader.NAME);
    for (ViaHeader via : sipManCallback.getLocalViaHeaders()) invite.addHeader(via);
    ContentTypeHeader contentTypeHeader = null;
    try {
        // content type should be application/sdp (not applications)
        // reported by Oleg Shevchenko (Miratech)
        contentTypeHeader = sipManCallback.headerFactory.createContentTypeHeader("application", "sdp");
    } catch (ParseException ex) {
        // Shouldn't happen
        throw new CommunicationsException("Failed to create a content type header for the INVITE request", ex);
    }
    try {
        invite.setContent(sdpContent, contentTypeHeader);
    } catch (ParseException ex) {
        throw new CommunicationsException("Failed to parse sdp data while creating invite request!", ex);
    }
    // Transaction
    ClientTransaction inviteTransaction;
    try {
        inviteTransaction = sipManCallback.sipProvider.getNewClientTransaction(invite);
        call.getDialog().sendRequest(inviteTransaction);
        call.setLastRequest(invite);
    } catch (SipException ee) {
        Log.error("hold", ee);
    }
    return;
}
Also used : ViaHeader(javax.sip.header.ViaHeader) ContentTypeHeader(javax.sip.header.ContentTypeHeader) ClientTransaction(javax.sip.ClientTransaction) Request(javax.sip.message.Request) ParseException(java.text.ParseException) SipException(javax.sip.SipException) SipSecurityException(net.java.sipmack.sip.security.SipSecurityException) InvalidArgumentException(javax.sip.InvalidArgumentException) ParseException(java.text.ParseException) SipException(javax.sip.SipException) TransactionUnavailableException(javax.sip.TransactionUnavailableException)

Example 39 with ViaHeader

use of javax.sip.header.ViaHeader in project Spark by igniterealtime.

the class RegisterProcessing method register.

synchronized void register(String registrarAddress, int registrarPort, String registrarTransport, int expires) throws CommunicationsException {
    try {
        isUnregistering = false;
        // From
        FromHeader fromHeader = sipManCallback.getFromHeader(true);
        Address fromAddress = fromHeader.getAddress();
        sipManCallback.fireRegistering(fromAddress.toString());
        // Request URI
        SipURI requestURI = null;
        try {
            requestURI = sipManCallback.addressFactory.createSipURI(null, registrarAddress);
        } catch (ParseException ex) {
            throw new CommunicationsException("Bad registrar address:" + registrarAddress, ex);
        } catch (NullPointerException ex) {
            // Do not throw an exc, we should rather silently notify the
            // user
            // throw new CommunicationsException("A registrar address was
            // not specified!", ex);
            sipManCallback.fireUnregistered(fromAddress.getURI().toString() + " (registrar not specified)");
            return;
        }
        /*
            requestURI.setPort(registrarPort);
            try {
                requestURI.setTransportParam(registrarTransport);
            }
            catch (ParseException ex) {
                throw new CommunicationsException(registrarTransport
                        + " is not a valid transport!", ex);
            }
            */
        // Call ID Header
        CallIdHeader callIdHeader = sipManCallback.sipProvider.getNewCallId();
        // CSeq Header
        CSeqHeader cSeqHeader = null;
        try {
            cSeqHeader = sipManCallback.headerFactory.createCSeqHeader((long) 1, Request.REGISTER);
        } catch (ParseException ex) {
            // Should never happen
            Log.error("register", ex);
        } catch (InvalidArgumentException ex) {
            // Should never happen
            Log.error("register", ex);
        }
        // To Header
        ToHeader toHeader = null;
        try {
            toHeader = sipManCallback.headerFactory.createToHeader(fromAddress, null);
        } catch (ParseException ex) {
            // throw was missing - reported by Eero Vaarnas
            throw new CommunicationsException("Could not create a To header " + "for address:" + fromHeader.getAddress(), ex);
        }
        // User Agent Header
        UserAgentHeader uaHeader = null;
        ArrayList<String> userAgentList = new ArrayList<String>();
        userAgentList.add(SoftPhoneManager.userAgent);
        try {
            uaHeader = sipManCallback.headerFactory.createUserAgentHeader(userAgentList);
        } catch (ParseException ex) {
            // throw was missing - reported by Eero Vaarnas
            throw new CommunicationsException("Could not create a To header " + "for address:" + fromHeader.getAddress(), ex);
        }
        // Via Headers
        ArrayList<ViaHeader> viaHeaders = sipManCallback.getLocalViaHeaders();
        // MaxForwardsHeader
        MaxForwardsHeader maxForwardsHeader = sipManCallback.getMaxForwardsHeader();
        // Request
        Request request = null;
        try {
            request = sipManCallback.messageFactory.createRequest(requestURI, Request.REGISTER, callIdHeader, cSeqHeader, fromHeader, toHeader, viaHeaders, maxForwardsHeader);
            request.setHeader(uaHeader);
        } catch (ParseException ex) {
            // throw was missing - reported by Eero Vaarnas
            throw new CommunicationsException("Could not create the register request!", ex);
        }
        // Expires Header
        ExpiresHeader expHeader = null;
        for (int retry = 0; retry < 2; retry++) {
            try {
                expHeader = sipManCallback.headerFactory.createExpiresHeader(expires);
            } catch (InvalidArgumentException ex) {
                if (retry == 0) {
                    expires = 3600;
                    continue;
                }
                throw new CommunicationsException("Invalid registrations expiration parameter - " + expires, ex);
            }
        }
        request.addHeader(expHeader);
        // Contact Header should contain IP - bug report - Eero Vaarnas
        ContactHeader contactHeader = sipManCallback.getRegistrationContactHeader();
        request.addHeader(contactHeader);
        AllowHeader allow = sipManCallback.headerFactory.createAllowHeader("INVITE, ACK, CANCEL, OPTIONS, BYE, REFER, NOTIFY, MESSAGE, SUBSCRIBE, INFO");
        request.addHeader(allow);
        // Transaction
        ClientTransaction regTrans = null;
        try {
            regTrans = sipManCallback.sipProvider.getNewClientTransaction(request);
        } catch (TransactionUnavailableException ex) {
            throw new CommunicationsException("Could not create a register transaction!\n" + "Check that the Registrar address is correct!");
        }
        try {
            regTrans.sendRequest();
        }// we sometimes get a null pointer exception here so catch them all
         catch (Exception ex) {
            // throw was missing - reported by Eero Vaarnas
            throw new CommunicationsException("Could not send out the register request!", ex);
        }
        this.registerRequest = request;
    } catch (Exception e) {
        Log.error("register", e);
        sipManCallback.fireRegistrationFailed(registrarAddress == null ? "" : registrarAddress, RegistrationEvent.Type.TimeOut);
    }
}
Also used : MaxForwardsHeader(javax.sip.header.MaxForwardsHeader) Address(javax.sip.address.Address) ArrayList(java.util.ArrayList) SipURI(javax.sip.address.SipURI) InvalidArgumentException(javax.sip.InvalidArgumentException) UserAgentHeader(javax.sip.header.UserAgentHeader) ToHeader(javax.sip.header.ToHeader) CallIdHeader(javax.sip.header.CallIdHeader) ExpiresHeader(javax.sip.header.ExpiresHeader) ContactHeader(javax.sip.header.ContactHeader) FromHeader(javax.sip.header.FromHeader) ClientTransaction(javax.sip.ClientTransaction) Request(javax.sip.message.Request) SipSecurityException(net.java.sipmack.sip.security.SipSecurityException) InvalidArgumentException(javax.sip.InvalidArgumentException) ParseException(java.text.ParseException) SipException(javax.sip.SipException) TransactionUnavailableException(javax.sip.TransactionUnavailableException) CSeqHeader(javax.sip.header.CSeqHeader) ViaHeader(javax.sip.header.ViaHeader) TransactionUnavailableException(javax.sip.TransactionUnavailableException) ParseException(java.text.ParseException) AllowHeader(javax.sip.header.AllowHeader)

Example 40 with ViaHeader

use of javax.sip.header.ViaHeader in project jain-sip.ha by RestComm.

the class SimpleB2BUAHandler method forwardResponse.

/**
 * @param responseEvent
 * @throws InvalidArgumentException
 */
@SuppressWarnings("unchecked")
private void forwardResponse(Response receivedResponse) throws SipException, InvalidArgumentException {
    if (receivedResponse.getStatusCode() == 202) {
        return;
    }
    System.out.println("Forwarding " + receivedResponse);
    final ServerTransaction origServerTransaction = this.getServerTransaction();
    Response forgedResponse = null;
    try {
        forgedResponse = messageFactory.createResponse(receivedResponse.getStatusCode(), origServerTransaction.getRequest());
    } catch (ParseException e) {
        throw new SipException("Failed to forge message", e);
    }
    // if ((getIncomingDialog() == null || getIncomingDialog().getState() == DialogState.EARLY) && localTag != null && getIncomingDialog() != null && getIncomingDialog().isServer()) {
    // // no tag set in the response, since the dialog creating transaction didn't had it
    // try {
    // ((ToHeader)forgedResponse.getHeader(ToHeader.NAME)).setTag(localTag);
    // } catch (ParseException e) {
    // throw new SipException("Failed to set local tag", e);
    // }
    // }
    // copy headers
    ListIterator<String> lit = receivedResponse.getHeaderNames();
    String headerName = null;
    ListIterator<Header> headersIterator = null;
    while (lit.hasNext()) {
        headerName = lit.next();
        if (SimpleB2BUAHandler.getHeadersToOmmitOnResponseCopy().contains(headerName)) {
            continue;
        } else {
            forgedResponse.removeHeader(headerName);
            headersIterator = receivedResponse.getHeaders(headerName);
            while (headersIterator.hasNext()) {
                forgedResponse.addLast((Header) headersIterator.next().clone());
            }
        }
    }
    // Copy content
    final byte[] rawOriginal = receivedResponse.getRawContent();
    if (rawOriginal != null && rawOriginal.length != 0) {
        final byte[] copy = new byte[rawOriginal.length];
        System.arraycopy(rawOriginal, 0, copy, 0, copy.length);
        try {
            forgedResponse.setContent(copy, (ContentTypeHeader) forgedResponse.getHeader(ContentTypeHeader.NAME));
        } catch (ParseException e) {
            throw new SipException("Failed to copy content.", e);
        }
    }
    // set contact if the received response had it
    if (receivedResponse.getHeader(ContactHeader.NAME) != null) {
        final String transport = ((ViaHeader) forgedResponse.getHeader(ViaHeader.NAME)).getTransport();
        forgedResponse.setHeader(((ListeningPointImpl) sipProvider.getListeningPoint(transport)).createContactHeader());
    }
    // set the to tag mandatory so that the dialog gets replicated
    if (((MessageExt) forgedResponse).getToHeader().getTag() == null) {
        try {
            ((MessageExt) forgedResponse).getToHeader().setTag("696");
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }
    origServerTransaction.sendResponse(forgedResponse);
}
Also used : Response(javax.sip.message.Response) MessageExt(gov.nist.javax.sip.message.MessageExt) ContactHeader(javax.sip.header.ContactHeader) ViaHeader(javax.sip.header.ViaHeader) CallIdHeader(javax.sip.header.CallIdHeader) ContentLengthHeader(javax.sip.header.ContentLengthHeader) ExpiresHeader(javax.sip.header.ExpiresHeader) Header(javax.sip.header.Header) CSeqHeader(javax.sip.header.CSeqHeader) ContentTypeHeader(javax.sip.header.ContentTypeHeader) ToHeader(javax.sip.header.ToHeader) RouteHeader(javax.sip.header.RouteHeader) SubscriptionStateHeader(javax.sip.header.SubscriptionStateHeader) EventHeader(javax.sip.header.EventHeader) FromHeader(javax.sip.header.FromHeader) RecordRouteHeader(javax.sip.header.RecordRouteHeader) ViaHeader(javax.sip.header.ViaHeader) ParseException(java.text.ParseException) ServerTransaction(javax.sip.ServerTransaction) SipException(javax.sip.SipException)

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