Esempio n. 1
0
  /**
   * Interrogates the Request parameters and returns a prepared and parsed set of rows from the CSV
   * file.
   *
   * @param params the Request parameters
   * @return The rows from the uploaded CSV file
   * @throws IOException
   */
  public static Iterator<String[]> getRowsFromCsv(final Parameters params) throws IOException {
    final Csv csv = new Csv();
    InputStream is = params.getFile();

    if (params.getDelimiter() != null) {
      log.debug("Setting Field Delimiter to [ {} ]", params.getDelimiter());
      csv.setFieldDelimiter(params.getDelimiter());
    }

    if (params.getSeparator() != null) {
      log.debug("Setting Field Separator to [ {} ]", params.getSeparator());
      csv.setFieldSeparatorRead(params.getSeparator());
    }

    // Hack to prevent empty-value ending lines from breaking
    is =
        terminateLines(
            is,
            params.getSeparator() != null ? params.getSeparator() : csv.getFieldSeparatorRead(),
            params.getCharset());

    return csv.read(is, params.getCharset());
  }
  @Override
  protected final void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response)
      throws ServletException, IOException {

    response.setContentType("application/json");
    response.setCharacterEncoding("UTF-8");

    final JSONObject jsonResponse = new JSONObject();

    final TagManager tagManager = request.getResourceResolver().adaptTo(TagManager.class);

    final RequestParameter charsetParam = request.getRequestParameter("charset");
    final RequestParameter cleanParam = request.getRequestParameter("clean");
    final RequestParameter delimiterParam = request.getRequestParameter("delimiter");
    final RequestParameter fileParameter = request.getRequestParameter("file");
    final RequestParameter separatorParam = request.getRequestParameter("separator");
    final RequestParameter converterParam = request.getRequestParameter("converter");
    final RequestParameter fallbackConverterParam =
        request.getRequestParameter("fallbackConverter");

    boolean clean = DEFAULT_CLEAN;
    if (cleanParam != null) {
      clean =
          Boolean.valueOf(
              StringUtils.defaultIfEmpty(cleanParam.toString(), String.valueOf(DEFAULT_CLEAN)));
    }

    String converter = DEFAULT_CONVERTER;
    if (converterParam != null) {
      converter = StringUtils.defaultIfEmpty(converterParam.toString(), DEFAULT_CONVERTER);
    }

    String fallbackConverter = NONE_CONVERTER;
    if (fallbackConverterParam != null) {
      fallbackConverter =
          StringUtils.defaultIfEmpty(fallbackConverterParam.toString(), NONE_CONVERTER);
    }

    String charset = DEFAULT_CHARSET;
    if (charsetParam != null) {
      charset = StringUtils.defaultIfEmpty(charsetParam.toString(), DEFAULT_CHARSET);
    }

    Character delimiter = null;
    if (delimiterParam != null && StringUtils.isNotBlank(delimiterParam.toString())) {
      delimiter = delimiterParam.toString().charAt(0);
    }

    Character separator = null;
    if (separatorParam != null && StringUtils.isNotBlank(separatorParam.toString())) {
      separator = separatorParam.toString().charAt(0);
    }

    final List<TagDataConverter> tagDataConverters = new ArrayList<TagDataConverter>();

    final TagDataConverter primaryTagConverter = this.getTagDataConverter(converter);
    if (primaryTagConverter != null) {
      tagDataConverters.add(primaryTagConverter);
    }

    final TagDataConverter fallbackTagConverter = this.getTagDataConverter(fallbackConverter);
    if (fallbackTagConverter != null) {
      tagDataConverters.add(fallbackTagConverter);
    }

    if (tagDataConverters.isEmpty()) {
      log.error("Could not find Tag Data Converter [ {} ]", converter);

      response.setStatus(SlingHttpServletResponse.SC_INTERNAL_SERVER_ERROR);

    } else if (fileParameter != null && fileParameter.getInputStream() != null) {

      InputStream is = fileParameter.getInputStream();

      final Csv csv = new Csv();

      if (delimiter != null) {
        log.debug("Setting Field Delimiter to [ {} ]", delimiter);
        csv.setFieldDelimiter(delimiter);
      }

      if (separator != null) {
        log.debug("Setting Field Separator to [ {} ]", separator);
        csv.setFieldSeparatorRead(separator);
      }

      if (clean) {
        is = this.stripLineEnds(is, charset, csv.getFieldSeparatorRead());
      }

      final Iterator<String[]> rows = csv.read(is, charset);

      try {
        final List<String> result = this.makeTags(tagManager, tagDataConverters, rows);

        try {
          jsonResponse.put("tagIds", result);
        } catch (JSONException e) {
          log.error("Could not serialized Tag Maker results into JSON", e);
        }

        this.addMessage(jsonResponse, result.size() + " tags were processed");
      } catch (InvalidTagFormatException e) {
        log.error("Could not create Tag due to illegal formatting", e);
        this.addMessage(jsonResponse, "Could not create tags due to illegal formatting");
        response.setStatus(SlingHttpServletResponse.SC_INTERNAL_SERVER_ERROR);
      } catch (RepositoryException e) {
        log.error("Could not save Tags to JCR", e);
        this.addMessage(jsonResponse, "Could not save tags");
        response.setStatus(SlingHttpServletResponse.SC_INTERNAL_SERVER_ERROR);
      }
    } else {
      log.error("Could not find CSV file in request.");
      this.addMessage(jsonResponse, "CSV file is missing");
      response.setStatus(SlingHttpServletResponse.SC_INTERNAL_SERVER_ERROR);
    }

    response.getWriter().print(jsonResponse.toString());
  }