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
  • Field Details

  • Constructor Details

    • MimeUtil

      public MimeUtil()
  • Method Details

    • setContentID

      public static void setContentID(jakarta.mail.BodyPart bp, String cid)
      Parameters:
      bp -
      cid -
    • setFileName

      public static void setFileName(jakarta.mail.BodyPart bp, String path)
      set the filename header of a bodypart to a string
      Parameters:
      bp - the bodypart
      path - the path to set
    • getFileName

      public static String getFileName(jakarta.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 String getContentID(jakarta.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 jakarta.mail.BodyPart[] extractMultipartMime(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 jakarta.mail.BodyPart[] getBodyParts(jakarta.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 jakarta.mail.BodyPart getPartByCID(jakarta.mail.Multipart mp, String cid)
      get the MIME BodyPart from a multiPart package with a given cid
      Parameters:
      mp - the multipart package to search in
      cid - the cid of the requested bodypart
      Returns:
      BodyPart the matching BodyPart, null if none is found
    • getPartByFileName

      public static BodyPartHelper getPartByFileName(jakarta.mail.Multipart mp, String name)
      get the MIME BodyPart from a multiPart package with a given file name
      Parameters:
      mp - the multipart package to search in
      name - the cid of the requested bodypart
      Returns:
      BodyPart the matching BodyPart, null if none is found
    • getCreatePartByCID

      public static jakarta.mail.BodyPart getCreatePartByCID(jakarta.mail.Multipart mp, 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 in
      cid - the cid of the requested bodypart
      Returns:
      BodyPart the matching BodyPart, null if none is found
    • getJDFDoc

      public static JDFDoc getJDFDoc(InputStream stream, int index)
      get the JDF Doc from a stream, also handle non mime streams gracefully
      Parameters:
      stream - the stream to search in
      index - 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(jakarta.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(jakarta.mail.BodyPart bp, String cid)
      check if a BodyPart matches a given cid
      Parameters:
      bp - the bodyPart to check
      cid - the cid string any '<' '>' or 'cid:' prefixes are removed if null, anything matches
      Returns:
      true if this bp matches the cid
    • getMultiPart

      public static jakarta.mail.Multipart getMultiPart(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 jakarta.mail.Multipart getMultiPart(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 String getMimeTypeFromExt(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(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(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(String toCheck, String mimeType)
      checks whether the mime type corresponds to mimetyoe
      Parameters:
      toCheck - the string to check against
      mimeType - the mime type
      Returns:
      true if matches
    • buildMimePackage

      @Deprecated public static jakarta.mail.Multipart buildMimePackage(JDFDoc docJMF, JDFDoc docJDF)
      Deprecated.
      use 3 parameter version
      Parameters:
      docJMF -
      docJDF -
      Returns:
    • buildMimePackage

      public static jakarta.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 CID
      docJDF - the JDFDoc representation of the JDF to package
      extendReferenced - if true, also package any further referenced files
      Returns:
      a Message representing the resulting MIME package, null if an error occurred
    • urlToCid

      public static String urlToCid(String urlString)
      Parameters:
      urlString -
      Returns:
    • urlToCid

      public static String urlToCid(String urlString, boolean raw)
      Parameters:
      normalized -
      Returns:
    • buildMimePackage

      public static jakarta.mail.Multipart buildMimePackage(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 jakarta.mail.BodyPart updateXMLMultipart(jakarta.mail.Multipart multipart, XMLDoc xmlDoc, String cid)
      Parameters:
      multipart -
      xmlDoc -
      cid -
      Returns:
    • setContent

      public static void setContent(jakarta.mail.BodyPart messageBodyPart, XMLDoc xmlDoc) throws jakarta.mail.MessagingException, IOException
      sets the content of a bodypart to the xmlDoc - correctly handling non-ascii features and setting the correct content type
      Parameters:
      messageBodyPart - the BodyPart to fill
      xmlDoc - the xmlDoc to fill in
      Throws:
      jakarta.mail.MessagingException
      IOException
    • writeToURL

      public static HttpURLConnection writeToURL(jakarta.mail.Multipart mp, String strUrl) throws IOException, jakarta.mail.MessagingException
      write 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 write
      strUrl - the URL to write to
      Returns:
      HttpURLConnection the opened http connection, null in case of error or file
      Throws:
      IOException
      jakarta.mail.MessagingException
    • writeToURL

      public static HttpURLConnection writeToURL(jakarta.mail.Multipart mp, String strUrl, MimeUtil.MIMEDetails mimeDetails) throws IOException, jakarta.mail.MessagingException
      write 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 write
      strUrl - the URL to write to
      mimeDetails -
      Returns:
      HttpURLConnection the opened http connection, null in case of error or file
      Throws:
      IOException
      jakarta.mail.MessagingException
    • writeToQueue

      public static JDFDoc writeToQueue(JDFDoc docJMF, JDFDoc docJDF, String strUrl, MimeUtil.MIMEDetails urlDet) throws IOException, jakarta.mail.MessagingException
      submit a multipart file to a queue
      Parameters:
      docJMF - the jmf document containing the submitqueueentry or resubmitqueueentry
      docJDF - the jdf to submit
      strUrl - the url to submit to
      urlDet - url details
      Returns:
      Throws:
      IOException
      jakarta.mail.MessagingException
    • writeToFile

      @Deprecated public static File writeToFile(jakarta.mail.Multipart m, String fileName)
      Deprecated.
      Parameters:
      m -
      fileName -
      Returns:
      the File that was written
    • writeToFile

      public static File writeToFile(jakarta.mail.Multipart m, String fileName, MimeUtil.MIMEDetails md)
      write a Multipart to an output file
      Parameters:
      m - the mime MultiPart to write
      fileName - the file name
      md -
      Returns:
    • writeToStream

      @Deprecated public static void writeToStream(jakarta.mail.Multipart m, OutputStream outStream) throws IOException, jakarta.mail.MessagingException
      Deprecated.
      Parameters:
      m -
      outStream -
      Throws:
      IOException
      jakarta.mail.MessagingException
    • writeToStream

      public static void writeToStream(jakarta.mail.Multipart m, OutputStream outStream, MimeUtil.MIMEDetails md) throws IOException, jakarta.mail.MessagingException
      write a Multipart to a Stream
      Parameters:
      m - the mime MultiPart to write
      outStream - the existing output stream, note that a buffered output stream is created in case outStream is unbuffered
      md - details for messaging
      Throws:
      IOException
      jakarta.mail.MessagingException
    • writeToDir

      public static void writeToDir(jakarta.mail.Multipart mp, File directory) throws jakarta.mail.MessagingException, IOException
      write a Message to a directory
      Parameters:
      mp - the mime Message to write
      directory - the directory to use as '.' for writing the mime parts
      Throws:
      jakarta.mail.MessagingException
      IOException
      jakarta.mail.MessagingException
    • writeBodyPartToFile

      public static void writeBodyPartToFile(jakarta.mail.BodyPart bp, File directory) throws IOException, jakarta.mail.MessagingException
      Parameters:
      bp -
      directory -
      Throws:
      jakarta.mail.MessagingException
      IOException
    • getJMFSubmission

      public static JDFDoc[] getJMFSubmission(jakarta.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(String contentType)
      Parameters:
      contentType -
      Returns: