Class MCRContent

java.lang.Object
org.mycore.common.content.MCRContent
Direct Known Subclasses:
MCRByteContent, MCROCFLContent, MCRPathContent, MCRStreamContent, MCRStringContent, MCRURLContent, MCRWrappedContent, MCRXMLContent

public abstract class MCRContent extends Object
Used to read/write content from any source to any target. Sources and targets can be strings, local files, Apache VFS file objects, XML documents, byte[] arrays and streams. The different sources are implemented by subclasses.
Author:
Frank Lützenkirchen, Thomas Scheffler (yagee)
  • Field Details

    • systemId

      protected String systemId
      Holds the systemID of the current content
    • docType

      protected String docType
      Holds the docType of the current content
    • length

      protected long length
      Size of content in bytes
    • lastModified

      protected long lastModified
      Last modified timestamp
    • mimeType

      protected String mimeType
    • encoding

      protected String encoding
    • name

      protected String name
    • usingSession

      protected boolean usingSession
  • Constructor Details

    • MCRContent

      public MCRContent()
  • Method Details

    • getSystemId

      public String getSystemId()
      Returns the systemID of the current content
    • getInputStream

      public abstract InputStream getInputStream() throws IOException
      Returns content as input stream. Be sure to close this stream properly!
      Returns:
      input stream to read content from
      Throws:
      IOException
    • getReadableByteChannel

      public ReadableByteChannel getReadableByteChannel() throws IOException
      Returns an readable bytechannel to this content or null if one is not available.
      Throws:
      IOException
    • getContentInputStream

      public MCRContentInputStream getContentInputStream() throws IOException
      Returns content as content input stream, which provides MD5 functionality. Be sure to close this stream properly!
      Returns:
      the content input stream
      Throws:
      IOException
    • getSource

      public Source getSource() throws IOException
      Return the content as Source
      Returns:
      content as Source
      Throws:
      IOException
    • sendTo

      public void sendTo(OutputStream out) throws IOException
      Sends content to the given OutputStream. The OutputStream is NOT automatically closed afterwards.
      Parameters:
      out - the OutputStream to write the content to
      Throws:
      IOException
    • sendTo

      public void sendTo(OutputStream out, boolean close) throws IOException
      Sends content to the given OutputStream.
      Parameters:
      out - the OutputStream to write the content to
      close - if true, close OutputStream afterwards
      Throws:
      IOException
    • getInputSource

      public InputSource getInputSource() throws IOException
      Returns content as SAX input source.
      Returns:
      input source to read content from
      Throws:
      IOException
    • sendTo

      public void sendTo(File target) throws IOException
      Sends content to the given local file
      Parameters:
      target - the file to write the content to
      Throws:
      IOException
    • sendTo

      public void sendTo(Path target, CopyOption... options) throws IOException
      Sends content to the given path.
      Parameters:
      target - target path to write content to
      options - see Files.copy(InputStream, Path, CopyOption...)} for help on copy options
      Throws:
      IOException
    • asByteArray

      public byte[] asByteArray() throws IOException
      Returns the raw content
      Returns:
      the content
      Throws:
      IOException
    • asString

      public String asString() throws IOException
      Returns content as String, assuming encoding from getEncoding() or MCRConstants.DEFAULT_ENCODING .
      Returns:
      content as String
      Throws:
      IOException
    • asDataURL

      public MCRDataURL asDataURL() throws IOException
      Returns content as "data:" URL.
      Throws:
      IOException
    • getDataURLEncoding

      protected MCRDataURLEncoding getDataURLEncoding() throws IOException
      Throws:
      IOException
    • asXML

      Parses content, assuming it is XML, and returns the parsed document.
      Returns:
      the XML document parsed from content
      Throws:
      JDOMException
      IOException
      SAXException
    • ensureXML

      public MCRContent ensureXML() throws IOException, JDOMException, SAXException
      Ensures that content is XML. The content is parsed as if asXML() is called. When content is XML, an MCRContent instance is returned that guarantees that. When XML can not be parsed, an exception is thrown.
      Throws:
      IOException
      JDOMException
      SAXException
    • getDocType

      public String getDocType() throws IOException
      Return the document type of the content, assuming content is XML
      Returns:
      document type as String
      Throws:
      IOException
    • setDocType

      public void setDocType(String docType)
      Overwrites DocType detection.
      See Also:
    • isReusable

      public boolean isReusable()
      If true, content can be read more than once by calling getInputStream() and similar methods. If false, content may be consumed when it is read more than once. Most subclasses provide reusable content.
    • getReusableCopy

      public MCRContent getReusableCopy() throws IOException
      Returns a reusable copy of this content, that is an instance (may be the same instance) thats content can be read more than once without consuming the stream.
      Throws:
      IOException
    • length

      public long length() throws IOException
      Return the length of this content.
      Returns:
      -1 if length is unknown
      Throws:
      IOException
    • lastModified

      public long lastModified() throws IOException
      Returns the last modified time
      Returns:
      -1 if last modified time is unknown
      Throws:
      IOException
    • getETag

      public String getETag() throws IOException
      Returns either strong or weak ETag.
      Returns:
      null, if no ETag could be generated
      Throws:
      IOException
    • getSimpleWeakETag

      protected String getSimpleWeakETag(String systemId, long length, long lastModified)
      Uses provided parameter to compute simple weak ETag.
      Parameters:
      systemId - != null, getSystemId()
      length - >= 0, length()
      lastModified - >= 0, lastModified()
      Returns:
      null if any preconditions are not met.
    • getMimeType

      public String getMimeType() throws IOException
      Throws:
      IOException
    • setMimeType

      public void setMimeType(String mimeType)
    • getName

      public String getName()
    • setName

      public void setName(String name)
    • setLastModified

      public void setLastModified(long lastModified)
    • isUsingSession

      public boolean isUsingSession()
      Tells if this content may contain data from the current MCRSession. Use this information to alter cache behavior.
      Returns:
      true if it MAY contain session data
    • setUsingSession

      public void setUsingSession(boolean usingSession)
    • getEncoding

      public String getEncoding()
    • getSafeEncoding

      protected String getSafeEncoding()
    • setEncoding

      public void setEncoding(String encoding) throws UnsupportedEncodingException
      Throws:
      UnsupportedEncodingException