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
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    }
055
056    /**
057     * Send an SMS message.
058     * <p>
059     * This uses the supplied object to construct a request and post it to the Nexmo API.<br>
060     * This method will respond with an array of SmsSubmissionResult objects. Depending on the nature and length of the submitted message, Nexmo may automatically
061     * 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
062     * into multiple 'concatenated' sms messages. The Nexmo service will handle this automatically for you.<br>
063     * The array of SmsSubmissionResult objects will contain a SmsSubmissionResult object for every actual sms that was required to submit the message.
064     * each message can potentially have a different status result, and each message will have a different message id.
065     * Delivery notifications will be generated for each sms message within this set and will be posted to your application containing the appropriate message id.
066     *
067     * @param message The message request object that describes the type of message and the contents to be submitted.
068     * @return SmsSubmissionResult[] an array of results, 1 object for each sms message that was required to submit this message in its entirety
069     * @throws NexmoResponseParseException if the HTTP response could not be parsed.
070     * @throws IOException                 There has been an error attempting to communicate with the Nexmo service (e.g. Network failure).
071     */
072    public SmsSubmissionResult[] submitMessage(Message message) throws IOException, NexmoClientException {
073        return this.message.execute(message);
074    }
075
076    /**
077     * Search for completed SMS transactions.
078     * <p>
079     * You should probably use the helper methods {@link #searchMessages(String, String...)} or
080     * {@link #searchMessages(String, String...)} instead.
081     */
082    public SearchSmsResponse searchMessages(SearchSmsRequest request)
083            throws IOException, NexmoClientException {
084        return this.search.execute(request);
085    }
086
087    /**
088     * Search for completed SMS transactions by ID
089     *
090     * @param id  the first ID to look up
091     * @param ids optional extra IDs to look up
092     * @return SMS data matching the provided criteria
093     */
094    public SearchSmsResponse searchMessages(String id, String... ids)
095            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)
110            throws IOException, NexmoClientException {
111        return this.searchMessages(new SmsDateSearchRequest(date, to));
112    }
113
114    /**
115     * Search for rejected SMS transactions using a {@link SearchRejectedMessagesRequest}.
116     * <p>
117     * You should probably use {@link #searchRejectedMessages(Date, String)} instead.
118
119     * @return rejection data matching the provided criteria
120     */
121    public SearchRejectedMessagesResponse searchRejectedMessages(SearchRejectedMessagesRequest request)
122            throws IOException, NexmoClientException {
123        return this.rejected.execute(request);
124    }
125
126    /**
127     * Search for rejected SMS transactions by date and recipient MSISDN.
128     *
129     * @param date the date of the rejected SMS message to be looked up
130     * @param to   the MSISDN number of the SMS recipient
131     * @return rejection data matching the provided criteria
132     */
133    public SearchRejectedMessagesResponse searchRejectedMessages(Date date, String to)
134            throws IOException, NexmoClientException {
135        return this.searchRejectedMessages(new SearchRejectedMessagesRequest(date, to));
136    }
137}