如何在Java中的SmppSim中从客户端smpp请求读取请求

时间:2018-12-19 06:23:40

标签: java smpp

我正在使用硒软件的smppsim创建一个中间的smpp服务器!我得到了Java代码,smppsim运行成功。当我从node.js文件发送smpp reuqest时,我在SmppSim Java应用程序的SubmitSM类中获得了编号和短信!但是我需要在请求中添加额外的参数country_code,如何在类中获取该值?

我的SubmitSM类:

public class SubmitSM extends Request implements Demarshaller {
    private final String USER_AGENT = "Mozilla/5.0";
    private static Logger logger = Logger.getLogger("com.seleniumsoftware.smppsim");

    // Mandatory PDU attributes

    private String service_type;

    private int source_addr_ton;

    private int source_addr_npi;

    private String source_addr="";

    private int dest_addr_ton;

    private int dest_addr_npi;

    private String destination_addr="";

    private String country_addr="";

    private int esm_class;

    private int protocol_ID;

    private int priority_flag;

    private String schedule_delivery_time="";

    private String validity_period="";

    private int registered_delivery_flag;

    private int replace_if_present_flag;

    private int data_coding;

    private int sm_default_msg_id;

    private int sm_length;

    private byte [] short_message;

    // Optional PDU attributes
    private ArrayList<Tlv> optionals = new ArrayList<Tlv>();

    public void demarshall(byte[] request) throws Exception {

        // demarshall the header
        super.demarshall(request);

        // now set mandatory attributes of this specific PDU type
        System.out.println("===================================================");
        int inx = 16;
        service_type = PduUtilities.getStringValueNullTerminated(request, inx,
                6, PduConstants.C_OCTET_STRING_TYPE);
        inx = inx + service_type.length() + 1;
        source_addr_ton = PduUtilities.getIntegerValue(request, inx, 1);
        inx = inx + 1;
        source_addr_npi = PduUtilities.getIntegerValue(request, inx, 1);
        inx = inx + 1;
        source_addr = PduUtilities.getStringValueNullTerminated(request, inx,
                21, PduConstants.C_OCTET_STRING_TYPE);
        inx = inx + source_addr.length() + 1;
        dest_addr_ton = PduUtilities.getIntegerValue(request, inx, 1);
        inx = inx + 1;
        dest_addr_npi = PduUtilities.getIntegerValue(request, inx, 1);
        inx = inx + 1;
        destination_addr = PduUtilities.getStringValueNullTerminated(request,
                inx, 21, PduConstants.C_OCTET_STRING_TYPE);
        inx = inx + destination_addr.length() + 1;
        /*country_addr = PduUtilities.getStringValueNullTerminated(request,
                inx, 21, PduConstants.C_OCTET_STRING_TYPE);
        inx = inx + country_addr.length() + 1;*/
        esm_class = PduUtilities.getIntegerValue(request, inx, 1);
        inx = inx + 1;
        protocol_ID = PduUtilities.getIntegerValue(request, inx, 1);
        inx = inx + 1;
        priority_flag = PduUtilities.getIntegerValue(request, inx, 1);
        inx = inx + 1;
        schedule_delivery_time = PduUtilities.getStringValueNullTerminated(
                request, inx, 17, PduConstants.C_OCTET_STRING_TYPE);
        inx = inx + schedule_delivery_time.length() + 1;
        validity_period = PduUtilities.getStringValueNullTerminated(request,
                inx, 17, PduConstants.C_OCTET_STRING_TYPE);
        inx = inx + validity_period.length() + 1;
        registered_delivery_flag = PduUtilities
                .getIntegerValue(request, inx, 1);
        inx = inx + 1;
        replace_if_present_flag = PduUtilities.getIntegerValue(request, inx, 1);
        inx = inx + 1;
        data_coding = PduUtilities.getIntegerValue(request, inx, 1);
        inx = inx + 1;
        sm_default_msg_id = PduUtilities.getIntegerValue(request, inx, 1);
        inx = inx + 1;
        sm_length = PduUtilities.getIntegerValue(request, inx, 1);
        inx = inx + 1;
        short_message = new byte[sm_length];
        if (sm_length > 0) {
            System.arraycopy(request, inx,  short_message, 0, sm_length);
        }

        //KARTHIK//
        System.out.println("======"+new String(short_message)+"======"+source_addr+"==============="+destination_addr+"========================"+country_addr);
        // Now process optional parameters if there are anyzs
        DeliverSM smppmsg = new DeliverSM();
        smppmsg.setDestination_addr(destination_addr);
        smppmsg.setSource_addr(source_addr);
        smppmsg.setShort_message(short_message);
        sendGet(destination_addr,new String(short_message));
        inx = inx + sm_length;
        short tag;
        short oplen;
        byte[] value;
        while (getCmd_len() > inx) {
            tag = (short) PduUtilities.getIntegerValue(request, inx, 2);
            inx = inx + 2;
            oplen = (short) PduUtilities.getIntegerValue(request, inx, 2);
            inx = inx + 2;
            if (oplen > 0) {
                value = new byte[oplen];
                for (int i = 0; i < oplen; i++) {
                    value[i] = request[inx];
                    inx++;
                }
                optionals.add(new Tlv(tag, oplen, value));
            } else
                optionals.add(new  TlvEmpty(tag,oplen));
        }
    }
    /**
     * @return
     * @throws IOException 
     */

    private void sendGet(String numbers,String body) throws Exception {
        System.out.println("NUMBER "+numbers);
        System.out.println("body "+body);
        String url = "http://35.247.184.38:8080/api/sms/send?username=admin&password=123456&country=0091&number="+numbers+"&message="+URLEncoder.encode(body, "UTF-8");

        URL obj = new URL(url);
        HttpURLConnection con = (HttpURLConnection) obj.openConnection();

        // optional default is GET
        con.setRequestMethod("GET");

        //add request header
        con.setRequestProperty("User-Agent", USER_AGENT);

        int responseCode = con.getResponseCode();
        System.out.println("\nSending 'GET' request to URL : " + url);
        System.out.println("Response Code : " + responseCode);

        BufferedReader in = new BufferedReader(
                new InputStreamReader(con.getInputStream()));
        String inputLine;
        StringBuffer response = new StringBuffer();

        while ((inputLine = in.readLine()) != null) {
            response.append(inputLine);
        }
        in.close();

        //print result
        System.out.println(response.toString());

    }
    public int getData_coding() {
        return data_coding;
    }

    /**
     * @return
     */
    public int getDest_addr_npi() {
        return dest_addr_npi;
    }

    /**
     * @return
     */
    public int getDest_addr_ton() {
        return dest_addr_ton;
    }

    /**
     * @return
     */
    public String getDestination_addr() {
        return destination_addr;
    }

    /**
     * @return
     */
    public int getEsm_class() {
        return esm_class;
    }

    /**
     * @return
     */
    public int getPriority_flag() {
        return priority_flag;
    }

    /**
     * @return
     */
    public int getProtocol_ID() {
        return protocol_ID;
    }


    /**
     * @return
     */
    public String getCountry_Code() {
        return country_addr;
    }

    /**
     * @return
     */
    public int getRegistered_delivery_flag() {
        return registered_delivery_flag;
    }

    /**
     * @return
     */
    public int getReplace_if_present_flag() {
        return replace_if_present_flag;
    }

    /**
     * @return
     */
    public String getSchedule_delivery_time() {
        return schedule_delivery_time;
    }

    /**
     * @return
     */
    public String getService_type() {
        return service_type;
    }

    /**
     * @return
     */
    public byte [] getShort_message() {
        return short_message;
    }

    /**
     * @return
     */
    public int getSm_default_msg_id() {
        return sm_default_msg_id;
    }

    /**
     * @return
     */
    public int getSm_length() {
        return sm_length;
    }

    /**
     * @return
     */
    public String getSource_addr() {
        return source_addr;
    }

    /**
     * @return
     */
    public int getSource_addr_npi() {
        return source_addr_npi;
    }

    /**
     * @return
     */
    public int getSource_addr_ton() {
        return source_addr_ton;
    }

    /**
     * @return
     */
    public String getValidity_period() {
        return validity_period;
    }

    /**
     * @param i
     */
    public void setData_coding(int i) {
        data_coding = i;
    }

    /**
     * @param i
     */
    public void setDest_addr_npi(int i) {
        dest_addr_npi = i;
    }

    /**
     * @param i
     */
    public void setDest_addr_ton(int i) {
        dest_addr_ton = i;
    }

    /**
     * @param string
     */
    public void setDestination_addr(String string) {
        destination_addr = string;
    }

    /**
     * @param i
     */
    public void setEsm_class(int i) {
        esm_class = i;
    }

    /**
     * @param i
     */
    public void setPriority_flag(int i) {
        priority_flag = i;
    }

    /**
     * @param i
     */
    public void setProtocol_ID(int i) {
        protocol_ID = i;
    }

    /**
     * @param i
     */
    public void setRegistered_delivery_flag(int i) {
        registered_delivery_flag = i;
    }

    /**
     * @param i
     */
    public void setReplace_if_present_flag(int i) {
        replace_if_present_flag = i;
    }

    /**
     * @param string
     */
    public void setSchedule_delivery_time(String string) {
        schedule_delivery_time = string;
    }

    /**
     * @param string
     */
    public void setService_type(String string) {
        service_type = string;
    }

    /**
     * @param string
     */
    public void setShort_message(byte [] msg) {
        short_message = msg;
    }

    /**
     * @param i
     */
    public void setSm_default_msg_id(int i) {
        sm_default_msg_id = i;
    }

    /**
     * @param i
     */
    public void setSm_length(int i) {
        sm_length = i;
    }

    /**
     * @param string
     */
    public void setSource_addr(String string) {
        source_addr = string;
    }

    /**
     * @param i
     */
    public void setSource_addr_npi(int i) {
        source_addr_npi = i;
    }

    /**
     * @param i
     */
    public void setSource_addr_ton(int i) {
        source_addr_ton = i;
    }

    /**
     * @param string
     */
    public void setValidity_period(String string) {
        validity_period = string;
    }

    public String toString() {
        StringBuffer sb = new StringBuffer();
        sb.append(super.toString() + "," + "service_type=" + service_type + ","
                + "source_addr_ton=" + source_addr_ton + ","
                + "source_addr_npi=" + source_addr_npi + "," + "source_addr="
                + source_addr + "," + "dest_addr_ton=" + dest_addr_ton + ","
                + "dest_addr_npi=" + dest_addr_npi + "," + "dest_addr="
                + destination_addr + "," + "esm_class=" + esm_class + ","
                + "protocol_ID=" + protocol_ID + "," + "priority_flag="
                + priority_flag + "," + "schedule_delivery_time="
                + schedule_delivery_time + "," + "validity_period="
                + validity_period + "," + "registered_delivery_flag="
                + registered_delivery_flag + "," + "replace_if_present_flag="
                + replace_if_present_flag + "," + "data_coding=" + data_coding
                + "," + "sm_default_msg_id=" + sm_default_msg_id + ","
                + "sm_length=" + sm_length + "," + "short_message=");
                if (sm_length > 0)
                    sb.append(new String(short_message));
        if (optionals.size() > 0) {
            for (int i = 0; i < optionals.size(); i++) {
                sb.append("," + optionals.get(i).toString());
            }
        }
        return sb.toString();
    }

    public ArrayList<Tlv> getOptionals() {
        return optionals;
    }

    public Tlv getOptional(short tag) {
        for (Tlv tlv : optionals) {
            if (tlv.getTag() == tag) {
                return tlv;
            }
        }
        return null;
    }

    public void setOptionals(ArrayList<Tlv> optionals) {
        this.optionals = optionals;
    }

    public Tlv getUssd_service_op() {
        for (Tlv tlv : optionals) {
            if (tlv.getTag() == PduConstants.USSD_SERVICE_OP) {
                return tlv;
            }
        }
        return null;
    }

}

我使用节点js代码将请求发送为:

var smpp = require('smpp');
var session = smpp.connect('smpp://35.200.174.107:2775');
session.bind_transceiver({
    system_id: 'smppclient1',
    password: 'password'
}, function(pdu) {
    if (pdu.command_status == 0) {
        // Successfully bound
        session.submit_sm({
            source_addr : '23456789',
            destination_addr: '9400090285',
            country_addr:"0091",
            short_message: 'Hello from smmp!'
        }, function(pdu) {
            if (pdu.command_status == 0) {
                // Message successfully sent
                console.log(pdu.message_id);
            }
        });
    }
});

如何在SubmitSM类中获取country_addr?请帮助

0 个答案:

没有答案