001    package org.apache.myfaces.tobago.util;
002    
003    /*
004     * Licensed to the Apache Software Foundation (ASF) under one or more
005     * contributor license agreements.  See the NOTICE file distributed with
006     * this work for additional information regarding copyright ownership.
007     * The ASF licenses this file to You under the Apache License, Version 2.0
008     * (the "License"); you may not use this file except in compliance with
009     * the License.  You may obtain a copy of the License at
010     *
011     *      http://www.apache.org/licenses/LICENSE-2.0
012     *
013     * Unless required by applicable law or agreed to in writing, software
014     * distributed under the License is distributed on an "AS IS" BASIS,
015     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016     * See the License for the specific language governing permissions and
017     * limitations under the License.
018     */
019    
020    
021    import org.slf4j.Logger;
022    import org.slf4j.LoggerFactory;
023    import org.apache.myfaces.tobago.component.Attributes;
024    import org.apache.myfaces.tobago.component.Facets;
025    import org.apache.myfaces.tobago.context.TobagoFacesContext;
026    
027    import javax.faces.component.UIComponent;
028    import javax.faces.context.FacesContext;
029    import javax.faces.event.PhaseId;
030    import javax.servlet.http.HttpServletResponse;
031    
032    public class ApplyRequestValuesCallback implements TobagoCallback {
033    
034      @SuppressWarnings("UnusedDeclaration")
035      private static final Logger LOG = LoggerFactory.getLogger(ApplyRequestValuesCallback.class);
036    
037      public void invokeContextCallback(FacesContext context, UIComponent component) {
038        if (context instanceof TobagoFacesContext && ((TobagoFacesContext) context).isAjax()) {
039          final String ajaxId = ((TobagoFacesContext) context).getAjaxComponentId();
040          UIComponent reload = component.getFacet(Facets.RELOAD);
041          if (ajaxId != null && ajaxId.equals(component.getClientId(context)) && reload != null && reload.isRendered()
042              && ajaxId.equals(ComponentUtils.findPage(context, component).getActionId())) {
043            Boolean immediate = (Boolean) reload.getAttributes().get(Attributes.IMMEDIATE);
044            if (immediate != null && immediate) {
045              Boolean update = (Boolean) reload.getAttributes().get(Attributes.UPDATE);
046              if (update != null && !update) {
047                if (context.getExternalContext().getResponse() instanceof HttpServletResponse) {
048                  ((HttpServletResponse) context.getExternalContext().getResponse())
049                      .setStatus(HttpServletResponse.SC_NOT_MODIFIED);
050                }
051                context.responseComplete();
052                return;
053              }
054            }
055          }
056        }
057        component.processDecodes(context);
058      }
059    
060      public PhaseId getPhaseId() {
061        return PhaseId.APPLY_REQUEST_VALUES;
062      }
063    }