View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  
20  package org.apache.myfaces.tobago.util;
21  
22  
23  import org.apache.myfaces.tobago.component.Attributes;
24  import org.apache.myfaces.tobago.component.Facets;
25  import org.apache.myfaces.tobago.internal.util.FacesContextUtils;
26  import org.slf4j.Logger;
27  import org.slf4j.LoggerFactory;
28  
29  import javax.faces.component.UIComponent;
30  import javax.faces.context.FacesContext;
31  import javax.faces.event.PhaseId;
32  import javax.portlet.PortletResponse;
33  import javax.servlet.http.HttpServletResponse;
34  
35  public class ApplyRequestValuesCallback implements TobagoCallback {
36  
37    @SuppressWarnings("UnusedDeclaration")
38    private static final Logger LOG = LoggerFactory.getLogger(ApplyRequestValuesCallback.class);
39  
40    @Override
41    public void invokeContextCallback(final FacesContext facesContext, final UIComponent component) {
42      if (FacesContextUtils.isAjax(facesContext)) {
43        final String ajaxId = FacesContextUtils.getAjaxComponentId(facesContext);
44        final String sourceId = facesContext.getExternalContext().getRequestParameterMap().get("javax.faces.source");
45        final UIComponent reload = ComponentUtils.getFacet(component, Facets.reload);
46        if (ajaxId != null && ajaxId.equals(component.getClientId(facesContext)) && reload != null && reload.isRendered()
47            && ajaxId.equals(sourceId)) {
48          final boolean immediate = ComponentUtils.getBooleanAttribute(reload, Attributes.immediate);
49          if (immediate) {
50            final boolean update = ComponentUtils.getBooleanAttribute(reload, Attributes.update);
51            if (!update) {
52              final Object response = facesContext.getExternalContext().getResponse();
53              if (response instanceof HttpServletResponse) {
54                ((HttpServletResponse) response).setStatus(HttpServletResponse.SC_NOT_MODIFIED);
55              } else if (response instanceof PortletResponse) {
56                // TBD: Portlet: what is to do here?
57              }
58              facesContext.responseComplete();
59              return;
60            }
61          }
62        }
63      }
64      component.processDecodes(facesContext);
65    }
66  
67    @Override
68    public PhaseId getPhaseId() {
69      return PhaseId.APPLY_REQUEST_VALUES;
70    }
71  }