LogoLogo
  • Documentation
  • PRODUCT REFERENCE
    • Introduction
    • Concepts and Terminology
      • Console
      • Servers (X Agents)
      • Projects
      • Configurations
      • Rules Editor
      • Rules, rule catalogue, protocol rules and rule sets
      • Test Data
      • Trace Data
      • Flight Recorders
      • Case Managers
      • Data Files
      • Content Files
      • Performance Data
      • Extensions
      • Protocols
      • Credential Vault
      • Custom Functions
      • Databases
      • Input Adaptors
      • Users
      • User Roles
      • Access Rules
      • Repositories
      • Audit Log
      • Proxies
    • Architectural Scenarios
      • Command and Control
      • Simplest Form
      • Servlet Filter
      • API Transformation
      • Active Web Proxy
      • Web Application Server
      • Active Proxy With Content
      • Mobile Application Server
      • Asynchronous Multi-Protocol
      • Data Loss Prevention Architecture
    • Getting Started
      • Login
      • Essential Things to do First
      • Keeping the Product Current
      • Common Console Management Tasks
      • Viewing Active Servers
    • Quick Product Introduction
      • Preparing the Browser Proxy
      • Setting up the Proxy in the Browser
      • Verifying the Browser Configuration
      • Understanding the Configuration
      • Understanding input and variables
      • Preparing a new repository
      • Locating the Page to Modify
      • Determining the Actions Required
      • Building the First Rule Set
      • Setting Rule Properties
      • Connecting up the First Rule
      • Getting a Server Result
      • Manipulating the Server Result
      • Returning the Result to the User
      • Creating a Configuration for the Rule Set
      • Selecting the Input Source
      • Deploying the New Configuration
      • Testing the Rules
    • X Agent details
      • Configuration Settings
      • Rule Sets
      • Tips and Techniques for Working with the Rules Editor
      • Web Application Rule Set Patterns
      • Building Rule Sets for Inclusion
      • Notes on Working with SOAP
      • Zero Installation Rules Testing
    • Data Files
    • Content Files
      • Previewing Content Files
      • Editing Content Files
      • Deploying Content Files
    • Test Data
      • Uploading and Downloading Test Data
    • Performance Data
      • Setting the Performance Collection Level
      • Retrieving the Performance Data
      • Viewing Transaction Counts
      • Viewing Transaction Counts and Inline Time
      • Viewing the Complete Performance Report
      • Understanding the URI Performance Information
      • Performance Benchmarks
    • Trace Data
      • Understanding the Trace
    • Tracing the Result of a System Failure
    • Conjoined Performance and Trace data
    • Live Performance Data and Probes
      • Setting Probes
      • Live Performance Impact Considerations
    • Accessing Server Logs
      • Viewing a log
    • Working with Flight Recorders
      • Searching Flight Recorder Information
      • Retrieving Flight Recorder Data
      • Graphing Flight Recorder Data
    • Working with Case Managers
      • Case Manager Definitions
      • Navigating the Case Managers
      • Creating a Case From Rules
      • Creating a Case From the Console
      • Viewing the Queues
      • Picking a Task
      • Case View
      • Completing a Task
      • Task History
      • Searching Tasks
      • Searching Cases
    • Managing Repositories
      • Moving Files Between Repositories
      • Copying Configurations and Rule Sets Together
      • Copying Dependent Rule Sets
      • Backing up
      • Moving Entire Repositories Between Consoles
      • Setting Repository Imports
      • Specifying Repository Rule Group Restrictions
    • Version Control and Restoring Files
    • Using the Portal UI
      • Using the Start Menu
      • Positioning Windows
      • Resizing Windows
      • Maximize, Restore and Minimize
      • Storing and Switching Desktops
    • Enterprise Level Projects
      • Creating a Project
      • Working with Tasks
      • Closing a Task
      • Closing a Project
      • Project Assistants and Work Output
    • Administration Functions
      • Extensions
      • Custom Function Setup
      • Setting up a Database
      • Input Adaptors
      • Credential Vault
      • Log Adaptors
      • Server Definitions
      • Managing Large Clusters
      • Setting up the Flight Recorder in the Console
      • Creating a Case Manager Definition
      • Work Output
      • Project Definition
      • Managing Users
      • Managing User Roles
      • Managing Access Rules
      • Authenticating via LDAP
      • Authenticating via SAML
      • Audit Log
    • Installation and Configuration
      • Installing on macOS
      • Installing on Red Hat Enterprise Linux
      • Installing on Windows - Quick Start
      • System Requirements
      • Server Installation
      • Understanding the Console Deployment Structure
      • Console Server Type Configuration
      • Console Server Configuration
      • Removing Other Unnecessary Components
      • Installing Inline with an Existing Application
      • Installing a Stand-alone Multi-Protocol Feed Server
      • Installing a Stand-alone Built in Forwarding Proxy
      • Creating a Cluster Slave Console Instance
      • Understanding the magic.properties Configuration Settings
      • Port Numbers and how to Change Them
      • Dealing with Internal Failures
      • Linking Multiple Applications Together into a Single Server
      • Monitoring by External Systems
      • Installation Considerations for Portals
      • Docker Configuration Guide
      • AWS User Deployment Guide
      • Google Cloud User Deployment Guide
    • Creating a Stand-alone Built in Forwarding Proxy
      • Installing with Ubuntu and mySQL
    • Setting up a DNS override for Data Loss Prevention
      • Deciding what to Override
      • Installing a DNS Server for the Overrides
      • Setting the Override
      • SSL Considerations
    • Backup and Restore
      • The Server Component
      • The Console Component
    • Third Party Software
      • Open Source Components
      • Closed Source Licensed Components
      • Licenses
  • PROGRAMMER'S GUIDE
    • Composable Agentic Platform Concepts
    • Setting up a Development Environment
    • Creating a Rule
    • The Kapow Extension - Example
    • Java Documentation
  • CONSOLE WIZARDS
    • Create REST with JSON service
    • Create JDBC database definitions
    • Create data set maintenance custom function
  • BEST PRACTICES
    • Naming Conventions
    • Comments & Descriptions
    • Application Configuration
    • Rules & Rule Sets
    • Repositories
    • Data Manipulation
    • Internationalization
    • Debugging
    • Performance Testing
    • Tag Replacement Techniques
    • Best Practices Repository
  • GUIDES
    • Hello, World!
    • TCL Script Writer Reference
    • Windows Automation Reference
    • Browser Certificate Installation Guide
    • Web Development Guide
    • Using the Push Notification Framework
    • Raspberry Pi with PiFace Reference
    • TomorrowX Portal User Guide
    • Examples
      • CSRF attack prevention
      • Two Factor Authentication
      • Frame Busting
      • Google Analytics
      • DNS Multi Protocol
      • Customer Satisfaction Survey
  • CHANGE HISTORY
    • New in Version 8
    • New in Version 7
    • New in Version 6
    • New in Version 5
    • New In Version 3.1
    • New In Version 3.0
  • 🌏TIME ZONES
  • ❓TROUBLESHOOTING
    • DB2 SQL error: SQLCODE: -443
    • Error 404 when trying to access the console
    • java.io.IOException: Too many open files
    • Accidental deploy to console server
  • ⚙️BROWSE COMPONENTS
  • EULSA
Powered by GitBook
On this page
  • KapowSMS.java
  • rules.xml
  • errors.properties
Export as PDF
  1. PROGRAMMER'S GUIDE

The Kapow Extension - Example

This example shows the full source code, error messages and manifest for the Kapow SMS gateway extension.

KapowSMS.java

/* Copyright 2024 TomorrowX Pty Ltd.
 *
 * This is licensed software. 
 * 
 * You must not distribute, alter or copy this code unless you have a current and valid 
 * Tomorrow Software license. Where you have a current and valid license, you may use 
 * this code as an example for further rules development only for use within your organisation.
 * 
 * You must not distribute or allow to be distributed whether by act or omission this code 
 * to any third party without written permission from the Chief Executive Officer of 
 * TomorrowX Pty Ltd.
 *  
 * To the extent permitted by law or agreed to in writing by the Chief Executive Officer of 
 * TomorrowX Pty Ltd, software distributed under the license is distributed 
 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
 *  
 * See the license agreement for the specific language governing permissions and limitations 
 * under the license.
 */

package software.tomorrow.rules.rules;

import software.tomorrow.rules.base.ChainPoint;
import software.tomorrow.rules.base.Encoding;
import software.tomorrow.rules.base.HttpBrowser;
import software.tomorrow.rules.base.HttpResponseAsString;
import software.tomorrow.rules.base.LogAdapter;
import software.tomorrow.rules.base.Rule;
import software.tomorrow.rules.base.VariableAttributeObject;

public class KapowSMS extends Rule {

	private String messageAttr;
	private int messageType;
	private String phoneAttr;
	private int phoneType;
	
	private String user;
	private String password;
	
	
	/**
	 * Initialize the rule upon load of the rule set
	 */
	public void initialize() {
		// Read the user ID and password from the credentials vault
		user = getCredentialsValue("KapowSMS","UserID");
		password = getCredentialsValue("KapowSMS","Password");
		
		// Check that the credentials are available
		if (user==null || password==null) {
			String[] errorInfo = { };
			setError("KPOW001", errorInfo);
		}

		// Read the message
		messageAttr = (String)getProperties().get("MessageAttribute");
		if (messageAttr==null || messageAttr.trim().equals("")) {
			String[] errorInfo = { };
			setError("KPOW003", errorInfo);
		}
		// Determine the type of value used. It can be a variable or a String in quotes
		messageType = getValueType(messageAttr);
		if (messageType==VAR_STRING) {
			messageAttr = getValueString(messageAttr);
		}

		// Read the phone number
		phoneAttr = (String)getProperties().get("PhoneAttribute");
		if (phoneAttr==null || phoneAttr.trim().equals("")) {
			String[] errorInfo = { };
			setError("KPOW004", errorInfo);
		}
		// Determine the type of value used. It can be a variable or a String in quotes
		phoneType = getValueType(phoneAttr);
		if (phoneType==VAR_STRING) {
			phoneAttr = getValueString(phoneAttr);
		}
		
	}
	
	/**
	 * Clean up resources upon unload of the rule set
	 */
	public void cleanup() {
		// Nothing here
	}

	/**
	 * Process the rule and send the provided message to Vodafone via it's Kapow service
	 * @return The updated VariableAttributeObject
	 */
	public VariableAttributeObject processRule(VariableAttributeObject vao, ChainPoint source) 
					throws Throwable {

		// Default to the error chain point
		ChainPoint cp = (ChainPoint)getRuleChainPoints().get("Failed");

		// Late determination of the value type. If it is a variable (not a constant), 
		// we must retrieve it now.
		String messageInp = messageAttr;
		if (messageType==VAR_VARIABLE) {
			messageInp = vao.getAttribute(messageAttr);
		}
		
		// Late determination of the value type. If it is a variable (not a constant), 
		// we must retrieve it now.
		String phone = phoneAttr;
		if (phoneType==VAR_VARIABLE) {
			phone = vao.getAttribute(phoneAttr);
		}
		
		// Prepare to send the SMS. It can be a real SMS or just a simulation on the test server
		if ((getRunType() & Rule.RUNTYPE_SEND_ALERTS)==0) {
			// This is a simulation - just let it through
			writeConsole("SMS: "+messageInp+" >> "+phone);
			cp = (ChainPoint)getRuleChainPoints().get("OK");
		} else {
			// Send SMS via Vodafone Kapow
			// Escape the message, so that special characters get encoded
			String message = Encoding.escape(messageInp);
			
			// Strip any special characters from the phone number
			StringBuffer no = new StringBuffer();
			for (int loop=0; loop<phone.length(); loop++) {
				char nc = phone.charAt(loop);
				if (nc=='+' || nc=='0' || nc=='1' || nc=='2' || nc=='3' || nc=='4' 	
					|| nc=='5' || nc=='6' || nc=='7' || nc=='8' || nc=='9') {
					no.append(nc);
				}
			}
			phone = no.toString();
				
			// Prepare the URL for sending
			HttpBrowser browser = new HttpBrowser(getRuleSet().getRulesEngine());
			try {
				HttpResponseAsString rsp = browser.doHttpGet("http://www.kapow.co.uk/scripts/sendsms.php?username="
						+user+"&password="+password+"&mobile="+phone+"&sms="+message,"UTF-8");
				browser.close();


				// Check if the reply contains an OK message
			    if (rsp.getResponse().indexOf("OK")>-1) {
					cp = (ChainPoint)getRuleChainPoints().get("OK");
			    } else {
					vao.setAttribute("ERROR",rsp.getResponse());
					writeConsole("WARNING: Kapow SMS fail >> "+phone+" >> "+rsp.getResponse());
			    }
				
			} catch(Exception ex) {
				// In case of an exception, ignore and set the ERROR attribute to the exception
				vao.setAttribute("ERROR",ex.toString());
				writeConsole("WARNING: Kapow SMS fail >> "+phone+" >> "+ex.toString());
				// Also log it
				String[] errorInfo = { "\""+ex.getLocalizedMessage()+"\"" };
				log(LogAdapter.LOGLEVEL_WARNING,renderMessage("BASE070", errorInfo),ex,this,null);
			} finally {
				browser.close();
			}
		}
		// Chain through to the correct chain point
		if (cp!=null) vao = cp.chain(vao,this);
		return vao;
	}
	
}

rules.xml

<?xml version="1.0" encoding="UTF-8"?>
	<rulesCatalogue> 
	 	<rule name="Send Kapow SMS" ruleClass="software.tomorrow.rules.rules.KapowSMS" image="sms.gif" addChainPoints="false" note="This rule sends an SMS to a nominated phone number using the Kapow service. Please note that for this rule to work, credentials must be set up in the credentials vault. The key for the credentials is 'KapowSMS' and the required fields are 'UserID' and 'Password'." group="Alert">
	 		<attribute name="MessageAttribute" label="Message Variable" type="text" quote="true"/>
	 		<attribute name="PhoneAttribute" label="Phone Number Variable" type="text" quote="true"/>
	 		<chainpoint name="OK" label="OK"/>
	 		<chainpoint name="Failed" label="Failed"/>
	 	</rule>
	</rulesCatalogue>

errors.properties

; Error codes for Kapow SMS
KPOW001=The rule &1 in ruleset &2 does not have the KapowSMS credentials correctly set in the credential vault.
KPOW003=The rule &1 in ruleset &2 is missing a message variable
KPOW004=The rule &1 in ruleset &2 is missing a phone number variable
PreviousCreating a RuleNextJava Documentation

Last updated 11 months ago