FileDocCategorySizeDatePackage
PPSMSAdapter.javaAPI DocphoneME MR2 API (J2ME)11771Wed May 02 18:00:44 BST 2007com.sun.j2me.payment

PPSMSAdapter

public class PPSMSAdapter extends PaymentAdapter
This Premium Priced SMS (PPSMS) Adapter sends messages to a PPSMS number, which defines a payment model. The value of a transaction is determined by the premium priced number to which the SMS is sent and/or by the content described in the body of that SMS.

Fields Summary
private static final int
SMS_LENGTH
private static SecurityToken
classSecurityToken
This class has a different security domain than the MIDlet suite
Constructors Summary
private PPSMSAdapter()
Creates a new instance of PPSMSAdapter

    
Methods Summary
public java.lang.StringgetDisplayName()
Gets a display name of this adapter.

return
the display name

        return "Premium Priced SMS";
    
public static com.sun.j2me.payment.PPSMSAdaptergetInstance(java.lang.String configuration)
Gets a new instance of this adapter.

param
configuration configuration info of PPSMSAdapter
return
the new instance of PPSMSAdapter

        return new PPSMSAdapter();
    
public static voidinitSecurityToken(SecurityToken token)
Initializes the security token for this class, so it can perform actions that a normal MIDlet Suite cannot.

param
token security token for this class.

    
                                  
         
        if (classSecurityToken != null) {
            return;
        }

        classSecurityToken = token;
    
private java.lang.Object[]parseAndValidateSpecInfo(java.lang.String paySpecificInfo)
Parses and validates specific payment information from input parameter

param
paySpecificInfo the string representing comma-separated specific payment information
return
array of specific information if all information is valid, otherwise null

        Object[] info = null;
        int firstIndex = paySpecificInfo.indexOf(',");
        
        if (firstIndex == -1) {
            return null;
        }
        
        int lastIndex = paySpecificInfo.lastIndexOf(',");
        String msisdn = paySpecificInfo.substring(0, firstIndex).trim();
        
        // check if msisdn is in right format
        try {
            long i = msisdn.startsWith("+")
            ? Long.parseLong(msisdn.substring(1))
            : Long.parseLong(msisdn);
        } catch (NumberFormatException nfe) {
            return null;
        }
        
        String prefix = "";
        String smsCount = null;
        
        // no number of messages parameter
        if (firstIndex == lastIndex) {
            prefix = paySpecificInfo.substring(firstIndex + 1,
                    paySpecificInfo.length()).trim();
        } else {
            prefix = paySpecificInfo.substring(firstIndex + 1,
                    lastIndex).trim();
            
            smsCount = paySpecificInfo.substring(lastIndex + 1,
                    paySpecificInfo.length()).trim();
        }
        
        // check if prefix meets all conditions described in specification
        if (!prefix.equals("")) {
            int maxLength = 8;
            if (prefix.startsWith("0x") || prefix.startsWith("0X")) {
                if (prefix.length() % 2 != 0) {
                    // the heximal value must have an even length
                    return null;
                }
                try {
                    prefix = prefix.substring(2);
                    // if prefix == 0xFFFFFFFFFFFFFFFF (allowed value),
                    // Long.parseLong would throw an exception, thus the
                    // string prefix has to be divided up
                    if (prefix.length() >= maxLength) {
                       Long.parseLong(prefix.substring(0, 7), 16);
                       Long.parseLong(prefix.substring(8), 16);
                    } else {
                        Long.parseLong(prefix, 16);
                    }
                    maxLength = 16;
                } catch (NumberFormatException nfe) {
                    return null;
                }
            }
            
            if (prefix.length() > maxLength) {
                return null;
            }
        }
        
        if (smsCount == null) {
            info = new Object[2];
        } else {
            info = new Object[3];
            try {
                info[2] = Integer.valueOf(smsCount);
            } catch (NumberFormatException nfe) {
                return null;
            }
        }
        
        info[0] = msisdn;
        info[1] = prefix;
        
        return info;
    
public Transactionprocess(Transaction transaction)
Processes the given transaction, updates its state and returns the same transaction instance or a new one (an instance of a Transaction subclass), which is based on the old transaction, but adds more (adapter specific) information to it.

param
transaction the transaction to be processed
return
the transaction after processing

        
        switch (transaction.getState()) {
            case Transaction.ASSIGNED:
                Thread thread = new PPSMSThread(transaction);
                transaction.setWaiting(true);
                thread.start();
                break;
                
            default:
                return super.process(transaction);
        }
       
        return transaction;
    
public voidvalidatePriceInfo(double price, java.lang.String paySpecificPriceInfo)
Validates the price information which are specified in the application manifest file for the provider handled by this adapter. It throws an PaymentException if the parameters are incorrect.

param
price the price to pay when using this provider
param
paySpecificPriceInfo the specific price information string from the manifest
throws
PaymentException if the provided information is correct

        
        if ((paySpecificPriceInfo == null) ||
                (paySpecificPriceInfo.length() == 0) ||
                parseAndValidateSpecInfo(paySpecificPriceInfo) == null) {
            throw new PaymentException(
                    PaymentException.INVALID_PRICE_INFORMATION, 
                    paySpecificPriceInfo);
        }