88
99import java .io .UnsupportedEncodingException ;
1010import java .net .URLEncoder ;
11+ import java .nio .charset .StandardCharsets ;
1112
1213public abstract class GXWebReport extends GXWebProcedure {
1314 private static Logger log = org .apache .logging .log4j .LogManager .getLogger (GXWebReport .class );
@@ -107,7 +108,7 @@ protected boolean initPrinter(String output, int gxXPage, int gxYPage, String in
107108 int y [] = {
108109 gxYPage
109110 };
110- setResponseOuputFileName ();
111+ setResponseOutputFileName ();
111112
112113 getPrinter ().GxRVSetLanguage (localUtil ._language );
113114 boolean ret = getPrinter ().GxPrintInit (output , x , y , iniFile , form , printer , mode , orientation , pageSize , pageLength , pageWidth , scale , copies , defSrc , quality , color , duplex );
@@ -118,19 +119,27 @@ protected boolean initPrinter(String output, int gxXPage, int gxYPage, String in
118119 return ret ;
119120 }
120121
121- private void setResponseOuputFileName () {
122- String outputFileName = filename != null ? filename : getClass ().getSimpleName ();
123- String outputFileType = filetype != null ? "." + filetype .toLowerCase () : ".pdf" ;
122+ private void setResponseOutputFileName () {
123+ String outputFileName = ( filename != null ? filename : getClass ().getSimpleName () );
124+ String outputFileType = ( filetype != null ? "." + filetype .toLowerCase () : ".pdf" ) ;
124125
125126 try {
126- // Encode the filename in UTF-8 according to RFC 5987
127127 String encodedFileName = URLEncoder .encode (outputFileName , "UTF-8" ).replace ("+" , "%20" );
128- String dispositionHeader = "inline; filename=\" " + asciiFileName (outputFileName ) +
129- "\" ; filename*=UTF-8''" + encodedFileName + outputFileType ;
128+ boolean isAscii = StandardCharsets .US_ASCII .newEncoder ().canEncode (outputFileName );
129+ String fallbackFileName = outputFileName ;
130+ if (!isAscii ) {
131+ fallbackFileName = outputFileName .replaceAll ("[^\\ p{ASCII}]" , "" );
132+ if (fallbackFileName .isEmpty ()) {
133+ fallbackFileName = "download" ;
134+ }
135+ }
136+ String contentDispositionValue =
137+ "inline; filename=\" " + fallbackFileName + outputFileType + "\" ; " +
138+ "filename*=UTF-8''" + encodedFileName + outputFileType ;
130139
131- httpContext .getResponse ().addHeader ("Content-Disposition" , dispositionHeader );
140+ httpContext .getResponse ().addHeader ("Content-Disposition" , contentDispositionValue );
132141 } catch (UnsupportedEncodingException e ) {
133- log . error ( "Failed to encode the name of the report" , e );
142+ httpContext . getResponse (). addHeader ( "Content-Disposition" , "inline; filename= \" download" + outputFileType + " \" " );
134143 }
135144 }
136145
0 commit comments