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