Class ResponseHelper

  • All Implemented Interfaces:

    
    public class ResponseHelper
    
                        
    Since:

    2021-03-10

    Author:

    trydofor

    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
    • Field Summary

      Fields 
      Modifier and Type Field Description
    • Constructor Summary

      Constructors 
      Constructor Description
      ResponseHelper()
    • Enum Constant Summary

      Enum Constants 
      Enum Constant Description
    • Method Summary

      Modifier and Type Method Description
      static String getDownloadContentType(String fileName) Get the correct ContentType for the download filename.
      static String getDownloadContentDisposition(@Nullable() String fileName) Get the correct ContentDisposition for the download filename.
      static void setDownloadContentType(@NotNull() HttpServletResponse response, @Nullable() String fileName) Set the correct ContentType to Response for the download filename.
      static void setDownloadContentDisposition(@NotNull() HttpServletResponse response, @Nullable() String fileName) Set the correct Content-Disposition to Response for the download filename.
      static void downloadFile(@NotNull() HttpServletResponse response, @Nullable() String fileName, @NotNull() @WillClose() InputStream stream) Directly download file with filename by HttpServletResponse, use stream as the content and close the stream.
      static OutputStream downloadFile(@NotNull() HttpServletResponse response, @Nullable() String fileName) Set the download filename to HttpServletResponse and return the output stream for user to write the content.
      static void downloadFile(@NotNull() HttpServletResponse response, @NotNull() File file) Directly download the file with its filename by HttpServletResponse
      static void downloadFileWithZip(@NotNull() HttpServletResponse response, @NotNull() @WillClose() Map<String, InputStream> files, @Nullable() String fileName)
      static void previewPDF(@NotNull() HttpServletResponse response, @Nullable() String fileName, @NotNull() @WillClose() InputStream stream) Directly preview the PDF by HttpServletResponse
      static void showCaptcha(HttpServletResponse response, String code) Output the image/jpeg captcha
      static void showCaptcha(HttpServletResponse response, String code, String fmt) Output the base64 image captcha in text/plain.
      static void bothHeadCookie(HttpServletResponse response, String key, String value, int second)
      static void writeBodyUtf8(HttpServletResponse response, String body)
      static InputStream tryCachingOutputStream(ServletResponse response)
      static void renderModelAndView(ModelAndView mav, HttpServletResponse res, HttpServletRequest req)
      static ResponseEntity<String> flatResponse(Map<String, out Object> data, String accept, String uri)
      Execute in the following order and return 3 forms, the Accept header strictly matches,
      as the browser sends multiple headers.
      
      (1) 200 xml,  accept=application/xml
      (2) 200 json, accept=application/json or uri=null or (3) if not match
      (3) 302 uri, uri != null
      
      • Methods inherited from class java.lang.Object

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

      • ResponseHelper

        ResponseHelper()
    • Method Detail

      • getDownloadContentType

        @NotNull() static String getDownloadContentType(String fileName)

        Get the correct ContentType for the download filename.

        Parameters:
        fileName - File name prompted during download
        Returns:

        ContentType, default APPLICATION_OCTET_STREAM_VALUE

      • setDownloadContentType

         static void setDownloadContentType(@NotNull() HttpServletResponse response, @Nullable() String fileName)

        Set the correct ContentType to Response for the download filename.

        Parameters:
        response - HttpServletResponse
        fileName - File name prompted during download
      • setDownloadContentDisposition

         static void setDownloadContentDisposition(@NotNull() HttpServletResponse response, @Nullable() String fileName)

        Set the correct Content-Disposition to Response for the download filename.

        Parameters:
        response - HttpServletResponse
        fileName - File name prompted during download
      • downloadFile

         static void downloadFile(@NotNull() HttpServletResponse response, @Nullable() String fileName, @NotNull() @WillClose() InputStream stream)

        Directly download file with filename by HttpServletResponse, use stream as the content and close the stream.

        Parameters:
        response - HttpServletResponse
        fileName - File name prompted during download
        stream - input stream
      • downloadFile

         static OutputStream downloadFile(@NotNull() HttpServletResponse response, @Nullable() String fileName)

        Set the download filename to HttpServletResponse and return the output stream for user to write the content.

        Parameters:
        response - HttpServletResponse
        fileName - File name prompted during download
      • downloadFile

         static void downloadFile(@NotNull() HttpServletResponse response, @NotNull() File file)

        Directly download the file with its filename by HttpServletResponse

        Parameters:
        response - HttpServletResponse
        file - file and filename to download
      • previewPDF

         static void previewPDF(@NotNull() HttpServletResponse response, @Nullable() String fileName, @NotNull() @WillClose() InputStream stream)

        Directly preview the PDF by HttpServletResponse

        Parameters:
        response - HttpServletResponse
        fileName - the PDF filename to preview
        stream - input stream
      • showCaptcha

         static void showCaptcha(HttpServletResponse response, String code)

        Output the image/jpeg captcha

        Parameters:
        response - response
        code - captcha
      • showCaptcha

         static void showCaptcha(HttpServletResponse response, String code, String fmt)

        Output the base64 image captcha in text/plain.

        Parameters:
        response - response
        code - captcha
        fmt - format, `{base64}` is placeholder
      • renderModelAndView

         static void renderModelAndView(ModelAndView mav, HttpServletResponse res, HttpServletRequest req)
      • flatResponse

        @NotNull() static ResponseEntity<String> flatResponse(Map<String, out Object> data, String accept, String uri)
        Execute in the following order and return 3 forms, the Accept header strictly matches,
        as the browser sends multiple headers.
        
        (1) 200 xml,  accept=application/xml
        (2) 200 json, accept=application/json or uri=null or (3) if not match
        (3) 302 uri, uri != null