001// Copyright 2008, 2009 The Apache Software Foundation 002// 003// Licensed under the Apache License, Version 2.0 (the "License"); 004// you may not use this file except in compliance with the License. 005// You may obtain a copy of the License at 006// 007// http://www.apache.org/licenses/LICENSE-2.0 008// 009// Unless required by applicable law or agreed to in writing, software 010// distributed under the License is distributed on an "AS IS" BASIS, 011// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 012// See the License for the specific language governing permissions and 013// limitations under the License. 014 015package org.apache.tapestry5.upload.internal.services; 016 017import org.apache.commons.fileupload.FileUploadException; 018import org.apache.tapestry5.annotations.Events; 019import org.apache.tapestry5.internal.services.ComponentResultProcessorWrapper; 020import org.apache.tapestry5.ioc.annotations.Primary; 021import org.apache.tapestry5.runtime.Component; 022import org.apache.tapestry5.services.*; 023import org.apache.tapestry5.upload.services.MultipartDecoder; 024import org.apache.tapestry5.upload.services.UploadEvents; 025 026import java.io.IOException; 027 028/** 029 * Determines if there was an {@link org.apache.commons.fileupload.FileUploadException} processing the request and, if 030 * so, triggers an exception event on the page. If the page fails to respond to the event, then 031 */ 032@Events(UploadEvents.UPLOAD_EXCEPTION + " when a exception occur processing the upload") 033public class UploadExceptionFilter implements ComponentEventRequestFilter 034{ 035 private final MultipartDecoder decoder; 036 037 private final ComponentEventResultProcessor resultProcessor; 038 039 private ComponentSource componentSource; 040 041 public UploadExceptionFilter(MultipartDecoder decoder, 042 @Traditional @Primary ComponentEventResultProcessor resultProcessor, 043 ComponentSource componentSource) 044 { 045 this.decoder = decoder; 046 this.resultProcessor = resultProcessor; 047 this.componentSource = componentSource; 048 } 049 050 @Override 051 public void handle(ComponentEventRequestParameters parameters, ComponentEventRequestHandler handler) 052 throws IOException 053 { 054 FileUploadException uploadException = decoder.getUploadException(); 055 056 if (uploadException != null) 057 { 058 Component page = componentSource.getPage(parameters.getActivePageName()); 059 060 ComponentResultProcessorWrapper callback = new ComponentResultProcessorWrapper(resultProcessor); 061 062 page.getComponentResources().triggerEvent(UploadEvents.UPLOAD_EXCEPTION, new Object[] { uploadException }, 063 callback); 064 065 // If an event handler exists and returns a value, then the callback will be aborted and a response 066 // (typically a redirect) will already have been sent to the client. 067 068 if (callback.isAborted()) return; 069 070 // If the page does not properly handle the exception, then we throw it now. 071 072 throw new RuntimeException(UploadMessages.unableToDecode(), uploadException); 073 } 074 075 076 handler.handle(parameters); 077 } 078}