commons-fileupload-1.2
内部では、ファイルサイズの上限を超えたことを検知しているが、それを外部に通知する仕組みがない。
org.apache.myfaces.webapp.filter.MultipartRequestWrapper#parseRequestメソッドに
// TODO: find a way to notify the user about the fact that the uploaded file exceeded size limit
と記載されいる箇所。
org.apache.commons.fileupload.FileUploadBase.SizeLimitExceededExceptionをcatchしているが、ログを書き出して、パラメータを空にしているだけとなっている。
それを下記のように書き換えRuntimeExceptionをthrowする。
… try{ requestParameters = fileUpload.parseRequest(request); } catch (FileUploadBase.SizeLimitExceededException e) { // TODO: find a way to notify the user about the fact that the uploaded file exceeded size limit if(log.isInfoEnabled()) log.info("user tried to upload a file that exceeded file-size limitations.",e); requestParameters = Collections.EMPTY_LIST; // add throw new RuntimeException(e); }catch(FileUploadException fue){ log.error("Exception while uploading file.", fue); requestParameters = Collections.EMPTY_LIST; } …
さらにfilterで、それを検出する(クラスExceptionUtilsによる検出は、commons-langに依存)。
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException { try { chain.doFilter(req, res); } catch (RuntimeException e) { if (ExceptionUtils.indexOfThrowable(t, FileUploadBase.SizeLimitExceededException.class) > -1) { // アップロードされたファイルのサイズが上限を超えた場合の処理 } else { throw e; } } }