1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 package fr.paris.lutece.portal.web.upload;
35
36 import java.io.IOException;
37 import java.text.DecimalFormat;
38 import java.text.NumberFormat;
39
40 import javax.servlet.Filter;
41 import javax.servlet.FilterChain;
42 import javax.servlet.FilterConfig;
43 import javax.servlet.ServletException;
44 import javax.servlet.ServletRequest;
45 import javax.servlet.ServletResponse;
46 import javax.servlet.http.HttpServletRequest;
47 import javax.servlet.http.HttpServletResponse;
48
49 import org.apache.commons.fileupload.FileUploadBase.SizeLimitExceededException;
50 import org.apache.commons.fileupload.FileUploadException;
51
52 import fr.paris.lutece.portal.service.util.AppLogService;
53 import fr.paris.lutece.util.http.MultipartUtil;
54
55
56
57
58 public abstract class UploadFilter implements Filter
59 {
60 private static final String PROPERTY_TITLE_FILE_SIZE_LIMIT_EXCEEDED = "portal.util.message.titleDefault";
61 private static final String PROPERTY_MESSAGE_FILE_SIZE_LIMIT_EXCEEDED = "portal.util.message.fileSizeLimitExceeded";
62 private static final int KILO_BYTE = 1024;
63 private static final String SIZE_THRESHOLD = "sizeThreshold";
64 private static final String REQUEST_SIZE_MAX = "requestSizeMax";
65 private static final String ACTIVATE_NORMALIZE_FILE_NAME = "activateNormalizeFileName";
66 private int _nSizeThreshold = -1;
67 private long _nRequestSizeMax = -1;
68 private boolean _bActivateNormalizeFileName;
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83 protected abstract String getMessageRelativeUrl( HttpServletRequest request, String strMessageKey, Object [ ] messageArgs, String strTitleKey );
84
85
86
87
88
89
90
91
92 @Override
93 public void init( FilterConfig config ) throws ServletException
94 {
95 try
96 {
97 String paramValue = config.getInitParameter( SIZE_THRESHOLD );
98
99 if ( paramValue != null )
100 {
101 _nSizeThreshold = Integer.parseInt( paramValue );
102 }
103
104 paramValue = config.getInitParameter( REQUEST_SIZE_MAX );
105
106 if ( paramValue != null )
107 {
108 _nRequestSizeMax = Long.parseLong( paramValue );
109 }
110
111 paramValue = config.getInitParameter( ACTIVATE_NORMALIZE_FILE_NAME );
112
113 if ( paramValue != null )
114 {
115 _bActivateNormalizeFileName = Boolean.valueOf( paramValue );
116 }
117 }
118 catch( NumberFormatException ex )
119 {
120 AppLogService.error( ex.getMessage( ), ex );
121 throw new ServletException( ex.getMessage( ), ex );
122 }
123 }
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138 @Override
139 public void doFilter( ServletRequest request, ServletResponse response, FilterChain chain ) throws IOException, ServletException
140 {
141 HttpServletRequest httpRequest = (HttpServletRequest) request;
142
143 if ( !MultipartUtil.isMultipart( httpRequest ) )
144 {
145 chain.doFilter( request, response );
146 }
147 else
148 {
149 try
150 {
151 MultipartHttpServletRequest multiHtppRequest = MultipartUtil.convert( _nSizeThreshold, _nRequestSizeMax, _bActivateNormalizeFileName,
152 httpRequest );
153 chain.doFilter( multiHtppRequest, response );
154 }
155 catch( SizeLimitExceededException e )
156 {
157 AppLogService.error( e.getMessage( ), e );
158
159 Object [ ] args = {
160 getDisplaySize( )
161 };
162 ( (HttpServletResponse) response ).sendRedirect(
163 getMessageRelativeUrl( httpRequest, PROPERTY_MESSAGE_FILE_SIZE_LIMIT_EXCEEDED, args, PROPERTY_TITLE_FILE_SIZE_LIMIT_EXCEEDED ) );
164 }
165 catch( FileUploadException e )
166 {
167 AppLogService.error( e.getMessage( ), e );
168 throw new ServletException( "Unkown error occured during the upload", e );
169 }
170 }
171 }
172
173
174
175
176
177
178 public long getRequestSizeMax( )
179 {
180 return _nRequestSizeMax;
181 }
182
183
184
185
186 @Override
187 public void destroy( )
188 {
189
190 }
191
192
193
194
195
196 private String getDisplaySize( )
197 {
198 long lSizeMax = getRequestSizeMax( );
199 DecimalFormat decimalFormat = (DecimalFormat) NumberFormat.getInstance( );
200 decimalFormat.applyPattern( "#" );
201
202 return ( lSizeMax >= KILO_BYTE ) ? ( String.valueOf( lSizeMax / KILO_BYTE ) ) : ( decimalFormat.format( lSizeMax / KILO_BYTE ) );
203 }
204 }