/** * 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://fabric8.com/examples/order/v7")) String country) { if (country.equals("AU")) { return APAC; } else if (country.equals("US")) { return AMER; } else { return EMEA; } } }
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; }
/** * 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://fabric8.com/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); } }