1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
34
35
36
37
38
39
40
41
42 public class HtmlFormRenderer extends HtmlFormRendererBase
43 {
44 private static String CLIENT_VALIDATON_SCRIPT = "return tomahawk.executeClientLifeCycle();";
45
46
47
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
70 if (request instanceof HttpServletRequest)
71 {
72 HttpServletRequest httpRequest = (HttpServletRequest) request;
73
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
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 }