<commit_before>package de.fhg.igd.georocket.commands;

import java.io.IOException;
import java.io.PrintWriter;
import java.net.URLEncoder;

import io.vertx.core.Handler;
import io.vertx.core.http.HttpClient;

/**
 * Abstract base class for commands that need to export data
 * @author <NAME>
 */
public abstract class AbstractQueryCommand extends AbstractGeoRocketCommand {
  /**
   * Export using a search query and a layer
   * @param query the search query (may be null)
   * @param layer the layer to export (may be null)
   * @param out the writer to write the results to
   * @param handler the handler that should be called when all
   * chunks have been exported
   * @throws IOException if the query or the layer was invalid
   */
  protected void export(String query, String layer, PrintWriter out,
      Handler<Integer> handler) throws IOException {
    if (layer == null || layer.isEmpty()) {
      layer = "/";
    }
    if (!layer.endsWith("/")) {
      layer += "/";
    }
    if (!layer.startsWith("/")) {
      layer = "/" + layer;
    }
    
    String urlQuery = "";
    if (query != null && !query.isEmpty()) {
      urlQuery = "?search=" + URLEncoder.encode(query, "UTF-8");
    }
    
    HttpClient client = vertx.createHttpClient();
    client.getNow(63074, "localhost", "/store" + layer + urlQuery, response -> {
      if (response.statusCode() != 200) {
        error(response.statusMessage());
        client.close();
        handler.handle(1);
      } else {
        response.handler(buf -> {
          out.write(buf.toString());
        });
        response.endHandler(v -> {
          client.close();
          handler.handle(0);
        });
      }
    });
  }
}<commit_msg>Better exception handling when querying<commit_after>package de.fhg.igd.georocket.commands;

import java.io.IOException;
import java.io.PrintWriter;
import java.net.URLEncoder;

import io.vertx.core.Handler;
import io.vertx.core.http.HttpClient;
import io.vertx.core.http.HttpClientRequest;
import io.vertx.core.logging.Logger;
import io.vertx.core.logging.LoggerFactory;

/**
 * Abstract base class for commands that need to export data
 * @author <NAME>
 */
public abstract class AbstractQueryCommand extends AbstractGeoRocketCommand {
  private static Logger log = LoggerFactory.getLogger(AbstractQueryCommand.class);
  
  /**
   * Export using a search query and a layer
   * @param query the search query (may be null)
   * @param layer the layer to export (may be null)
   * @param out the writer to write the results to
   * @param handler the handler that should be called when all
   * chunks have been exported
   * @throws IOException if the query or the layer was invalid
   */
  protected void export(String query, String layer, PrintWriter out,
      Handler<Integer> handler) throws IOException {
    if (layer == null || layer.isEmpty()) {
      layer = "/";
    }
    if (!layer.endsWith("/")) {
      layer += "/";
    }
    if (!layer.startsWith("/")) {
      layer = "/" + layer;
    }
    
    String urlQuery = "";
    if (query != null && !query.isEmpty()) {
      urlQuery = "?search=" + URLEncoder.encode(query, "UTF-8");
    }
    
    HttpClient client = vertx.createHttpClient();
    HttpClientRequest request = client.get(63074, "localhost", "/store" + layer + urlQuery);
    request.exceptionHandler(t -> {
      error(t.getMessage());
      log.error("Could not query store", t);
      client.close();
      handler.handle(1);
    });
    request.handler(response -> {
      if (response.statusCode() != 200) {
        error(response.statusMessage());
        client.close();
        handler.handle(1);
      } else {
        response.handler(buf -> {
          out.write(buf.toString());
        });
        response.endHandler(v -> {
          client.close();
          handler.handle(0);
        });
      }
    });
    request.end();
  }
}
{'fraction_non_alphanumeric': 0.13195876288659794, 'fraction_numerical': 0.0059278350515463915, 'mean_word_length': 2.696190476190476, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 14, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6724162', 'n_tokens_mistral': 1142, 'n_tokens_neox': 1122, 'n_words': 313}