Class GatewayUtils


  • public class GatewayUtils
    extends java.lang.Object
    • Constructor Summary

      Constructors 
      Constructor Description
      GatewayUtils()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static void autoBackupDirectory​(java.io.File base, java.io.File sink, int count, java.lang.String prefix)
      Creates an auto-backup folder underneath the base folder with all of the file contents of the base folder.
      static void autoBackupDirectory​(java.io.File base, java.io.File sink, int count, java.lang.String prefix, java.io.FileFilter filter)  
      static void clearDirectory​(java.io.File dir)
      Removes all files from the given directory, but not any subfolders
      static void clearDirectory​(java.io.File dir, java.io.FileFilter filter)
      Removes all files from the given directory that match the criteria, but not any subfolders
      static void clearDirectoryDeep​(java.io.File dir)
      Removes all files and subfolders from the given directory, but not the directory itself
      static void copyAll​(java.io.File sourceFolder, java.io.File destFolder)
      Copies all of the files (shallow) in the source folder into the destination folder
      static void copyFile​(java.io.File source, java.io.File dest)
      Copies the source file to the destination file, creating the destination if necessary.
      static void copyFile​(java.io.InputStream fin, java.io.File dest)
      Copies all remaining bytes in the input stream to the output file, creating the destination file if necessary.
      static void createFile​(java.io.File contextFile, java.lang.String filePath, byte[] fileData)
      Creates a file at ${contextFile}/filePath.
      static java.io.File createTempDirectory()
      Creates a new, empty temp dir.
      static java.lang.String getCertCommonName​(java.security.cert.X509Certificate certificate, boolean useIssuer)
      Returns the CN field in a certificate.
      static java.io.File getMostRecentAutobackupDirectory​(java.io.File base, java.lang.String prefix)  
      static java.io.File getSubDir​(java.io.File base, java.lang.String... subDirs)
      Returns a File representing a subdirectory, by following the paths from the base folder.
      static java.io.File getSubFile​(java.io.File base, java.lang.String filePath)
      Returns a subfile beneath directory.
      static java.lang.String md5​(java.lang.String source)  
      static void noCache​(javax.servlet.http.HttpServletResponse res)
      Adds headers to a servlet response that should prevent it from caching at any level
      static void noCache_IE6​(javax.servlet.http.HttpServletResponse res)  
      static void removeDirectory​(java.io.File dir)
      Removes a directory, including all subfiles and subfolders recursively
      static void removeDirectory​(java.io.File contextFile, java.lang.String dirPath)
      Removes the directory at ${contextFile}/dirPath.
      static boolean removeFile​(java.io.File contextFile, java.lang.String filePath)
      Removes a file at ${contextFile}/filePath.
      static java.lang.String sha256​(java.lang.String source)  
      static java.lang.String unzipFile​(java.io.File zipFile, java.io.OutputStream output, java.lang.String name)
      This version, which takes a direct file, is more efficient than using a stream.
      static byte[] unzipFile​(java.io.File zipFile, java.lang.String name)
      Unzips the given file name from zip file at the path specified.
      static java.lang.String unzipFile​(java.io.InputStream zipFile, java.io.OutputStream output, java.lang.String name)
      Unzips the given file from the given input stream, which will be wrapped in a ZipInputStream within this function.
      static byte[] unzipFile​(java.io.InputStream zipFile, java.lang.String name)
      Unzips the given file from the given input stream, which will be wrapped in a ZipInputStream within this function.
      static java.lang.String unzipFile​(java.util.zip.ZipFile zip, java.io.OutputStream output, java.lang.String name)  
      static byte[] unzipFile​(java.util.zip.ZipFile zipFile, java.lang.String name)
      Unzips the given file name from the provided ZipFile.
      static boolean zipEntryExists​(java.io.InputStream zipFile, java.lang.String name)
      Returns true if the zip file contains an entry with the given name (case insensitive)
      static boolean zipEntryExists​(java.util.zip.ZipFile file, java.lang.String name)  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • GatewayUtils

        public GatewayUtils()
    • Method Detail

      • noCache

        public static void noCache​(javax.servlet.http.HttpServletResponse res)
        Adds headers to a servlet response that should prevent it from caching at any level
      • noCache_IE6

        public static void noCache_IE6​(javax.servlet.http.HttpServletResponse res)
      • createTempDirectory

        public static java.io.File createTempDirectory()
                                                throws java.io.IOException
        Creates a new, empty temp dir. Does not delete automatically, make sure you remove it.
        Throws:
        java.io.IOException
      • autoBackupDirectory

        public static void autoBackupDirectory​(java.io.File base,
                                               java.io.File sink,
                                               int count,
                                               java.lang.String prefix)
                                        throws java.io.IOException

        Creates an auto-backup folder underneath the base folder with all of the file contents of the base folder. Using naming conventions, aims to keep a maximum of count autobackup directories at any one time.

        For instance, given the following folder structure,

         folder/
         folder/file1.xyz
         folder/file2.txt
         

        calling autobackup on Nov 3rd, 2008 at 2:30:59PM would make this:

         folder/
         folder/file1.xyz
         folder/file2.txt
         folder/autobackup_20081103_143059/
         folder/autobackup_20081103_143059/file1.xyz
         folder/autobackup_20081103_143059/file2.txt
         
        Throws:
        java.io.IOException
      • autoBackupDirectory

        public static void autoBackupDirectory​(java.io.File base,
                                               java.io.File sink,
                                               int count,
                                               java.lang.String prefix,
                                               java.io.FileFilter filter)
                                        throws java.io.IOException
        Throws:
        java.io.IOException
      • getMostRecentAutobackupDirectory

        public static java.io.File getMostRecentAutobackupDirectory​(java.io.File base,
                                                                    java.lang.String prefix)
      • copyAll

        public static void copyAll​(java.io.File sourceFolder,
                                   java.io.File destFolder)
                            throws java.io.IOException
        Copies all of the files (shallow) in the source folder into the destination folder
        Throws:
        java.io.IOException
      • copyFile

        public static void copyFile​(java.io.File source,
                                    java.io.File dest)
                             throws java.io.IOException
        Copies the source file to the destination file, creating the destination if necessary.
        Throws:
        java.io.IOException
      • copyFile

        public static void copyFile​(java.io.InputStream fin,
                                    java.io.File dest)
                             throws java.io.IOException
        Copies all remaining bytes in the input stream to the output file, creating the destination file if necessary. Input steam is not closed.
        Throws:
        java.io.IOException
      • getSubDir

        public static java.io.File getSubDir​(java.io.File base,
                                             java.lang.String... subDirs)
        Returns a File representing a subdirectory, by following the paths from the base folder. For example, if base is "C:\Program Files\Inductive Automation\SCADARail\" and you send "abc", "xyz" as subDirs, you'll get the following File back: "C:\Program Files\Inductive Automation\SCADARail\abc\xyz\"
        Parameters:
        base - A File that represents the starting folders.
        subDirs - Names of folders to follow from the base.
      • getSubFile

        public static java.io.File getSubFile​(java.io.File base,
                                              java.lang.String filePath)
        Returns a subfile beneath directory. The filePath may contain multiple levels, separated by "/"
      • removeDirectory

        public static void removeDirectory​(java.io.File dir)
        Removes a directory, including all subfiles and subfolders recursively
      • clearDirectoryDeep

        public static void clearDirectoryDeep​(java.io.File dir)
        Removes all files and subfolders from the given directory, but not the directory itself
      • clearDirectory

        public static void clearDirectory​(java.io.File dir)
        Removes all files from the given directory, but not any subfolders
      • clearDirectory

        public static void clearDirectory​(java.io.File dir,
                                          java.io.FileFilter filter)
        Removes all files from the given directory that match the criteria, but not any subfolders
      • createFile

        public static void createFile​(java.io.File contextFile,
                                      java.lang.String filePath,
                                      byte[] fileData)
                               throws java.io.IOException
        Creates a file at ${contextFile}/filePath. The passed contextFile must be a File object that can be returned from the context, such as context.getDataDir() or context.getUserlibDir(). File path should be like "abc.txt" or "folder/abc.txt"
        Throws:
        java.io.IOException
      • removeFile

        public static boolean removeFile​(java.io.File contextFile,
                                         java.lang.String filePath)
        Removes a file at ${contextFile}/filePath. The passed contextFile must be a File object that can be returned from the context, such as context.getDataDir() or context.getUserlibDir().
      • removeDirectory

        public static void removeDirectory​(java.io.File contextFile,
                                           java.lang.String dirPath)
        Removes the directory at ${contextFile}/dirPath. The passed contextFile must be a File object that can be returned from the context, such as context.getDataDir() or context.getUserlibDir().
      • unzipFile

        public static byte[] unzipFile​(java.io.InputStream zipFile,
                                       java.lang.String name)
                                throws java.io.IOException
        Unzips the given file from the given input stream, which will be wrapped in a ZipInputStream within this function.
        Parameters:
        name - the filename entry in the zip file to extract.
        Returns:
        a byte[] representing the file
        Throws:
        java.io.IOException - if the name was not found, or if any other IOException occurrs
      • unzipFile

        public static byte[] unzipFile​(java.io.File zipFile,
                                       java.lang.String name)
                                throws java.io.IOException
        Unzips the given file name from zip file at the path specified.
        Parameters:
        zipFile - the path to a file, which should be a zip file.
        name - the filename entry in the zip file to extract.
        Returns:
        a byte[] representing the file
        Throws:
        java.io.IOException - if the name was not found, or if any other IOException occurrs
      • unzipFile

        public static byte[] unzipFile​(java.util.zip.ZipFile zipFile,
                                       java.lang.String name)
                                throws java.io.IOException
        Unzips the given file name from the provided ZipFile.
        Parameters:
        zipFile - the ZipFile representation of a zip.
        name - the filename entry in the zip file to extract.
        Returns:
        a byte[] representing the file
        Throws:
        java.io.IOException - if the name was not found, or if any other IOException occurrs
      • unzipFile

        public static java.lang.String unzipFile​(java.io.InputStream zipFile,
                                                 java.io.OutputStream output,
                                                 java.lang.String name)
                                          throws java.io.IOException
        Unzips the given file from the given input stream, which will be wrapped in a ZipInputStream within this function. The file will be written to the given outputstream. The streams will be closed.
        Parameters:
        zipFile - An input stream for the zip file. Should not be a ZipInputStream. Will be closed.
        output - An output stream to write the bytes to.
        name - Path to the file. If the name ends in '*', the first matching entry that starts with the name will be used.
        Returns:
        the full name of the entry. Useful when using wildcard names, to see what the actual entry was.
        Throws:
        java.io.IOException - On IO error or if the entry isn't found.
      • unzipFile

        public static java.lang.String unzipFile​(java.io.File zipFile,
                                                 java.io.OutputStream output,
                                                 java.lang.String name)
                                          throws java.io.IOException
        This version, which takes a direct file, is more efficient than using a stream.
        Throws:
        java.io.IOException
      • unzipFile

        public static java.lang.String unzipFile​(java.util.zip.ZipFile zip,
                                                 java.io.OutputStream output,
                                                 java.lang.String name)
                                          throws java.io.IOException
        Throws:
        java.io.IOException
      • zipEntryExists

        public static boolean zipEntryExists​(java.io.InputStream zipFile,
                                             java.lang.String name)
                                      throws java.io.IOException
        Returns true if the zip file contains an entry with the given name (case insensitive)
        Throws:
        java.io.IOException
      • zipEntryExists

        public static boolean zipEntryExists​(java.util.zip.ZipFile file,
                                             java.lang.String name)
      • md5

        public static java.lang.String md5​(java.lang.String source)
      • sha256

        public static java.lang.String sha256​(java.lang.String source)
      • getCertCommonName

        public static java.lang.String getCertCommonName​(java.security.cert.X509Certificate certificate,
                                                         boolean useIssuer)
        Returns the CN field in a certificate. If useIssuer is true, then the issuer's CN field is used. Otherwise, the subject's CN field is used
        Parameters:
        certificate - the certificate to extract CN from.
        useIssuer - if true, extract the issuer CN; otherwise the subject SN.
        Returns:
        either the issuer or subject CN, depending on useIssuer.