/** 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);
    }
  }
  /** Starts a new search using the specified uri target and query parameters. */
  private void doStart(String uriTarget, Map<String, String> queryParams, HttpResponse response)
      throws IOException {

    // Get parameters to start search.
    String searchQuery = queryParams.get("q");

    // Create search.
    SearchDetails searchDetails = RestSearchDetails.createKeywordSearch(searchQuery);
    Search search = searchFactory.createSearch(searchDetails);

    // Add search to search manager.  The search is monitored so it will
    // be cancelled if we stop polling for its results.
    SearchResultList searchList = searchManager.addMonitoredSearch(search, searchDetails);

    // Start search.
    search.start();

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

    } catch (JSONException ex) {
      throw new IOException(ex);
    }
  }
  /** Stops the search and removes the current search listener. */
  @Override
  public void dispose() {
    // Stop search.
    search.stop();

    // Remove search listener.
    if (searchListener != null) {
      search.removeSearchListener(searchListener);
      searchListener = null;
    }
  }
  /**
   * Installs the specified search listener and starts the search. The search listener should handle
   * search results by calling the <code>addSearchResult(SearchResult)</code> method.
   */
  @Override
  public void start(SearchListener searchListener) {
    if (searchListener == null) {
      throw new IllegalArgumentException("Search listener cannot be null");
    }

    // Install search listener.
    this.searchListener = searchListener;
    search.addSearchListener(searchListener);

    // Start search.
    search.start();
  }