URLDecodeOperation implements EventOperation { @Override public InternalEvent perform(InternalEvent ievent) { if (ievent == null || ievent.getEventObj() == null || ievent.getEventObj().getPayload() == null) { return ievent; } for (String fieldName : fieldNames) { String value = null; try { value = ievent.getEventObj().getFieldAsString(fieldName); } catch (FieldNotFoundException e) { continue; } if (value == null) { return ievent; } try { for (int i = 0; i < this.times; i++) { value = codec.decode(value); } } catch (DecoderException e) { continue; } try { ievent.getEventObj().setField(fieldName, value); } catch (FieldNotFoundException e) { throw new OperationException("failed to update original field in URL decode"); } } return ievent; } URLDecodeOperation(List<String> fieldNames, int times); @Override InternalEvent perform(InternalEvent ievent); static final URLCodec codec; }
@Test public void testDecodeOnce() throws FieldNotFoundException { DummpyMapEvent devent = new DummpyMapEvent(); devent.setField("foo", "%3Ffoo%3Dbar%26baz%3Dqux"); URLDecodeOperation op = new URLDecodeOperation(Arrays.asList("foo"), 1); InternalEvent ievent = new InternalEvent("", null, 0); ievent.setEventObj(devent); op.perform(ievent); assertEquals(1, devent.payload.size()); assertEquals("?foo=bar&baz=qux", devent.getFieldAsString("foo")); }
@Test public void testDecodeTwice() throws FieldNotFoundException { DummpyMapEvent devent = new DummpyMapEvent(); devent.setField("foo", "%253Ffoo%253Dbar%2526baz%253Dqux"); URLDecodeOperation op = new URLDecodeOperation(Arrays.asList("foo"), 2); InternalEvent ievent = new InternalEvent("", null, 0); ievent.setEventObj(devent); op.perform(ievent); assertEquals(1, devent.payload.size()); assertEquals("?foo=bar&baz=qux", devent.getFieldAsString("foo")); }
@Test public void testDecodeTwoThings() throws FieldNotFoundException { DummpyMapEvent devent = new DummpyMapEvent(); devent.setField("foo", "%3Ffoo%3Dbar%26baz%3Dqux"); devent.setField("bar", "bar%3D%28abc%29"); URLDecodeOperation op = new URLDecodeOperation(Arrays.asList("foo", "bar"), 1); InternalEvent ievent = new InternalEvent("", null, 0); ievent.setEventObj(devent); op.perform(ievent); assertEquals(2, devent.payload.size()); assertEquals("?foo=bar&baz=qux", devent.getFieldAsString("foo")); assertEquals("bar=(abc)", devent.getFieldAsString("bar")); }
@Test public void testDecodeTwoThingsEmpty() throws FieldNotFoundException { DummpyMapEvent devent = new DummpyMapEvent(); devent.setField("foo", "%3Ffoo%3Dbar%26baz%3Dqux"); URLDecodeOperation op = new URLDecodeOperation(Arrays.asList("foo", "bar"), 1); InternalEvent ievent = new InternalEvent("", null, 0); ievent.setEventObj(devent); op.perform(ievent); assertEquals(1, devent.payload.size()); assertEquals("?foo=bar&baz=qux", devent.getFieldAsString("foo")); }