Class TikaResource

java.lang.Object
org.apache.tika.server.core.resource.TikaResource

@Path("/tika") public class TikaResource extends Object
  • Field Details

    • GREETING

      public static final String GREETING
  • Constructor Details

    • TikaResource

      public TikaResource(TikaLoader tikaLoader, ServerStatus serverStatus, PipesParsingHelper pipesParsingHelper, boolean allowPerRequestConfig)
      Parameters:
      tikaLoader - the Tika loader
      serverStatus - server status tracker
      pipesParsingHelper - helper for pipes-based parsing, may be null if /tika endpoint is not enabled
      allowPerRequestConfig - whether per-request config injection is permitted
  • Method Details

    • getPipesParsingHelper

      public PipesParsingHelper getPipesParsingHelper()
      Gets the PipesParsingHelper instance.
      Returns:
      the helper
    • createRequestContext

      public ParseContext createRequestContext()
      Creates a ParseContext holding only what this request itself specifies.

      Config-level parse-context defaults are deliberately absent. The forked worker loads them from the same config and overlays the request on top, so sending them is redundant -- and worse than redundant at the trust boundary: the worker clamps request-supplied timeout limits but trusts its own config's, so a default that arrives as request data gets treated as caller input and clamped.

      Returns:
      an empty, request-scoped ParseContext
    • newRequestMetadata

      public Metadata newRequestMetadata()
      Creates request metadata bounded by the config's metadata write limiter.

      The limiter no longer rides in the request context, so it is applied here instead. This metadata holds caller-supplied values (filename, headers), which is exactly what the limiter is meant to bound.

    • newConfigUnpackConfig

      public UnpackConfig newConfigUnpackConfig()
      A fresh copy of the config's unpack-config, or null if none is declared.

      The unpack path is the one place a config default must still travel: it is mutated per request (zip, suffix strategy, emitter) and so overrides whatever the worker would have loaded. Starting from a default-constructed instance instead would silently reset operator settings the request never touches -- maxUnpackBytes, for one. Re-read per call because the caller mutates the result; unpack requests are heavyweight enough that the config read does not register.

    • getTikaLoader

      public TikaLoader getTikaLoader()
    • detectFilename

      public static String detectFilename(jakarta.ws.rs.core.MultivaluedMap<String,String> httpHeaders)
    • mergeParseContextFromConfig

      public static void mergeParseContextFromConfig(String configJson, ParseContext context) throws IOException, TikaConfigException
      Parses config JSON and merges parseContext entries into the provided ParseContext.
      Parameters:
      configJson - the JSON config string
      context - the ParseContext to merge into
      Throws:
      IOException - if parsing fails
      TikaConfigException
    • fillMetadata

      public static void fillMetadata(Parser parser, Metadata metadata, jakarta.ws.rs.core.MultivaluedMap<String,String> httpHeaders)
    • setupMultipartConfig

      public TikaInputStream setupMultipartConfig(List<org.apache.cxf.jaxrs.ext.multipart.Attachment> attachments, Metadata metadata, ParseContext context) throws IOException, TikaConfigException
      Processes multipart attachments for /config endpoints. Extracts the "file" and optional "config" attachments, sets up metadata (filename, content-type) from the file attachment, and processes any config JSON into the ParseContext.
      Parameters:
      attachments - the multipart attachments
      metadata - metadata to populate with filename and content-type
      context - parse context to populate from config JSON
      Returns:
      TikaInputStream wrapping the file attachment's content
      Throws:
      IOException - if file attachment is missing or config processing fails
      TikaConfigException
    • parseWithPipes

      public List<Metadata> parseWithPipes(TikaInputStream tis, Metadata metadata, ParseContext parseContext, ParseMode parseMode) throws IOException
      Parses using pipes-based parsing with process isolation.

      The TikaInputStream should already be spooled to a temp file via TikaInputStream.getPath(). The caller is responsible for closing the TikaInputStream after this method returns, which will clean up any temp files.

      Parameters:
      tis - the TikaInputStream to parse
      metadata - metadata to pass to the parser
      parseContext - parse context with handler configuration
      parseMode - RMETA or CONCATENATE
      Returns:
      list of metadata objects from parsing
      Throws:
      IOException - if parsing fails
    • logRequest

      public static void logRequest(org.slf4j.Logger logger, String endpoint, Metadata metadata)
    • setupContentHandlerFactory

      public void setupContentHandlerFactory(ParseContext context, String handlerTypeName)
      Sets up the ContentHandlerFactory in the ParseContext, taking the write limits from OutputLimits in the context.
      Parameters:
      context - the ParseContext to configure
      handlerTypeName - the handler type name (text, html, xml, ignore), may be null for default
    • setupContentHandlerFactoryIfNeeded

      public void setupContentHandlerFactoryIfNeeded(ParseContext context, String handlerTypeName)
      Sets up the ContentHandlerFactory in the ParseContext if not already set. Used when a ParseContext may already have a factory configured.
      Parameters:
      context - the ParseContext to configure
      handlerTypeName - the handler type name
    • getMessage

      @GET @Produces("text/plain;charset=UTF-8") public String getMessage()
    • getDefault

      @PUT @Consumes("*/*") @Produces("text/plain;charset=UTF-8") public jakarta.ws.rs.core.Response getDefault(InputStream is, @Context jakarta.ws.rs.core.HttpHeaders httpHeaders) throws IOException
      Parse document and return Markdown content. This is the default output of the bare /tika endpoint; use /tika/xml, /tika/html, /tika/text for the other formats.
      Throws:
      IOException
    • getText

      @PUT @Consumes("*/*") @Produces("text/plain;charset=UTF-8") @Path("text") public jakarta.ws.rs.core.Response getText(InputStream is, @Context jakarta.ws.rs.core.HttpHeaders httpHeaders) throws IOException
      Parse document and return body-only plain text.
      Throws:
      IOException
    • getHtml

      @PUT @Consumes("*/*") @Produces("text/html;charset=UTF-8") @Path("html") public jakarta.ws.rs.core.Response getHtml(InputStream is, @Context jakarta.ws.rs.core.HttpHeaders httpHeaders) throws IOException
      Parse document and return HTML content.
      Throws:
      IOException
    • getXml

      @PUT @Consumes("*/*") @Produces("text/xml;charset=UTF-8") @Path("xml") public jakarta.ws.rs.core.Response getXml(InputStream is, @Context jakarta.ws.rs.core.HttpHeaders httpHeaders) throws IOException
      Parse document and return XML content.
      Throws:
      IOException
    • getMarkdown

      @PUT @Consumes("*/*") @Produces("text/plain;charset=UTF-8") @Path("md") public jakarta.ws.rs.core.Response getMarkdown(InputStream is, @Context jakarta.ws.rs.core.HttpHeaders httpHeaders) throws IOException
      Parse document and return Markdown content.
      Throws:
      IOException
    • getJsonDefault

      @PUT @Consumes("*/*") @Produces("application/json") @Path("json") public Metadata getJsonDefault(InputStream is, @Context jakarta.ws.rs.core.HttpHeaders httpHeaders) throws IOException
      Parse document and return JSON with metadata and Markdown content (the default handler).
      Throws:
      IOException
    • getJson

      @PUT @Consumes("*/*") @Produces("application/json") @Path("json/{handler}") public Metadata getJson(InputStream is, @Context jakarta.ws.rs.core.HttpHeaders httpHeaders, @PathParam("handler") String handlerTypeName) throws IOException
      Parse document and return JSON with metadata and the named handler's content.
      Parameters:
      handlerTypeName - content handler type: text, html, xml, body, markdown, ignore
      Throws:
      IOException
    • postRaw

      @POST @Consumes("multipart/form-data") @Produces("text/plain;charset=UTF-8") @Path("config") public jakarta.ws.rs.core.Response postRaw(List<org.apache.cxf.jaxrs.ext.multipart.Attachment> attachments, @Context jakarta.ws.rs.core.HttpHeaders httpHeaders) throws IOException, TikaConfigException
      Multipart document with optional config; returns Markdown unless the config names a handler.
      Throws:
      IOException
      TikaConfigException
    • postText

      @POST @Consumes("multipart/form-data") @Produces("text/plain;charset=UTF-8") @Path("config/text") public jakarta.ws.rs.core.Response postText(List<org.apache.cxf.jaxrs.ext.multipart.Attachment> attachments, @Context jakarta.ws.rs.core.HttpHeaders httpHeaders) throws IOException, TikaConfigException
      Multipart document with optional config; returns body-only plain text.
      Throws:
      IOException
      TikaConfigException
    • postHtml

      @POST @Consumes("multipart/form-data") @Produces("text/html;charset=UTF-8") @Path("config/html") public jakarta.ws.rs.core.Response postHtml(List<org.apache.cxf.jaxrs.ext.multipart.Attachment> attachments, @Context jakarta.ws.rs.core.HttpHeaders httpHeaders) throws IOException, TikaConfigException
      Multipart document with optional config; returns HTML.
      Throws:
      IOException
      TikaConfigException
    • postXml

      @POST @Consumes("multipart/form-data") @Produces("text/xml;charset=UTF-8") @Path("config/xml") public jakarta.ws.rs.core.Response postXml(List<org.apache.cxf.jaxrs.ext.multipart.Attachment> attachments, @Context jakarta.ws.rs.core.HttpHeaders httpHeaders) throws IOException, TikaConfigException
      Multipart document with optional config; returns XML.
      Throws:
      IOException
      TikaConfigException
    • postMarkdown

      @POST @Consumes("multipart/form-data") @Produces("text/plain;charset=UTF-8") @Path("config/md") public jakarta.ws.rs.core.Response postMarkdown(List<org.apache.cxf.jaxrs.ext.multipart.Attachment> attachments, @Context jakarta.ws.rs.core.HttpHeaders httpHeaders) throws IOException, TikaConfigException
      Multipart document with optional config; returns Markdown.
      Throws:
      IOException
      TikaConfigException
    • postJson

      @POST @Consumes("multipart/form-data") @Produces("application/json") @Path("config/json") public Metadata postJson(List<org.apache.cxf.jaxrs.ext.multipart.Attachment> attachments, @Context jakarta.ws.rs.core.HttpHeaders httpHeaders) throws IOException, TikaConfigException
      Multipart document with optional config; returns JSON with the default (markdown) handler.
      Throws:
      IOException
      TikaConfigException
    • postJsonWithHandler

      @POST @Consumes("multipart/form-data") @Produces("application/json") @Path("config/json/{handler}") public Metadata postJsonWithHandler(List<org.apache.cxf.jaxrs.ext.multipart.Attachment> attachments, @Context jakarta.ws.rs.core.HttpHeaders httpHeaders, @PathParam("handler") String handlerTypeName) throws IOException, TikaConfigException
      Multipart sibling of PUT /tika/json/{handlerType}. Without it, a POST caller who wants text-in-JSON has nowhere to go: /tika/config/text returns raw text with no metadata envelope.
      Parameters:
      handlerTypeName - content handler type: text, html, xml, body, markdown, ignore
      Throws:
      IOException
      TikaConfigException