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.beanmodel.services.*;
020import org.apache.tapestry5.internal.services.ComponentResultProcessorWrapper;
021import org.apache.tapestry5.ioc.annotations.Primary;
022import org.apache.tapestry5.runtime.Component;
023import org.apache.tapestry5.services.ComponentEventRequestFilter;
024import org.apache.tapestry5.services.ComponentEventRequestHandler;
025import org.apache.tapestry5.services.ComponentEventRequestParameters;
026import org.apache.tapestry5.services.ComponentEventResultProcessor;
027import org.apache.tapestry5.services.ComponentSource;
028import org.apache.tapestry5.services.Traditional;
029import org.apache.tapestry5.upload.services.MultipartDecoder;
030import org.apache.tapestry5.upload.services.UploadEvents;
031
032import java.io.IOException;
033
034/**
035 * Determines if there was an {@link org.apache.commons.fileupload.FileUploadException} processing the request and, if
036 * so, triggers an exception event on the page. If the page fails to respond to the event, then
037 */
038@Events(UploadEvents.UPLOAD_EXCEPTION + " when a exception occur processing the upload")
039public class UploadExceptionFilter implements ComponentEventRequestFilter
040{
041    private final MultipartDecoder decoder;
042
043    private final ComponentEventResultProcessor resultProcessor;
044
045    private ComponentSource componentSource;
046
047    public UploadExceptionFilter(MultipartDecoder decoder,
048                                 @Traditional @Primary ComponentEventResultProcessor resultProcessor,
049                                 ComponentSource componentSource)
050    {
051        this.decoder = decoder;
052        this.resultProcessor = resultProcessor;
053        this.componentSource = componentSource;
054    }
055
056    @Override
057    public void handle(ComponentEventRequestParameters parameters, ComponentEventRequestHandler handler)
058            throws IOException
059    {
060        FileUploadException uploadException = decoder.getUploadException();
061
062        if (uploadException != null)
063        {
064            Component page = componentSource.getPage(parameters.getActivePageName());
065
066            ComponentResultProcessorWrapper callback = new ComponentResultProcessorWrapper(resultProcessor);
067
068            page.getComponentResources().triggerEvent(UploadEvents.UPLOAD_EXCEPTION, new Object[] { uploadException },
069                                                      callback);
070
071            // If an event handler exists and returns a value, then the callback will be aborted and a response
072            // (typically a redirect) will already have been sent to the client.
073
074            if (callback.isAborted()) return;
075
076            // If the page does not properly handle the exception, then we throw it now.
077
078            throw new RuntimeException(UploadMessages.unableToDecode(), uploadException);
079        }
080
081
082        handler.handle(parameters);
083    }
084}