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.legacyutils.XmlUtil; 031import com.nexmo.client.verify.BaseResult; 032import com.nexmo.client.verify.CheckRequest; 033import com.nexmo.client.verify.CheckResult; 034import com.nexmo.client.voice.endpoints.AbstractMethod; 035import org.apache.commons.logging.Log; 036import org.apache.commons.logging.LogFactory; 037import org.apache.http.HttpResponse; 038import org.apache.http.client.methods.RequestBuilder; 039import org.apache.http.impl.client.BasicResponseHandler; 040import org.w3c.dom.Document; 041import org.w3c.dom.Element; 042import org.w3c.dom.Node; 043import org.w3c.dom.NodeList; 044 045import java.io.IOException; 046import java.io.UnsupportedEncodingException; 047 048public class VerifyCheckMethod extends AbstractMethod<CheckRequest, CheckResult> { 049 private static final Log log = LogFactory.getLog(VerifyCheckMethod.class); 050 private static final Class[] ALLOWED_AUTH_METHODS = new Class[]{SignatureAuthMethod.class, TokenAuthMethod.class}; 051 052 private static final String DEFAULT_URI = "https://api.nexmo.com/verify/check/xml"; 053 054 private XmlParser xmlParser = new XmlParser(); 055 private String uri = DEFAULT_URI; 056 057 public VerifyCheckMethod(HttpWrapper httpWrapper) { 058 super(httpWrapper); 059 } 060 061 @Override 062 protected Class[] getAcceptableAuthMethods() { 063 return ALLOWED_AUTH_METHODS; 064 } 065 066 @Override 067 public RequestBuilder makeRequest(CheckRequest request) throws NexmoClientException, UnsupportedEncodingException { 068 if (request.getRequestId() == null || request.getCode() == null) 069 throw new IllegalArgumentException("request ID and code parameters are mandatory."); 070 071 RequestBuilder result = RequestBuilder.post(this.uri) 072 .addParameter("request_id", request.getRequestId()) 073 074 .addParameter("code", request.getCode()); 075 if (request.getIpAddress() != null) 076 result.addParameter("ip_address", request.getIpAddress()); 077 078 return result; 079 } 080 081 @Override 082 public CheckResult parseResponse(HttpResponse response) throws IOException { 083 String body = new BasicResponseHandler().handleResponse(response); 084 return parseCheckResponse(body); 085 } 086 087 private CheckResult parseCheckResponse(String response) throws NexmoResponseParseException { 088 Document doc = xmlParser.parseXml(response); 089 090 Element root = doc.getDocumentElement(); 091 if (!"verify_response".equals(root.getNodeName())) 092 throw new NexmoResponseParseException("No valid response found [ " + response + "] "); 093 094 String eventId = null; 095 int status = -1; 096 float price = -1; 097 String currency = null; 098 String errorText = null; 099 100 NodeList fields = root.getChildNodes(); 101 for (int i = 0; i < fields.getLength(); i++) { 102 Node node = fields.item(i); 103 if (node.getNodeType() != Node.ELEMENT_NODE) 104 continue; 105 106 String name = node.getNodeName(); 107 if ("event_id".equals(name)) { 108 eventId = XmlUtil.stringValue(node); 109 } else if ("status".equals(name)) { 110 String str = XmlUtil.stringValue(node); 111 try { 112 if (str != null) 113 status = Integer.parseInt(str); 114 } catch (NumberFormatException e) { 115 log.error("xml parser .. invalid value in <status> node [ " + str + " ] "); 116 status = BaseResult.STATUS_INTERNAL_ERROR; 117 } 118 } else if ("price".equals(name)) { 119 String str = XmlUtil.stringValue(node); 120 try { 121 if (str != null) 122 price = Float.parseFloat(str); 123 } catch (NumberFormatException e) { 124 log.error("xml parser .. invalid value in <price> node [ " + str + " ] "); 125 } 126 } else if ("currency".equals(name)) { 127 currency = XmlUtil.stringValue(node); 128 } else if ("error_text".equals(name)) { 129 errorText = XmlUtil.stringValue(node); 130 } 131 } 132 133 if (status == -1) 134 throw new NexmoResponseParseException("Xml Parser - did not find a <status> node"); 135 136 // Is this a temporary error ? 137 boolean temporaryError = (status == BaseResult.STATUS_THROTTLED || status == BaseResult.STATUS_INTERNAL_ERROR); 138 139 return new CheckResult(status, eventId, price, currency, errorText, temporaryError); 140 } 141}