@Override public Expression createExpression(CamelContext camelContext, Annotation annotation, LanguageAnnotation languageAnnotation, Class<?> expressionReturnType) { String xpath = getExpressionFromAnnotation(annotation); Class<?> resultType = getResultType(annotation); if (resultType.equals(Object.class)) { resultType = expressionReturnType; } XPathBuilder builder = XPathBuilder.xpath(xpath, resultType); NamespacePrefix[] namespaces = getExpressionNameSpacePrefix(annotation); if (namespaces != null) { for (NamespacePrefix namespacePrefix : namespaces) { builder = builder.namespace(namespacePrefix.prefix(), namespacePrefix.uri()); } } // Set the header name that we want the XPathBuilder to apply the XPath expression to String headerName = getHeaderName(annotation); if (ObjectHelper.isNotEmpty(headerName)) { builder.setHeaderName(headerName); } return builder; }
/** * Get the region code that corresponds to the given country code. * * This method can be used as a plain Java method. However, when it is used inside a Camel route, the @XPath annotation will * evaluate the XPath expression and use the result as the method parameter. In this case, it will fetch the country code * from the order XML message. So, the method will determine the region code for the country that is in the XML message. * * @param country the country code * @return the region code */ public String getRegion(@XPath(value = "/order:order/order:customer/order:country", namespaces = @NamespacePrefix(prefix = "order", uri = "http://org.jboss.fuse.quickstarts/examples/order/v7")) String country) { if (country.equals("AU")) { return APAC; } else if (country.equals("US")) { return AMER; } else { return EMEA; } }
/** * Validate the order date - orders should only be place from Monday to Saturday. * <p/> * This method can be used as a plain Java method, but when it is used inside a Camel route, the @XPath annotation will kick * in, evaluating the XPath expression and using the result as the method parameter. In this case, it will fetch the order * date from the order XML message. * * @param date the order date * @throws OrderValidationException when the order date is a Sunday */ public void validateOrderDate( @XPath(value = "/order:order/order:date", namespaces = @NamespacePrefix(prefix = "order", uri = "http://org.jboss.fuse.quickstarts/examples/order/v7")) String date) throws OrderValidationException { final Calendar calendar = new GregorianCalendar(); try { calendar.setTime(DATE_FORMAT.parse(date)); if (calendar.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) { LOGGER.warn("Order validation failure: order date " + date + " should not be a Sunday"); throw new OrderValidationException("Order date should not be a Sunday: " + date); } } catch (ParseException e) { throw new OrderValidationException("Invalid order date: " + date); } }
@Override public Expression createExpression(CamelContext camelContext, Annotation annotation, LanguageAnnotation languageAnnotation, Class<?> expressionReturnType) { String xQuery = getExpressionFromAnnotation(annotation); XQueryBuilder builder = XQueryBuilder.xquery(xQuery); if (annotation instanceof XQuery) { XQuery xQueryAnnotation = (XQuery)annotation; builder.setStripsAllWhiteSpace(xQueryAnnotation.stripsAllWhiteSpace()); if (ObjectHelper.isNotEmpty(xQueryAnnotation.headerName())) { builder.setHeaderName(xQueryAnnotation.headerName()); } NamespacePrefix[] namespaces = xQueryAnnotation.namespaces(); if (namespaces != null) { for (NamespacePrefix namespacePrefix : namespaces) { builder = builder.namespace(namespacePrefix.prefix(), namespacePrefix.uri()); } } } if (expressionReturnType.isAssignableFrom(String.class)) { builder.setResultsFormat(ResultFormat.String); } else if (expressionReturnType.isAssignableFrom(Collection.class)) { builder.setResultsFormat(ResultFormat.List); } else if (expressionReturnType.isAssignableFrom(Node.class)) { builder.setResultsFormat(ResultFormat.DOM); } else if (expressionReturnType.isAssignableFrom(byte[].class)) { builder.setResultsFormat(ResultFormat.Bytes); } return builder; }
public Document handleIncomingOrder(@Body Document xml, @XPath(value = "/c:order/@customerId", namespaces = @NamespacePrefix( prefix = "c", uri = "http://camelinaction.com/order")) int customerId, @Bean(ref = "guid", method = "generate") int orderId) { Attr attr = xml.createAttribute("orderId"); attr.setValue("" + orderId); Node node = xml.getElementsByTagName("order").item(0); node.getAttributes().setNamedItem(attr); return xml; }
protected NamespacePrefix[] getExpressionNameSpacePrefix(Annotation annotation) { return (NamespacePrefix[]) getAnnotationObjectValue(annotation, "namespaces"); }