All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.anaptecs.jeaf.workload.proxy.KeyResolutionRule Maven / Gradle / Ivy

The newest version!
/**
 * Copyright 2004 - 2020 anaptecs GmbH, Burgstr. 96, 72764 Reutlingen, Germany
 *
 * All rights reserved.
 */
package com.anaptecs.jeaf.workload.proxy;

import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathException;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathFactory;

/**
 * Class represents a key resolution rule. Key resolution rules are executed on incoming request in order to resolve
 * their request key.
 * 
 * Class can be serialized using Yaml:
 */
public class KeyResolutionRule {
  /**
   * Request key that is used if the rule defined by this class matches.
   */
  private String requestKey;

  /**
   * XPath expression that is evaluated to check if this rule matches.
   */
  private String xpathExpression;

  /**
   * Creating an XPath expression is rather expensive. So we avoid to do that more often than required ;-)
   */
  private XPathExpression cachedXPathExpression;

  public String getRequestKey( ) {
    return requestKey;
  }

  public void setRequestKey( String pRequestKey ) {
    requestKey = pRequestKey;
  }

  public String getXpathExpression( ) {
    return xpathExpression;
  }

  public void setXpathExpression( String pXpathExpression ) {
    xpathExpression = pXpathExpression;
  }

  public XPathExpression getCachedXPathExpression( ) throws XPathException {
    // A real XPath expression is configured, so let's try to compile it.
    XPathExpression lXPathExpression;
    if (xpathExpression != null) {
      if (cachedXPathExpression == null) {
        // Create XPath object
        XPathFactory lXPathFactory = XPathFactory.newInstance();
        XPath lXPath = lXPathFactory.newXPath();
        cachedXPathExpression = lXPath.compile(xpathExpression);
      }
      lXPathExpression = cachedXPathExpression;
    }
    // No XPath expression is configured.
    else {
      lXPathExpression = null;
    }
    return lXPathExpression;
  }

  public boolean isDefaultRule( ) {
    return xpathExpression == null;
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy