From ab30707954fcf245ae38196607ba7bd00d56aa35 Mon Sep 17 00:00:00 2001 From: atos-mohamed-b <126660650+atos-mohamed-b@users.noreply.github.com> Date: Mon, 10 Jun 2024 17:41:41 +0200 Subject: [PATCH] LUT-28047: Fixed SONAR issues with 'Exceptions' handling --- .../web/download/AbstractDownloadServlet.java | 39 +++++++++++++++++-- 1 file changed, 35 insertions(+), 4 deletions(-) diff --git a/src/java/fr/paris/lutece/portal/web/download/AbstractDownloadServlet.java b/src/java/fr/paris/lutece/portal/web/download/AbstractDownloadServlet.java index 4eec643449..b84b08a0a8 100644 --- a/src/java/fr/paris/lutece/portal/web/download/AbstractDownloadServlet.java +++ b/src/java/fr/paris/lutece/portal/web/download/AbstractDownloadServlet.java @@ -49,6 +49,7 @@ import fr.paris.lutece.portal.service.message.SiteMessageException; import fr.paris.lutece.portal.service.message.SiteMessageService; import fr.paris.lutece.portal.service.security.UserNotSignedException; +import fr.paris.lutece.portal.service.util.AppLogService; import fr.paris.lutece.portal.service.util.AppPathService; import fr.paris.lutece.portal.web.PortalJspBean; @@ -91,19 +92,25 @@ protected void doGet( HttpServletRequest request, HttpServletResponse response ) } catch( UserNotSignedException e ) { - response.sendRedirect( response.encodeRedirectURL( PortalJspBean.redirectLogin( request ) ) ); + sendRedirection( response, response.encodeRedirectURL( PortalJspBean.redirectLogin( request ) ) ); } } if ( file == null || file.getPhysicalFile( ) == null ) { - SiteMessageService.setMessage( request, MESSAGE_UNKNOWN_FILE ); + try + { + SiteMessageService.setMessage( request, MESSAGE_UNKNOWN_FILE ); + } + catch( SiteMessageException e ) + { + sendRedirection( response, AppPathService.getSiteMessageUrl( request ) ); + } } - } catch( SiteMessageException e ) { - response.sendRedirect( AppPathService.getSiteMessageUrl( request ) ); + sendRedirection( response, AppPathService.getSiteMessageUrl( request ) ); } if ( file != null ) @@ -115,6 +122,30 @@ protected void doGet( HttpServletRequest request, HttpServletResponse response ) response.setHeader( "Content-Disposition", "attachment; filename=\"" + file.getTitle( ) + "\";" ); outputStream.write( file.getPhysicalFile( ).getValue( ) ); } + catch ( IOException e ) + { + AppLogService.error( "The file could not be sent", e ); + } + } + } + + /** + * Send a redirect response to the client, with a specific redirection URL + * + * @param response + * The response + * @param redirectUrl + * The redirection URL + */ + private void sendRedirection( HttpServletResponse response, String redirectUrl ) + { + try + { + response.sendRedirect( redirectUrl ); + } + catch( IOException e ) + { + AppLogService.error( "Unable to redirect", e ); } }