public void writeResponse()

in solr/core/src/java/org/apache/solr/response/CSVResponseWriter.java [163:370]


  public void writeResponse() throws IOException {
    SolrParams params = req.getParams();

    char delimiter = ',';
    String sep = params.get(CSV_SEPARATOR);
    if (sep != null) {
      if (sep.length() != 1)
        throw new SolrException(
            SolrException.ErrorCode.BAD_REQUEST, "Invalid separator:'" + sep + "'");
      delimiter = sep.charAt(0);
    }

    String printerNewline = "\n";
    String nl = params.get(CSV_NEWLINE);
    if (nl != null) {
      if (nl.length() == 0)
        throw new SolrException(
            SolrException.ErrorCode.BAD_REQUEST, "Invalid newline:'" + nl + "'");
      printerNewline = nl;
    }

    String encapsulator = params.get(CSV_ENCAPSULATOR);
    String escape = params.get(CSV_ESCAPE);

    char encapsulatorChar = '"';
    if (encapsulator != null) {
      if (encapsulator.length() != 1)
        throw new SolrException(
            SolrException.ErrorCode.BAD_REQUEST, "Invalid encapsulator:'" + encapsulator + "'");
      encapsulatorChar = encapsulator.charAt(0);
    }

    char escapeChar = CSVStrategy.ESCAPE_DISABLED;
    if (escape != null) {
      if (escape.length() != 1)
        throw new SolrException(
            SolrException.ErrorCode.BAD_REQUEST, "Invalid escape:'" + escape + "'");
      escapeChar = escape.charAt(0);
      if (encapsulator == null) {
        encapsulatorChar = CSVStrategy.ENCAPSULATOR_DISABLED;
      }
    }

    boolean interpretUnicodeEscapes = false;
    if (escapeChar == '\\') {
      // If the escape is the standard backslash, then also enable
      // unicode escapes (it's harmless since 'u' would not otherwise
      // be escaped.
      interpretUnicodeEscapes = true;
    }

    strategy =
        new CSVStrategy(
            delimiter,
            encapsulatorChar,
            CSVStrategy.COMMENTS_DISABLED,
            escapeChar,
            false,
            false,
            interpretUnicodeEscapes,
            true,
            printerNewline);

    printer = new CSVPrinter(writer, strategy);

    char mvStrategyDelimiter = strategy.getDelimiter();
    sep = params.get(MV_SEPARATOR);
    if (sep != null) {
      if (sep.length() != 1)
        throw new SolrException(
            SolrException.ErrorCode.BAD_REQUEST, "Invalid mv separator:'" + sep + "'");
      mvStrategyDelimiter = sep.charAt(0);
    }

    encapsulator = params.get(MV_ENCAPSULATOR);
    escape = params.get(MV_ESCAPE);

    char mvStrategyEncapsulatorChar = CSVStrategy.ENCAPSULATOR_DISABLED;
    char mvStrategyEscape = '\\';

    if (encapsulator != null) {
      if (encapsulator.length() != 1)
        throw new SolrException(
            SolrException.ErrorCode.BAD_REQUEST, "Invalid mv encapsulator:'" + encapsulator + "'");
      mvStrategyEncapsulatorChar = encapsulator.charAt(0);
      if (escape == null) {
        mvStrategyEscape = CSVStrategy.ESCAPE_DISABLED;
      }
    }

    escape = params.get(MV_ESCAPE);
    if (escape != null) {
      if (escape.length() != 1)
        throw new SolrException(
            SolrException.ErrorCode.BAD_REQUEST, "Invalid mv escape:'" + escape + "'");
      mvStrategyEscape = escape.charAt(0);
      // encapsulator will already be disabled if it wasn't specified
    }

    CSVStrategy mvStrategy =
        new CSVStrategy(
            mvStrategyDelimiter,
            mvStrategyEncapsulatorChar,
            CSVStrategy.COMMENTS_DISABLED,
            mvStrategyEscape,
            false,
            false,
            false,
            false,
            "\n");

    Collection<String> fields = getFields();
    CSVSharedBufPrinter csvPrinterMV = new CSVSharedBufPrinter(mvWriter, mvStrategy);

    for (String field : fields) {
      if (!returnFields.wantsField(field)) {
        continue;
      }
      if (field.equals("score")) {
        CSVField csvField = new CSVField();
        csvField.name = "score";
        csvFields.put("score", csvField);
        continue;
      }

      if (shouldSkipField(field)) {
        continue;
      }

      SchemaField sf = schema.getFieldOrNull(field);
      if (sf == null) {
        FieldType ft = new StrField();
        sf = new SchemaField(field, ft);
      }

      // check for per-field overrides
      sep = params.get("f." + field + '.' + CSV_SEPARATOR);
      encapsulator = params.get("f." + field + '.' + CSV_ENCAPSULATOR);
      escape = params.get("f." + field + '.' + CSV_ESCAPE);

      // if polyfield and no escape is provided, add "\\" escape by default
      if (sf.isPolyField()) {
        escape = (escape == null) ? "\\" : escape;
      }

      CSVSharedBufPrinter csvPrinter = csvPrinterMV;
      if (sep != null || encapsulator != null || escape != null) {
        // create a new strategy + printer if there were any per-field overrides
        char fDelimiter = mvStrategy.getDelimiter();
        char fEncapsulator = mvStrategy.getEncapsulator();
        char fEscape = mvStrategy.getEscape();
        if (sep != null) {
          if (sep.length() != 1)
            throw new SolrException(
                SolrException.ErrorCode.BAD_REQUEST, "Invalid mv separator:'" + sep + "'");
          fDelimiter = sep.charAt(0);
        }
        if (encapsulator != null) {
          if (encapsulator.length() != 1)
            throw new SolrException(
                SolrException.ErrorCode.BAD_REQUEST,
                "Invalid mv encapsulator:'" + encapsulator + "'");
          fEncapsulator = encapsulator.charAt(0);
          if (escape == null) {
            fEscape = CSVStrategy.ESCAPE_DISABLED;
          }
        }
        if (escape != null) {
          if (escape.length() != 1)
            throw new SolrException(
                SolrException.ErrorCode.BAD_REQUEST, "Invalid mv escape:'" + escape + "'");
          fEscape = escape.charAt(0);
          if (encapsulator == null) {
            fEncapsulator = CSVStrategy.ENCAPSULATOR_DISABLED;
          }
        }
        final CSVStrategy strat =
            new CSVStrategy(
                fDelimiter,
                fEncapsulator,
                mvStrategy.getCommentStart(),
                fEscape,
                mvStrategy.getIgnoreLeadingWhitespaces(),
                mvStrategy.getIgnoreTrailingWhitespaces(),
                mvStrategy.getUnicodeEscapeInterpretation(),
                mvStrategy.getIgnoreEmptyLines(),
                mvStrategy.getPrinterNewline());
        csvPrinter = new CSVSharedBufPrinter(mvWriter, strat);
      }

      CSVField csvField = new CSVField();
      csvField.name = field;
      csvField.sf = sf;
      csvField.mvPrinter = csvPrinter;
      csvFields.put(field, csvField);
    }

    NullValue = params.get(CSV_NULL, "");

    if (params.getBool(CSV_HEADER, true)) {
      for (CSVField csvField : csvFields.values()) {
        printer.print(csvField.name);
      }
      printer.println();
    }

    writeResponse(rsp.getResponse());
  }