We’ve moved!

Hi all! We’ve moved our blog to http://kreitech.io/blog

Please visit us there for more amazing posts and information about our experience in software development and Internet of Things 🙂


Create an IVR in Asterisk using Java

In this post we will explain how to install and run FreePBX (GPL), a Web-based GUI to control and manage Asterisk PBX, and how to control an incoming phone call using Java and the Asterisk FastAGI with a custom IVR. We will explain how to configure the system to run with its basic features. For further management, read FreePBX user manuals. First, lets install FreePBX:

Installing FreeBPX

  1. Download the official release image from http://schmoozecom.com/distro-download.php
  2. Install the ISO on a PC or a Virtual Machine.
  3. After installing FreePBX, access the GUI using a WebBrowser. Access the IP address of the PC hosting the FreePBX.
  4. Set your main username and password.
  5. Select FreeBPX Administration to start managing your Asterisk PBX.

Screen Shot 2015-02-10 at 4.10.35 PM Congratulations! You have now Asterisk PBX up and running.

In order to receive incoming calls you should configure your SIP trunk into Asterisk:

Connect Asterisk to SIP Trunk

  1. Go to Connectivity – Trunks. In our example, select Add SIP (chain_sip) Trunk.
  2. Configure your SIP parameters. It is usual of SIP Trunks to provide every parameter needed to configure it in Asterisk. After configuring the SIP Trunk, go to Connectivity-Inbound Routes to create rules for incoming calls. In the Inbound Routes add a Route specifying at least your DID Number from the SIP Trunk. Set a destination for the incoming calls from the DID number. For example, select Phonebook Directory to ask the caller for a phonebook number.
    Screen Shot 2015-02-10 at 4.18.11 PMScreen Shot 2015-02-10 at 4.18.11 PM
  3. Save and apply changes.
    Screen Shot 2015-02-10 at 4.18.11 PM

Test your Asterisk calling from your phone to your SIP number. You should listen to a machine asking for a phonebook number.

Program de IVR

To control the incoming call using custom programming you should use the Asterisk FastAGI. In our case we will create an AGI script using Java and Asterisk-Java library:

  1. Download the Asterisk-Java library from http://www.asterisk-java.org/ from its GIT repository as follows:
    git clone https://github.com/asterisk-java/asterisk-java.git
  2. Access the asterisk-java folder where you downloaded the project and install the JAR file in your local Maven repository:
    make install
  3. Get the JAR file named asterisk-java.jar built and copy it to your test folder where you will place your AGI script. Write a JAVA file called HelloIVRScript.java where you will write you IVR steps. For example, write the following example into the JAVA file:
    import org.asteriskjava.fastagi.AgiChannel;
    import org.asteriskjava.fastagi.AgiException;
    import org.asteriskjava.fastagi.AgiRequest;
    import org.asteriskjava.fastagi.BaseAgiScript; 
    public class HelloAgiScript extends BaseAgiScript {
        public void service(AgiRequest request, AgiChannel channel) throws AgiException {
            //Answer the channel... answer();
            //Play speed-enternumber sound while waiting for the user
            //to enter 4 digits. We wait for a 20 seconds timeout
            //in case digits are not dialed.
            String numbers = getData("speed-enternumber", 20000L, 4); 
            //Work with dialed digits
            //If the user pressed 2-0-0-1, ask for another number and store it
            //If the user pressed 1-0-0-1, let the user leave a voice message
                String finalNumbers = getData("vqplus-enter-number", 20000L, 2); 
               //Store finalNumbers into your database or do something else
            }else if(numbers.equal("1001")){
               //Record the voice for 10 seconds or until the user press 2
            //Terminate the phone call

    Note: You can use many default sounds, located in folder


    or you can record files on your own to use it. Compile your Java file:

    javac -cp asterisk-java.jar HelloIVRScript.java
  4. In the same folder, create a file named:
    and place the following line inside the file:

    hello.agi = HelloIVRScript

    Doing this, you specified that your IVR script is published into the URL:
    which you will use in the next steps.

  5. Edit the file:

    and add the following two lines:

    exten => 1000, 1, Agi(agi://localhost/hello.agi)

    This means that when a phone call arrives at extension 1000 in your Asterisk, the hello.agi script will be invoked.

  6. Go to Admin – Custom Destinations and create a new Custom Destination with the following information:
    Screen Shot 2015-02-10 at 4.18.11 PM
  7. Save and go to Connectivity – Inbound Routes as previously done.
  8. Select the Inbound Route you previously created and in the Set Destination section, set the following configuration:Screen Shot 2015-02-10 at 5.00.49 PMSave and apply changes.
  9. Test your IVR calling your SIP number again, and introducing numbers 2001 to enter two additional numbers or 1001 to leave a voice message. In case you leave a voice message, it will be stored in your “/var/lib/asterisk/sounds” folder in a file named voiceRecord.wav.

Run JBoss AS 7 as a service in Ubuntu

Hi, today I’m going to present a few simple steps to configure and run JBossAS 7 as a sevice.

This was tested with JBossAS 7 on Ubuntu 14.04, but it should work in other versions also.

First of all, create a file in /etc/init.d/, which is the script for the service. This folder contains all the scripts that should run on startup of a linux instance.

sudo nano /etc/init.d/jbossas7

After that, insert the following code, which defines the start and stop commands for the service.
You should replace the variable JBOSS_HOME with your local jboss install folder.
This will run the jboss as a service using the root user, you can change the commands accordingly to run as a different user.

# Provides:          jbossas7  
# Required-Start:    $local_fs $remote_fs $network $syslog  
# Required-Stop:     $local_fs $remote_fs $network $syslog  
# Default-Start:     2 3 4 5  
# Default-Stop:      0 1 6  
# Short-Description: Start/Stop JBoss AS 7  
# chkconfig: 35 92 1  
AS7_OPTS="$AS7_OPTS -Dorg.apache.tomcat.util.http.ServerCookie.ALLOW_HTTP_SEPARATORS_IN_V0=true"   
AS7_OPTS="$AS7_OPTS -Djboss.bind.address.management="  
AS7_OPTS="$AS7_OPTS -Djboss.bind.address="  
case "$1" in  
        /bin/echo "Starting JBoss AS 7..."  
        su - root -c "sudo ${JBOSS_HOME}/bin/standalone.sh $AS7_OPTS > /dev/null & "  
        /bin/echo "Stopping JBoss AS 7..."  
        su - root -c "${JBOSS_HOME}/bin/jboss-cli.sh --connect command=:shutdown"    
        /bin/echo "Usage: /etc/init.d/jbossas7 {start|stop}"; exit 1;  
exit 0  

After this, the only thing remaining is to define when the service will start and shutdown.
Simply run the following two lines

sudo chmod +x /etc/init.d/jbossas7
sudo update-rc.d jbossas7 defaults

The first line enables execution of the created file. The second line creates the symbolic links in /etc/rcX using the options configured in the service script comments (Default-Start and Default-Stop).

In order to test the service before restarting, run:

sudo service jbossas7 start
sudo service jbossas7 stop

How to use Log4J in ejb module in Jboss7.1

De Gaucho a Comecables

JBoss 7.1 already ships with Log4J, but the config is somehow different from previous versions of the server.
The same Log4J architecture applies, you have appenders and categories, but the config files are different, so I’ll explain how to configure a new category and appender for this.

When developing your EJB you need to:

  1. import the Logger class: import org.apache.log4j.Logger;
  2. If the import does not resolve, add this jar to the path: %JBOSS_HOME%modulesorgapachelog4jmainlog4j-1.2.16.jar
  3. create a field with the logger: private static Logger logger = Logger.getLogger(YourEJBClass.class);
  4. use the logger in any method: logger.info("my log");

JBoss already comes with a configuration file under %JBOSS_FOLDER%/standalone/configuration/standalone.xml. By default, your logger will output to server.log and console, but you can add some appender to output your app logs to another file.

Here is an example, edit that file and add the following snippets (following the definition of the file):

  • This one is to add…

View original post 104 more words

Using LdapExtLoginModule with JaasSecurityDomain (securing passwords)

De Gaucho a Comecables

In my last post I wrote about how to connect a JBoss to LDAP defining an LdapExtLoginModule. Clearly, as suggested by the comment of Terry, the password in the xml is in plain text. In this post I’ll explain how to secure this password.

This is really easy to do as suggested in the JBoss docs, just add the following xml to the file $JBOSS_HOME/server/$PROFILE/conf/jboss-service.xml, which will add a JaasSecurityDomain bean to the jmx-console, which will be available for encrypting passwords in Base64:

After this, start the JBoss server and navigate to the JMX Console (http://localhost:8080/jmx-console/ by default) and select the org.jboss.security.plugins.JaasSecurityDomain MBean.

On the org.jboss.security.plugins.JaasSecurityDomain page, look for the encode64(String password) method. Pass the plain text version of the password being used by the LdapExtLoginModule to this method, and invoke it. The return value will be the encrypted version of the password encoded as Base64.

After this…

View original post 84 more words

Authenticate JBoss application using JAAS and LDAP

De Gaucho a Comecables

It is very easy to connect a JBoss to an LDAP server and creating Java EE applications that use the LDAP information for authorization and authentication. Following these simple steps you will be able to configure your JBoss and a web application (configuring an ejb deployment is similar, just read the Java EE API to map the attributes from web.xml to ejb annotations).

For this example I already have an Apache Directory Server running locally, with the sample LDIF with the sevenSeas company imported. You can find the file and tutorial in the Apache DS documentation.

There are two main steps to be able to use LDAP as an authentication mechanism:

  1. Configure JBoss to connect to LDAP server
  2. Configure the application to use the application policy

Configure JBoss to connect to LDAP server

The JBoss connects to the LDAP server using an application-policy, which is configured in %server_path%/conf/login-config.xml

View original post 229 more words

Search LDAP from Java

De Gaucho a Comecables

This post explains how to connect to an LDAP server (in my case Apache DS) and retrieve elements which match a certain filter.

I have deployed an Apache Directory Server version 2.0 and imported the demo LDIF containing users and groups for the “sevenSeas” organization. You can download the file from the apache DS documentation.

This java code connects to the Apache DS deployed locally using the default port and user, and searches the context “ou=groups,o=sevenSeas” for groups the user “Fletcher Christian” belongs to.

With the demo LDIF imported in Apache DS the output will be:

View original post