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.NexmoResponseParseException;
025import com.nexmo.client.legacyutils.XmlUtil;
026import com.nexmo.client.verify.BaseResult;
027import com.nexmo.client.verify.VerifyResult;
028import org.apache.commons.logging.Log;
029import org.apache.commons.logging.LogFactory;
030import org.w3c.dom.Element;
031import org.w3c.dom.Node;
032import org.w3c.dom.NodeList;
033
034public class SharedParsers {
035    private static final Log log = LogFactory.getLog(SharedParsers.class);
036
037    protected static VerifyResult parseVerifyResponseXmlNode(Element root) throws NexmoResponseParseException {
038        String requestId = null;
039        int status = -1;
040        String errorText = null;
041
042        NodeList fields = root.getChildNodes();
043        for (int i = 0; i < fields.getLength(); i++) {
044            Node node = fields.item(i);
045            if (node.getNodeType() != Node.ELEMENT_NODE)
046                continue;
047
048            String name = node.getNodeName();
049            if ("request_id".equals(name)) {
050                requestId = XmlUtil.stringValue(node);
051            } else if ("status".equals(name)) {
052                String str = XmlUtil.stringValue(node);
053                try {
054                    if (str != null)
055                        status = Integer.parseInt(str);
056                } catch (NumberFormatException e) {
057                    log.error("xml parser .. invalid value in <status> node [ " + str + " ] ");
058                    status = BaseResult.STATUS_INTERNAL_ERROR;
059                }
060            } else if ("error_text".equals(name)) {
061                errorText = XmlUtil.stringValue(node);
062            }
063        }
064
065        if (status == -1)
066            throw new NexmoResponseParseException("Xml Parser - did not find a <status> node");
067
068        // Is this a temporary error ?
069        boolean temporaryError = (status == BaseResult.STATUS_THROTTLED || status == BaseResult.STATUS_INTERNAL_ERROR);
070
071        return new VerifyResult(status,
072                requestId,
073                errorText,
074                temporaryError);
075    }
076}