public static int checkSpam(String action, String iplogin, long merchantId) throws Exception {
    int spamResult = 0;

    try {
      spamResult =
          WSSpamImpl.addSpam("0", action, "", action, "", 0, "", 0, merchantId, 0, iplogin);
    } catch (Exception e) {
      throw e;
    }

    return spamResult;
  }
  public static synchronized void authenticate(
      ServiceRequest request, String service, ServiceResponse response, String iplogin)
      throws Exception {
    SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
    String action = "LOGIN";
    int resultSpam = 0;
    MerchantEntry agent = null;
    try {
      agent = getAgent(request);

      if (agent == null) {
        agent = MerchantEntryImpl.getMerchant(request.getAgentId(), request.getPartnerId());

        if (agent != null) {
          cMap.put(agent.getMerchantId(), agent);
        } else {
          resultSpam = checkSpam(action, iplogin, request.getPartnerId());
          if (resultSpam == Constants.EXPIRE_SPAM_ACTION) {
            throw new AppException(ErrorCode.SVC_SPAM);
          }

          throw new AppException(ErrorCode.SVC_ACCESS_AUTHENTICATION_ERROR);
        }
      }

      boolean checkip = checkIP(agent.getIpauthorize(), iplogin);

      if (!checkip) {
        throw new AppException(ErrorCode.SVC_IP_REJECT);
      }

      // check spam

      resultSpam = WSSpamImpl.checkSpam(action, iplogin, request.getPartnerId(), "0");
      if (resultSpam == Constants.EXPIRE_SPAM_ACTION) {
        throw new AppException(ErrorCode.SVC_SPAM);
      }

      String password =
          NonceGenerator.getInstance()
              .getNonce(request.getPartnerId() + agent.getPassword() + request.getrequestDate());
      System.out.println("passs: " + password);
      System.out.println("agent passs: " + agent.getPassword());
      if (request.getPartnerId() != agent.getMerchantId()
          || !request.getPassword().equals(password)) {

        resultSpam = checkSpam(action, iplogin, request.getPartnerId());
        if (resultSpam == Constants.EXPIRE_SPAM_ACTION) {
          throw new AppException(ErrorCode.SVC_SPAM);
        }

        throw new AppException(ErrorCode.SVC_ACCESS_AUTHENTICATION_ERROR);
      }

      if (!agent.getPermisstion().contains(service.toLowerCase())) {
        throw new AppException(ErrorCode.SVC_NOT_HAS_PERMISSION);
      }

      if (agent.getConnectionCounter() < agent.getMaxConnection()) {
        agent.increement();
      } else {
        throw new AppException(ErrorCode.SVC_CONNECTION_LIMIT);
      }
      System.out.println(agent.toString());
      if (agent.getTpsCounter() > agent.getMaxTps()) {
        throw new AppException(ErrorCode.SVC_TPS_LIMIT);
      }

      if (ServicesImpl.checkRequestId(
          request.getrequestId(), request.getPartnerId(), request.getAgentId())) {
        throw new AppException(ErrorCode.SVC_INVALID_REQUESTID);
      }

    } catch (Exception e) {
      e.printStackTrace();
      response.setResult(e.toString());
      response.setResultDescription(ErrorCode.getErrorDetail(e.toString()));
    } finally {
      if (response.getResult() == null) {
        response.setResult(ErrorCode.SVC_SUCCESS);
      }
      if (agent != null) {
        agent.decreement();
        cMap.replace(request.getPartnerId(), agent);
      }
    }
  }