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.io.IOException;
032import java.util.ArrayList;
033import java.util.Arrays;
034import java.util.Date;
035import java.util.List;
036
037
038/**
039 * A client for talking to the Nexmo Voice API. The standard way to obtain an instance of this class is to use
040 * {@link NexmoClient#getSmsClient()}.
041 */
042public class SmsClient {
043    private SendMessageEndpoint message;
044    private SmsSearchEndpoint search;
045    private SearchRejectedMessagesEndpoint rejected;
046    private SmsSingleSearchEndpoint singleSearch;
047
048    /**
049     * Create a new SmsClient.
050     */
051    public SmsClient(HttpWrapper httpWrapper) {
052        this.message = new SendMessageEndpoint(httpWrapper);
053        this.search = new SmsSearchEndpoint(httpWrapper);
054        this.rejected = new SearchRejectedMessagesEndpoint(httpWrapper);
055        this.singleSearch = new SmsSingleSearchEndpoint(httpWrapper);
056    }
057
058    /**
059     * Send an SMS message.
060     * <p>
061     * This uses the supplied object to construct a request and post it to the Nexmo API.<br>
062     * This method will respond with an array of SmsSubmissionResult objects. Depending on the nature and length of the submitted message, Nexmo may automatically
063     * split the message into multiple sms messages in order to deliver to the handset. For example, a long text sms of greater than 160 chars will need to be split
064     * into multiple 'concatenated' sms messages. The Nexmo service will handle this automatically for you.<br>
065     * The array of SmsSubmissionResult objects will contain a SmsSubmissionResult object for every actual sms that was required to submit the message.
066     * each message can potentially have a different status result, and each message will have a different message id.
067     * Delivery notifications will be generated for each sms message within this set and will be posted to your application containing the appropriate message id.
068     *
069     * @param message The message request object that describes the type of message and the contents to be submitted.
070     * @return SmsSubmissionResult[] an array of results, 1 object for each sms message that was required to submit this message in its entirety
071     * @throws NexmoResponseParseException if the HTTP response could not be parsed.
072     * @throws IOException                 There has been an error attempting to communicate with the Nexmo service (e.g. Network failure).
073     */
074    public SmsSubmissionResult[] submitMessage(Message message) throws IOException, NexmoClientException {
075        return this.message.execute(message);
076    }
077
078    /**
079     * Search for completed SMS transactions.
080     * <p>
081     * You should probably use the helper methods {@link #searchMessages(String, String...)} or
082     * {@link #searchMessages(String, String...)} instead.
083     */
084    public SearchSmsResponse searchMessages(SearchSmsRequest request) throws IOException, NexmoClientException {
085        return this.search.execute(request);
086    }
087
088    /**
089     * Search for completed SMS transactions by ID
090     *
091     * @param id  the first ID to look up
092     * @param ids optional extra IDs to look up
093     * @return SMS data matching the provided criteria
094     */
095    public SearchSmsResponse searchMessages(String id, String... ids) throws IOException, NexmoClientException {
096        List<String> idList = new ArrayList<>(ids.length + 1);
097        idList.add(id);
098        idList.addAll(Arrays.asList(ids));
099        return this.searchMessages(new SmsIdSearchRequest(idList));
100    }
101
102    /**
103     * Search for completed SMS transactions by date and recipient MSISDN.
104     *
105     * @param date the date of the SMS message to be looked up
106     * @param to   the MSISDN number of the SMS recipient
107     * @return SMS data matching the provided criteria
108     */
109    public SearchSmsResponse searchMessages(Date date, String to) throws IOException, NexmoClientException {
110        return this.searchMessages(new SmsDateSearchRequest(date, to));
111    }
112
113    /**
114     * Search for rejected SMS transactions using a {@link SearchRejectedMessagesRequest}.
115     * <p>
116     * You should probably use {@link #searchRejectedMessages(Date, String)} instead.
117     *
118     * @return rejection data matching the provided criteria
119     */
120    public SearchRejectedMessagesResponse searchRejectedMessages(SearchRejectedMessagesRequest request) throws IOException, NexmoClientException {
121        return this.rejected.execute(request);
122    }
123
124    /**
125     * Search for rejected SMS transactions by date and recipient MSISDN.
126     *
127     * @param date the date of the rejected SMS message to be looked up
128     * @param to   the MSISDN number of the SMS recipient
129     * @return rejection data matching the provided criteria
130     */
131    public SearchRejectedMessagesResponse searchRejectedMessages(Date date,
132                                                                 String to) throws IOException, NexmoClientException {
133        return this.searchRejectedMessages(new SearchRejectedMessagesRequest(date, to));
134    }
135
136    /**
137     * Search for a single SMS by id.
138     *
139     * @param id The message id to search for.
140     * @return SmsSingleSearchResponse object containing the details of the SMS.
141     * @throws NexmoResponseParseException if the HTTP response could not be parsed.
142     * @throws IOException                 There has been an error attempting to communicate with the Nexmo service (e.g. Network failure).
143     */
144    public SmsSingleSearchResponse getSms(String id) throws IOException, NexmoClientException {
145        return this.singleSearch.execute(id);
146    }
147}