@Override public AmazonServiceException unmarshall(Node in) throws Exception { XPath xpath = xpath(); String errorCode = parseErrorCode(in, xpath); String message = asString("Response/Errors/Error/Message", in, xpath); String requestId = asString("Response/RequestID", in, xpath); String errorType = asString("Response/Errors/Error/Type", in, xpath); Constructor<? extends AmazonServiceException> constructor = exceptionClass.getConstructor(String.class); AmazonServiceException ase = constructor.newInstance(message); ase.setErrorCode(errorCode); ase.setRequestId(requestId); if (errorType == null) { ase.setErrorType(ErrorType.Unknown); } else if (errorType.equalsIgnoreCase("server")) { ase.setErrorType(ErrorType.Service); } else if (errorType.equalsIgnoreCase("client")) { ase.setErrorType(ErrorType.Client); } return ase; }
public static Document documentFrom(URL url) throws SAXException, IOException, ParserConfigurationException { return documentFrom(url.openStream()); }
/** * Same as {@link #asInteger(String, Node)} but allows an xpath to be passed * in explicitly for reuse. */ public static Integer asInteger(String expression, Node node, XPath xpath) throws XPathExpressionException { String intString = evaluateAsString(expression, node, xpath); return (isEmptyString(intString)) ? null : Integer.parseInt(intString); }
/** * Evaluates the specified XPath expression and returns the result as a * Node. * * @param nodeName * The XPath expression to evaluate. * @param node * The node to run the expression on. * * @return The Node result. * * @throws XPathExpressionException * If there was a problem processing the specified XPath * expression. */ public static Node asNode(String nodeName, Node node) throws XPathExpressionException { return asNode(nodeName, node, xpath()); }
/** * Evaluates the specified XPath expression and returns the result as a * Float. * <p> * This method can be expensive as a new xpath is instantiated per * invocation. Consider passing in the xpath explicitly via { * {@link #asDouble(String, Node, XPath)} instead. Note {@link XPath} is * not thread-safe and not reentrant. * * @param expression * The XPath expression to evaluate. * @param node * The node to run the expression on. * * @return The Float result. * * @throws XPathExpressionException * If there was a problem processing the specified XPath * expression. */ public static Float asFloat(String expression, Node node) throws XPathExpressionException { return asFloat(expression, node, xpath()); }
@Override public AmazonServiceException unmarshall(Node node) throws Exception { // Bail out if this isn't the right error code that this // marshaller understands String errorCode = parseErrorCode(node); if (errorCode == null || !errorCode.equals("InvalidSnsTopic")) return null; InvalidSnsTopicException e = (InvalidSnsTopicException) super.unmarshall(node); e.setTopic(XpathUtils.asString(getErrorPropertyPath("Topic"), node)); return e; } }
/** * Same as {@link #asByteBuffer(String, Node)} but allows an xpath to be * passed in explicitly for reuse. */ public static ByteBuffer asByteBuffer(String expression, Node node, XPath xpath) throws XPathExpressionException { String base64EncodedString = evaluateAsString(expression, node, xpath); if (isEmptyString(base64EncodedString)) return null; if (!isEmpty(node)) { byte[] decodedBytes = Base64.decode(base64EncodedString); return ByteBuffer.wrap(decodedBytes); } return null; }
/** * Evaluates the specified expression on the specified node and returns the * result as a String. * * @param expression The Xpath expression to evaluate. * @param node The node on which to evaluate the expression. * @return The result of evaluating the specified expression, or null if the * evaluation didn't return any result. * @throws XPathExpressionException If there are any problems evaluating the * Xpath expression. */ private static String evaluateAsString(String expression, Node node) throws XPathExpressionException { if (isEmpty(node)) return null; if (!".".equals(expression)) { /* * If the expression being evaluated doesn't select a node, we want * to return null to distinguish between cases where a node isn't * present (which should be represented as null) and when a node is * present, but empty (which should be represented as the empty * string). We skip this test if the expression is "." since we've * already checked that the node exists. */ if (asNode(expression, node) == null) return null; } String s = xpath().evaluate(expression, node); return s.trim(); }
@Test public void testGetRoot() throws Exception { Document document = XpathUtils.documentFrom(DOCUMENT); Node childTitleNode = XpathUtils.asNode("Foo/Title", document); Node childAsRoot = XpathUtils.asNode("/", childTitleNode); assertEquals("#document", childAsRoot.getNodeName()); }
@Override public AmazonServiceException unmarshall(Node node) throws Exception { // Bail out if this isn't the right error code that this // marshaller understands String errorCode = parseErrorCode(node); if (errorCode == null || !errorCode.equals("NumberDomainAttributesExceeded")) return null; NumberDomainAttributesExceededException e = (NumberDomainAttributesExceededException) super.unmarshall(node); e.setBoxUsage(XpathUtils.asFloat(getErrorPropertyPath("BoxUsage"), node)); return e; } }
@Test public void testIsEmpty() throws Exception { Document document = XpathUtils.documentFrom(DOCUMENT); Node emptyNode = XpathUtils.asNode("Foo/Fake", document); Node realNode = XpathUtils.asNode("Foo/Count", document); assertTrue(XpathUtils.isEmpty(emptyNode)); assertFalse(XpathUtils.isEmpty(realNode)); }
@Test public void testAsString() throws Exception { Document document = XpathUtils.documentFrom(DOCUMENT); assertEquals("Boo", XpathUtils.asString("Foo/Title", document)); assertEquals("", XpathUtils.asString("Foo/Empty", document)); assertEquals("Bar", XpathUtils.asString("Foo/Count/@Foo", document)); Document namespaceDocument = XpathUtils.documentFrom(DOCUMENT_WITH_NAMESPACE); assertEquals("174.129.242.223", XpathUtils.asString("/AllocateAddressResponse/publicIp", namespaceDocument)); }
/** * Evaluates the specified XPath expression and returns the result as a * string. * <p> * This method can be expensive as a new xpath is instantiated per * invocation. Consider passing in the xpath explicitly via { * {@link #asDouble(String, Node, XPath)} instead. Note {@link XPath} is * not thread-safe and not reentrant. * * @param expression * The XPath expression to evaluate. * @param node * The node to run the expression on. * * @return The string result. * * @throws XPathExpressionException * If there was a problem processing the specified XPath * expression. */ public static String asString(String expression, Node node) throws XPathExpressionException { return evaluateAsString(expression, node, xpath()); }
@Test public void testAsFloat() throws Exception { Document document = XpathUtils.documentFrom(DOCUMENT); assertEquals((Float) 0.0000071759f, XpathUtils.asFloat("Foo/Usage", document)); assertEquals(null, XpathUtils.asFloat("Foo/Empty", document)); }
/** * Evaluates the specified XPath expression and returns the result as a * Boolean. * <p> * This method can be expensive as a new xpath is instantiated per * invocation. Consider passing in the xpath explicitly via { * {@link #asDouble(String, Node, XPath)} instead. Note {@link XPath} is * not thread-safe and not reentrant. * * @param expression * The XPath expression to evaluate. * @param node * The node to run the expression on. * * @return The Boolean result. * * @throws XPathExpressionException * If there was a problem processing the specified XPath * expression. */ public static Boolean asBoolean(String expression, Node node) throws XPathExpressionException { return asBoolean(expression, node, xpath()); }
/** * Evaluates the specified XPath expression and returns the result as a * Long. * <p> * This method can be expensive as a new xpath is instantiated per * invocation. Consider passing in the xpath explicitly via { * {@link #asDouble(String, Node, XPath)} instead. Note {@link XPath} is * not thread-safe and not reentrant. * * @param expression * The XPath expression to evaluate. * @param node * The node to run the expression on. * * @return The Long result. * * @throws XPathExpressionException * If there was a problem processing the specified XPath * expression. */ public static Long asLong(String expression, Node node) throws XPathExpressionException { return asLong(expression, node, xpath()); }
/** * Evaluates the specified XPath expression and returns the result as a * Byte. * <p> * This method can be expensive as a new xpath is instantiated per * invocation. Consider passing in the xpath explicitly via { * {@link #asDouble(String, Node, XPath)} instead. Note {@link XPath} is * not thread-safe and not reentrant. * * @param expression * The XPath expression to evaluate. * @param node * The node to run the expression on. * * @return The Byte result. * * @throws XPathExpressionException * If there was a problem processing the specified XPath * expression. */ public static Byte asByte(String expression, Node node) throws XPathExpressionException { return asByte(expression, node, xpath()); }
/** * Evaluates the specified XPath expression and returns the results as a * Double. * <p> * This method can be expensive as a new xpath is instantiated per * invocation. Consider passing in the xpath explicitly via { * {@link #asDouble(String, Node, XPath)} instead. Note {@link XPath} is * not thread-safe and not reentrant. * * @param expression * The XPath expression to evaluate. * @param node * The node to run the expression on. * * @return The Double result. * * @throws XPathExpressionException * If there was a problem processing the specified XPath * expression. */ public static Double asDouble(String expression, Node node) throws XPathExpressionException { return asDouble(expression, node, xpath()); }
/** * Evaluates the specified xpath expression, base64 decodes the data and * returns the result as a ByteBuffer. * <p> * This method can be expensive as a new xpath is instantiated per * invocation. Consider passing in the xpath explicitly via { * {@link #asDouble(String, Node, XPath)} instead. Note {@link XPath} is * not thread-safe and not reentrant. * * @param expression * The Xpath expression to evaluate. * @param node * The node on which to evaluate the expression. * * @return A ByteBuffer of base64 decoded data from the result of evaluating * the specified Xpath expression. * * @throws XPathExpressionException * If there are any problems evaluating the Xpath expression. */ public static ByteBuffer asByteBuffer(String expression, Node node) throws XPathExpressionException { return asByteBuffer(expression, node, xpath()); }