/*
   * (non-Javadoc)
   * @see org.jasig.portlet.weather.dao.IWeatherDao#find(java.lang.String)
   */
  public Collection<Location> find(String location) {
    final String url =
        FIND_URL
            .replace("@KEY@", key)
            .replace("@QUERY@", QuietUrlCodec.encode(location, Constants.URL_ENCODING));

    HttpMethod getMethod = new GetMethod(url);
    InputStream inputStream = null;
    try {
      // Execute the method.
      int statusCode = httpClient.executeMethod(getMethod);
      if (statusCode != HttpStatus.SC_OK) {
        final String statusText = getMethod.getStatusText();
        throw new DataRetrievalFailureException(
            "get of '"
                + url
                + "' failed with status '"
                + statusCode
                + "' due to '"
                + statusText
                + "'");
      }

      // Read the response body
      inputStream = getMethod.getResponseBodyAsStream();

      List<Location> locations = deserializeSearchResults(inputStream);

      return locations;

    } catch (HttpException e) {
      throw new RuntimeException(
          "http protocol exception while getting data from weather service from: " + url, e);
    } catch (IOException e) {
      throw new RuntimeException(
          "IO exception while getting data from weather service from: " + url, e);
    } catch (JAXBException e) {
      throw new RuntimeException(
          "Parsing exception while getting data from weather service from: " + url, e);
    } finally {
      // try to close the inputstream
      IOUtils.closeQuietly(inputStream);
      // release the connection
      getMethod.releaseConnection();
    }
  }
  protected Object getAndDeserialize(String url, TemperatureUnit unit) {
    HttpMethod getMethod = new GetMethod(url);
    InputStream inputStream = null;
    try {
      // Execute the method.
      int statusCode = httpClient.executeMethod(getMethod);
      if (statusCode != HttpStatus.SC_OK) {
        final String statusText = getMethod.getStatusText();
        throw new DataRetrievalFailureException(
            "get of '"
                + url
                + "' failed with status '"
                + statusCode
                + "' due to '"
                + statusText
                + "'");
      }

      // Read the response body
      inputStream = getMethod.getResponseBodyAsStream();

      Weather weather = deserializeWeatherResult(inputStream, unit);

      return weather;

    } catch (HttpException e) {
      throw new RuntimeException(
          "http protocol exception while getting data from weather service from: " + url, e);
    } catch (IOException e) {
      throw new RuntimeException(
          "IO exception while getting data from weather service from: " + url, e);
    } catch (JAXBException e) {
      throw new RuntimeException(
          "Parsing exception while getting data from weather service from: " + url, e);
    } catch (ParseException e) {
      throw new RuntimeException(
          "Parsing exception while getting data from weather service from: " + url, e);
    } finally {
      // try to close the inputstream
      IOUtils.closeQuietly(inputStream);
      // release the connection
      getMethod.releaseConnection();
    }
  }
  /**
   * Makes a rest request of any type at the specified urlSuffix. The urlSuffix should be of the
   * form /userService/users. If CS throws an exception it handled and transalated to a Openfire
   * exception if possible. This is done using the check fault method that tries to throw the best
   * maching exception.
   *
   * @param type Must be GET or DELETE
   * @param urlSuffix The url suffix of the rest request
   * @param xmlParams The xml with the request params, must be null if type is GET or DELETE only
   * @return The response as a xml doc.
   * @throws ConnectionException Thrown if there are issues perfoming the request.
   * @throws Exception Thrown if the response from Clearspace contains an exception.
   */
  public Element executeRequest(HttpType type, String urlSuffix, String xmlParams)
      throws ConnectionException, Exception {
    if (Log.isDebugEnabled()) {
      Log.debug("Outgoing REST call [" + type + "] to " + urlSuffix + ": " + xmlParams);
    }

    String wsUrl = getConnectionURI() + WEBSERVICES_PATH + urlSuffix;

    String secret = getSharedSecret();

    HttpClient client = new HttpClient();
    HttpMethod method;

    // Configures the authentication
    client.getParams().setAuthenticationPreemptive(true);
    Credentials credentials = new UsernamePasswordCredentials(OPENFIRE_USERNAME, secret);
    AuthScope scope = new AuthScope(host, port, AuthScope.ANY_REALM);
    client.getState().setCredentials(scope, credentials);

    // Creates the method
    switch (type) {
      case GET:
        method = new GetMethod(wsUrl);
        break;
      case POST:
        PostMethod pm = new PostMethod(wsUrl);
        StringRequestEntity requestEntity = new StringRequestEntity(xmlParams);
        pm.setRequestEntity(requestEntity);
        method = pm;
        break;
      case PUT:
        PutMethod pm1 = new PutMethod(wsUrl);
        StringRequestEntity requestEntity1 = new StringRequestEntity(xmlParams);
        pm1.setRequestEntity(requestEntity1);
        method = pm1;
        break;
      case DELETE:
        method = new DeleteMethod(wsUrl);
        break;
      default:
        throw new IllegalArgumentException();
    }

    method.setRequestHeader("Accept", "text/xml");
    method.setDoAuthentication(true);

    try {
      // Executes the request
      client.executeMethod(method);

      // Parses the result
      String body = method.getResponseBodyAsString();
      if (Log.isDebugEnabled()) {
        Log.debug("Outgoing REST call results: " + body);
      }

      // Checks the http status
      if (method.getStatusCode() != 200) {
        if (method.getStatusCode() == 401) {
          throw new ConnectionException(
              "Invalid password to connect to Clearspace.",
              ConnectionException.ErrorType.AUTHENTICATION);
        } else if (method.getStatusCode() == 404) {
          throw new ConnectionException(
              "Web service not found in Clearspace.", ConnectionException.ErrorType.PAGE_NOT_FOUND);
        } else if (method.getStatusCode() == 503) {
          throw new ConnectionException(
              "Web service not avaible in Clearspace.",
              ConnectionException.ErrorType.SERVICE_NOT_AVAIBLE);
        } else {
          throw new ConnectionException(
              "Error connecting to Clearspace, http status code: " + method.getStatusCode(),
              new HTTPConnectionException(method.getStatusCode()),
              ConnectionException.ErrorType.OTHER);
        }
      } else if (body.contains("Clearspace Upgrade Console")) {
        // TODO Change CS to send a more standard error message
        throw new ConnectionException(
            "Clearspace is in an update state.", ConnectionException.ErrorType.UPDATE_STATE);
      }

      Element response = localParser.get().parseDocument(body).getRootElement();

      // Check for exceptions
      checkFault(response);

      // Since there is no exception, returns the response
      return response;
    } catch (DocumentException e) {
      throw new ConnectionException(
          "Error parsing the response of Clearspace.", e, ConnectionException.ErrorType.OTHER);
    } catch (HttpException e) {
      throw new ConnectionException(
          "Error performing http request to Clearspace", e, ConnectionException.ErrorType.OTHER);
    } catch (UnknownHostException e) {
      throw new ConnectionException(
          "Unknown Host " + getConnectionURI() + " trying to connect to Clearspace",
          e,
          ConnectionException.ErrorType.UNKNOWN_HOST);
    } catch (IOException e) {
      throw new ConnectionException(
          "Error peforming http request to Clearspace.", e, ConnectionException.ErrorType.OTHER);
    } finally {
      method.releaseConnection();
    }
  }
  String getConfigureInformation(
      String dataId, String group, long timeout, boolean skipContentCache) {
    start();
    if (!isRun) {
      throw new RuntimeException(
          "DiamondSubscriber is not running, so can't fetch from ConfigureInformation");
    }
    if (null == group) {
      group = Constants.DEFAULT_GROUP;
    }
    // =======================ʹ�ò���ģʽ=======================
    if (MockServer.isTestMode()) {
      return MockServer.getConfigInfo(dataId, group);
    }

    if (!skipContentCache) {
      String key = makeCacheKey(dataId, group);
      String content = contentCache.get(key);
      if (content != null) {
        return content;
      }
    }

    long waitTime = 0;

    String uri = getUriString(dataId, group);
    if (log.isInfoEnabled()) {
      log.info(uri);
    }

    CacheData cacheData = getCacheData(dataId, group);

    int retryTimes = this.getDiamondConfigure().getRetrieveDataRetryTimes();
    log.info("Retry times is " + retryTimes);
    int tryCount = 0;

    while (0 == timeout || timeout > waitTime) {
      tryCount++;
      if (tryCount > retryTimes + 1) {
        log.warn("Retry time reach the limit, so break");
        break;
      }
      log.info("Fetch config " + tryCount + "times, waitTime:" + waitTime);

      long onceTimeOut = getOnceTimeOut(waitTime, timeout);
      waitTime += onceTimeOut;

      HttpMethod httpMethod = new GetMethod(uri);

      configureHttpMethod(skipContentCache, cacheData, onceTimeOut, httpMethod);

      try {
        int httpStatus = httpClient.executeMethod(httpMethod);

        switch (httpStatus) {
          case SC_OK:
            {
              String result = getSuccess(dataId, group, cacheData, httpMethod);
              return result;
            }

          case SC_NOT_MODIFIED:
            {
              String result = getNotModified(dataId, cacheData, httpMethod);
              return result;
            }

          case SC_NOT_FOUND:
            {
              log.warn("DataID:" + dataId + "not found");
              cacheData.setMd5(Constants.NULL);
              this.snapshotConfigInfoProcessor.removeSnapshot(dataId, group);
              return null;
            }

          case SC_SERVICE_UNAVAILABLE:
            {
              rotateToNextDomain();
            }
            break;

          default:
            {
              log.warn("HTTP State: " + httpStatus + ":" + httpClient.getState());
              rotateToNextDomain();
            }
        }
      } catch (HttpException e) {
        log.error("Fetch config HttpException", e);
        rotateToNextDomain();
      } catch (IOException e) {
        log.error("Fetch config IOException", e);
        rotateToNextDomain();
      } catch (Exception e) {
        log.error("Unknown Exception", e);
        rotateToNextDomain();
      } finally {
        httpMethod.releaseConnection();
      }
    }
    throw new RuntimeException(
        "Fetch config timeout, DataID=" + dataId + ", Group=" + group + ",timeout=" + timeout);
  }