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

com.anaptecs.jeaf.workload.proxy.ProxyTarget 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 java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
import org.apache.hc.core5.http.ClassicHttpRequest;

import com.anaptecs.jeaf.workload.api.RequestType;

import io.github.resilience4j.circuitbreaker.CircuitBreaker;

/**
 * Class represents a so called proxy target. Proxy targets are target server to which requests will be delegated.
 * 
 * @author JEAF Development Team
 */
public class ProxyTarget {
  /**
   * Name for the proxy target.
   */
  private String name;

  /**
   * Host name of the target server.
   */
  private String targetServer;

  /**
   * Type of request that are processed by the target. The value controls the possible mechanisms that are available to
   * dispatch the request to the matching pipelines.
   */
  private RequestTypeCategory requestTypeCategory;

  /**
   * Request URI that is used to detect that an incoming request on the workload proxy should be sent to this proxy
   * target.
   */
  private String requestURI;

  /**
   * Default http request executor that is used when ever no specific executor is defined for a request type.
   */
  private HttpRequestExecutor defaultRequestExecutor = new HttpRequestExecutor();

  /**
   * List contains all custom request executors.
   */
  private List customRequestExecutors = new ArrayList<>();

  /**
   * Map contains mapping from request key to custom request executors.
   */
  private Map> executorMappings = new HashMap<>();

  private Map requestExecutors = new HashMap<>();

  /**
   * List contains all defined key resolution rules that belong to this proxy target.
   */
  private List keyResolutionRules = new ArrayList<>();

  /**
   * Attribute defines if this proxy target only has default key resolution rule.
   */
  private boolean hasOnlyDefaultRule = false;

  public String getName( ) {
    return name;
  }

  public void setName( String pName ) {
    name = pName;
  }

  public String getTargetServer( ) {
    return targetServer;
  }

  public void setTargetServer( String pTargetServer ) {
    targetServer = pTargetServer;
  }

  public RequestTypeCategory getRequestTypeCategory( ) {
    return requestTypeCategory;
  }

  public void setRequestTypeCategory( RequestTypeCategory pRequestTypeCategory ) {
    requestTypeCategory = pRequestTypeCategory;
  }

  public String getRequestURI( ) {
    return requestURI;
  }

  public void setRequestURI( String pRequestURI ) {
    requestURI = pRequestURI;
  }

  public List getCustomRequestExecutors( ) {
    return customRequestExecutors;
  }

  public void setCustomRequestExecutors( List pRequestExecutors ) {
    customRequestExecutors = pRequestExecutors;
  }

  public Map> getExecutorMappings( ) {
    return executorMappings;
  }

  public void setExecutorMappings( Map> pExecutorMappings ) {
    executorMappings = pExecutorMappings;
  }

  public List getKeyResolutionRules( ) {
    return keyResolutionRules;
  }

  public void setKeyResolutionRules( List pKeyResolutionRules ) {
    keyResolutionRules = pKeyResolutionRules;
    this.analyzeKeyResolutionRules();
  }

  public void addKeyResolutionRule( KeyResolutionRule pKeyResolutionRule ) {
    keyResolutionRules.add(pKeyResolutionRule);
    this.analyzeKeyResolutionRules();
  }

  public boolean hasOnlyDefaultRule( ) {
    return hasOnlyDefaultRule;
  }

  public void initialize( ) {
    // Build up map that contains mapping from request key to its matching executor.
    Map lExecutors = new HashMap<>();
    customRequestExecutors.forEach(x -> lExecutors.put(x.getName(), x));

    for (Entry> lNext : executorMappings.entrySet()) {
      HttpRequestExecutor lHttpRequestExecutor = lExecutors.get(lNext.getKey());
      lNext.getValue().forEach(x -> requestExecutors.put(x, lHttpRequestExecutor));
    }

    // Initialize all request executors.
    defaultRequestExecutor.initialize();
    customRequestExecutors.forEach(x -> x.initialize());
  }

  public HttpRequestExecutor getDefaultRequestExecutor( ) {
    return defaultRequestExecutor;
  }

  public HttpRequestExecutor getRequestExecutor( RequestType pRequestType ) {
    return requestExecutors.getOrDefault(pRequestType.getRequestKey().getKey(), defaultRequestExecutor);
  }

  public void setDefaultRequestExecutor( HttpRequestExecutor pDefaultHttpRequestExecutor ) {
    defaultRequestExecutor = pDefaultHttpRequestExecutor;
  }

  public CloseableHttpClient getDefaultHttpClient( ) {
    return defaultRequestExecutor.getHttpClient();
  }

  public CircuitBreaker getDefaultCircuitBreaker( ) {
    return defaultRequestExecutor.getCircuitBreaker();
  }

  public CloseableHttpResponse executeRequest( ClassicHttpRequest pRequest ) {
    return defaultRequestExecutor.executeRequest(pRequest);
  }

  private void analyzeKeyResolutionRules( ) {
    // Check if we only have a default rule.
    if (keyResolutionRules.size() == 1) {
      hasOnlyDefaultRule = keyResolutionRules.get(0).isDefaultRule();
    }
    else {
      hasOnlyDefaultRule = false;
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy