public APIKeyValidationInfoDTO getAPIKeyData(
      String context,
      String apiVersion,
      String apiKey,
      String requiredAuthenticationLevel,
      String clientDomain)
      throws APISecurityException {

    CarbonUtils.setBasicAccessSecurityHeaders(
        username, password, true, clientStub._getServiceClient());
    if (cookie != null) {
      clientStub._getServiceClient().getOptions().setProperty(HTTPConstants.COOKIE_STRING, cookie);
    }
    try {
      org.wso2.carbon.apimgt.impl.dto.xsd.APIKeyValidationInfoDTO dto =
          clientStub.validateKey(
              context, apiVersion, apiKey, requiredAuthenticationLevel, clientDomain);
      ServiceContext serviceContext =
          clientStub._getServiceClient().getLastOperationContext().getServiceContext();
      cookie = (String) serviceContext.getProperty(HTTPConstants.COOKIE_STRING);
      return toDTO(dto);
    } catch (APIKeyValidationServiceAPIManagementException ex) {
      throw new APISecurityException(
          APISecurityConstants.API_AUTH_FORBIDDEN, "Resource forbidden", ex);
    } catch (Exception e) {
      throw new APISecurityException(
          APISecurityConstants.API_AUTH_GENERAL_ERROR,
          "Error while accessing backend services for API key validation",
          e);
    }
  }
  public ArrayList<URITemplate> getAllURITemplates(String context, String apiVersion)
      throws APISecurityException {

    CarbonUtils.setBasicAccessSecurityHeaders(
        username, password, true, keyValidationServiceStub._getServiceClient());
    if (cookie != null) {
      keyValidationServiceStub
          ._getServiceClient()
          .getOptions()
          .setProperty(HTTPConstants.COOKIE_STRING, cookie);
    }
    try {
      org.wso2.carbon.apimgt.api.model.xsd.URITemplate[] dto =
          keyValidationServiceStub.getAllURITemplates(context, apiVersion);
      ServiceContext serviceContext =
          keyValidationServiceStub
              ._getServiceClient()
              .getLastOperationContext()
              .getServiceContext();
      cookie = (String) serviceContext.getProperty(HTTPConstants.COOKIE_STRING);
      ArrayList<URITemplate> templates = new ArrayList<URITemplate>();
      for (org.wso2.carbon.apimgt.api.model.xsd.URITemplate aDto : dto) {
        URITemplate temp = toTemplates(aDto);
        templates.add(temp);
      }
      return templates;
    } catch (Exception e) {
      throw new APISecurityException(
          APISecurityConstants.API_AUTH_GENERAL_ERROR,
          "Error while accessing backend services for API key validation",
          e);
    }
  }
  @edu.umd.cs.findbugs.annotations.SuppressWarnings(
      value = "PRMC_POSSIBLY_REDUNDANT_METHOD_CALLS",
      justification = "It is required to set two options on the Options object")
  public APIKeyValidatorClient() throws APISecurityException {
    APIManagerConfiguration config =
        ServiceReferenceHolder.getInstance().getAPIManagerConfiguration();
    String serviceURL = config.getFirstProperty(APIConstants.API_KEY_VALIDATOR_URL);
    username = config.getFirstProperty(APIConstants.API_KEY_VALIDATOR_USERNAME);
    password = config.getFirstProperty(APIConstants.API_KEY_VALIDATOR_PASSWORD);
    if (serviceURL == null || username == null || password == null) {
      throw new APISecurityException(
          APISecurityConstants.API_AUTH_GENERAL_ERROR,
          "Required connection details for the key management server not provided");
    }

    try {
      ConfigurationContext ctx =
          ConfigurationContextFactory.createConfigurationContextFromFileSystem(null, null);
      keyValidationServiceStub =
          new APIKeyValidationServiceStub(ctx, serviceURL + "APIKeyValidationService");
      ServiceClient client = keyValidationServiceStub._getServiceClient();
      Options options = client.getOptions();
      options.setTimeOutInMilliSeconds(TIMEOUT_IN_MILLIS);
      options.setProperty(HTTPConstants.SO_TIMEOUT, TIMEOUT_IN_MILLIS);
      options.setProperty(HTTPConstants.CONNECTION_TIMEOUT, TIMEOUT_IN_MILLIS);
      options.setCallTransportCleanup(true);
      options.setManageSession(true);

    } catch (AxisFault axisFault) {
      throw new APISecurityException(
          APISecurityConstants.API_AUTH_GENERAL_ERROR,
          "Error while initializing the API key validation stub",
          axisFault);
    }
  }
  public APIKeyValidatorClient() throws APISecurityException {
    APIManagerConfiguration config =
        ServiceReferenceHolder.getInstance().getAPIManagerConfiguration();
    String serviceURL = config.getFirstProperty(APIConstants.API_KEY_MANAGER_URL);
    username = config.getFirstProperty(APIConstants.API_KEY_MANAGER_USERNAME);
    password = config.getFirstProperty(APIConstants.API_KEY_MANAGER_PASSWORD);
    if (serviceURL == null || username == null || password == null) {
      throw new APISecurityException(
          APISecurityConstants.API_AUTH_GENERAL_ERROR,
          "Required connection details for the key management server not provided");
    }

    try {
      clientStub = new APIKeyValidationServiceStub(null, serviceURL + "APIKeyValidationService");
      ServiceClient client = clientStub._getServiceClient();
      Options options = client.getOptions();
      options.setTimeOutInMilliSeconds(TIMEOUT_IN_MILLIS);
      options.setProperty(HTTPConstants.SO_TIMEOUT, TIMEOUT_IN_MILLIS);
      options.setProperty(HTTPConstants.CONNECTION_TIMEOUT, TIMEOUT_IN_MILLIS);
      options.setCallTransportCleanup(true);
      options.setManageSession(true);
    } catch (AxisFault axisFault) {
      throw new APISecurityException(
          APISecurityConstants.API_AUTH_GENERAL_ERROR,
          "Error while initializing the API key validation stub",
          axisFault);
    }
  }
  public APIKeyValidationInfoDTO getAPIKeyData(
      String context,
      String apiVersion,
      String apiKey,
      String requiredAuthenticationLevel,
      String clientDomain,
      String matchingResource,
      String httpVerb)
      throws APISecurityException {

    CarbonUtils.setBasicAccessSecurityHeaders(
        username, password, true, keyValidationServiceStub._getServiceClient());
    if (cookie != null) {
      keyValidationServiceStub
          ._getServiceClient()
          .getOptions()
          .setProperty(HTTPConstants.COOKIE_STRING, cookie);
    }
    try {
      List headerList =
          (List)
              keyValidationServiceStub
                  ._getServiceClient()
                  .getOptions()
                  .getProperty(org.apache.axis2.transport.http.HTTPConstants.HTTP_HEADERS);
      Map headers =
          (Map)
              MessageContext.getCurrentMessageContext()
                  .getProperty(org.apache.axis2.context.MessageContext.TRANSPORT_HEADERS);
      if (headers != null) {
        headerList.add(
            new Header(APIConstants.ACTIVITY_ID, (String) headers.get(APIConstants.ACTIVITY_ID)));
      }
      keyValidationServiceStub
          ._getServiceClient()
          .getOptions()
          .setProperty(org.apache.axis2.transport.http.HTTPConstants.HTTP_HEADERS, headerList);
      /**/

      org.wso2.carbon.apimgt.impl.dto.xsd.APIKeyValidationInfoDTO dto =
          keyValidationServiceStub.validateKey(
              context,
              apiVersion,
              apiKey,
              requiredAuthenticationLevel,
              clientDomain,
              matchingResource,
              httpVerb);

      ServiceContext serviceContext =
          keyValidationServiceStub
              ._getServiceClient()
              .getLastOperationContext()
              .getServiceContext();
      cookie = (String) serviceContext.getProperty(HTTPConstants.COOKIE_STRING);
      return toDTO(dto);
    } catch (Exception e) {
      throw new APISecurityException(
          APISecurityConstants.API_AUTH_GENERAL_ERROR,
          "Error while accessing backend services for API key validation",
          e);
    }
  }