📄 sendsms.java
字号:
package com.bv.api.send;import java.io.IOException;import java.net.MalformedURLException;import java.util.Iterator;import java.util.Vector;import com.bv.api.Error;import com.bv.api.InvalidParameterException;import com.bv.api.ParamCall;import com.bv.api.ParamNotPresentException;import com.bv.api.response.SendSMSResponse;/** * An API wrapper class that allows you to send an SMS to one or more destinations. * @author C. Oattes, Kris Nobes */public class SendSMS extends ParamCall { /** * The text of the message to be sent */ private String message = null; /** * A list of destination numbers */ private Vector<String> destinations = null; /** * Create a SendSMS object */ public SendSMS() { super(); destinations = new Vector<String>(); url = "/api/send/sms"; } /** * Create a SendSMS object * @param message The message to send * @param destination The number to send the message to */ public SendSMS(String message, String destination) { this(); addDestination(destination); setMessage(message); } /** * Create a SendSMS object * @param message The message to send * @param destinations The number to send the message to */ public SendSMS(String message, Vector<String> destinations) { this(); this.destinations = destinations; setMessage(message); } /** * Sets the message to be sent * @param message The message to be sent. */ public void setMessage(String message) { this.message = message; } /** * Gets the message to be sent * @return A String containing the message to be sent. */ public String getMessage() { return message; } /** * Adds a destination to send the SMS message to. * @param number The number to send the SMS message to. Should be in the format [country code][number] (e.g. 07999123456 becomes 447999123456) */ public void addDestination(String number) { destinations.add(number); } /** * Sets the destinations of the SMS message * @param numbers A {@link Vector} containing the destinations for the SMS message. */ public void setDestinations(Vector<String> numbers) { this.destinations = numbers; } /** * Gets the list of destinations * @return a {@link Vector} which contains all of the destinations that have been set. */ public Vector<String> getDestinations() { return destinations; } protected boolean checkParams(String uaid) throws ParamNotPresentException, InvalidParameterException { Vector<String> unsetParams = new Vector<String>(); if (destinations.size() == 0) { unsetParams.add("destinations"); } if (uaid == null || uaid.equals("")) { unsetParams.add("uaid"); } else if (uaid.length() != 32) { throw new InvalidParameterException("UAID"); } if (message == null || message.equals("")) { unsetParams.add("message"); } if (unsetParams.size() > 0) { throw new ParamNotPresentException(unsetParams); } else { return true; } } protected String constructParameterString(String uaid) throws InvalidParameterException{ Iterator<String> destinationsIterator; String parameters = ""; parameters += getEncodedParams("uaid", uaid); parameters += "&"; parameters += getEncodedParams("message", message); parameters += "&"; destinationsIterator = destinations.iterator(); while (destinationsIterator.hasNext()) { String number = destinationsIterator.next(); if (number.equals("")) { throw new InvalidParameterException("destination"); } else { parameters += getEncodedParams("dest", number); } if (destinationsIterator.hasNext()) { parameters += "&"; } } return parameters; } /** * Process the response from the server and return an {@link SendSMSResponse} object * @param uaid The user uaid * @param serverURL The server to connect to * @return SendSMSResponse, with "success" set to <code>true</code> if the request was successful, <code>false</code> otherwise. */ public SendSMSResponse execute(String uaid, String serverURL) throws ParamNotPresentException, InvalidParameterException { try { String response = connect(uaid, serverURL); SendSMSResponse smsResponse = new SendSMSResponse(); return smsResponse.processResponse(response); } catch (MalformedURLException mue) { return new SendSMSResponse(Error.ConnectionError, "Invalid server URL"); } catch (IOException ioe) { return new SendSMSResponse(Error.ConnectionError); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -