2009/05/20 - Apache Shale has been retired.

For more information, please explore the Attic.

View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to you under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.shale.renderer;
19  
20  import java.io.IOException;
21  import java.util.Map;
22  
23  import javax.faces.component.UIComponent;
24  import javax.faces.context.FacesContext;
25  import javax.faces.context.ResponseWriter;
26  import javax.faces.render.Renderer;
27  
28  import org.apache.shale.component.Token;
29  
30  /***
31   * <p>Renderer for a {@link Token} component, dealing with a transaction
32   * token used to catch duplicate form submits.</p>
33   *
34   * $Id: TokenRenderer.java 464373 2006-10-16 04:21:54Z rahul $
35   */
36  public class TokenRenderer extends Renderer {
37  
38  
39      // -------------------------------------------------------- Renderer Methods
40  
41  
42      /***
43       * <p>Save the submitted value if this component was actually rendered.</p>
44       *
45       * @param context <code>FacesContext</code> for the current request
46       * @param component <code>UIComponent</code> to be decoded
47       */
48      public void decode(FacesContext context, UIComponent component) {
49  
50          if ((context == null) || (component == null)) {
51              throw new NullPointerException();
52          }
53  
54          Token token = (Token) component;
55          if (token.isRendered()) {
56              String clientId = token.getClientId(context);
57              Map map = context.getExternalContext().getRequestParameterMap();
58              token.setSubmittedValue(map.get(clientId));
59          }
60  
61      }
62  
63  
64      /***
65       * <p>Render the start of a hidden input element containing the transaction
66       * token value for this component.</p>
67       *
68       * @param context <code>FacesContext</code> for the current request
69       * @param component <code>UIComponent</code> to be decoded
70       *
71       * @exception IOException if an input/output error occurs
72       */
73      public void encodeBegin(FacesContext context, UIComponent component)
74        throws IOException {
75  
76          if ((context == null) || (component == null)) {
77              throw new NullPointerException();
78          }
79  
80          Token token = (Token) component;
81          if (token.isRendered()) {
82              ResponseWriter writer = context.getResponseWriter();
83              writer.startElement("input", token);
84              String clientId = token.getClientId(context);
85              writer.writeAttribute("id", clientId, "id");
86              writer.writeAttribute("name", clientId, "id");
87              writer.writeAttribute("type", "hidden", null);
88              writer.writeAttribute("value", token.getToken(), null);
89          }
90  
91      }
92  
93  
94      /***
95       * <p>Render the end of a hidden input element containing the transaction
96       * token value for this component.</p>
97       *
98       * @param context <code>FacesContext</code> for the current request
99       * @param component <code>UIComponent</code> to be decoded
100      *
101      * @exception IOException if an input/output error occurs
102      */
103     public void encodeEnd(FacesContext context, UIComponent component)
104       throws IOException {
105 
106         if ((context == null) || (component == null)) {
107             throw new NullPointerException();
108         }
109 
110         Token token = (Token) component;
111         if (token.isRendered()) {
112             ResponseWriter writer = context.getResponseWriter();
113             writer.endElement("input");
114         }
115 
116     }
117 
118 
119 }