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  package org.apache.myfaces.custom.form;
20  
21  import java.io.IOException;
22  
23  import javax.faces.component.UIComponent;
24  import javax.faces.component.UIForm;
25  import javax.faces.context.FacesContext;
26  import javax.servlet.http.HttpServletRequest;
27  
28  import org.apache.myfaces.custom.clientvalidation.common.CVUtils;
29  import org.apache.myfaces.shared_tomahawk.renderkit.html.HtmlFormRendererBase;
30  
31  /**
32   * 
33   * @JSFRenderer
34   *   renderKitId = "HTML_BASIC" 
35   *   family = "javax.faces.Form"
36   *   type = "org.apache.myfaces.Form"
37   *
38   * @author Mathias Broekelmann (latest modification by $Author$)
39   * @version $Revision$ $Date$
40   *
41   */
42  public class HtmlFormRenderer extends HtmlFormRendererBase
43  {
44      private static String CLIENT_VALIDATON_SCRIPT = "return tomahawk.executeClientLifeCycle();";
45      
46      /**
47       * @see org.apache.myfaces.shared_tomahawk.renderkit.html.HtmlFormRendererBase#getActionUrl(javax.faces.context.FacesContext, javax.faces.component.UIForm )
48       */
49      protected String getActionUrl(FacesContext facesContext, UIForm form)
50      {
51          String actionUrl = getActionUrl(facesContext);
52          if (form instanceof HtmlForm)
53          {
54              HtmlForm htmlForm = (HtmlForm) form;
55  
56              if(htmlForm.getAction()!=null)
57              {
58                  return htmlForm.getAction();
59              }
60              else
61              {
62                  String scheme = htmlForm.getScheme();
63                  String serverName = htmlForm.getServerName();
64                  Integer portObj = htmlForm.getPort();
65                  if (!(scheme == null && serverName == null && portObj == null))
66                  {
67                      Object request = facesContext.getExternalContext().getRequest();
68  
69                      //todo: fix this to work in PortletRequest as well
70                      if (request instanceof HttpServletRequest)
71                      {
72                          HttpServletRequest httpRequest = (HttpServletRequest) request;
73                          // build absolute url
74                          int serverPort = 0;
75                          if (portObj == null)
76                          {
77                              serverPort = httpRequest.getServerPort();
78                          }
79                          else
80                          {
81                              serverPort = portObj.intValue();
82                          }
83                          if (scheme == null)
84                          {
85                              scheme = httpRequest.getScheme();
86                          }
87                          else if (portObj == null)
88                          {
89                              serverPort = 0;
90                          }
91  
92                          if (serverName == null)
93                          {
94                              serverName = httpRequest.getServerName();
95                          }
96                          StringBuffer sb = new StringBuffer();
97                          sb.append(scheme);
98                          sb.append("://");
99                          sb.append(serverName);
100 
101                         if (serverPort != 0)
102                         {
103                             if (("http".equals(scheme) && serverPort != 80)
104                                             || ("https".equals(scheme) && serverPort != 443))
105                             {
106                                 sb.append(":");
107                                 sb.append(serverPort);
108                             }
109                         }
110 
111                         sb.append(actionUrl);
112                         actionUrl = sb.toString();
113                     }
114                 }
115             }
116         }
117         return actionUrl;
118     }
119     /**
120      * @see org.apache.myfaces.shared_tomahawk.renderkit.html.HtmlFormRendererBase#getMethod(javax.faces.context.FacesContext, javax.faces.component.UIForm )
121      */
122     protected String getMethod(FacesContext facesContext, UIForm form)
123     {
124         if(form instanceof HtmlForm)
125         {
126             String method = ((HtmlForm) form).getMethod();
127 
128             if(method != null)
129                 return method;
130         }
131 
132         return "post";
133     }
134     
135     public void encodeBegin(FacesContext facesContext, UIComponent component) throws IOException {
136         if(CVUtils.isCVEnabled()) {
137             if(!isDecorated(facesContext, component))
138             decorateOnSubmit(facesContext, component);
139         }
140         super.encodeBegin(facesContext, component);
141     }
142     
143     public void encodeEnd(FacesContext facesContext, UIComponent component) throws IOException {
144         super.encodeEnd(facesContext, component);
145         if(CVUtils.isCVEnabled()) {
146             CVUtils.encodeJavascript(facesContext);
147             CVUtils.queueCVCalls(facesContext.getViewRoot());
148             CVUtils.encodeValidationScript(facesContext);
149         }
150     }
151     
152     private boolean isDecorated(FacesContext facesContext, UIComponent child) {
153         String onSubmit= (String) child.getAttributes().get("onsubmit");
154         
155         if(onSubmit == null || onSubmit.indexOf(CLIENT_VALIDATON_SCRIPT) == -1)
156             return false;
157         else
158             return true;
159     }
160     
161     private void decorateOnSubmit(FacesContext facesContext, UIComponent child) {
162         String onSubmitEvent = (String) child.getAttributes().get("onsubmit");
163         if(onSubmitEvent == null)
164             child.getAttributes().put("onsubmit", CLIENT_VALIDATON_SCRIPT);
165         else
166             child.getAttributes().put("onsubmit", onSubmitEvent + ";" + CLIENT_VALIDATON_SCRIPT);
167     }
168 
169 }