001/*
002 * Copyright (c) 2011-2017 Nexmo Inc
003 *
004 * Permission is hereby granted, free of charge, to any person obtaining a copy
005 * of this software and associated documentation files (the "Software"), to deal
006 * in the Software without restriction, including without limitation the rights
007 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008 * copies of the Software, and to permit persons to whom the Software is
009 * furnished to do so, subject to the following conditions:
010 *
011 * The above copyright notice and this permission notice shall be included in
012 * all copies or substantial portions of the Software.
013 *
014 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
020 * THE SOFTWARE.
021 */
022package com.nexmo.client.sms;
023
024
025import com.nexmo.client.HttpWrapper;
026import com.nexmo.client.NexmoClient;
027import com.nexmo.client.NexmoClientException;
028import com.nexmo.client.NexmoResponseParseException;
029import com.nexmo.client.sms.messages.Message;
030
031import java.util.ArrayList;
032import java.util.Arrays;
033import java.util.Date;
034import java.util.List;
035
036
037/**
038 * A client for talking to the Nexmo Voice API. The standard way to obtain an instance of this class is to use {@link
039 * NexmoClient#getSmsClient()}.
040 */
041public class SmsClient {
042    private SendMessageEndpoint message;
043    private SmsSearchEndpoint search;
044    private SearchRejectedMessagesEndpoint rejected;
045    private SmsSingleSearchEndpoint singleSearch;
046
047    /**
048     * Create a new SmsClient.
049     */
050    public SmsClient(HttpWrapper httpWrapper) {
051        this.message = new SendMessageEndpoint(httpWrapper);
052        this.search = new SmsSearchEndpoint(httpWrapper);
053        this.rejected = new SearchRejectedMessagesEndpoint(httpWrapper);
054        this.singleSearch = new SmsSingleSearchEndpoint(httpWrapper);
055    }
056
057    /**
058     * Send an SMS message.
059     * <p>
060     * This uses the supplied object to construct a request and post it to the Nexmo API.<br> This method will respond
061     * with an SmsSubmissionResponse object. Depending on the nature and length of the submitted message, Nexmo may
062     * automatically split the message into multiple sms messages in order to deliver to the handset. For example, a
063     * long text sms of greater than 160 chars will need to be split into multiple 'concatenated' sms messages. The
064     * Nexmo service will handle this automatically for you.<br> The messages are stored as a Collection of
065     * SmsSubmissionResponseMessage objects on the SmsSubmissionResponse object. Each message can potentially have a
066     * different status result, and each message will have a different message id. Delivery notifications will be
067     * generated for each sms message within this set and will be posted to your application containing the appropriate
068     * message id.
069     *
070     * @param message The message request object that describes the type of message and the contents to be submitted.
071     *
072     * @return SmsSubmissionResponse an object containing a collection of SmsSubmissionResponseMessage objects for each
073     * actual sms that was required to submit the message.
074     *
075     * @throws NexmoClientException        if there was a problem with the Nexmo request or response objects.
076     * @throws NexmoResponseParseException if the response from the API could not be parsed.
077     */
078    public SmsSubmissionResponse submitMessage(Message message) throws NexmoResponseParseException, NexmoClientException {
079        return this.message.execute(message);
080    }
081
082    /**
083     * Search for completed SMS transactions.
084     * <p>
085     * You should probably use the helper methods {@link #searchMessages(String, String...)} or {@link
086     * #searchMessages(String, String...)} instead.
087     * <p>
088     *
089     * @throws NexmoClientException        if there was a problem with the Nexmo request or response objects.
090     * @throws NexmoResponseParseException if the response from the API could not be parsed.
091     */
092    public SearchSmsResponse searchMessages(SearchSmsRequest request) throws NexmoResponseParseException, NexmoClientException {
093        return this.search.execute(request);
094    }
095
096    /**
097     * Search for completed SMS transactions by ID
098     *
099     * @param id  the first ID to look up
100     * @param ids optional extra IDs to look up
101     *
102     * @return SMS data matching the provided criteria.
103     *
104     * @throws NexmoClientException        if there was a problem with the Nexmo request or response objects.
105     * @throws NexmoResponseParseException if the response from the API could not be parsed.
106     */
107    public SearchSmsResponse searchMessages(String id, String... ids) throws NexmoResponseParseException, NexmoClientException {
108        List<String> idList = new ArrayList<>(ids.length + 1);
109        idList.add(id);
110        idList.addAll(Arrays.asList(ids));
111        return this.searchMessages(new SmsIdSearchRequest(idList));
112    }
113
114    /**
115     * Search for completed SMS transactions by date and recipient MSISDN.
116     *
117     * @param date the date of the SMS message to be looked up
118     * @param to   the MSISDN number of the SMS recipient
119     *
120     * @return SMS data matching the provided criteria
121     *
122     * @throws NexmoClientException        if there was a problem with the Nexmo request or response objects.
123     * @throws NexmoResponseParseException if the response from the API could not be parsed.
124     */
125    public SearchSmsResponse searchMessages(Date date, String to) throws NexmoResponseParseException, NexmoClientException {
126        return this.searchMessages(new SmsDateSearchRequest(date, to));
127    }
128
129    /**
130     * Search for rejected SMS transactions using a {@link SearchRejectedMessagesRequest}.
131     * <p>
132     * You should probably use {@link #searchRejectedMessages(Date, String)} instead.
133     *
134     * @return rejection data matching the provided criteria
135     *
136     * @throws NexmoClientException        if there was a problem with the Nexmo request or response objects.
137     * @throws NexmoResponseParseException if the response from the API could not be parsed.
138     */
139    public SearchRejectedMessagesResponse searchRejectedMessages(SearchRejectedMessagesRequest request) throws NexmoResponseParseException, NexmoClientException {
140        return this.rejected.execute(request);
141    }
142
143    /**
144     * Search for rejected SMS transactions by date and recipient MSISDN.
145     *
146     * @param date the date of the rejected SMS message to be looked up
147     * @param to   the MSISDN number of the SMS recipient
148     *
149     * @return rejection data matching the provided criteria
150     *
151     * @throws NexmoClientException        if there was a problem with the Nexmo request or response objects.
152     * @throws NexmoResponseParseException if the response from the API could not be parsed.
153     */
154    public SearchRejectedMessagesResponse searchRejectedMessages(Date date, String to) throws NexmoResponseParseException, NexmoClientException {
155        return this.searchRejectedMessages(new SearchRejectedMessagesRequest(date, to));
156    }
157
158    /**
159     * Search for a single SMS by id.
160     *
161     * @param id The message id to search for.
162     *
163     * @return SmsSingleSearchResponse object containing the details of the SMS.
164     *
165     * @throws NexmoClientException        if there was a problem with the Nexmo request or response objects.
166     * @throws NexmoResponseParseException if the response from the API could not be parsed.
167     */
168    public SmsSingleSearchResponse getSms(String id) throws NexmoResponseParseException, NexmoClientException {
169        return this.singleSearch.execute(id);
170    }
171}