Search in sources :

Example 36 with ParseException

use of java.text.ParseException in project Openfire by igniterealtime.

the class SipUtil method sendInvite.

/**
     * Builds and sends a standard INVITE message, with a
     * CSeq number 1.
     * @param cp the CallParticipant
     * @param tpccName String identifying who is making the call.
     * @return transaction Id of newly created transaction.
     * @throws ParseException if message cannot be parsed
     * @throws SipException if general Sip Exception occurs.
     */
public ClientTransaction sendInvite(CallParticipant cp, String sdp) throws ParseException, InvalidArgumentException, SipException {
    // variables used for message building
    SipURI fromAddress = null;
    Address fromNameAddress = null;
    FromHeader fromHeader = null;
    SipURI toAddress = null;
    Address toNameAddress = null;
    ToHeader toHeader = null;
    SipURI requestURI = null;
    CallIdHeader callIdHeader = null;
    CSeqHeader cSeqHeader = null;
    AllowEventsHeader allowEventsHeader = null;
    ViaHeader viaHeader = null;
    ArrayList viaHeaders = null;
    ContentTypeHeader contentTypeHeader = null;
    Request invite = null;
    String obProxy = null;
    /*
         * We need a name and number to identify the party placing the call.
         * There are some restrictions when using the Vocal Proxy.
         *
         * If the number being called has 4 digits, then the request is
	 * going to the proxy.  The vocal proxy requires the <tpccName>
	 * in the From: header to be provisioned.
         * We use 4099 which we provisioned.
         *
         * We generate a header like this:
         *
         *    From: "<display name>" <sip:<tpccName>@129.148.75.131:5060>
         *    To: "20315" <sip:20315@129.148.75.22:5060>
         *
         * 20315 is the phone being called.
	 *
         * "<display name>" is the string that will show up as the callerID
         * on phone 20315.
         *
         * <tpccName> is the identifier of who's making the call.
         */
    String fromName = cp.getDisplayName();
    //cp.getFromPhoneNumber();
    String fromNumber = cp.getDisplayName();
    String toNumber = cp.getPhoneNumber();
    String transport = "udp";
    Logger.println("XXX from = " + fromName + " " + cp);
    // int toSipPort = SipServer.getSipAddress().getPort();
    // XXX this should be the proxy or gateway port!
    int toSipPort = 5060;
    String proxy = cp.getSipProxy();
    if (proxy == null) {
        proxy = SipServer.getDefaultSipProxy();
    }
    String voipGateway = null;
    if (toNumber.indexOf("sip:") == 0) {
        /*
		 * If a SIP URI is specified, parse it and send
		 * the request directly to the target unless sendSipUriToProxy is false.
		 *
		 * If this request is sent to the proxy
		 * the endpoint must be registered with the proxy.
		 */
        Address address = null;
        SipURI sipURI = null;
        String host = null;
        String user = null;
        try {
            address = addressFactory.createAddress(toNumber);
            sipURI = (SipURI) address.getURI();
            host = sipURI.getHost();
            user = sipURI.getUser();
        } catch (ParseException e) {
            Logger.println("parse exception:  " + toNumber + " sipUri " + sipURI + " host " + host + " user " + user);
        }
        if (Logger.logLevel >= Logger.LOG_SIP) {
            Logger.println("address: " + address);
            Logger.println("sipURI: " + sipURI);
            Logger.println("host: " + host);
            Logger.println("user: " + user);
        }
        if (SipServer.getSendSipUriToProxy() == false && user != null) {
            InetAddress inetAddress;
            try {
                inetAddress = InetAddress.getByName(host);
                //inetAddress.getHostAddress();
                voipGateway = host;
                int port = sipURI.getPort();
                if (port > 0) {
                    toSipPort = port;
                }
                toNumber = user;
                /*
				 * Keep just the User information from the URI.
				 * XXX Not sure why I should do this.
				 */
                //cp.setPhoneNumber(toNumber);
                Logger.println("Call " + cp + " Sending INVITE directly to " + inetAddress + ":" + toSipPort);
            } catch (UnknownHostException e) {
                /*
				 * Let proxy handle it
				 */
                voipGateway = proxy;
                Logger.println("Call " + cp + " Using proxy " + proxy + " for " + toNumber);
                // XXX Not sure why I should do this.
                //cp.setPhoneNumber(toNumber.substring(4));  // skip sip:
                toNumber = toNumber.substring(4);
            }
        } else {
            voipGateway = proxy;
            Logger.println("Call " + cp + " Using proxy " + proxy + " for " + toNumber);
            // XXX Not sure why I should do this.
            //cp.setPhoneNumber(toNumber.substring(4));  // skip sip:
            toNumber = toNumber.substring(4);
        }
    } else {
        // telephone number
        transport = System.getProperty("com.sun.voip.server.PROTOCOL");
        if (toNumber.indexOf("tel:") == 0) {
            toNumber = toNumber.substring(4);
        }
        voipGateway = proxy;
        Logger.println("Call " + cp + " Using proxy " + proxy + " for " + toNumber);
    }
    if (toNumber.indexOf("@") < 0 && CallHandler.enablePSTNCalls() == false) {
        throw new SipException("PSTN calls are not allowed:  " + cp);
    }
    ArrayList<ProxyCredentials> proxyCredentialList = SipServer.getProxyCredentials();
    boolean gatewayRequired = false;
    if (voipGateway == null) {
        if (proxy == null) {
            if (proxyCredentialList.size() == 0) {
                Logger.println("Call " + cp + " no voipGateway is available!");
                throw new SipException("No voip Gateway! " + cp);
            } else
                gatewayRequired = true;
        } else {
            voipGateway = proxy;
            gatewayRequired = true;
        }
    } else {
        if (voipGateway.equals(proxy))
            gatewayRequired = true;
    }
    if (gatewayRequired) {
        Logger.println("XXXX gatewayRequired");
        if (proxyCredentialList.size() != 0) {
            Logger.println("XXXX gatewayRequired 1");
            int voipIndex = 0;
            for (int i = 0; i < proxyCredentialList.size(); i++) {
                ProxyCredentials proxyCredentials = proxyCredentialList.get(i);
                if (voipGateway.equals(proxyCredentials.getName())) {
                    voipIndex = i;
                }
            }
            ProxyCredentials proxyCredentials = proxyCredentialList.get(voipIndex);
            //fromName = proxyCredentials.getUserDisplay();
            voipGateway = proxyCredentials.getHost();
            obProxy = proxyCredentials.getProxy();
            //fromAddress = addressFactory.createSipURI(proxyCredentials.getUserName(), voipGateway);
            fromAddress = addressFactory.createSipURI(fromName, voipGateway);
        //cp.setProxyCredentials(proxyCredentials);				// we need this to match SIP transaction later
        //cp.setDisplayName(proxyCredentials.getUserDisplay());	// we need this to get proxy authentication details later
        } else {
            Logger.println("XXXX gatewayRequired 2");
        }
        Logger.println("XXXX gatewayRequired 3");
        toAddress = addressFactory.createSipURI(toNumber, voipGateway);
    } else {
        Logger.println("XXXX gatewayRequired 4");
        Logger.println("fromNumber " + fromNumber);
        if (fromNumber.startsWith("sip:"))
            fromAddress = (SipURI) addressFactory.createAddress(fromNumber).getURI();
        else
            fromAddress = addressFactory.createSipURI(fromNumber, ourIpAddress);
        fromAddress.setPort(ourSipPort);
        toAddress = addressFactory.createSipURI(toNumber, voipGateway);
    }
    Logger.println("XXXX gatewayRequired 5");
    Logger.println("from " + fromAddress);
    Logger.println("to " + toAddress);
    fromNameAddress = addressFactory.createAddress(fromName, fromAddress);
    fromHeader = headerFactory.createFromHeader(fromNameAddress, new Integer((int) (Math.random() * 10000)).toString());
    /* create To Header
	 * e.g. "Willie Walker"<sip:30039@152.70.1.28:5060>
	 *   where "Willie Walker" == cp.getName()
	 *                      30039 == cp.getNumber()
	 *             152.70.1.28 == cp.getIpAddress()
	 *                    5060 == cp.getPort()
	 */
    Logger.println("XXXX gatewayRequired 6");
    if (Bridge.getPrivateHost().startsWith("127.") && voipGateway.equals("127.0.0.1") == false) {
        throw new SipException("The bridge's ip address is " + Bridge.getPrivateHost() + ". It is not possible to initiate a call to " + voipGateway);
    }
    if (Bridge.getPrivateHost().startsWith("127.") == false && voipGateway.startsWith("127.") == true) {
        throw new SipException("The bridge's ip address must be " + Bridge.getPrivateHost() + " in order to issue a call to " + voipGateway);
    }
    Logger.writeFile("Call " + cp + " voip gateway / proxy " + voipGateway + " port " + toSipPort);
    /*
	 * Don't do this because port should be that of the toNumber if specified
	 * otherwise that of the voipGateway
	 */
    // toAddress.setPort(toSipPort);
    toNameAddress = addressFactory.createAddress(toNumber, toAddress);
    toHeader = headerFactory.createToHeader(toNameAddress, null);
    /* create request URI (the first line of the request)
         * e.g. INVITE sip:61202@129.145.176.239:5060;transport=udp SIP/2.0
         *   where                    INVITE == (specfied later)
         *                      (requestURI) == (same as toAddress)
         *             transport=udp SIP/2.0 == (transport)
         */
    int ix = toNumber.indexOf("@");
    if (ix >= 0) {
        toNumber = toNumber.substring(0, ix);
    }
    Logger.println("XXXX gatewayRequired 7");
    requestURI = addressFactory.createSipURI(toNumber, voipGateway);
    requestURI.setPort(toSipPort);
    requestURI.setTransportParam(sipProvider.getListeningPoint(transport).getTransport());
    /* create Via headers
         * e.g. Via: SIP/2.0/UDP 152.70.1.43:5060;branch=z9hG4bK5
         *   where      SIP/2.0/UDP == (transport)
         *         152.70.1.43:5060 == (local address and SIP port)
         *          branch=z9hG4bk5 == (auto generated branch id)
         */
    viaHeader = headerFactory.createViaHeader(ourIpAddress, ourSipPort, sipProvider.getListeningPoint(transport).getTransport(), null);
    //viaHeader.setBranch(MessageFactoryImpl.generateBranchId());
    viaHeaders = new ArrayList();
    viaHeaders.add(viaHeader);
    /* create CallId header
         * e.g. Call-Id: 7727452ebac7ff5ac0c65baa7250e2f5@152.70.1.43
         *   where 77274.... == (globally unique identifier)
         */
    callIdHeader = sipProvider.getNewCallId();
    /* create Seq header
         * e.g. CSeq: 1 INVITE
         *   where      1 == (CSeq number - motonically increasing within
         *                    each SIP callId)
         *         INVITE == (request method)
         */
    cSeqHeader = headerFactory.createCSeqHeader(1, Request.INVITE);
    // Create a new MaxForwardsHeader
    MaxForwardsHeader maxForwards = headerFactory.createMaxForwardsHeader(70);
    // create INVITE message.  Put everything together
    invite = messageFactory.createRequest(requestURI, Request.INVITE, callIdHeader, cSeqHeader, fromHeader, toHeader, viaHeaders, maxForwards);
    Logger.println("XXXX gatewayRequired 10");
    if (SdpManager.useTelephoneEvent() == true) {
        allowEventsHeader = headerFactory.createAllowEventsHeader("telephone-event");
        invite.addHeader(allowEventsHeader);
    }
    /* Contact Header (where subsequent requests should be sent to)
         * e.g. Contact: "Awarenex" <sip:Awarenex@152.70.1.43:5060>;
         *   where   "Awarenex" <sip:... == (local Address)
         */
    SipURI contactURI = null;
    if (fromNumber.startsWith("sip:"))
        contactURI = (SipURI) addressFactory.createAddress(fromNumber).getURI();
    else
        contactURI = addressFactory.createSipURI(fromNumber, ourPublicIpAddress);
    Logger.println("XXXX gatewayRequired 12");
    contactURI.setPort(ourPublicSipPort);
    Address contactAddress = addressFactory.createAddress(contactURI);
    contactAddress.setDisplayName(fromName);
    ContactHeader contactHeader = headerFactory.createContactHeader(contactAddress);
    invite.addHeader(contactHeader);
    Logger.println("XXXX gatewayRequired 14");
    if (obProxy != null) {
        try {
            SipURI routeURI = (SipURI) addressFactory.createURI("sip:" + obProxy + ";lr");
            RouteHeader routeHeader = headerFactory.createRouteHeader(addressFactory.createAddress(routeURI));
            invite.addHeader(routeHeader);
        } catch (Exception e) {
            Logger.error("Creating registration route error " + e);
        }
    }
    Logger.println("XXXX gatewayRequired 16");
    if (cp.isAutoAnswer()) {
        Logger.println("Call " + cp + " alert-info added");
        try {
            SIPAlertInfo alertInfo = new SIPAlertInfo();
            alertInfo.setNamePair("info=alert-autoanswer");
            invite.addHeader(alertInfo);
        } catch (Exception e) {
            Logger.error("Creating alert info error " + e);
        }
    }
    Logger.println("XXXX gatewayRequired 18");
    if (sdp != null) {
        contentTypeHeader = headerFactory.createContentTypeHeader("application", "sdp");
        invite.setContent(sdp, contentTypeHeader);
    }
    // Create the client transaction.
    ClientTransaction clientTransaction;
    Logger.writeFile("Invite\n" + invite);
    try {
        clientTransaction = sipProvider.getNewClientTransaction(invite);
    } catch (Exception e) {
        Logger.println("Call " + cp + " sendInvite failed:  " + e.getMessage());
        return null;
    }
    Logger.println("XXXX gatewayRequired 19");
    clientTransaction.sendRequest();
    Logger.println("XXXX gatewayRequired 20");
    return clientTransaction;
}
Also used : InetSocketAddress(java.net.InetSocketAddress) InetAddress(java.net.InetAddress) ArrayList(java.util.ArrayList) UnknownHostException(java.net.UnknownHostException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) ParseException(java.text.ParseException) ParseException(java.text.ParseException) InetAddress(java.net.InetAddress)

Example 37 with ParseException

use of java.text.ParseException in project Openfire by igniterealtime.

the class SipUtil method getSdpInfo.

public SdpInfo getSdpInfo(String sdpBody, boolean isRequest) throws ParseException {
    SdpInfo remoteSdpInfo = sdpManager.parseSdp(sdpBody);
    MediaInfo myPreferredMediaInfo = sdpManager.getPreferredMediaInfo();
    byte payload;
    Logger.writeFile("My preferred media " + myPreferredMediaInfo);
    /*
	 * If this is a remote SIP REQUEST and the remote side supports our
	 * preferred mediaInfo, we will reply selecting our preferred media.
	 * Otherwise, we reply using the remote's media choice, which is
	 * either the remote's preferred or the "best" choice.
	 */
    if (isRequest && remoteSdpInfo.isSupported(myPreferredMediaInfo)) {
        payload = myPreferredMediaInfo.getPayload();
        remoteSdpInfo.setMediaInfo(myPreferredMediaInfo);
        Logger.println("My preferred payload being used " + payload);
    } else {
        if (isRequest) {
            Logger.writeFile("My preferred media " + myPreferredMediaInfo + " not supported...");
        }
        try {
            payload = remoteSdpInfo.getMediaInfo().getPayload();
            remoteSdpInfo.setMediaInfo(sdpManager.findMediaInfo(payload));
            Logger.writeFile("media setting is " + remoteSdpInfo.getMediaInfo());
        } catch (ParseException e) {
            remoteSdpInfo.setMediaInfo(new MediaInfo((byte) 0, RtpPacket.PCMU_ENCODING, 8000, 1, false));
        }
    }
    return remoteSdpInfo;
}
Also used : MediaInfo(com.sun.voip.MediaInfo) SdpInfo(com.sun.voip.SdpInfo) ParseException(java.text.ParseException)

Example 38 with ParseException

use of java.text.ParseException in project Openfire by igniterealtime.

the class SipUtil method getDistributedBridgeFromSdp.

public static boolean getDistributedBridgeFromSdp(Request request) {
    byte[] rawContent = request.getRawContent();
    if (rawContent == null) {
        return false;
    }
    String sdpBody = new String(rawContent);
    SdpInfo sdpInfo;
    try {
        sdpInfo = SdpManager.parseSdp(sdpBody);
    } catch (ParseException e) {
        return false;
    }
    return sdpInfo.isDistributedBridge();
}
Also used : SdpInfo(com.sun.voip.SdpInfo) ParseException(java.text.ParseException)

Example 39 with ParseException

use of java.text.ParseException in project Openfire by igniterealtime.

the class ConferenceManager method getConference.

public static ConferenceManager getConference(String conferenceId, String mediaPreference, String displayName, boolean permanent) {
    ConferenceManager conferenceManager;
    try {
        conferenceManager = findConferenceManager(conferenceId);
        if (Logger.logLevel >= Logger.LOG_INFO) {
            Logger.println("found existing conference:  '" + conferenceId + "'");
        }
        return conferenceManager;
    } catch (ParseException e) {
    }
    try {
        conferenceManager = new ConferenceManager(conferenceId, mediaPreference, displayName);
    } catch (SocketException e) {
        Logger.error("Can't create conference " + conferenceId + " " + e.getMessage());
        return null;
    }
    synchronized (conferenceList) {
        conferenceList.add(conferenceManager);
    }
    Logger.println("starting new conference:  '" + conferenceId + "'.  " + " conferences in progress:  " + conferenceList.size());
    conferenceManager.setPermanent(permanent);
    String id = conferenceManager.getId();
    if (displayName != null) {
        id += ":" + mediaPreference + ":" + displayName;
    }
    ConferenceManager.conferenceEventNotification(new ConferenceEvent(ConferenceEvent.CONFERENCE_STARTED, id));
    return conferenceManager;
}
Also used : SocketException(java.net.SocketException) ParseException(java.text.ParseException) ConferenceEvent(com.sun.voip.ConferenceEvent)

Example 40 with ParseException

use of java.text.ParseException in project Openfire by igniterealtime.

the class ConferenceManager method end.

private void end() {
    try {
        recordConference(false, null, null);
    } catch (ParseException e) {
        Logger.println(conferenceId + ":  Failed to stop recording conference! " + e.getMessage());
    }
    Logger.writeFile("ending conf " + conferenceId + ":  permanent " + permanent + ", mediaPreference " + mediaPreference);
    if (permanent) {
        conferenceSender.printStatistics();
        if (mediaPreference != null) {
            try {
                setMediaInfo(mediaPreference);
            } catch (ParseException e) {
                Logger.println(conferenceId + ":  Can't change meeting media setting to " + mediaPreference + ": " + e.getMessage());
            }
            mediaPreference = null;
        }
    } else {
        if (done) {
            return;
        }
        done = true;
        ConferenceManager.conferenceEventNotification(new ConferenceEvent(ConferenceEvent.CONFERENCE_ENDED, conferenceId));
        synchronized (conferenceList) {
            conferenceList.remove(this);
        }
        if (conferenceReceiver != loneConferenceReceiver) {
            conferenceReceiver.end();
        }
        conferenceSender.printStatistics();
    }
    int activeConferences = 0;
    synchronized (conferenceList) {
        for (int i = 0; i < conferenceList.size(); i++) {
            ConferenceManager conferenceManager = (ConferenceManager) conferenceList.get(i);
            if (conferenceManager.getMemberList().size() > 0) {
                activeConferences++;
            }
        }
    }
    Logger.println("");
    Logger.println("Conference:  '" + conferenceId + "' has ended.  " + "conferences still in progress:  " + activeConferences);
    Logger.println("");
    Logger.flush();
    if (totalMembers == 0) {
        /*
             * This is a great time to do a full garbage collection
             */
        Logger.println("No conferences in progress, doing a full GC...");
        System.gc();
    }
}
Also used : ParseException(java.text.ParseException) ConferenceEvent(com.sun.voip.ConferenceEvent)

Aggregations

ParseException (java.text.ParseException)3530 Date (java.util.Date)1381 SimpleDateFormat (java.text.SimpleDateFormat)1296 IOException (java.io.IOException)366 ArrayList (java.util.ArrayList)334 DateFormat (java.text.DateFormat)305 Calendar (java.util.Calendar)288 Test (org.junit.Test)274 HashMap (java.util.HashMap)139 Matcher (java.util.regex.Matcher)99 File (java.io.File)97 GregorianCalendar (java.util.GregorianCalendar)97 Map (java.util.Map)95 List (java.util.List)92 BigDecimal (java.math.BigDecimal)72 Locale (java.util.Locale)68 ParsePosition (java.text.ParsePosition)57 Timestamp (java.sql.Timestamp)55 InputStream (java.io.InputStream)53 DecimalFormat (java.text.DecimalFormat)50