Search in sources :

Example 1 with CommunicationsException

use of net.java.sipmack.sip.CommunicationsException in project Spark by igniterealtime.

the class MessageProcessing method sendMessage.

/**
 * Sends an instant message in pager-mode using a SIMPLE/SIP MESSAGE
 * request. In pager-mode, each message is independent of any other
 * messages. An instant message will be the body of the MESSAGE request to
 * be sent, therefore, its format must conform to the values in the
 * "Content-Type" and "Content-Encoding" header fields. Refer to Message for
 * details.
 *
 * @param to              the address of receiver.
 * @param messageBody     the message to be sent. The messageBody will be the body of
 *                        the MESSAGE request to be sent and its format must conform to
 *                        the values in the parameters contentType and contentEncoding.
 *                        Please refer to the setBody method for details.
 * @param contentType     the Internet media type of the messageBody. Please refer to
 *                        the Message.setBody method for details.
 * @param contentEncoding the encodings that have been applied to the messageBody in
 *                        addition to those specified by contentType. Please refer to
 *                        the Message.setBody method for details.
 * @return the transaction ID associated with the MESSAGE request sent by
 *         this method.
 * @throws CommunicationsException
 */
public java.lang.String sendMessage(java.lang.String to, byte[] messageBody, java.lang.String contentType, java.lang.String contentEncoding) throws CommunicationsException {
    try {
        to = to.trim();
        // Handle default domain name (i.e. transform 1234 -> 1234@sip.com
        String defaultDomainName = SIPConfig.getDefaultDomain();
        if (// no sip scheme
        defaultDomainName != null && !to.trim().startsWith("tel:") && // most
        to.indexOf('@') == -1) // probably
        // a
        // sip
        // uri
        {
            to = to + "@" + defaultDomainName;
        }
        // Let's be uri fault tolerant
        if (// no sip scheme
        to.toLowerCase().indexOf("sip:") == -1 && // most probably a sip uri
        to.indexOf('@') != -1) {
            to = "sip:" + to;
        }
        // Request URI
        URI requestURI;
        try {
            requestURI = sipManCallback.addressFactory.createURI(to);
        } catch (ParseException ex) {
            throw new CommunicationsException(to + " is not a legal SIP uri!", ex);
        }
        // Call ID
        CallIdHeader callIdHeader = sipManCallback.sipProvider.getNewCallId();
        // CSeq
        CSeqHeader cSeqHeader;
        try {
            cSeqHeader = sipManCallback.headerFactory.createCSeqHeader(1L, Request.MESSAGE);
        } catch (Exception 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);
        }
        ContentTypeHeader contentTypeHeader = null;
        try {
            String[] contentTypeTab = contentType.split("/");
            contentTypeHeader = sipManCallback.headerFactory.createContentTypeHeader(contentTypeTab[0], contentTypeTab[1]);
        } catch (ParseException ex) {
            throw new CommunicationsException("ContentType Header must look like type/subtype!", ex);
        }
        ContentLengthHeader contentLengthHeader = null;
        try {
            contentLengthHeader = sipManCallback.headerFactory.createContentLengthHeader(messageBody.length);
        } catch (InvalidArgumentException ex) {
            throw new CommunicationsException("Cseq Header must contain a integer value!", ex);
        }
        // ExpiresHeader expiresHeader = null;
        try {
            sipManCallback.headerFactory.createExpiresHeader(30);
        } catch (InvalidArgumentException ex) {
            throw new CommunicationsException("Expires Header must be an integer!", ex);
        }
        // ViaHeaders
        ArrayList<ViaHeader> viaHeaders = sipManCallback.getLocalViaHeaders();
        // MaxForwards
        MaxForwardsHeader maxForwards = sipManCallback.getMaxForwardsHeader();
        Request message = null;
        try {
            message = sipManCallback.messageFactory.createRequest(requestURI, Request.MESSAGE, callIdHeader, cSeqHeader, fromHeader, toHeader, viaHeaders, maxForwards);
            message.setContent(messageBody, contentTypeHeader);
            message.setContentLength(contentLengthHeader);
        // message.addHeader(eventHeader);
        } catch (ParseException ex) {
            throw new CommunicationsException("Failed to create message Request!", ex);
        }
        ClientTransaction messageTransaction = null;
        try {
            messageTransaction = sipManCallback.sipProvider.getNewClientTransaction(message);
        // 
        } catch (TransactionUnavailableException ex) {
            throw new CommunicationsException("Failed to create messageTransaction.", ex);
        }
        try {
            messageTransaction.sendRequest();
        } catch (SipException ex) {
            throw new CommunicationsException("An error occurred while sending message request", ex);
        }
        return messageTransaction.toString();
    } finally {
    }
}
Also used : MaxForwardsHeader(javax.sip.header.MaxForwardsHeader) Address(javax.sip.address.Address) FromHeader(javax.sip.header.FromHeader) ContentTypeHeader(javax.sip.header.ContentTypeHeader) ClientTransaction(javax.sip.ClientTransaction) ContentLengthHeader(javax.sip.header.ContentLengthHeader) Request(javax.sip.message.Request) URI(javax.sip.address.URI) SipSecurityException(net.java.sipmack.sip.security.SipSecurityException) SipException(javax.sip.SipException) CommunicationsException(net.java.sipmack.sip.CommunicationsException) InvalidArgumentException(javax.sip.InvalidArgumentException) TransactionUnavailableException(javax.sip.TransactionUnavailableException) ParseException(java.text.ParseException) 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) CommunicationsException(net.java.sipmack.sip.CommunicationsException) SipException(javax.sip.SipException)

Example 2 with CommunicationsException

use of net.java.sipmack.sip.CommunicationsException in project Spark by igniterealtime.

the class MessageProcessing method processAuthenticationChallenge.

/**
 * Attempts to re-generate the corresponding request with the proper
 * credentials and terminates the call if it fails.
 *
 * @param clientTransaction the corresponding transaction
 * @param response          the challenge
 */
void processAuthenticationChallenge(ClientTransaction clientTransaction, Response response) {
    try {
        ClientTransaction retryTran = sipManCallback.sipSecurityManager.handleChallenge(response, clientTransaction);
        retryTran.sendRequest();
    } catch (SipSecurityException exc) {
        sipManCallback.fireCommunicationsError(new CommunicationsException("Authorization failed!", exc));
    } catch (Exception exc) {
        sipManCallback.fireCommunicationsError(new CommunicationsException("Failed to resend a request " + "after a security challenge!", exc));
    } finally {
    }
}
Also used : SipSecurityException(net.java.sipmack.sip.security.SipSecurityException) ClientTransaction(javax.sip.ClientTransaction) CommunicationsException(net.java.sipmack.sip.CommunicationsException) SipSecurityException(net.java.sipmack.sip.security.SipSecurityException) SipException(javax.sip.SipException) CommunicationsException(net.java.sipmack.sip.CommunicationsException) InvalidArgumentException(javax.sip.InvalidArgumentException) TransactionUnavailableException(javax.sip.TransactionUnavailableException) ParseException(java.text.ParseException)

Example 3 with CommunicationsException

use of net.java.sipmack.sip.CommunicationsException in project Spark by igniterealtime.

the class MessageProcessing method sendKeepAlive.

public void sendKeepAlive() throws CommunicationsException {
    String to = "";
    byte[] messageBody = "".getBytes();
    try {
        to = to.trim();
        // Handle default domain name (i.e. transform 1234 -> 1234@sip.com
        String defaultDomainName = SIPConfig.getDefaultDomain();
        if (// no sip scheme
        defaultDomainName != null && !to.trim().startsWith("tel:") && to.indexOf('@') == -1) {
            to = to + "@" + defaultDomainName;
        }
        // Let's be uri fault tolerant
        if (// no sip scheme
        to.toLowerCase().indexOf("sip:") == -1 && // most probably a sip uri
        to.indexOf('@') != -1) {
            to = "sip:" + to;
        }
        // Request URI
        URI requestURI;
        try {
            requestURI = sipManCallback.addressFactory.createURI(to);
        } catch (ParseException ex) {
            throw new CommunicationsException(to + " is not a legal SIP uri!", ex);
        }
        // Call ID
        CallIdHeader callIdHeader = sipManCallback.sipProvider.getNewCallId();
        // CSeq
        CSeqHeader cSeqHeader;
        try {
            cSeqHeader = sipManCallback.headerFactory.createCSeqHeader(1L, Request.MESSAGE);
        } catch (Exception 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);
        }
        ContentLengthHeader contentLengthHeader = null;
        try {
            contentLengthHeader = sipManCallback.headerFactory.createContentLengthHeader(messageBody.length);
        } catch (InvalidArgumentException ex) {
            throw new CommunicationsException("Cseq Header must contain a integer value!", ex);
        }
        // ExpiresHeader expiresHeader = null;
        try {
            sipManCallback.headerFactory.createExpiresHeader(30);
        } catch (InvalidArgumentException ex) {
            throw new CommunicationsException("Expires Header must be an integer!", ex);
        }
        String contentType = "text/plain";
        ContentTypeHeader contentTypeHeader = null;
        try {
            String[] contentTypeTab = contentType.split("/");
            contentTypeHeader = sipManCallback.headerFactory.createContentTypeHeader(contentTypeTab[0], contentTypeTab[1]);
        } catch (ParseException ex) {
            throw new CommunicationsException("ContentType Header must look like type/subtype!", ex);
        }
        // ViaHeaders
        ArrayList<ViaHeader> viaHeaders = sipManCallback.getLocalViaHeaders();
        // MaxForwards
        MaxForwardsHeader maxForwards = sipManCallback.getMaxForwardsHeader();
        Request message = null;
        try {
            message = sipManCallback.messageFactory.createRequest(requestURI, Request.MESSAGE, callIdHeader, cSeqHeader, fromHeader, toHeader, viaHeaders, maxForwards);
            message.setContentLength(contentLengthHeader);
            message.setContent(messageBody, contentTypeHeader);
        // message.addHeader(eventHeader);
        } catch (Exception e) {
            Log.error("sendKeepAlive", e);
        }
        try {
            sipManCallback.sipProvider.getNewClientTransaction(message);
        } catch (TransactionUnavailableException e) {
            e.printStackTrace();
        }
        try {
        // TODO : Anpassen
        // ((SIPClientTransaction) messageTransaction).sendRequest("\0".getBytes(), InetAddress.getByName(SIPConfig.getDefaultDomain()), SIPConfig.getRegistrarPort());
        } catch (Exception e) {
            Log.error("sendKeepAlive", e);
        }
    } finally {
        try {
            this.finalize();
        } catch (Throwable e) {
        }
    }
}
Also used : MaxForwardsHeader(javax.sip.header.MaxForwardsHeader) Address(javax.sip.address.Address) FromHeader(javax.sip.header.FromHeader) ContentTypeHeader(javax.sip.header.ContentTypeHeader) ContentLengthHeader(javax.sip.header.ContentLengthHeader) Request(javax.sip.message.Request) URI(javax.sip.address.URI) SipSecurityException(net.java.sipmack.sip.security.SipSecurityException) SipException(javax.sip.SipException) CommunicationsException(net.java.sipmack.sip.CommunicationsException) InvalidArgumentException(javax.sip.InvalidArgumentException) TransactionUnavailableException(javax.sip.TransactionUnavailableException) ParseException(java.text.ParseException) 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) CommunicationsException(net.java.sipmack.sip.CommunicationsException)

Aggregations

ParseException (java.text.ParseException)3 InvalidArgumentException (javax.sip.InvalidArgumentException)3 SipException (javax.sip.SipException)3 TransactionUnavailableException (javax.sip.TransactionUnavailableException)3 CommunicationsException (net.java.sipmack.sip.CommunicationsException)3 SipSecurityException (net.java.sipmack.sip.security.SipSecurityException)3 ClientTransaction (javax.sip.ClientTransaction)2 Address (javax.sip.address.Address)2 URI (javax.sip.address.URI)2 CSeqHeader (javax.sip.header.CSeqHeader)2 CallIdHeader (javax.sip.header.CallIdHeader)2 ContentLengthHeader (javax.sip.header.ContentLengthHeader)2 ContentTypeHeader (javax.sip.header.ContentTypeHeader)2 FromHeader (javax.sip.header.FromHeader)2 MaxForwardsHeader (javax.sip.header.MaxForwardsHeader)2 ToHeader (javax.sip.header.ToHeader)2 ViaHeader (javax.sip.header.ViaHeader)2 Request (javax.sip.message.Request)2