pfx-filepreview Component


Overview

The pfx-filepreview component generates structured previews of CSV and XLSX files. It reads a configurable number of rows, auto-detects column types (STRING, NUMBER, INTEGER, BOOLEAN, DATE, DATE_TIME), and returns a JSON preview (or base64-encoded CSV when resultAsBase64=true). Supports compressed (ZIP), base64-encoded, and plain inputs.

URI pattern: pfx-filepreview:preview[?options]


Method

Method

Description

preview

Generate a JSON column/row preview from the input file


Parameters

Parameter

Type

Default

Description

fileType

String

—

Ignored. The parser is chosen from the file name extension: a .xlsx name uses the Excel parser, everything else uses the CSV parser. (Exception: when inputBase64Encoded=true the body is always parsed as CSV — see below.)

maxRecords

Integer

3

Max data rows to include in preview (header excluded)

specifiedCharset

String

—

Character encoding for CSV (UTF-8, windows-1250, ISO-8859-1, …). Auto-detected if not set — see Charset Resolution.

separator

String

,

CSV column delimiter (practical default from parser)

quoteChar

String

"

CSV quote character (practical default from parser)

escapeChar

String

\\

CSV escape character (practical default from parser)

useVirtualHeader

boolean

false

Generate virtual headers (column1, column2, …) when file has no header row

decimalSeparator

String

.

Decimal separator for number parsing (practical default from parser; use , for European format)

dateFormat

String

yyyy-MM-dd

Java DateTimeFormatter pattern for date parsing (practical default from parser)

inputBase64Encoded

boolean

false

Decode body from base64 before processing. A base64 body is a re-submitted preview and is always parsed as CSV (the file name is ignored), so a CSV preview re-sent under its original .xlsx name is not routed to the Excel parser.

resultAsBase64

boolean

false

When true, return the preview as base64-encoded CSV text (header + up to maxRecords rows) — for both CSV and XLSX inputs. When false, return the structured JSON preview.

checkUtf

boolean

false

When true, check whether the whole file is valid UTF-8 and report the verdict in the FilePreviewIsUtf header. Reads the input to its end. Applies only when the file itself is read (inputBase64Encoded=false) — see Charset Resolution.

storeResultTo

String

body

Where to store result: body, header, or property

storeResultToName

String

—

Header/property name when storeResultTo is header or property


Output Headers

Header

Type

Description

FilePreviewType

String

Type of the returned preview: CSV or XLSX for the structured JSON preview (resultAsBase64=false); always CSV when resultAsBase64=true, since the base64 body is CSV text

FilePreviewNumberOfRecords

Integer

Actual number of records in the preview

FilePreviewIsUtf

Boolean

Whether the whole file is valid UTF-8. Present only when the endpoint sets checkUtf=true and the file itself is read (inputBase64Encoded=false), and only for CSV/text previews — an XLSX preview never carries this header. Reports the content as it is, also when specifiedCharset was used to read it.


Charset Resolution

With checkUtf=false (the default) the charset is the specifiedCharset when the caller set a usable one, and the auto-detected encoding otherwise. This is the behavior of every existing route.

With checkUtf=true the component is UTF-8 first. After the input is normalized — .gz/.zip decompressed — the content is validated as UTF-8 and the answer is reported in FilePreviewIsUtf. The charset is then picked in this order (first match wins):

  1. specifiedCharset, when it is set to an encoding the JVM knows. The caller stays in control.

  2. UTF-8, when FilePreviewIsUtf is true. No detection runs at all — the verdict is deterministic, so there is nothing left to guess.

  3. Auto-detection, when FilePreviewIsUtf is false. The encoding is detected from the content, as before.

The verdict covers the entire file, not only the previewed rows: one invalid byte in the last row of a 2 GB file makes it false. The check therefore reads the input to its end, in 8 KB chunks and with no extra memory — and in the same single pass that counts the records when returnTotalNumberOfRecords=true. This is the reason the check is opt-in: a preview that leaves checkUtf at its default still reads only the bytes it needs.

The verdict is taken on the decompressed bytes, so a UTF-8 CSV inside a ZIP archive is true.

A base64 input never produces a verdict. With inputBase64Encoded=true the body is a snippet the client sent back, not the file, so a verdict over it would describe the snippet while claiming to describe the file. On that pathway the check is skipped whatever checkUtf says, no FilePreviewIsUtf header is set, and the charset is resolved the usual way — specifiedCharset, else auto-detection.

Auto-detection is a heuristic and can pick the wrong single-byte encoding for short content — supply specifiedCharset when the source encoding is known and is not UTF-8.


Output Format

JSON
{
  "fileName": "products.csv",
  "columns": [
    {
      "name": "sku",
      "type": "STRING",
      "order": 0,
      "parsedValues": ["SKU001", "SKU002", "SKU003"],
      "originalValues": ["SKU001", "SKU002", "SKU003"]
    },
    {
      "name": "price",
      "type": "NUMBER",
      "order": 1,
      "parsedValues": [29.99, 149.50, 89.00],
      "originalValues": ["29.99", "149.50", "89.00"]
    }
  ]
}

With resultAsBase64=true the result is not this JSON structure but base64-encoded CSV text (header row + up to maxRecords rows). For XLSX inputs the parsed rows are serialized to CSV, so the preview is consistent with FilePreviewType=CSV.


Examples

Preview CSV from inbound directory

XML
<routes xmlns="http://camel.apache.org/schema/spring">
    <route id="previewCsv">
        <from uri="file:{{inbound.path}}?{{archive.file}}&amp;{{read.lock}}"/>
        <to uri="pfx-filepreview:preview?fileType=CSV&amp;maxRecords=5"/>
        <log message="Preview: ${body}"/>
    </route>
</routes>

Preview XLSX, store result in header (preserve body)

XML
<to uri="pfx-filepreview:preview?fileType=XLSX&amp;maxRecords=3&amp;storeResultTo=header&amp;storeResultToName=previewJson"/>
<!-- body still contains original file; header.previewJson has the JSON -->

European CSV (semicolon delimiter, comma decimal)

XML
<to uri="pfx-filepreview:preview?fileType=CSV&amp;separator=;&amp;decimalSeparator=,&amp;dateFormat=dd.MM.yyyy"/>

Preview with a UTF-8 verdict

XML
<to uri="pfx-filepreview:preview?fileType=CSV&amp;maxRecords=5&amp;checkUtf=true"/>
<log message="Valid UTF-8: ${header.FilePreviewIsUtf}"/>

Base64 input/output (for API transport)

XML
<to uri="pfx-filepreview:preview?fileType=CSV&amp;inputBase64Encoded=true&amp;resultAsBase64=true"/>

Column Type Detection

Types are detected in this precedence order (first match wins):

Type

Rule

DATE_TIME

All non-empty values parse as LocalDateTime

DATE

All non-empty values parse as LocalDate

BOOLEAN

All non-empty values are true or false

NUMBER

All non-empty values parse as decimal

STRING

Default fallback

Empty values are ignored during type detection. A column with all-empty values defaults to STRING.


Common Pitfalls

  • fileType is required — the component does not auto-detect file format.

  • ZIP-compressed files are decompressed automatically — no special configuration needed.

  • When storeResultTo=header, you must also set storeResultToName.

  • Keep maxRecords low (≤20) for preview use cases — the component reads the full file for type detection but stops collecting rows after maxRecords.

  • For XLSX, the first sheet (index 0) is always used.