001    // Copyright 2010 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    
015    package org.apache.tapestry5.internal.services.ajax;
016    
017    import java.io.IOException;
018    
019    import org.apache.tapestry5.internal.services.RequestConstants;
020    import org.apache.tapestry5.ioc.internal.util.InternalUtils;
021    import org.apache.tapestry5.services.Ajax;
022    import org.apache.tapestry5.services.ComponentEventRequestFilter;
023    import org.apache.tapestry5.services.ComponentEventRequestHandler;
024    import org.apache.tapestry5.services.ComponentEventRequestParameters;
025    import org.apache.tapestry5.services.Request;
026    
027    /**
028     * Filter for the {@link Ajax} {@link ComponentEventRequestHandler} that informs the {@link AjaxFormUpdateController}
029     * about the form's client id and component id. Partial renders work with the
030     * AjaxFormUpdateController to ensure that the Form data, if any, is collected and rendered
031     * as part of the response.
032     * 
033     * @since 5.2.0
034     */
035    public class AjaxFormUpdateFilter implements ComponentEventRequestFilter
036    {
037        private final Request request;
038    
039        private final AjaxFormUpdateController ajaxFormUpdateController;
040    
041        public AjaxFormUpdateFilter(Request request, AjaxFormUpdateController ajaxFormUpdateController)
042        {
043            this.request = request;
044            this.ajaxFormUpdateController = ajaxFormUpdateController;
045        }
046    
047        public void handle(ComponentEventRequestParameters parameters, ComponentEventRequestHandler handler)
048                throws IOException
049        {
050            String formClientId = request.getParameter(RequestConstants.FORM_CLIENTID_PARAMETER);
051            String formComponentId = request.getParameter(RequestConstants.FORM_COMPONENTID_PARAMETER);
052    
053            if (InternalUtils.isNonBlank(formClientId) && InternalUtils.isNonBlank(formComponentId))
054                ajaxFormUpdateController.initializeForForm(formComponentId, formClientId);
055    
056            handler.handle(parameters);
057        }
058    }