/** Deletes a search using the specified uri target. */
  private void doDelete(String uriTarget, HttpResponse response) throws IOException {
    // Use GUID to get search result list.
    SearchResultList searchList;
    try {
      String guidStr = parseGuid(uriTarget);
      searchList = searchManager.getSearchResultList(new GUID(guidStr));
    } catch (Exception ex) {
      searchList = null;
    }

    if (searchList != null) {
      Search search = searchList.getSearch();

      // Stop search.
      search.stop();

      // Remove search from core management.
      searchManager.removeSearch(search);

      // Set OK status.
      response.setStatusCode(HttpStatus.SC_OK);

    } else {
      response.setStatusCode(HttpStatus.SC_NOT_FOUND);
    }
  }
  /** Processes request to retrieve data for a single search. */
  private void doGetSearch(String uriTarget, Map<String, String> queryParams, HttpResponse response)
      throws IOException {

    // Use GUID to get search result list.
    SearchResultList searchList;
    try {
      String guidStr = parseGuid(uriTarget);
      searchList = searchManager.getSearchResultList(new GUID(guidStr));
    } catch (Exception ex) {
      searchList = null;
    }

    // Return empty object if null.
    if (searchList == null) {
      response.setEntity(RestUtils.createStringEntity("{}"));
      response.setStatusCode(HttpStatus.SC_OK);
      return;
    }

    try {
      if (uriTarget.indexOf(FILES) < 0) {
        // Return search metadata.
        JSONObject jsonObj = RestUtils.createSearchJson(searchList);
        HttpEntity entity = RestUtils.createStringEntity(jsonObj.toString());
        response.setEntity(entity);
        response.setStatusCode(HttpStatus.SC_OK);

      } else {
        // Get query parameters.
        String offsetStr = queryParams.get("offset");
        String limitStr = queryParams.get("limit");
        int offset = (offsetStr != null) ? Integer.parseInt(offsetStr) : 0;
        int limit =
            (limitStr != null) ? Math.min(Integer.parseInt(limitStr), MAX_LIMIT) : MAX_LIMIT;

        // Create search result array.
        List<GroupedSearchResult> resultList =
            new ArrayList<GroupedSearchResult>(searchList.getGroupedResults());
        JSONArray jsonArr = new JSONArray();
        for (int i = offset, max = Math.min(offset + limit, resultList.size()); i < max; i++) {
          GroupedSearchResult result = resultList.get(i);
          jsonArr.put(RestUtils.createSearchResultJson(result));
        }

        // Set response entity and status.
        HttpEntity entity = RestUtils.createStringEntity(jsonArr.toString());
        response.setEntity(entity);
        response.setStatusCode(HttpStatus.SC_OK);
      }

    } catch (JSONException ex) {
      throw new IOException(ex);
    }
  }