Package org.mycore.common.content
Class MCRContent
java.lang.Object
org.mycore.common.content.MCRContent
- Direct Known Subclasses:
MCRByteContent
,MCROCFLContent
,MCRPathContent
,MCRStreamContent
,MCRStringContent
,MCRURLContent
,MCRWrappedContent
,MCRXMLContent
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 Summary
Modifier and TypeFieldDescriptionprotected String
Holds the docType of the current contentprotected String
protected long
Last modified timestampprotected long
Size of content in bytesprotected String
protected String
protected String
Holds the systemID of the current contentprotected boolean
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionbyte[]
Returns the raw contentReturns content as "data:" URL.asString()
Returns content as String, assuming encoding fromgetEncoding()
orMCRConstants.DEFAULT_ENCODING
.asXML()
Parses content, assuming it is XML, and returns the parsed document.Ensures that content is XML.Returns content as content input stream, which provides MD5 functionality.protected MCRDataURLEncoding
Return the document type of the content, assuming content is XMLgetETag()
Returns either strong or weak ETag.Returns content as SAX input source.abstract InputStream
Returns content as input stream.getName()
Returns an readable bytechannel to this content or null if one is not available.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.protected String
protected String
getSimpleWeakETag
(String systemId, long length, long lastModified) Uses provided parameter to compute simple weak ETag.Return the content as SourceReturns the systemID of the current contentboolean
If true, content can be read more than once by calling getInputStream() and similar methods.boolean
Tells if this content may contain data from the current MCRSession.long
Returns the last modified timelong
length()
Return the length of this content.void
Sends content to the given local filevoid
sendTo
(OutputStream out) Sends content to the given OutputStream.void
sendTo
(OutputStream out, boolean close) Sends content to the given OutputStream.void
sendTo
(Path target, CopyOption... options) Sends content to the given path.void
setDocType
(String docType) Overwrites DocType detection.void
setEncoding
(String encoding) void
setLastModified
(long lastModified) void
setMimeType
(String mimeType) void
void
setUsingSession
(boolean usingSession)
-
Field Details
-
systemId
Holds the systemID of the current content -
docType
Holds the docType of the current content -
length
protected long lengthSize of content in bytes -
lastModified
protected long lastModifiedLast modified timestamp -
mimeType
-
encoding
-
name
-
usingSession
protected boolean usingSession
-
-
Constructor Details
-
MCRContent
public MCRContent()
-
-
Method Details
-
getSystemId
Returns the systemID of the current content -
getInputStream
Returns content as input stream. Be sure to close this stream properly!- Returns:
- input stream to read content from
- Throws:
IOException
-
getReadableByteChannel
Returns an readable bytechannel to this content or null if one is not available.- Throws:
IOException
-
getContentInputStream
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
Return the content as Source- Returns:
- content as Source
- Throws:
IOException
-
sendTo
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
Sends content to the given OutputStream.- Parameters:
out
- the OutputStream to write the content toclose
- if true, close OutputStream afterwards- Throws:
IOException
-
getInputSource
Returns content as SAX input source.- Returns:
- input source to read content from
- Throws:
IOException
-
sendTo
Sends content to the given local file- Parameters:
target
- the file to write the content to- Throws:
IOException
-
sendTo
Sends content to the given path.- Parameters:
target
- target path to write content tooptions
- seeFiles.copy(InputStream, Path, CopyOption...)
} for help on copy options- Throws:
IOException
-
asByteArray
Returns the raw content- Returns:
- the content
- Throws:
IOException
-
asString
Returns content as String, assuming encoding fromgetEncoding()
orMCRConstants.DEFAULT_ENCODING
.- Returns:
- content as String
- Throws:
IOException
-
asDataURL
Returns content as "data:" URL.- Throws:
IOException
-
getDataURLEncoding
- 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
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
Return the document type of the content, assuming content is XML- Returns:
- document type as String
- Throws:
IOException
-
setDocType
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
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
Return the length of this content.- Returns:
- -1 if length is unknown
- Throws:
IOException
-
lastModified
Returns the last modified time- Returns:
- -1 if last modified time is unknown
- Throws:
IOException
-
getETag
Returns either strong or weak ETag.- Returns:
- null, if no ETag could be generated
- Throws:
IOException
-
getSimpleWeakETag
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
- Throws:
IOException
-
setMimeType
-
getName
-
setName
-
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
-
getSafeEncoding
-
setEncoding
- Throws:
UnsupportedEncodingException
-