kcb.billerengine.utils.Utils.java Source code

Java tutorial

Introduction

Here is the source code for kcb.billerengine.utils.Utils.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package kcb.billerengine.utils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import javax.crypto.Mac;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import kcb.billerengine.isoserver.PackagerFactory;
import org.apache.commons.codec.binary.Base64;
import org.jboss.logging.Logger;
import org.jpos.iso.ISOException;
import org.jpos.iso.ISOMsg;
import org.jpos.iso.ISOPackager;

/**
 *
 * @author NKSONGOK
 */
public class Utils {

    private static final Logger LOG = Logger.getLogger("Utils");

    private static final String PU = "BilllerEnginePU";

    public String getPU() {
        return PU;
    }

    public static String removeSpeacialXters(String myString) {
        myString = myString.replaceAll("!", "");
        myString = myString.replaceAll("\"", "");
        myString = myString.replaceAll("#", "");
        myString = myString.replaceAll("&", "");
        myString = myString.replaceAll("%", "");
        myString = myString.replaceAll("$", "");
        myString = myString.replaceAll("~", "");
        myString = myString.replaceAll("`", "");
        myString = myString.replaceAll("^", "");
        myString = myString.replaceAll("\\*", "");
        myString = myString.replaceAll("\\(", "");
        myString = myString.replaceAll("\\)", "");
        myString = myString.replaceAll("_", "");
        myString = myString.replaceAll("-", "");
        myString = myString.replaceAll("=", "");
        myString = myString.replaceAll("\\+", "");
        myString = myString.replaceAll("]", "");
        myString = myString.replaceAll("\\[", "");
        myString = myString.replaceAll("\\{", "");
        myString = myString.replaceAll("\\}", "");
        myString = myString.replaceAll("|", "");
        myString = myString.replaceAll("'", "");
        myString = myString.replaceAll(";", "");
        myString = myString.replaceAll(":", "");
        myString = myString.replaceAll("\\?", "");
        myString = myString.replaceAll("\\.", "");
        myString = myString.replaceAll(">", "");
        myString = myString.replaceAll("<", "");
        myString = myString.replaceAll(",", "");
        myString = myString.replaceAll("/", "");
        myString = myString.replaceAll("@", "");

        return myString;

    }

    public static void logISOMsg(ISOMsg msg) {
        LOG.info("===============ISO MESSAGE===================");
        try {
            System.out.println("  MTI : " + msg.getMTI());
            for (int i = 1; i <= msg.getMaxField(); i++) {
                if (msg.hasField(i)) {
                    System.out.println("    Field-" + i + " : " + msg.getString(i));
                }
            }
        } catch (ISOException e) {
            e.printStackTrace();
        } finally {
            LOG.info("===============================================");
        }
    }

    public ISOMsg unpackRequest(String message) throws ISOException {
        ISOPackager packager = PackagerFactory.getPackager();
        ISOMsg isoMsg = null;
        isoMsg = new ISOMsg();
        isoMsg.setPackager(packager);
        isoMsg.unpack(message.getBytes());
        isoMsg.dump(System.out, " ");
        return isoMsg;
    }

    public static String getStringFromInputStream(InputStream is) {
        BufferedReader br = null;
        StringBuilder sb = new StringBuilder();
        String line;
        try {
            br = new BufferedReader(new InputStreamReader(is));
            while ((line = br.readLine()) != null) {
                sb.append(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (br != null) {
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return sb.toString();
    }

    public static String computeToken(String key, String data) {
        String token = null;
        try {
            SecretKey secretKey = null;
            byte[] keyBytes = key.getBytes("UTF-8");
            Mac mac = Mac.getInstance("HMACSHA256");
            secretKey = new SecretKeySpec(keyBytes, mac.getAlgorithm());
            mac.init(secretKey);
            byte[] text = data.getBytes("UTF-8");
            byte[] encodedText = mac.doFinal(text);

            token = new String(Base64.encodeBase64(encodedText)).trim();
        } catch (UnsupportedEncodingException | InvalidKeyException | NoSuchAlgorithmException ex) {
            LOG.error("ComputeToken error : " + ex);
        }

        return token;
    }

}