Esempio n. 1
0
 /**
  * Checks the response body as a substring or regular expression match according to the
  * leading-tilde convention
  *
  * @param config ConnectionConfig object from which response-text property is extracted
  * @param response Body of HTTP response to check
  * @return Whether the response matches the response-text property
  */
 protected boolean checkResponseBody(ConnectionConfig config, String response) {
   String expectedResponse = config.getKeyedString(PROPERTY_NAME_RESPONSE_TEXT, null);
   if (expectedResponse == null) {
     return true;
   }
   if (expectedResponse.startsWith("~")) {
     Pattern bodyPat = Pattern.compile(expectedResponse.substring(1), Pattern.DOTALL);
     return bodyPat.matcher(response).matches();
   } else {
     return response.contains(expectedResponse);
   }
 }
Esempio n. 2
0
  /** {@inheritDoc} */
  @Override
  protected boolean checkProtocol(Socket socket, ConnectionConfig config) throws IOException {
    boolean isAServer = false;

    m_queryString =
        "GET " + config.getKeyedString(PROPERTY_NAME_URL, DEFAULT_URL) + " HTTP/1.0\r\n\r\n";

    LOG.debug("Query: {}", m_queryString);

    try {
      BufferedReader lineRdr = new BufferedReader(new InputStreamReader(socket.getInputStream()));

      socket.getOutputStream().write(m_queryString.getBytes());
      char[] cbuf = new char[1024];
      int chars = 0;
      StringBuffer response = new StringBuffer();
      try {
        while ((chars = lineRdr.read(cbuf, 0, 1024)) != -1) {
          String line = new String(cbuf, 0, chars);
          LOG.debug("Read: {} bytes: [{}] from socket.", line.length(), line);
          response.append(line);
        }
      } catch (java.net.SocketTimeoutException timeoutEx) {
        if (timeoutEx.bytesTransferred > 0) {
          String line = new String(cbuf, 0, timeoutEx.bytesTransferred);
          LOG.debug("Read: {} bytes: [{}] from socket @ timeout!", line.length(), line);
          response.append(line);
        }
      }
      if (response.toString() != null && response.toString().indexOf(m_responseString) > -1) {
        if (m_checkReturnCode) {
          int maxRetCode = config.getKeyedInteger(PROPERTY_NAME_MAX_RET_CODE, 399);
          if ((DEFAULT_URL.equals(config.getKeyedString(PROPERTY_NAME_URL, DEFAULT_URL)))
              || (config.getKeyedBoolean(PROPERTY_NAME_RETURN_CODE, true) == false)) {
            maxRetCode = 600;
          }
          StringTokenizer t = new StringTokenizer(response.toString());
          t.nextToken();
          int rVal = Integer.parseInt(t.nextToken());
          LOG.debug("{} : Request returned code: {}", getPluginName(), rVal);
          if (rVal >= 99 && rVal <= maxRetCode) isAServer = true;
        } else {
          isAServer = true;
        }
        if (isAServer) {
          isAServer = checkResponseBody(config, response.toString());
        }
      }
    } catch (SocketException e) {
      LOG.debug(
          "{}: a protocol error occurred talking to host {}",
          getPluginName(),
          InetAddressUtils.str(config.getInetAddress()),
          e);
      isAServer = false;
    } catch (NumberFormatException e) {
      LOG.debug(
          "{}: failed to parse response code from host {}",
          getPluginName(),
          InetAddressUtils.str(config.getInetAddress()),
          e);
      isAServer = false;
    }
    return isAServer;
  }