All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.github.atkawa7.kannel.sms.clients.HttpSmsSender Maven / Gradle / Ivy

package io.github.atkawa7.kannel.sms.clients;

import io.github.atkawa7.kannel.sms.SendStatus;
import io.github.atkawa7.kannel.sms.Sms;

import java.net.URL;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

/**
 * Send SMS to the smsbox using the HTTP GET method and URL parameters.
 *
 * @author Garth Patil {@literal }
 */
public class HttpSmsSender extends SmsSender {

    public HttpSmsSender(URL smsbox, SmsClient smsClient) {
        super(smsbox, smsClient);
    }

    public SendStatus send(Sms sms) throws Exception {
        URL u = new URL(smsbox.toString() + "?" + createQuery(sms));
        return smsClient.sendPlain(u);
    }

    private String createQuery(Sms sms) {
        Map params = new HashMap();
        add("username", sms.getUsername(), params);
        add("password", sms.getPassword(), params);
        add("from", sms.getFrom(), params);
        add("to", sms.getTo(), params);
        add("text", sms.getText(), params);
        //charset
        // Charset of text message. Used to convert to a format suitable for 7 bits or to UCS-2.
        // Defaults to UTF-8 if coding is 7 bits and UTF-16BE if coding is UCS-2.
        //add("charset", sms.getCharset(), params);
        //udh
        // Optional User Data Header (UDH) part of the message. Must be URL encoded.
        //add("udh", new String(sms.getUdh()), params);
        add("smsc", sms.getSmsc(), params);
        add("mclass", sms.getMclass(), params);
        add("mwi", sms.getMwi(), params);
        add("compress", sms.getCompress(), params);
        add("coding", sms.getCoding(), params);
        add("validity", sms.getValidity(), params);
        add("deferred", sms.getDeferred(), params);
        add("dlr-mask", sms.getDlrMask(), params);
        //dlr-url
        add("dlr-url", sms.getDlrUrl(), params);
        //pid
        //add("pid", sms.getPid().intValue(), params);
        if (sms.getPid() != null) params.put("pid", Integer.toString(sms.getPid().intValue()));
        add("alt-dcs", sms.getAltDcs(), params);
        add("rpi", sms.getRpi(), params);
        add("account", sms.getAccount(), params);
        add("binfo", sms.getBinfo(), params);
        add("priority", sms.getPriority(), params);
        return join(params);
    }

    private Map add(String name, Object value, Map m) {
        if (value != null) {
            String s = value.toString();
            if (!s.equals("")) m.put(name, s);
        }
        return m;
    }

    private String join(Map params) {
        Set> entries = params.entrySet();
        int i = 0;
        StringBuilder o = new StringBuilder();
        for (Entry entry : entries) {
            i++;
            o.append(entry.getKey()).append("=").append(URLEncoder.encode(entry.getValue()));
            if (i == entries.size()) break;
            else o.append("&");
        }
        return o.toString();
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy