Package org.cip4.jdflib.util
Class MimeUtil
- java.lang.Object
-
- org.cip4.jdflib.util.UrlUtil
-
- org.cip4.jdflib.util.MimeUtil
-
public class MimeUtil extends UrlUtil
MIME utilities for reading and writing MIME/MULTIPART/RELATED streams
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classMimeUtil.ByteArrayDataSourcedata source for binary filesstatic classMimeUtil.MIMEDetailshelper class to set mime details-
Nested classes/interfaces inherited from class org.cip4.jdflib.util.UrlUtil
UrlUtil.HTTPDetails, UrlUtil.HttpMethod, UrlUtil.URLProtocol, UrlUtil.URLWriter
-
-
Field Summary
Fields Modifier and Type Field Description static java.lang.StringCIDstatic java.lang.StringMULTIPART_FORMDATAstatic java.lang.StringMULTIPART_RELATEDcommonly used strings-
Fields inherited from class org.cip4.jdflib.util.UrlUtil
ACCEPT, APPLICATION_CFF2, APPLICATION_JSON, APPLICATION_PDF, APPLICATION_PS, APPLICATION_XML, APPLICATION_XZIP, APPLICATION_ZIP, AUTHORIZATION, BASE64, BINARY, CLOSE, CONNECT, CONNECTION, CONTENT_ID, CONTENT_LENGTH, CONTENT_TRANSFER_ENCODING, CONTENT_TYPE, DELETE, GET, HEAD, KEEPALIVE, m_UNCEscape, m_URIEscape, MAX_STREAM, OPTIONS, PATCH, POST, PUT, TEXT_CSV, TEXT_HTML, TEXT_JSON, TEXT_PLAIN, TEXT_UNKNOWN, TEXT_XML, TRACE, VND_JDF, VND_JMF, VND_PPF, VND_PTK, VND_PTK_J, VND_XJDF, VND_XJDF_J, VND_XJMF, VND_XJMF_J, ZIP
-
-
Constructor Summary
Constructors Constructor Description MimeUtil()
-
Method Summary
All Methods Static Methods Concrete Methods Deprecated Methods Modifier and Type Method Description static javax.mail.MultipartbuildMimePackage(java.util.Vector<? extends XMLDoc> vXMLDocs)Builds a MIME package.static javax.mail.MultipartbuildMimePackage(JDFDoc docJMF, JDFDoc docJDF)Deprecated.use 3 parameter versionstatic javax.mail.MultipartbuildMimePackage(JDFDoc docJMF, XMLDoc docJDF, boolean extendReferenced)build a MIME package that contains all references in all FileSpecs of a given JDFDoc the doc is modified so that all URLs are cidsstatic javax.mail.BodyPart[]extractMultipartMime(java.io.InputStream mimeStream)Extracts all the parts of a multipart MIME message and returns an array of InputStream for each of the separate MIME parts.static javax.mail.BodyPart[]getBodyParts(javax.mail.Multipart mp)get all the parts of of a multipart anstatic java.lang.StringgetContentID(javax.mail.BodyPart bp)get the ContentID header of a bodypart a stringstatic javax.mail.BodyPartgetCreatePartByCID(javax.mail.Multipart mp, java.lang.String cid)get the MIME BodyPart from a multiPart package with a given cid create one if it does not exist;static java.lang.StringgetFileName(javax.mail.BodyPart bp)get the filename header of a bodypart a string if no file name is set, a unique filename is generated from cid and content typestatic JDFDocgetJDFDoc(java.io.InputStream stream, int index)get the JDF Doc from a stream, also handle non mime streams gracefullystatic JDFDocgetJDFDoc(javax.mail.BodyPart bp)get the JDF Doc from a given body partstatic JDFDoc[]getJMFSubmission(javax.mail.Multipart mp)gets the JMF document of a submitqueueentry or returnqueuentry and the attached jdf documentstatic java.lang.StringgetMimeTypeFromExt(java.lang.String fileName)Deprecated.use UrlUtil.getMimeTypeFromURL(fileName);static javax.mail.MultipartgetMultiPart(java.io.InputStream mimeStream)create a root multipart from an input streamstatic javax.mail.MultipartgetMultiPart(java.lang.String fileName)helper to create a root multipart from a filestatic javax.mail.BodyPartgetPartByCID(javax.mail.Multipart mp, java.lang.String cid)get the MIME BodyPart from a multiPart package with a given cidstatic BodyPartHelpergetPartByFileName(javax.mail.Multipart mp, java.lang.String name)get the MIME BodyPart from a multiPart package with a given file namestatic booleanisJDFMimeType(java.lang.String mimeType)checks whether the mime type corresponds to one of "application/vnd.cip4-jdf+xml"; "application/vnd.cip4-jmf+xml"; "text/xml"; or the respective XJDF tapesstatic booleanisMimeMultiPart(java.lang.String contentType)static booleanisMimeType(java.lang.String toCheck, java.lang.String mimeType)checks whether the mime type corresponds to mimetyoestatic booleanisPPFMimeType(java.lang.String mimeType)checks whether the mime type corresponds to ppfstatic booleanmatchesCID(javax.mail.BodyPart bp, java.lang.String cid)check if a BodyPart matches a given cidstatic voidsetContent(javax.mail.BodyPart messageBodyPart, XMLDoc xmlDoc)sets the content of a bodypart to the xmlDoc - correctly handling non-ascii features and setting the correct content typestatic voidsetContentID(javax.mail.BodyPart bp, java.lang.String cid)static voidsetFileName(javax.mail.BodyPart bp, java.lang.String path)set the filename header of a bodypart to a stringstatic javax.mail.BodyPartupdateXMLMultipart(javax.mail.Multipart multipart, XMLDoc xmlDoc, java.lang.String cid)static java.lang.StringurlToCid(java.lang.String urlString)static voidwriteBodyPartToFile(javax.mail.BodyPart bp, java.io.File directory)static voidwriteToDir(javax.mail.Multipart mp, java.io.File directory)write a Message to a directorystatic java.io.FilewriteToFile(javax.mail.Multipart m, java.lang.String fileName)Deprecated.static java.io.FilewriteToFile(javax.mail.Multipart m, java.lang.String fileName, MimeUtil.MIMEDetails md)write a Multipart to an output filestatic JDFDocwriteToQueue(JDFDoc docJMF, JDFDoc docJDF, java.lang.String strUrl, MimeUtil.MIMEDetails urlDet)submit a multipart file to a queuestatic voidwriteToStream(javax.mail.Multipart m, java.io.OutputStream outStream)Deprecated.static voidwriteToStream(javax.mail.Multipart m, java.io.OutputStream outStream, MimeUtil.MIMEDetails md)write a Multipart to a Streamstatic java.net.HttpURLConnectionwriteToURL(javax.mail.Multipart mp, java.lang.String strUrl)write a Multipart to an output URL File: and http: are currently supported Use HttpURLConnection.getInputStream() to retrieve the http responsestatic java.net.HttpURLConnectionwriteToURL(javax.mail.Multipart mp, java.lang.String strUrl, MimeUtil.MIMEDetails mimeDetails)write a Multipart to an output URL File: and http: are currently supported Use HttpURLConnection.getInputStream() to retrieve the http response-
Methods inherited from class org.cip4.jdflib.util.UrlUtil
addParameter, addPath, addSecure, cleanDots, cleanHttpURL, createHttpUrl, escape, escape, extension, fileToUrl, getBytesFromIP, getCidURLStream, getConnectionTimeout, getCreateDirectory, getExtensionFromMimeType, getFileName, getIPFromBytes, getLocalURL, getMimeTypeFromURL, getParameter, getParentDirectory, getProtocol, getRelativePath, getRelativeURL, getSecurePath, getURLInputStream, getURLInputStream, getURLInputStream, getURLParts, getURLWithDirectory, isCID, isEscaped, isFile, isFileOK, isFtp, isHttp, isHttps, isIRL, isJSONType, isMIME, isMIMEExtenstension, isNet, isNotCID, isRedirect, isRelativeURL, isReturnCodeOK, isReturnCodeOK, isUNC, isURL, isWindowsLocalPath, isXMLType, isZip, isZIPType, moveToDir, moveToDir, moveToDir, newExtension, normalize, prefix, removeExtension, removeProtocol, setConnectionTimeout, setParameter, stringToURL, uncToUrl, unEscape, urlToFile, urlToFileName, urlToString, urlToUNC, writerToURL, writeToURL
-
-
-
-
Field Detail
-
CID
public static final java.lang.String CID
- See Also:
- Constant Field Values
-
MULTIPART_RELATED
public static final java.lang.String MULTIPART_RELATED
commonly used strings- See Also:
- Constant Field Values
-
MULTIPART_FORMDATA
public static final java.lang.String MULTIPART_FORMDATA
- See Also:
- Constant Field Values
-
-
Method Detail
-
setContentID
public static void setContentID(javax.mail.BodyPart bp, java.lang.String cid)- Parameters:
bp-cid-
-
setFileName
public static void setFileName(javax.mail.BodyPart bp, java.lang.String path)set the filename header of a bodypart to a string- Parameters:
bp- the bodypartpath- the path to set
-
getFileName
public static java.lang.String getFileName(javax.mail.BodyPart bp)
get the filename header of a bodypart a string if no file name is set, a unique filename is generated from cid and content type- Parameters:
bp- the bodypart- Returns:
- the file name, null if bp is null
-
getContentID
public static java.lang.String getContentID(javax.mail.BodyPart bp)
get the ContentID header of a bodypart a string- Parameters:
bp- the bodypart- Returns:
- the cid, null if there was an error
-
extractMultipartMime
public static javax.mail.BodyPart[] extractMultipartMime(java.io.InputStream mimeStream)
Extracts all the parts of a multipart MIME message and returns an array of InputStream for each of the separate MIME parts.- Parameters:
mimeStream-- Returns:
-
getBodyParts
public static javax.mail.BodyPart[] getBodyParts(javax.mail.Multipart mp)
get all the parts of of a multipart an- Parameters:
mp- the multiPart to extract- Returns:
- the array of parts, null if snafu...
-
getPartByCID
public static javax.mail.BodyPart getPartByCID(javax.mail.Multipart mp, java.lang.String cid)get the MIME BodyPart from a multiPart package with a given cid- Parameters:
mp- the multipart package to search incid- the cid of the requested bodypart- Returns:
- BodyPart the matching BodyPart, null if none is found
-
getPartByFileName
public static BodyPartHelper getPartByFileName(javax.mail.Multipart mp, java.lang.String name)
get the MIME BodyPart from a multiPart package with a given file name- Parameters:
mp- the multipart package to search inname- the cid of the requested bodypart- Returns:
- BodyPart the matching BodyPart, null if none is found
-
getCreatePartByCID
public static javax.mail.BodyPart getCreatePartByCID(javax.mail.Multipart mp, java.lang.String cid)get the MIME BodyPart from a multiPart package with a given cid create one if it does not exist;- Parameters:
mp- the multipart package to search incid- the cid of the requested bodypart- Returns:
- BodyPart the matching BodyPart, null if none is found
-
getJDFDoc
public static JDFDoc getJDFDoc(java.io.InputStream stream, int index)
get the JDF Doc from a stream, also handle non mime streams gracefully- Parameters:
stream- the stream to search inindex- the index of the body part to search- Returns:
- JDFDoc the parsed xml JDFDoc, null if stream does not contain xml
-
getJDFDoc
public static JDFDoc getJDFDoc(javax.mail.BodyPart bp)
get the JDF Doc from a given body part- Parameters:
bp- the BodyPart to search in- Returns:
- JDFDoc the parsed xml JDFDoc, null if bp does not contain xml
-
matchesCID
public static boolean matchesCID(javax.mail.BodyPart bp, java.lang.String cid)check if a BodyPart matches a given cid- Parameters:
bp- the bodyPart to checkcid- the cid string any '<' '>' or 'cid:' prefixes are removed if null, anything matches- Returns:
- true if this bp matches the cid
-
getMultiPart
public static javax.mail.Multipart getMultiPart(java.lang.String fileName)
helper to create a root multipart from a file- Parameters:
fileName- the name of the file used as input- Returns:
- MultiPart the Multipart that represents the root mime, null if something went wrong
-
getMultiPart
public static javax.mail.Multipart getMultiPart(java.io.InputStream mimeStream)
create a root multipart from an input stream- Parameters:
mimeStream- the input stream- Returns:
- MultiPart the Multipart that represents the root mime, null if something went wrong
-
getMimeTypeFromExt
@Deprecated public static java.lang.String getMimeTypeFromExt(java.lang.String fileName)
Deprecated.use UrlUtil.getMimeTypeFromURL(fileName);checkst whether the mime type corresponds to one of "application/vnd.cip4-jdf+xml"; "application/vnd.cip4-jmf+xml"; "text/xml";- Parameters:
fileName- the string to test- Returns:
- the mime type
-
isJDFMimeType
public static boolean isJDFMimeType(java.lang.String mimeType)
checks whether the mime type corresponds to one of "application/vnd.cip4-jdf+xml"; "application/vnd.cip4-jmf+xml"; "text/xml"; or the respective XJDF tapes- Parameters:
mimeType- the string to test- Returns:
- true if matches
-
isPPFMimeType
public static boolean isPPFMimeType(java.lang.String mimeType)
checks whether the mime type corresponds to ppf- Parameters:
mimeType- the string to test- Returns:
- true if matches
-
isMimeType
public static boolean isMimeType(java.lang.String toCheck, java.lang.String mimeType)checks whether the mime type corresponds to mimetyoe- Parameters:
toCheck- the string to check againstmimeType- the mime type- Returns:
- true if matches
-
buildMimePackage
@Deprecated public static javax.mail.Multipart buildMimePackage(JDFDoc docJMF, JDFDoc docJDF)
Deprecated.use 3 parameter version- Parameters:
docJMF-docJDF-- Returns:
-
buildMimePackage
public static javax.mail.Multipart buildMimePackage(JDFDoc docJMF, XMLDoc docJDF, boolean extendReferenced)
build a MIME package that contains all references in all FileSpecs of a given JDFDoc the doc is modified so that all URLs are cids- Parameters:
docJMF- the JDFDoc representation of the JMF that references the jdf to package, if null only the jdf is packaged note that the URL of docJDF must already be specified as a CIDdocJDF- the JDFDoc representation of the JDF to packageextendReferenced- if true, also package any further referenced files- Returns:
- a Message representing the resulting MIME package, null if an error occurred
-
urlToCid
public static java.lang.String urlToCid(java.lang.String urlString)
- Parameters:
urlString-- Returns:
-
buildMimePackage
public static javax.mail.Multipart buildMimePackage(java.util.Vector<? extends XMLDoc> vXMLDocs)
Builds a MIME package.- Parameters:
vXMLDocs- the Vector of XMLDoc representing the JMF and JDFs to be stored as the first part of the package t- Returns:
- a Message representing the resulting MIME package, null if an error occured
-
updateXMLMultipart
public static javax.mail.BodyPart updateXMLMultipart(javax.mail.Multipart multipart, XMLDoc xmlDoc, java.lang.String cid)- Parameters:
multipart-xmlDoc-cid-- Returns:
-
setContent
public static void setContent(javax.mail.BodyPart messageBodyPart, XMLDoc xmlDoc) throws javax.mail.MessagingException, java.io.IOExceptionsets the content of a bodypart to the xmlDoc - correctly handling non-ascii features and setting the correct content type- Parameters:
messageBodyPart- the BodyPart to fillxmlDoc- the xmlDoc to fill in- Throws:
javax.mail.MessagingExceptionjava.io.IOException
-
writeToURL
public static java.net.HttpURLConnection writeToURL(javax.mail.Multipart mp, java.lang.String strUrl) throws java.io.IOException, javax.mail.MessagingExceptionwrite a Multipart to an output URL File: and http: are currently supported Use HttpURLConnection.getInputStream() to retrieve the http response- Parameters:
mp- the mime MultiPart to writestrUrl- the URL to write to- Returns:
HttpURLConnectionthe opened http connection, null in case of error or file- Throws:
java.io.IOExceptionjavax.mail.MessagingException
-
writeToURL
public static java.net.HttpURLConnection writeToURL(javax.mail.Multipart mp, java.lang.String strUrl, MimeUtil.MIMEDetails mimeDetails) throws java.io.IOException, javax.mail.MessagingExceptionwrite a Multipart to an output URL File: and http: are currently supported Use HttpURLConnection.getInputStream() to retrieve the http response- Parameters:
mp- the mime MultiPart to writestrUrl- the URL to write tomimeDetails-- Returns:
HttpURLConnectionthe opened http connection, null in case of error or file- Throws:
java.io.IOExceptionjavax.mail.MessagingException
-
writeToQueue
public static JDFDoc writeToQueue(JDFDoc docJMF, JDFDoc docJDF, java.lang.String strUrl, MimeUtil.MIMEDetails urlDet) throws java.io.IOException, javax.mail.MessagingException
submit a multipart file to a queue- Parameters:
docJMF- the jmf document containing the submitqueueentry or resubmitqueueentrydocJDF- the jdf to submitstrUrl- the url to submit tourlDet- url details- Returns:
- Throws:
java.io.IOExceptionjavax.mail.MessagingException
-
writeToFile
@Deprecated public static java.io.File writeToFile(javax.mail.Multipart m, java.lang.String fileName)Deprecated.- Parameters:
m-fileName-- Returns:
- the File that was written
-
writeToFile
public static java.io.File writeToFile(javax.mail.Multipart m, java.lang.String fileName, MimeUtil.MIMEDetails md)write a Multipart to an output file- Parameters:
m- the mime MultiPart to writefileName- the file namemd-- Returns:
-
writeToStream
@Deprecated public static void writeToStream(javax.mail.Multipart m, java.io.OutputStream outStream) throws java.io.IOException, javax.mail.MessagingExceptionDeprecated.- Parameters:
m-outStream-- Throws:
java.io.IOExceptionjavax.mail.MessagingException
-
writeToStream
public static void writeToStream(javax.mail.Multipart m, java.io.OutputStream outStream, MimeUtil.MIMEDetails md) throws java.io.IOException, javax.mail.MessagingExceptionwrite a Multipart to a Stream- Parameters:
m- the mime MultiPart to writeoutStream- the existing output stream, note that a buffered output stream is created in case outStream is unbufferedmd- details for messaging- Throws:
java.io.IOExceptionjavax.mail.MessagingException
-
writeToDir
public static void writeToDir(javax.mail.Multipart mp, java.io.File directory) throws javax.mail.MessagingException, java.io.IOExceptionwrite a Message to a directory- Parameters:
mp- the mime Message to writedirectory- the directory to use as '.' for writing the mime parts- Throws:
javax.mail.MessagingExceptionjava.io.IOExceptionjavax.mail.MessagingException
-
writeBodyPartToFile
public static void writeBodyPartToFile(javax.mail.BodyPart bp, java.io.File directory) throws java.io.IOException, javax.mail.MessagingException- Parameters:
bp-directory-- Throws:
javax.mail.MessagingExceptionjava.io.IOException
-
getJMFSubmission
public static JDFDoc[] getJMFSubmission(javax.mail.Multipart mp)
gets the JMF document of a submitqueueentry or returnqueuentry and the attached jdf document- Parameters:
mp- the Multipart to search- Returns:
- one or two JDFDocs: bp[0] is the jmf, bp[1] is the jdf, if a JDF is referenced;
-
isMimeMultiPart
public static boolean isMimeMultiPart(java.lang.String contentType)
- Parameters:
contentType-- Returns:
-
-