ExtractMetadataService extends MetadataServiceImpl { @Override public void doService(AdaptrisMessage msg) throws ServiceException { try { String value = msg.getMetadataValue(getSourceKey()); Matcher matcher = regexpPattern.matcher(value); if (matcher.matches()) { Set<MetadataElement> extracted = MetadataHelper.metadataFromMatchGroups(matcher, metadataKeys()); logMetadata("Adding metadata: {}", extracted); msg.setMetadata(extracted); } else { log.trace("{} did not match, no metadata set", getRegularExpression()); } } catch (Exception e) { throw ExceptionHelper.wrapServiceException(e); } } ExtractMetadataService(); @Override void prepare(); @Override void doService(AdaptrisMessage msg); String getSourceKey(); void setSourceKey(String sourceKey); ExtractMetadataService withSourceKey(String s); String getRegularExpression(); void setRegularExpression(String regExp); ExtractMetadataService withRegularExpression(String s); List<String> getMetadataKeys(); void setMetadataKeys(List<String> metadataKeys); ExtractMetadataService withMetadataKeys(List<String> keys); ExtractMetadataService withMetadataKeys(String... keys);  }
@Test public void testDoService() throws Exception { ExtractMetadataService service = new ExtractMetadataService().withRegularExpression("/path/(.*)/(.*)").withMetadataKeys("p1", "p2").withSourceKey("key"); AdaptrisMessage msg = AdaptrisMessageFactory.getDefaultInstance().newMessage(); msg.addMessageHeader("key", "/path/value1/value2"); execute(service, msg); assertTrue(msg.headersContainsKey("p1")); assertEquals("value1", msg.getMetadataValue("p1")); assertTrue(msg.headersContainsKey("p2")); assertEquals("value2", msg.getMetadataValue("p2")); }