Search in sources :

Example 71 with MetadataElement

use of com.adaptris.core.MetadataElement in project interlok by adaptris.

the class ReformatMetadata method doService.

/**
 * <p>
 * Adds the configured metadata to the message.
 * </p>
 *
 * @param msg the message to process
 */
@Override
public void doService(AdaptrisMessage msg) throws ServiceException {
    if (isBlank(getMetadataKeyRegexp())) {
        return;
    }
    try {
        Set<MetadataElement> metadata = msg.getMetadata();
        Set<MetadataElement> modifiedMetadata = new HashSet<MetadataElement>();
        for (MetadataElement e : metadata) {
            if (e.getKey().matches(metadataKeyRegexp)) {
                modifiedMetadata.add(new MetadataElement(e.getKey(), reformat(e.getValue(), msg)));
            }
        }
        logMetadata("Modified metadata : {}", modifiedMetadata);
        msg.setMetadata(modifiedMetadata);
    } catch (Exception e) {
        throw ExceptionHelper.wrapServiceException(e);
    }
}
Also used : MetadataElement(com.adaptris.core.MetadataElement) ServiceException(com.adaptris.core.ServiceException) HashSet(java.util.HashSet)

Example 72 with MetadataElement

use of com.adaptris.core.MetadataElement in project interlok by adaptris.

the class RegexpMetadataQuery method doQuery.

/**
 * <p>
 * Performs the query against the payload of the supplied String and
 * constructs a MetdataElement with the configured Key and the result as the
 * Value.
 * </p>
 * @param message the String to run the Query on
 * @return a MetadataElement with the configured Key and the result of the
 * Query as it's Value <b>NOTE: the Value of the MetadataElement will be null
 * if nulls have been allowed</b>
 * @throws CoreException wrapping any underlying Exception
 */
public synchronized MetadataElement doQuery(String message) throws Exception {
    Args.notBlank(getMetadataKey(), "metadata-key");
    Args.notBlank(getQueryExpression(), "query-expression");
    if (pattern == null) {
        pattern = Pattern.compile(getQueryExpression());
    }
    Matcher matcher = pattern.matcher(message);
    MetadataElement elem = new MetadataElement();
    elem.setKey(getMetadataKey());
    if (matcher.find()) {
        elem.setValue(matcher.group(1));
    } else {
        if (!allowNullResults()) {
            throw new CoreException("Failed to match pattern [" + metadataKey + "] to input string");
        }
    }
    return elem;
}
Also used : CoreException(com.adaptris.core.CoreException) Matcher(java.util.regex.Matcher) MetadataElement(com.adaptris.core.MetadataElement)

Example 73 with MetadataElement

use of com.adaptris.core.MetadataElement in project interlok by adaptris.

the class RegexpMetadataService method doService.

@Override
public void doService(AdaptrisMessage msg) throws ServiceException {
    String message = msg.getContent();
    List<MetadataElement> added = new ArrayList<>();
    try {
        for (RegexpMetadataQuery q : getRegexpMetadataQueries()) {
            MetadataElement elem = q.doQuery(message);
            if (BooleanUtils.or(new boolean[] { !isEmpty(elem.getValue()), addNullValues() })) {
                msg.addMetadata(elem);
                added.add(elem);
            }
        }
    } catch (Exception e) {
        throw ExceptionHelper.wrapServiceException(e);
    }
    logMetadata("Added metadata : {}", added);
}
Also used : ArrayList(java.util.ArrayList) MetadataElement(com.adaptris.core.MetadataElement) ServiceException(com.adaptris.core.ServiceException)

Example 74 with MetadataElement

use of com.adaptris.core.MetadataElement in project interlok by adaptris.

the class UrlEncodedMetadataValues method buildEncodedString.

protected String buildEncodedString(AdaptrisMessage msg) throws Exception {
    MetadataCollection filtered = metadataFilter().filter(msg);
    StringBuilder result = new StringBuilder();
    for (MetadataElement e : filtered) {
        if (result.length() > 1) {
            // This is not the first parameter so add a separator
            result.append(separator());
        }
        result.append(e.getKey()).append("=").append(URLEncoder.encode(e.getValue(), StandardCharsets.UTF_8.name()));
    }
    return result.toString();
}
Also used : MetadataCollection(com.adaptris.core.MetadataCollection) MetadataElement(com.adaptris.core.MetadataElement)

Example 75 with MetadataElement

use of com.adaptris.core.MetadataElement in project interlok by adaptris.

the class MapMetadataService method doService.

/**
 * @see com.adaptris.core.Service#doService(AdaptrisMessage)
 */
@Override
public void doService(AdaptrisMessage msg) throws ServiceException {
    if (metadataKey == null || !msg.headersContainsKey(metadataKey)) {
        log.debug("Message does not contain metadatakey [" + metadataKey + "]");
        return;
    }
    String metadataValue = msg.getMetadataValue(metadataKey);
    metadataValue = ObjectUtils.defaultIfNull(metadataValue, "");
    List<MetadataElement> mapped = new ArrayList<>();
    for (KeyValuePair k : getMetadataKeyMap()) {
        if (metadataValue.matches(k.getKey())) {
            String newMetadataValue = doSubstitution(metadataValue, k, msg);
            MetadataElement e = new MetadataElement(metadataKey, newMetadataValue);
            msg.addMetadata(e);
            mapped.add(e);
            break;
        }
    }
    logMetadata("Modified Metadata : {}", mapped);
}
Also used : KeyValuePair(com.adaptris.util.KeyValuePair) ArrayList(java.util.ArrayList) MetadataElement(com.adaptris.core.MetadataElement)

Aggregations

MetadataElement (com.adaptris.core.MetadataElement)151 Test (org.junit.Test)101 AdaptrisMessage (com.adaptris.core.AdaptrisMessage)94 XPath (com.adaptris.util.text.xml.XPath)23 Document (org.w3c.dom.Document)19 MetadataCollection (com.adaptris.core.MetadataCollection)18 AddMetadataService (com.adaptris.core.services.metadata.AddMetadataService)18 ServiceException (com.adaptris.core.ServiceException)17 ServiceList (com.adaptris.core.ServiceList)14 Cache (com.adaptris.core.cache.Cache)14 MetadataCacheValueTranslator (com.adaptris.core.services.cache.translators.MetadataCacheValueTranslator)12 ArrayList (java.util.ArrayList)11 CoreException (com.adaptris.core.CoreException)10 Service (com.adaptris.core.Service)10 HashSet (java.util.HashSet)9 ExpiringMapCache (com.adaptris.core.cache.ExpiringMapCache)8 StandaloneProducer (com.adaptris.core.StandaloneProducer)7 StaticCacheValueTranslator (com.adaptris.core.services.cache.translators.StaticCacheValueTranslator)7 MockMessageProducer (com.adaptris.core.stubs.MockMessageProducer)6 Session (javax.jms.Session)6