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.verify.endpoints;
023
024import com.nexmo.client.HttpWrapper;
025import com.nexmo.client.NexmoClientException;
026import com.nexmo.client.NexmoResponseParseException;
027import com.nexmo.client.auth.SignatureAuthMethod;
028import com.nexmo.client.auth.TokenAuthMethod;
029import com.nexmo.client.legacyutils.XmlParser;
030import com.nexmo.client.verify.VerifyRequest;
031import com.nexmo.client.verify.VerifyResult;
032import com.nexmo.client.voice.endpoints.AbstractMethod;
033import org.apache.http.HttpResponse;
034import org.apache.http.client.methods.RequestBuilder;
035import org.apache.http.impl.client.BasicResponseHandler;
036import org.apache.http.message.BasicNameValuePair;
037import org.w3c.dom.Document;
038import org.w3c.dom.Element;
039
040import java.io.IOException;
041import java.io.UnsupportedEncodingException;
042import java.util.Locale;
043
044/**
045 * @deprecated Relies on XML Endpoint, use {@link com.nexmo.client.verify.VerifyClient#verify} instead.
046 */
047@Deprecated
048public class VerifyEndpoint extends AbstractMethod<VerifyRequest, VerifyResult> {
049    private static final Class[] ALLOWED_AUTH_METHODS = new Class[]{SignatureAuthMethod.class, TokenAuthMethod.class};
050
051    private static final String DEFAULT_URI = "https://api.nexmo.com/verify/xml";
052
053    private XmlParser xmlParser = new XmlParser();
054
055    private String uri = DEFAULT_URI;
056
057    /**
058     * Create a new VerifyMethod.
059     * <p>
060     * This client is used for calling the verify API's verify endpoint.
061     */
062    public VerifyEndpoint(HttpWrapper httpWrapper) {
063        super(httpWrapper);
064    }
065
066    @Override
067    protected Class[] getAcceptableAuthMethods() {
068        return ALLOWED_AUTH_METHODS;
069    }
070
071    @Override
072    public RequestBuilder makeRequest(VerifyRequest request) throws NexmoClientException, UnsupportedEncodingException {
073        RequestBuilder result = RequestBuilder.post(this.uri)
074                .addParameter("number", request.getNumber())
075                .addParameter("brand", request.getBrand());
076
077        if (request.getFrom() != null) {
078            result.addParameter("sender_id", request.getFrom());
079        }
080
081        if (request.getLength() > 0) {
082            result.addParameter("code_length", Integer.toString(request.getLength()));
083        }
084
085        if (request.getLocale() != null) {
086            result.addParameter(new BasicNameValuePair("lg",
087                    (request.getLocale().getLanguage() + "-" + request.getLocale().getCountry()).toLowerCase()));
088        }
089
090        if (request.getType() != null) {
091            result.addParameter("require_type", request.getType().toString());
092        }
093
094        if (request.getCountry() != null) {
095            result.addParameter("country", request.getCountry());
096        }
097
098        if (request.getPinExpiry() != null) {
099            result.addParameter("pin_expiry", request.getPinExpiry().toString());
100        }
101
102        if (request.getNextEventWait() != null) {
103            result.addParameter("next_event_wait", request.getNextEventWait().toString());
104        }
105
106        return result;
107    }
108
109    @Override
110    public VerifyResult parseResponse(HttpResponse response) throws IOException {
111        return parseVerifyResponse(new BasicResponseHandler().handleResponse(response));
112    }
113
114    public VerifyResult verify(final String number, final String brand) throws IOException, NexmoClientException {
115        return execute(new VerifyRequest(number, brand));
116    }
117
118    public VerifyResult verify(final String number,
119                               final String brand,
120                               final String from) throws IOException, NexmoClientException {
121        return execute(new VerifyRequest(number, brand, from));
122    }
123
124    public VerifyResult verify(final String number,
125                               final String brand,
126                               final String from,
127                               final int length,
128                               final Locale locale) throws IOException, NexmoClientException {
129        return execute(new VerifyRequest(number, brand, from, length, locale));
130    }
131
132    public VerifyResult verify(final String number,
133                               final String brand,
134                               final String from,
135                               final int length,
136                               final Locale locale,
137                               final VerifyRequest.LineType type) throws IOException, NexmoClientException {
138        return execute(new VerifyRequest(number, brand, from, length, locale, type));
139    }
140
141    public VerifyResult verify(VerifyRequest request) throws IOException, NexmoClientException {
142        return execute(request);
143    }
144
145    protected VerifyResult parseVerifyResponse(String response) throws NexmoResponseParseException {
146        Document doc = xmlParser.parseXml(response);
147
148        Element root = doc.getDocumentElement();
149        if (!"verify_response".equals(root.getNodeName()))
150            throw new NexmoResponseParseException("No valid response found [ " + response + "] ");
151
152        return SharedParsers.parseVerifyResponseXmlNode(root);
153    }
154}