Class Tools


  • public class Tools
    extends Object
    The Class Tools.
    Author:
    Martin Scharm
    • Constructor Summary

      Constructors 
      Constructor Description
      Tools()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static void addOmexMetaData​(de.unirostock.sems.cbarchive.meta.MetaDataHolder entity, de.unirostock.sems.cbarchive.meta.omex.VCard creator, boolean create)
      Adds current date as modification and adds the creator, if not done yet, to every Omex description Also creates new Omex description, if create is set to true and only if necessary.
      static void addOmexMetaData​(de.unirostock.sems.cbarchive.meta.MetaDataHolder entity, de.unirostock.sems.cbarchive.meta.omex.VCard creator, String additionalDescription, boolean create)
      Adds current date as modification and an additional description, as well as the creator, if not done yet, to every Omex description.
      static boolean areVCardEqual​(de.unirostock.sems.cbarchive.meta.omex.VCard vcard1, de.unirostock.sems.cbarchive.meta.omex.VCard vcard2)
      Compares 2 VCards and returns true if both are identical in means of String.equal() or if both are null
      static boolean checkQuota​(long currentValue, long quota)
      Returns false, if a quota is exceeded.
      static void checkQuotasOrFail​(long fileSize, Archive archive, UserManager user)
      Checks for all quotas required to add/update an file within a CombineArchive.
      static String cleanUpFileName​(String fileName)
      removes all non alpha-numeric symbols from a file name.
      static boolean containsVCard​(Collection<de.unirostock.sems.cbarchive.meta.omex.VCard> collection, de.unirostock.sems.cbarchive.meta.omex.VCard vcard)
      Checks if the given VCard exists already in the Collection.
      static long copyStream​(InputStream input, OutputStream output, long maxLength)
      Copies an InputStream into an OutputStream and closes all streams afterwards.
      static UserManager doLogin​(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)
      Tries to obtain user instance (workspace), if fails it crates a new one
      static UserManager doLogin​(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response, boolean createNew)
      Tries to obtain user instance (workspace)
      if createNew is true, it also tries to create a new user instance.
      static String extractFileName​(javax.servlet.http.Part part)
      Extract file name.
      static URI generateArchiveRedirectUri​(javax.servlet.http.HttpServletRequest requestContext, String archiveId)
      Generates a redirect URI to an archive.
      static String generateHashId​(String input)
      Generate hash id.
      static URI generateWorkspaceRedirectUri​(javax.servlet.http.HttpServletRequest requestContext, String workspaceId)
      Generates Share URI to a workspace.
      static UserManager getUser​(CookieManager cookies)
      Gets the user.
      static boolean isFilenameBlacklisted​(String filename)
      checks whether a filename is blacklisted or not
      static void storeUserCookies​(CookieManager cookies, UserManager user)
      Store user cookies.
      static String suggestFileNameFromHttpResponse​(String remoteUrl, org.apache.http.HttpResponse response)
      Suggests a filename for the queried file, base on the Content-Disposition Header field or the URL.
      static String suggestFileNameFromHttpResponse​(org.apache.http.HttpRequest request, org.apache.http.HttpResponse response)
      Suggests a filename for the queried file, base on the Content-Disposition Header field or the URL.
      static String suggestFileNameFromHttpResponse​(org.apache.http.HttpResponse response)
      Suggests a filename for the queried file, base on the Content-Disposition Header field.
      static Path writeStreamToTempFile​(String tempFileName, InputStream input)
      writes a input stream entirely into a newly created temp file.
    • Field Detail

      • DATE_FORMATTER

        public static final SimpleDateFormat DATE_FORMATTER
        The Constant DATE_FORMATTER.
    • Constructor Detail

      • Tools

        public Tools()
    • Method Detail

      • storeUserCookies

        public static void storeUserCookies​(CookieManager cookies,
                                            UserManager user)
        Store user cookies.
        Parameters:
        cookies - the cookies
        user - the user
      • extractFileName

        public static final String extractFileName​(javax.servlet.http.Part part)
        Extract file name.
        Parameters:
        part - the part
        Returns:
        the string
      • generateHashId

        public static String generateHashId​(String input)
        Generate hash id.
        Parameters:
        input - the input
        Returns:
        the hash id
      • checkQuota

        public static boolean checkQuota​(long currentValue,
                                         long quota)
        Returns false, if a quota is exceeded. Otherwise true
        Parameters:
        currentValue - the current size
        quota - the quota size
        Returns:
        true, if quota is not exceeded
      • checkQuotasOrFail

        public static void checkQuotasOrFail​(long fileSize,
                                             Archive archive,
                                             UserManager user)
                                      throws QuotaException
        Checks for all quotas required to add/update an file within a CombineArchive. If a quota is exceeded it fails by throwing an QuotaException.
        Parameters:
        fileSize - of the uploaded file
        archive - the archive
        user - the user
        Throws:
        QuotaException - an error if the quotas on the server are exhausted
      • generateArchiveRedirectUri

        public static URI generateArchiveRedirectUri​(javax.servlet.http.HttpServletRequest requestContext,
                                                     String archiveId)
        Generates a redirect URI to an archive.
        Parameters:
        requestContext - the request context
        archiveId - the archive id
        Returns:
        the uri
      • generateWorkspaceRedirectUri

        public static URI generateWorkspaceRedirectUri​(javax.servlet.http.HttpServletRequest requestContext,
                                                       String workspaceId)
        Generates Share URI to a workspace.
        Parameters:
        requestContext - the request context
        workspaceId - the workspace id
        Returns:
        the uri
      • isFilenameBlacklisted

        public static boolean isFilenameBlacklisted​(String filename)
        checks whether a filename is blacklisted or not
        Parameters:
        filename - the file name
        Returns:
        true if filename is blacklisted
      • addOmexMetaData

        public static void addOmexMetaData​(de.unirostock.sems.cbarchive.meta.MetaDataHolder entity,
                                           de.unirostock.sems.cbarchive.meta.omex.VCard creator,
                                           boolean create)
        Adds current date as modification and adds the creator, if not done yet, to every Omex description Also creates new Omex description, if create is set to true and only if necessary.
        Parameters:
        entity - the entity
        creator - the creator
        create - should omex description be created?
      • addOmexMetaData

        public static void addOmexMetaData​(de.unirostock.sems.cbarchive.meta.MetaDataHolder entity,
                                           de.unirostock.sems.cbarchive.meta.omex.VCard creator,
                                           String additionalDescription,
                                           boolean create)
        Adds current date as modification and an additional description, as well as the creator, if not done yet, to every Omex description. Also creates new Omex description, if create is set to true and only if necessary
        Parameters:
        entity - the entity
        creator - the creator
        additionalDescription - the additional description
        create - should omex description be created?
      • containsVCard

        public static boolean containsVCard​(Collection<de.unirostock.sems.cbarchive.meta.omex.VCard> collection,
                                            de.unirostock.sems.cbarchive.meta.omex.VCard vcard)
        Checks if the given VCard exists already in the Collection.
        Parameters:
        collection - the vcard collection
        vcard - the vcard in question
        Returns:
        true, if collection contains vcard
      • areVCardEqual

        public static boolean areVCardEqual​(de.unirostock.sems.cbarchive.meta.omex.VCard vcard1,
                                            de.unirostock.sems.cbarchive.meta.omex.VCard vcard2)
        Compares 2 VCards and returns true if both are identical in means of String.equal() or if both are null
        Parameters:
        vcard1 - the vcard1
        vcard2 - the vcard2
        Returns:
        true, if vcards are equal
      • copyStream

        public static long copyStream​(InputStream input,
                                      OutputStream output,
                                      long maxLength)
                               throws IOException
        Copies an InputStream into an OutputStream and closes all streams afterwards. Stops at max length.
        Parameters:
        input - the input
        output - the output
        maxLength - the max length
        Returns:
        the size of the stream copied
        Throws:
        IOException - the IO exception
      • writeStreamToTempFile

        public static Path writeStreamToTempFile​(String tempFileName,
                                                 InputStream input)
                                          throws IOException
        writes a input stream entirely into a newly created temp file. Closes all streams afterwards
        Parameters:
        tempFileName - the temp file name
        input - the input
        Returns:
        Path to temp file
        Throws:
        IOException - the IO exception
      • suggestFileNameFromHttpResponse

        public static String suggestFileNameFromHttpResponse​(org.apache.http.HttpRequest request,
                                                             org.apache.http.HttpResponse response)
        Suggests a filename for the queried file, base on the Content-Disposition Header field or the URL.
        Parameters:
        request - the request
        response - the response
        Returns:
        A name suggestion or null
      • suggestFileNameFromHttpResponse

        public static String suggestFileNameFromHttpResponse​(String remoteUrl,
                                                             org.apache.http.HttpResponse response)
        Suggests a filename for the queried file, base on the Content-Disposition Header field or the URL.
        Parameters:
        remoteUrl - the remote url
        response - the response
        Returns:
        A name suggestion or null
      • suggestFileNameFromHttpResponse

        public static String suggestFileNameFromHttpResponse​(org.apache.http.HttpResponse response)
        Suggests a filename for the queried file, base on the Content-Disposition Header field.
        Parameters:
        response - the response
        Returns:
        A name suggestion or null
      • cleanUpFileName

        public static String cleanUpFileName​(String fileName)
        removes all non alpha-numeric symbols from a file name.
        Parameters:
        fileName - the file name
        Returns:
        the cleaned string