JBoss.orgCommunity Documentation

Mobicents JAIN SLEE USSD Example User Guide


This manual uses several conventions to highlight certain words and phrases and draw attention to specific pieces of information.

In PDF and paper editions, this manual uses typefaces drawn from the Liberation Fonts set. The Liberation Fonts set is also used in HTML editions if the set is installed on your system. If not, alternative but equivalent typefaces are displayed. Note: Red Hat Enterprise Linux 5 and later includes the Liberation Fonts set by default.

Four typographic conventions are used to call attention to specific words and phrases. These conventions, and the circumstances they apply to, are as follows.

Mono-spaced Bold

Used to highlight system input, including shell commands, file names and paths. Also used to highlight key caps and key-combinations. For example:

The above includes a file name, a shell command and a key cap, all presented in Mono-spaced Bold and all distinguishable thanks to context.

Key-combinations can be distinguished from key caps by the hyphen connecting each part of a key-combination. For example:

The first sentence highlights the particular key cap to press. The second highlights two sets of three key caps, each set pressed simultaneously.

If source code is discussed, class names, methods, functions, variable names and returned values mentioned within a paragraph will be presented as above, in Mono-spaced Bold. For example:

Proportional Bold

This denotes words or phrases encountered on a system, including application names; dialogue box text; labelled buttons; check-box and radio button labels; menu titles and sub-menu titles. For example:

The above text includes application names; system-wide menu names and items; application-specific menu names; and buttons and text found within a GUI interface, all presented in Proportional Bold and all distinguishable by context.

Note the > shorthand used to indicate traversal through a menu and its sub-menus. This is to avoid the difficult-to-follow 'Select Mouse from the Preferences sub-menu in the System menu of the main menu bar' approach.

Mono-spaced Bold Italic or Proportional Bold Italic

Whether Mono-spaced Bold or Proportional Bold, the addition of Italics indicates replaceable or variable text. Italics denotes text you do not input literally or displayed text that changes depending on circumstance. For example:

Note the words in bold italics above username, domain.name, file-system, package, version and release. Each word is a placeholder, either for text you enter when issuing a command or for text displayed by the system.

Aside from standard usage for presenting the title of a work, italics denotes the first use of a new and important term. For example:

If you find a typographical error in this manual, or if you have thought of a way to make this manual better, we would love to hear from you! Please submit a report in the the Issue Tracker, against the product Mobicents JAIN SLEE USSD Example, or contact the authors.

When submitting a bug report, be sure to mention the manual's identifier: JAIN_SLEE_USSD_EXAMPLE_User_Guide

If you have a suggestion for improving the documentation, try to be as specific as possible when describing it. If you have found an error, please include the section number and some of the surrounding text so we can find it easily.

  1. Downloading the source code

    Use SVN to checkout a specific release source, the base URL is http://mobicents.googlecode.com/svn/tags/servers/jain-slee/2.x.y/examples/ussd, then add the specific release version, lets consider 1.0.0.BETA1.

    [usr]$ svn co http://mobicents.googlecode.com/svn/tags/servers/jain-slee/2.x.y/examples/ussd/1.0.0.BETA1 slee-example-ussd-1.0.0.BETA1
  2. Building the source code

    Important

    Maven 2.0.9 (or higher) is used to build the release. Instructions for using Maven2, including install, can be found at http://maven.apache.org

    Use Maven to build the binary.

    				    [usr]$ cd slee-example-ussd-1.0.0.BETA1
    				    [usr]$ mvn install
    				    

    Once the process finishes you should have the deployable-unit jar file in the target directory, if Mobicents JAIN SLEE is installed and environment variable JBOSS_HOME is pointing to its underlying JBoss Application Server directory, then the deployable unit jar will also be deployed in the container.

Similar process as for Section 2.2.1, “Release Source Code Building”, the only change is the SVN source code URL, which is http://mobicents.googlecode.com/svn/trunk/servers/jain-slee/examples/ussd.

USSD Example is very simple example in terms of code complexity. It responds to USSD requests carried within SIP messages. Based on requests and JBPM process declaration, service responds with specific prompt.

General design is depicted on following diagram:

USSD Top overview

USSD Example JBPM module is used to drive conversation with USSD user.

Process defined in standard JPDL XML consumes USSD string (digits) and provides prompt sent back to user. Logically JBPM process(state and transitions) create interactive menu accessed by USSD user.

USSD Example JBPM related source is simple. Example defines two custom classes to perform additional tasks within flow. Also XML process definition consists of simple, not complex menu to ease learning process.

Decisions are performed by custom handler, its source looks as follows:



package org.mobicents.example.ss7.ussd.jbpm;
import java.util.Map;
import org.apache.log4j.Logger;
import org.jbpm.graph.exe.ExecutionContext;
import org.jbpm.graph.node.DecisionHandler;
public class USSDDecisionHandler implements DecisionHandler {
    private static final Logger logger = Logger.getLogger(USSDDecisionHandler.class);
    
    public static final String _INPUT_ = "ussd.input";
    public static final String _END_ = "end";
    public String decide(ExecutionContext ctx) throws Exception {
        //get what we got from user
        String input = (String) ctx.getVariable(_INPUT_);
        Map transitions = ctx.getNode().getLeavingTransitionsMap();
        if(transitions.get(input) == null)
        {
            return _END_;
        }else
        {
            return ""+input;
        }
                
        
    }
}
        

Handler expects user input to be passed as context variable with specific name. Its value is set by process user(here, by service, refer Section 4.2.3.4, “USSD Processing” ). In case there is no transition under passed input, handler falls back to predefined transition(not it MUST be present in XML definition).

Decision handler is bound into process with following declaration:



<decision name="welcome_decision" expr="#{ussd.input}">
        <handler class="org.mobicents.example.ss7.ussd.jbpm.USSDDecisionHandler" />
        
        <transition name="1" to="schedule" />
        <transition name="2" to="location" />
        <transition name="3" to="discount" />
        <transition name="4" to="promo" />
        <!-- this is default transition to end, it has to be present -->    
        <transition name="end" to="end" />  
        
    </decision>
            
            

Prompt is set in process context by custom handler. Handler is activated once process enters state(it is defined in XML definition of process). Source looks as follows:



package org.mobicents.example.ss7.ussd.jbpm;
import org.jbpm.graph.def.ActionHandler;
import org.jbpm.graph.exe.ExecutionContext;
public class USSDPromptHandler implements ActionHandler {
    public static final String _PROMPT_ = "ussd.prompt";
    private String prompt;
    private boolean end = false;
    /**
     * @return the end
     */
    public boolean isEnd() {
        return end;
    }
    /**
     * @param end
     *            the end to set
     */
    public void setEnd(boolean end) {
        this.end = end;
    }
    /**
     * @return the prompt
     */
    public String getPrompt() {
        return prompt;
    }
    /**
     * @param prompt
     *            the prompt to set
     */
    public void setPrompt(String prompt) {
        this.prompt = prompt;
    }
    public void execute(ExecutionContext ctx) throws Exception {
        ctx.setVariable(_PROMPT_, prompt);
        if (end) {
            // make transition
            ctx.leaveNode();
        }
    }
}   
        

Note that this handler has additional property. This property controls whether process should move to another state, once proper prompt is set, or not. Process state which set this property to true MUST declare single transition, refer to Section 4.1.3, “XML Process definition” for details

Prompt handler is bound into process with following declaration:



<state name="welcome">

        <event type='node-enter'>
            <action class='org.mobicents.example.ss7.ussd.jbpm.USSDPromptHandler'>
                <prompt>Welcome to train station scheduler service. Press following digits for 1) Train schedule.  2) Station location. 3) Discount rates. 4) Promo</prompt>
            </action>
        </event>
 
        <transition name="welcome" to="welcome_decision" />

</state>
            
            

Process is defined in single file, trainstation.jpdl.xml. Its content looks as follows:



        
<?xml version="1.0" encoding="UTF-8"?>

<process name="TrainStationDecisions"  xmlns="urn:jbpm.org:jpdl-3.2">

    <start-state>
        <transition to="welcome" />
    </start-state>

    <state name="welcome">
        
1
        <event type='node-enter'>
            <action class='org.mobicents.example.ss7.ussd.jbpm.USSDPromptHandler'>
                <prompt>Welcome to train station scheduler service. Press following digits for 1) Train schedule.  2) Station location. 3) Discount rates. 4) Promo</prompt>
            </action>
        </event>
            
2
        <transition name="welcome" to="welcome_decision" />

    </state>

    
3
    <decision name="welcome_decision" expr="#{ussd.input}">
    
4
        <handler class="org.mobicents.example.ss7.ussd.jbpm.USSDDecisionHandler" />
        
5
        <transition name="1" to="schedule" />
        <transition name="2" to="location" />
        <transition name="3" to="discount" />
        <transition name="4" to="promo" />
        
6
        <!-- this is default transition to end, it has to be present -->    
        <transition name="end" to="end" />  
        
    </decision>
    <!-- SCHEDULE -->
    <state name="schedule">
    
        <event type='node-enter'>
            <action class='org.mobicents.example.ss7.ussd.jbpm.USSDPromptHandler'>
                <prompt>Please pick city 1) Pune.  2) Test location. 3) Volgograd. 4) End</prompt>
            </action>
        </event>
        <transition name="decide" to="schedule_decision" />
    </state>

    
    <decision name="schedule_decision" expr="#{ussd.input}">
        <handler class="org.mobicents.example.ss7.ussd.jbpm.USSDDecisionHandler" />
        
        <transition name="1" to="schedule_pune" />
        <transition name="2" to="schedule_test" />
        <transition name="3" to="schedule_volgograd" />
        <transition name="4" to="end" />
        <!-- this is default transition to end, it has to be present -->    
        <transition name="end" to="end" />  
    </decision>
    
    <state name="schedule_pune">
    
        <event type='node-enter'>
            <action class='org.mobicents.example.ss7.ussd.jbpm.USSDPromptHandler'>
                <prompt>Trains leave to each destination on each odd hour.</prompt>
                <end>true</end>
            </action>
        </event>
        <transition name="end" to="end" />
    </state>>
    <state name="schedule_test">
    
        <event type='node-enter'>
            <action class='org.mobicents.example.ss7.ussd.jbpm.USSDPromptHandler'>
                <prompt>Test location does not have any means of transport!</prompt>
                <end>true</end>
            </action>
        </event>
        <transition name="end" to="end" />
    </state>
    <state name="schedule_volgograd">
    
        <event type='node-enter'>
            <action class='org.mobicents.example.ss7.ussd.jbpm.USSDPromptHandler'>
                <prompt>Test location does not have any means of transport!</prompt>
                <end>true</end>
            </action>
        </event>
        <transition name="end" to="end" />
    </state>
    
    <!-- LOCATION -->
    <state name="location">
    
        <event type='node-enter'>
            <action class='org.mobicents.example.ss7.ussd.jbpm.USSDPromptHandler'>
                <prompt>Please pick city 1) Pune.  2) Test location. 3) Volgograd. 4) End</prompt>
            </action>
        </event>
        <transition name="decide" to="location_decision" />
    </state>

    
    <decision name="location_decision" expr="#{ussd.input}">
        <handler class="org.mobicents.example.ss7.ussd.jbpm.USSDDecisionHandler" />
        
        <transition name="1" to="location_pune" />
        <transition name="2" to="location_test" />
        <transition name="3" to="location_volgograd" />
        <transition name="4" to="end" />    
        <!-- this is default transition to end, it has to be present -->    
        <transition name="end" to="end" />  
    </decision>
    
    <state name="location_pune">
    
        <event type='node-enter'>
            <action class='org.mobicents.example.ss7.ussd.jbpm.USSDPromptHandler'>
                <prompt>Train station is located at 353, Laxmi Road, Near Vijay Talkies .</prompt>
                <end>true</end>
            </action>
        </event>
        <transition name="end" to="end" />
    </state>
    <state name="location_test">
    
        <event type='node-enter'>
            <action class='org.mobicents.example.ss7.ussd.jbpm.USSDPromptHandler'>
                <prompt>Test location does not have any means of transport!</prompt>
                <end>true</end>
            </action>
        </event>
        <transition name="end" to="end" />
    </state>
    <state name="location_volgograd">
    
        <event type='node-enter'>
            <action class='org.mobicents.example.ss7.ussd.jbpm.USSDPromptHandler'>
                <prompt>Train stations are located at: xxx</prompt>
                <end>true</end>
            </action>
        </event>
        <transition name="end" to="end" />
    </state>
    <!-- DISCOUNT -->
    <state name="discount">
    
        <event type='node-enter'>
            <action class='org.mobicents.example.ss7.ussd.jbpm.USSDPromptHandler'>
                <prompt>There are no DISCOUNT! Pay the price!</prompt>
                <end>true</end>
            </action>
        </event>
        <transition name="end" to="end" />
    </state>
    <!-- PROMO -->
    <state name="promo">
    
        <event type='node-enter'>
            <action class='org.mobicents.example.ss7.ussd.jbpm.USSDPromptHandler'>
                <prompt>You are now trapped in endles loop! Press something!</prompt>
                <end>true</end>
            </action>
        </event>
        <transition name="decide" to="discount_decision" />
    </state>
    <decision name="discount_decision" expr="#{ussd.input}">
        <handler class="org.mobicents.example.ss7.ussd.jbpm.USSDDecisionHandler" />
        
        <transition name="0" to="end" />
        <transition name="end" to="discount" /> 
        
    </decision>
    
    
    <end-state name="end" />

</process>

            
            

XML definition has following, distinct elements:

1

declaration of prompt for current state.

2

declaration of transition to decision element.

3

declaration of decision element.

4

declaration of decision handler.

5

declaration of transitions from decision element.

6

declaration of default transition.

USSD Example SLEE part is very small. It consists of single service and SBB.

SBB reuses USSD gateway library to perform XML encoding/decoding, please refer to gateway documentation for details.

SBB is defined with single, XML file: sbb-jar.xml

It contains following definitions:

SBB ID


        
        <description>XXX</description>
        <sbb-name>USSDSipSbb</sbb-name>
        <sbb-vendor>org.mobicents</sbb-vendor>
        <sbb-version>1.0</sbb-version>
        <sbb-alias>XxX</sbb-alias>
                
                
Library references


        
        <library-ref>
            <library-name>library-ussdgateway</library-name>
            <library-vendor>org.mobicents</library-vendor>
            <library-version>2.0</library-version>
        </library-ref>
        <library-ref>
            <library-name>library-ussdexample</library-name>
            <library-vendor>org.mobicents</library-vendor>
            <library-version>1.0</library-version>
        </library-ref>
                
                
SBB class and CMP


        
        <sbb-classes>
            <sbb-abstract-class>
                <sbb-abstract-class-name>
                    org.mobicents.example.ss7.ussd.SipUSSDSbb
                </sbb-abstract-class-name>
                <cmp-field>
                    <description>Holds active JBPM process instance</description>
                    <cmp-field-name>processInstance</cmp-field-name>
                </cmp-field>
            </sbb-abstract-class>
        </sbb-classes>
                
                
Event handlers


        
        <!-- SLEE -->
        <event event-direction="Receive" initial-event="True">
            <event-name>StartServiceEvent</event-name>
            <event-type-ref>
                <event-type-name>
                    javax.slee.serviceactivity.ServiceStartedEvent
                </event-type-name>
                <event-type-vendor>javax.slee</event-type-vendor>
                <event-type-version>1.1</event-type-version>
            </event-type-ref>
            <initial-event-select variable="ActivityContext" />
        </event>
        <event event-direction="Receive" initial-event="False">
            <event-name>ActivityEndEvent</event-name>
            <event-type-ref>
                <event-type-name>
                    javax.slee.ActivityEndEvent
                </event-type-name>
                <event-type-vendor>javax.slee</event-type-vendor>
                <event-type-version>1.0</event-type-version>
            </event-type-ref>
        </event>


        <!-- initial -->

        <event event-direction="Receive" initial-event="True">
            <event-name>InviteEvent</event-name>
            <event-type-ref>
                <event-type-name>javax.sip.message.Request.INVITE</event-type-name>
                <event-type-vendor>net.java.slee</event-type-vendor>
                <event-type-version>1.2</event-type-version>
            </event-type-ref>
            <initial-event-selector-method-name>
                callIDSelect
            </initial-event-selector-method-name>
        </event>


        <!-- Intermediate: if any -->

        <event event-direction="Receive" initial-event="True">
            <event-name>InfoEvent</event-name>
            <event-type-ref>
                <event-type-name>javax.sip.Dialog.INFO</event-type-name>
                <event-type-vendor>net.java.slee</event-type-vendor>
                <event-type-version>1.2</event-type-version>
            </event-type-ref>
            <initial-event-selector-method-name>
                callIDSelect
            </initial-event-selector-method-name>
        </event>



        <!-- final -->
        <event event-direction="Receive" initial-event="True">
            <event-name>ByeEvent</event-name>
            <event-type-ref>
                <event-type-name>javax.sip.Dialog.BYE</event-type-name>
                <event-type-vendor>net.java.slee</event-type-vendor>
                <event-type-version>1.2</event-type-version>
            </event-type-ref>
            <initial-event-selector-method-name>
                callIDSelect
            </initial-event-selector-method-name>
        </event>

        <event event-direction="Receive" initial-event="True">
            <event-name>CancelEvent</event-name>
            <event-type-ref>
                <event-type-name>javax.sip.message.Request.CANCEL</event-type-name>
                <event-type-vendor>net.java.slee</event-type-vendor>
                <event-type-version>1.2</event-type-version>
            </event-type-ref>
            <initial-event-selector-method-name>
                callIDSelect
            </initial-event-selector-method-name>
        </event>


        <!-- success -->
        <event event-direction="Receive" initial-event="False">
            <event-name>SuccessEvent</event-name>
            <event-type-ref>
                <event-type-name>javax.sip.message.Response.SUCCESS</event-type-name>
                <event-type-vendor>net.java.slee</event-type-vendor>
                <event-type-version>1.2</event-type-version>
            </event-type-ref>
            <initial-event-selector-method-name>
                callIDSelect
            </initial-event-selector-method-name>
        </event>
                
                
Resource Adaptor reference



        <resource-adaptor-type-binding>
            <resource-adaptor-type-ref>
                <resource-adaptor-type-name>
                    JAIN SIP
                </resource-adaptor-type-name>
                <resource-adaptor-type-vendor>
                    javax.sip
                </resource-adaptor-type-vendor>
                <resource-adaptor-type-version>
                    1.2
                </resource-adaptor-type-version>
            </resource-adaptor-type-ref>
            <activity-context-interface-factory-name>
                slee/resources/jainsip/1.2/acifactory
            </activity-context-interface-factory-name>
            <resource-adaptor-entity-binding>
                <resource-adaptor-object-name>
                    slee/resources/jainsip/1.2/provider
                </resource-adaptor-object-name>
                <resource-adaptor-entity-link>
                    SipRA
                </resource-adaptor-entity-link>
            </resource-adaptor-entity-binding>
        </resource-adaptor-type-binding>        
                
                

Source of service is contained in single java compilation unit: org.mobicents.example.ss7.ussd.SipUSSDSbb.java

Aside regular callback methods SLEE requires to be implemented, it has following content:

SIP event handlers are input point for this example. SIP messages (either INVITE or INFO ) carry XML encoded USSD string.



    // initial
    public void onInviteEvent(RequestEvent event, ActivityContextInterface ac) {
        // its initial request
        try {
            DialogActivity da = (DialogActivity) this.provider.getNewDialog(event.getServerTransaction());
            da.terminateOnBye(true);
            ActivityContextInterface daACI = this.acif.getActivityContextInterface(da);
            daACI.attach(this.sbbContext.getSbbLocalObject());
            ProcessInstance pi = jbpmContext.newProcessInstance(PROCESS_NAME);
            this.setProcessInstance(pi);
        } catch (SipException e) {
            e.printStackTrace();
            handleError(event.getServerTransaction(), Response.BAD_REQUEST, e);
            return;
        }
        processUssd(event);
    }
    // intermediate
    public void onInfoEvent(RequestEvent event, ActivityContextInterface ac) {
        processUssd(event);
    }
    // final
    public void onByeEvent(RequestEvent event, ActivityContextInterface ac) {
        // something should be here?
        sendResponse(null, event.getServerTransaction());
    }
    public void onCancelEvent(CancelRequestEvent event, ActivityContextInterface ac) {
        this.provider.acceptCancel(event, false);
    }
    // success
    public void onSuccessEvent(ResponseEvent event, ActivityContextInterface ac) {
        // nothing,
    }
            

USSD string is extracted with JAXB library from USSD gateway and fed into JBPM, it is done as follows:



    private void processUssd(RequestEvent event) {
        //this method is called for INVITE and INFO received by SBB
        // now lets get USSD
        USSDRequest extracted = extractUssd(event);
        if (extracted == null) {
            // error has been handled
            return;
        }
        String drooled = processUssd(extracted);
        // send ok
        if (drooled != null) {
            sendResponse(drooled, event.getServerTransaction());
            if (isSessionDead()) {
                // in this case, send bye over dialog
                sendBye();
            }
        }
    }
    
    private boolean isSessionDead() {
        ProcessInstance pi = this.getProcessInstance();
        if(pi == null || pi.getEnd()!=null)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    private USSDRequest extractUssd(RequestEvent event) {
    
        Request sipRequest = event.getRequest();
        ContentTypeHeader cth = (ContentTypeHeader) event.getRequest()
            .getHeader(ContentTypeHeader.NAME);
        if (cth == null) {
            // FIXME: break
            return null;
        } else {
            if (!cth.getContentType().equals(CONTENT_TYPE) 
                ||  !cth.getContentSubType().equals(CONTENT_SUB_TYPE)
                    || sipRequest.getContent() == null) {
                // break,
                return null;
            }
        }
        try {
            
1
            Unmarshaller uMarshaller = jAXBContext.createUnmarshaller();
            ByteArrayInputStream bis = new ByteArrayInputStream(sipRequest.getRawContent());
            JAXBElement<USSDRequest> data = (JAXBElement<USSDRequest>) uMarshaller.unmarshal(bis);
            return data.getValue();
        } catch (JAXBException e) {
            // FIXME: tear down
            e.printStackTrace();
        }
        return null;
    }
    private String processUssd(USSDRequest extracted) {
        
        
2
        ProcessInstance pi = this.getProcessInstance();
        
3
        pi.getContextInstance().setVariable(USSDDecisionHandler._INPUT_, extracted.getUssdString());
        
4
        pi.signal();
        
5
        String data = (String) pi.getContextInstance().getVariable(USSDPromptHandler._PROMPT_);
        
6
        USSDResponse response = this.objectFactory.createUSSDResponse();
        response.setInvokeId(extracted.getInvokeId());
        response.setUssdCoding(extracted.getUssdCoding());
        response.setUssdString(data);
        response.setEnd(isSessionDead());
        response.setLastResult(true);
        
7
        try {
            Marshaller marshaller = jAXBContext.createMarshaller();
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            JAXBElement<USSDResponse> res = this.objectFactory.createResponse(response);
            marshaller.marshal(res, bos);
            return new String(bos.toByteArray());
        } catch (JAXBException e) {
    
            e.printStackTrace();
        }
        return null;
    }
            

1

Unmarshall JAXB pojo from SIP message content, if present.

2

Fetch JBPM process instance from CMP.

3

Set process instance context variable to received USSD string.

4

Signal process instance to transit to another state(to decision element actually).

5

Fetch context instance variable with prompt content.

6

Create JAXB pojo based on received request (JAXB pojo) and USSD prompt.

7

Marshal USSDResponse to XML string in order to send it back with SIP response.

Revision History
Revision 1.0Wed June 2 2010Bartosz Baranowski
Creation of the Mobicents JAIN SLEE USSD Example User Guide.