EvaluateURIActionHandler extends ProcessorActionHandler { @Override public boolean process(Trace trace, Node node, Direction direction, Map<String, ?> headers, Object[] values) { if (super.process(trace, node, direction, headers, values)) { if (node.getUri() != null && pathTemplate != null) { StringTokenizer uriTokens = new StringTokenizer(node.getUri(), "/"); StringTokenizer templateTokens = new StringTokenizer(pathTemplate, "/"); if (uriTokens.countTokens() == templateTokens.countTokens()) { Set<Property> props = null; while (uriTokens.hasMoreTokens()) { String uriToken = uriTokens.nextToken(); String templateToken = templateTokens.nextToken(); if (templateToken.charAt(0) == '{' && templateToken.charAt(templateToken.length() - 1) == '}') { String name = templateToken.substring(1, templateToken.length() - 1); if (props == null) { props = new HashSet<Property>(); } try { props.add(new Property(name, URLDecoder.decode(uriToken, "UTF-8"))); } catch (UnsupportedEncodingException e) { if (log.isLoggable(Level.FINEST)) { log.finest("Failed to decode value '" + uriToken + "': " + e); } } } else if (!uriToken.equals(templateToken)) { return false; } } boolean processed = false; if (props != null) { node.getProperties().addAll(props); NodeUtil.rewriteURI(node, pathTemplate); processed = true; } if (!queryParameters.isEmpty()) { if (processQueryParameters(trace, node)) { processed = true; } } return processed; } } } return false; } EvaluateURIActionHandler(ProcessorAction action); @Override List<ConfigMessage> init(Processor processor); @Override boolean process(Trace trace, Node node, Direction direction, Map<String, ?> headers,
            Object[] values); static final String TEMPLATE_MUST_BE_SPECIFIED; }
@Test public void testMismatchURIAndTemplate1() { EvaluateURIAction action = new EvaluateURIAction(); action.setTemplate("/not/same/elements"); EvaluateURIActionHandler handler = new EvaluateURIActionHandler(action); Consumer consumer = new Consumer(); consumer.setUri("/not/same"); assertFalse(handler.process(null, consumer, Direction.In, null, null)); }
@Test public void testMismatchURIAndTemplate2() { EvaluateURIAction action = new EvaluateURIAction(); action.setTemplate("/same/{name}/elements"); EvaluateURIActionHandler handler = new EvaluateURIActionHandler(action); Consumer consumer = new Consumer(); consumer.setUri("/same/fred/elementsbutdiffvalues"); assertFalse(handler.process(null, consumer, Direction.In, null, null)); }
@Test public void testEvaluateURIPathAndRewrite() { EvaluateURIAction action = new EvaluateURIAction(); action.setTemplate("/my/{name}/and/{num}"); EvaluateURIActionHandler handler = new EvaluateURIActionHandler(action); Trace trace = new Trace(); Consumer consumer = new Consumer(); consumer.setUri("/my/fred/and/5"); trace.getNodes().add(consumer); assertTrue(handler.process(trace, consumer, Direction.In, null, null)); assertEquals(action.getTemplate(), consumer.getUri()); assertTrue(consumer.hasProperty("name")); assertTrue(consumer.hasProperty("num")); assertEquals("fred", consumer.getProperties("name").iterator().next().getValue()); assertEquals("5", consumer.getProperties("num").iterator().next().getValue()); }
@Test public void testEvaluateURIQueryStringAndNoRewrite() { EvaluateURIAction action = new EvaluateURIAction(); action.setTemplate("/my/uri?{name}&{num}"); EvaluateURIActionHandler handler = new EvaluateURIActionHandler(action); Trace trace = new Trace(); Consumer consumer = new Consumer(); consumer.setUri("/my/uri"); consumer.getProperties().add(new Property(Constants.PROP_HTTP_QUERY, "num=5&another=value&name=hello%20world")); trace.getNodes().add(consumer); assertTrue(handler.process(trace, consumer, Direction.In, null, null)); assertFalse(NodeUtil.isURIRewritten(consumer)); assertTrue(consumer.hasProperty("name")); assertTrue(consumer.hasProperty("num")); assertEquals("hello world", consumer.getProperties("name").iterator().next().getValue()); assertEquals("5", consumer.getProperties("num").iterator().next().getValue()); assertFalse(trace.hasProperty("another")); }
@Test public void testEvaluateURIPathAndQueryStringWithRewrite() { EvaluateURIAction action = new EvaluateURIAction(); action.setTemplate("/my/{pathParam}/uri?{name}&{num}"); EvaluateURIActionHandler handler = new EvaluateURIActionHandler(action); Trace trace = new Trace(); Consumer consumer = new Consumer(); consumer.setUri("/my/test%20param/uri"); consumer.getProperties().add(new Property(Constants.PROP_HTTP_QUERY, "num=5&another=value&name=hello%20world")); trace.getNodes().add(consumer); assertTrue(handler.process(trace, consumer, Direction.In, null, null)); assertEquals("/my/{pathParam}/uri", consumer.getUri()); assertTrue(consumer.hasProperty("pathParam")); assertTrue(consumer.hasProperty("name")); assertTrue(consumer.hasProperty("num")); assertEquals("test param", consumer.getProperties("pathParam").iterator().next().getValue()); assertEquals("hello world", consumer.getProperties("name").iterator().next().getValue()); assertEquals("5", consumer.getProperties("num").iterator().next().getValue()); assertFalse(trace.hasProperty("another")); }