Class ServletUtil


  • public class ServletUtil
    extends Object
    Some helper utilities to download/upload a document and its resources. The downloaded document is also added to the recent files of the user.
    Author:
    Sebastian Stein
    • Constructor Summary

      Constructors 
      Constructor Description
      ServletUtil()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static Session checkEvenOneMenu​(javax.servlet.http.HttpServletRequest request, long... menuIds)
      Checks if at least one of the given menus is accessible by the user in the current session
      static Session checkMenu​(javax.servlet.http.HttpServletRequest request, long menuId)  
      static void downloadDocument​(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response, String sid, long docId, String fileVersion, String fileName, String suffix, User user)
      Sends the specified document to the response object; the client will receive it as a download
      static void downloadDocument​(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response, String sid, String docId, String fileVersion, String fileName, User user)
      Sends the specified document to the response object; the client will receive it as a download
      static void downloadDocumentText​(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response, long docId, User user)
      Sends the specified document's indexed text to the response object; the client will receive it as a download
      static void downloadFile​(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response, File file, String fileName)
      Sends the specified file to the response object; the client will receive it as a download Sends the specified file to the response object; the client will receive it as a download
      static void downloadPluginResource​(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response, String sid, String pluginName, String resourcePath, String fileName)
      Downloads a plugin resource
      static void setContentDisposition​(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response, String filename)
      Sets the correct Content-Disposition header into the response
      static void uploadDocumentResource​(javax.servlet.http.HttpServletRequest request, String docId, String suffix, String fileVersion, String docVersion)
      Uploads a document's related resource.
      static Session validateSession​(javax.servlet.http.HttpServletRequest request)  
    • Constructor Detail

      • ServletUtil

        public ServletUtil()
    • Method Detail

      • validateSession

        public static Session validateSession​(javax.servlet.http.HttpServletRequest request)
                                       throws javax.servlet.ServletException
        Throws:
        javax.servlet.ServletException
      • checkMenu

        public static Session checkMenu​(javax.servlet.http.HttpServletRequest request,
                                        long menuId)
                                 throws javax.servlet.ServletException
        Throws:
        javax.servlet.ServletException
      • checkEvenOneMenu

        public static Session checkEvenOneMenu​(javax.servlet.http.HttpServletRequest request,
                                               long... menuIds)
                                        throws javax.servlet.ServletException
        Checks if at least one of the given menus is accessible by the user in the current session
        Parameters:
        request - the HTTP request
        menuIds - identifiers of the menus
        Returns:
        if at least one of the menus is accessible
        Throws:
        javax.servlet.ServletException - error in the servlet container
      • downloadPluginResource

        public static void downloadPluginResource​(javax.servlet.http.HttpServletRequest request,
                                                  javax.servlet.http.HttpServletResponse response,
                                                  String sid,
                                                  String pluginName,
                                                  String resourcePath,
                                                  String fileName)
                                           throws FileNotFoundException,
                                                  IOException,
                                                  javax.servlet.ServletException,
                                                  InvalidSessionException
        Downloads a plugin resource
        Parameters:
        request - the HTTP request
        response - server's response
        pluginName - name of the plug-in
        resourcePath - Relative path of the plug-in's resource
        fileName - Optional file name to give to the downloaded content
        sid - identifier of the session
        Throws:
        FileNotFoundException - cannot find the plugin dir
        IOException - generic I/O error
        javax.servlet.ServletException - error in the servlet container
        InvalidSessionException - the session is unexisting or not valid
      • downloadDocument

        public static void downloadDocument​(javax.servlet.http.HttpServletRequest request,
                                            javax.servlet.http.HttpServletResponse response,
                                            String sid,
                                            long docId,
                                            String fileVersion,
                                            String fileName,
                                            String suffix,
                                            User user)
                                     throws FileNotFoundException,
                                            IOException,
                                            javax.servlet.ServletException,
                                            PersistenceException
        Sends the specified document to the response object; the client will receive it as a download
        Parameters:
        request - the current request
        response - the document is written to this object
        sid - Session identifier, if not provided the request parameter is inspected
        docId - Id of the document
        fileVersion - name of the file version; if null the latest version will be returned
        fileName - name of the file
        suffix - suffix of the linked document's resource
        user - current user
        Throws:
        javax.servlet.ServletException - error in the servlet container
        FileNotFoundException - resource's file not found
        IOException - generic I/O error
        PersistenceException - error in the database
      • downloadFile

        public static void downloadFile​(javax.servlet.http.HttpServletRequest request,
                                        javax.servlet.http.HttpServletResponse response,
                                        File file,
                                        String fileName)
                                 throws FileNotFoundException,
                                        IOException,
                                        javax.servlet.ServletException
        Sends the specified file to the response object; the client will receive it as a download Sends the specified file to the response object; the client will receive it as a download
        Parameters:
        request - the current request
        response - the file is written to this object
        file - file to serve
        fileName - client file name
        Throws:
        FileNotFoundException - cannot find the file to download
        IOException - generic I/O error
        javax.servlet.ServletException - error in the servlet container
      • setContentDisposition

        public static void setContentDisposition​(javax.servlet.http.HttpServletRequest request,
                                                 javax.servlet.http.HttpServletResponse response,
                                                 String filename)
                                          throws UnsupportedEncodingException
        Sets the correct Content-Disposition header into the response
        Parameters:
        request - the HTTP request
        response - the server's response
        filename - name of the file
        Throws:
        UnsupportedEncodingException - error trying to encode the response
      • downloadDocumentText

        public static void downloadDocumentText​(javax.servlet.http.HttpServletRequest request,
                                                javax.servlet.http.HttpServletResponse response,
                                                long docId,
                                                User user)
                                         throws FileNotFoundException,
                                                IOException,
                                                PersistenceException
        Sends the specified document's indexed text to the response object; the client will receive it as a download
        Parameters:
        request - the current request
        response - the document is written to this object
        docId - identifier of the document
        user - current user
        Throws:
        FileNotFoundException - cannot find the file to download
        IOException - generic I/O error
        PersistenceException - error at database level
      • downloadDocument

        public static void downloadDocument​(javax.servlet.http.HttpServletRequest request,
                                            javax.servlet.http.HttpServletResponse response,
                                            String sid,
                                            String docId,
                                            String fileVersion,
                                            String fileName,
                                            User user)
                                     throws FileNotFoundException,
                                            IOException,
                                            NumberFormatException,
                                            javax.servlet.ServletException,
                                            PersistenceException
        Sends the specified document to the response object; the client will receive it as a download
        Parameters:
        request - the current request
        response - the document is written to this object
        sid - identifier of the session
        docId - Id of the document
        fileVersion - name of the file version; if null the latest version will be returned
        fileName - name of the file
        user - current user
        Throws:
        FileNotFoundException - the document's file cannot be found
        IOException - generic I/O exception retrieving the document's file
        javax.servlet.ServletException - error in the servlet container
        NumberFormatException - error if docId is not a number
        PersistenceException - error in the database
      • uploadDocumentResource

        public static void uploadDocumentResource​(javax.servlet.http.HttpServletRequest request,
                                                  String docId,
                                                  String suffix,
                                                  String fileVersion,
                                                  String docVersion)
                                           throws Exception
        Uploads a document's related resource. The resource will be stored in the folder where the document's files reside using the following pattern: fileVersion-suffix If no version is specified, the current one is used instead
        Parameters:
        request - the current request
        docId - Id of the document
        suffix - Suffix of the document
        fileVersion - id of the file version; if null the latest version will returned
        docVersion - id of the doc version; if null the latest version will returned
        Throws:
        Exception - a generic error