1+ /*
2+ * Copyright 2011 <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
3+ *
4+ * Licensed under the Apache License, Version 2.0 (the "License");
5+ * you may not use this file except in compliance with the License.
6+ * You may obtain a copy of the License at
7+ *
8+ * http://www.apache.org/licenses/LICENSE-2.0
9+ *
10+ * Unless required by applicable law or agreed to in writing, software
11+ * distributed under the License is distributed on an "AS IS" BASIS,
12+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+ * See the License for the specific language governing permissions and
14+ * limitations under the License.
15+ */
16+ /*
17+ * Copyright 2011 <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
18+ *
19+ * Licensed under the Apache License, Version 2.0 (the "License");
20+ * you may not use this file except in compliance with the License.
21+ * You may obtain a copy of the License at
22+ *
23+ * http://www.apache.org/licenses/LICENSE-2.0
24+ *
25+ * Unless required by applicable law or agreed to in writing, software
26+ * distributed under the License is distributed on an "AS IS" BASIS,
27+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
28+ * See the License for the specific language governing permissions and
29+ * limitations under the License.
30+ */
31+ package org .ocpsoft .rewrite .faces .annotation .handler ;
32+
33+ import javax .faces .context .FacesContext ;
34+
35+ import org .ocpsoft .rewrite .annotation .api .ClassContext ;
36+ import org .ocpsoft .rewrite .annotation .api .HandlerChain ;
37+ import org .ocpsoft .rewrite .annotation .api .MethodContext ;
38+ import org .ocpsoft .rewrite .annotation .handler .HandlerWeights ;
39+ import org .ocpsoft .rewrite .annotation .spi .AnnotationHandler ;
40+ import org .ocpsoft .rewrite .config .Operation ;
41+ import org .ocpsoft .rewrite .context .EvaluationContext ;
42+ import org .ocpsoft .rewrite .event .Rewrite ;
43+ import org .ocpsoft .rewrite .faces .annotation .IgnorePostback ;
44+
45+ public class IgnorePostbackHandler implements AnnotationHandler <IgnorePostback >
46+ {
47+
48+ @ Override
49+ public Class <IgnorePostback > handles ()
50+ {
51+ return IgnorePostback .class ;
52+ }
53+
54+ @ Override
55+ public int priority ()
56+ {
57+ // must execute after @Deferred
58+ return HandlerWeights .WEIGHT_TYPE_ENRICHING + 10 ;
59+ }
60+
61+ @ Override
62+ public void process (ClassContext context , IgnorePostback annotation , HandlerChain chain )
63+ {
64+
65+ chain .proceed ();
66+
67+ if (context instanceof MethodContext ) {
68+ Operation operation = (Operation ) context .get (Operation .class );
69+ if (operation != null ) {
70+ // replace the operation with a wrapped one that ignores postbacks
71+ context .put (Operation .class , new IgnorePostbackOperation (operation ));
72+ }
73+ }
74+
75+ }
76+
77+ private static class IgnorePostbackOperation implements Operation
78+ {
79+
80+ private final Operation delegate ;
81+
82+ public IgnorePostbackOperation (Operation delegate )
83+ {
84+ this .delegate = delegate ;
85+ }
86+
87+ @ Override
88+ public void perform (Rewrite event , EvaluationContext context )
89+ {
90+
91+ FacesContext facesContext = FacesContext .getCurrentInstance ();
92+ if (facesContext == null ) {
93+ throw new IllegalStateException ("FacesContext.getCurrentInstance() returned null" );
94+ }
95+
96+ boolean postback = facesContext .getRenderKit ().getResponseStateManager ().isPostback (facesContext );
97+ if (!postback ) {
98+ delegate .perform (event , context );
99+ }
100+
101+ }
102+
103+ }
104+ }
0 commit comments