/**
   * @param request -Httpservlet request
   * @param accessToken
   * @return
   * @throws APIFaultException
   */
  public boolean doThrottle(HttpServletRequest request, String accessToken)
      throws APIFaultException {

    String apiName = request.getContextPath();
    String apiVersion = APIManagetInterceptorUtils.getAPIVersion(request);
    String apiIdentifier = apiName + "-" + apiVersion;

    APIThrottleHandler throttleHandler = null;
    ConfigurationContext cc = DataHolder.getServerConfigContext();

    if (cc.getProperty(apiIdentifier) == null) {
      throttleHandler = new APIThrottleHandler();
      /* Add the Throttle handler to ConfigContext against API Identifier */
      cc.setProperty(apiIdentifier, throttleHandler);
    } else {
      throttleHandler = (APIThrottleHandler) cc.getProperty(apiIdentifier);
    }

    if (throttleHandler.doThrottle(request, apiKeyValidationDTO, accessToken)) {
      return true;
    } else {
      throw new APIFaultException(
          APIManagerErrorConstants.API_THROTTLE_OUT, "You have exceeded your quota");
    }
  }
Ejemplo n.º 2
0
  public boolean doThrottle(
      HttpServletRequest request,
      APIKeyValidationInfoDTO apiKeyValidationInfoDTO,
      String accessToken) {
    ThrottleManager throttleManager = new ThrottleManager(id, key);
    ConfigurationContext cc = DataHolder.getServerConfigContext();
    ClusteringAgent clusteringAgent = cc.getAxisConfiguration().getClusteringAgent();
    if (clusteringAgent != null && clusteringAgent.getStateManager() != null) {
      isClusteringEnable = true;
    }
    // check the availability of the ConcurrentAccessController
    // if this is a clustered environment
    if (isClusteringEnable) {
      concurrentAccessController = (ConcurrentAccessController) cc.getProperty(key);
    }
    initThrottle(cc);

    // perform concurrency throttling
    boolean canAccess = throttleManager.doThrottleByConcurrency(false, concurrentAccessController);

    if (canAccess) {
      String remoteIP = request.getRemoteAddr();
      canAccess =
          throttleManager.throttleByAccessRate(
                  remoteIP, remoteIP, cc, isClusteringEnable, concurrentAccessController, throttle)
              && throttleManager.doRoleBasedAccessThrottling(
                  isClusteringEnable, cc, apiKeyValidationInfoDTO, accessToken, throttle);
    }
    // All the replication functionality of the access rate based throttling handled by itself
    // Just replicate the current state of ConcurrentAccessController
    if (isClusteringEnable && concurrentAccessController != null) {
      if (cc != null) {
        try {
          Replicator.replicate(cc);
        } catch (ClusteringFault clusteringFault) {
          handleException("Error during the replicating  states ", clusteringFault);
        }
      }
    }
    if (!canAccess) {
      return false;
    }
    return true;
  }