1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package org.primefaces.extensions.component.speedtest;
23
24 import java.util.Arrays;
25 import java.util.Collection;
26 import java.util.Collections;
27 import java.util.Map;
28
29 import javax.faces.FacesException;
30 import javax.faces.application.ResourceDependency;
31 import javax.faces.component.UIComponentBase;
32 import javax.faces.component.behavior.ClientBehaviorHolder;
33 import javax.faces.context.FacesContext;
34 import javax.faces.event.AjaxBehaviorEvent;
35 import javax.faces.event.FacesEvent;
36
37 import org.primefaces.component.api.Widget;
38 import org.primefaces.extensions.event.SpeedTestEvent;
39 import org.primefaces.util.Constants;
40
41
42
43
44
45
46
47 @ResourceDependency(library = "primefaces", name = "jquery/jquery.js")
48 @ResourceDependency(library = "primefaces", name = "jquery/jquery-plugins.js")
49 @ResourceDependency(library = "primefaces", name = "core.js")
50 @ResourceDependency(library = "primefaces", name = "raphael/raphael.js")
51 @ResourceDependency(library = "primefaces-extensions", name = "primefaces-extensions.js")
52 @ResourceDependency(library = "primefaces-extensions", name = "speedtest/speedtest.js")
53 public class Speedtest extends UIComponentBase implements ClientBehaviorHolder, Widget {
54
55 public static final String COMPONENT_FAMILY = "org.primefaces.extensions.component";
56 public static final String COMPONENT_TYPE = COMPONENT_FAMILY + ".Speedtest";
57 private static final String DEFAULT_RENDERER = COMPONENT_FAMILY + ".SpeedtestRenderer";
58 private static final Collection<String> EVENT_NAMES = Collections.unmodifiableCollection(Arrays.asList(SpeedTestEvent.NAME));
59
60 @SuppressWarnings("java:S115")
61 protected enum PropertyKeys {
62
63 widgetVar,
64 captionPing,
65 captionJitter,
66 captionDownload,
67 captionUpload,
68 colorPing,
69 colorJitter,
70 colorDownload,
71 colorUpload,
72 file,
73 style,
74 styleClass
75
76 }
77
78
79
80
81 public Speedtest() {
82 setRendererType(DEFAULT_RENDERER);
83 }
84
85
86
87
88 @Override
89 public String getFamily() {
90 return COMPONENT_FAMILY;
91 }
92
93
94
95
96 @Override
97 public Collection<String> getEventNames() {
98 return EVENT_NAMES;
99 }
100
101
102
103
104 @Override
105 public String getDefaultEventName() {
106 return SpeedTestEvent.NAME;
107 }
108
109 private static Double convertParam(final String clientId, final String paramName, final Map<String, String> params) {
110 Double res = 0.0;
111 try {
112 res = Double.valueOf(params.get(clientId + paramName));
113 }
114 catch (final Exception e) {
115 throw new FacesException("Speedtest: can not convert result value for '" + paramName + "'");
116 }
117 return res;
118 }
119
120
121
122
123 @Override
124 public void queueEvent(final FacesEvent event) {
125 final FacesContext fc = FacesContext.getCurrentInstance();
126
127 if (isSelfRequest(fc) && event instanceof AjaxBehaviorEvent) {
128 final Map<String, String> params = fc.getExternalContext().getRequestParameterMap();
129 final String eventName = params.get(Constants.RequestParams.PARTIAL_BEHAVIOR_EVENT_PARAM);
130 final String clientId = getClientId(fc);
131 final AjaxBehaviorEvent behaviorEvent = (AjaxBehaviorEvent) event;
132 if (SpeedTestEvent.NAME.equals(eventName)) {
133
134 final Double pingTimeMS = convertParam(clientId, "_PingTimeMS", params);
135 final Double jitterTimeMS = convertParam(clientId, "_JitterTimeMS", params);
136 final Double speedMbpsDownload = convertParam(clientId, "_SpeedMbpsDownload", params);
137 final Double speedMbpsUpload = convertParam(clientId, "_SpeedMbpsUpload", params);
138 final SpeedTestEvent speedtestEvent = new SpeedTestEvent(this, behaviorEvent.getBehavior(),
139 pingTimeMS, jitterTimeMS, speedMbpsDownload, speedMbpsUpload);
140 speedtestEvent.setPhaseId(event.getPhaseId());
141 super.queueEvent(speedtestEvent);
142 return;
143 }
144 }
145 super.queueEvent(event);
146 }
147
148 private boolean isSelfRequest(final FacesContext context) {
149 return getClientId(context)
150 .equals(context.getExternalContext().getRequestParameterMap().get(
151 Constants.RequestParams.PARTIAL_SOURCE_PARAM));
152 }
153
154 public String getWidgetVar() {
155 return (String) getStateHelper().eval(PropertyKeys.widgetVar, null);
156 }
157
158 public void setWidgetVar(final String _widgetVar) {
159 getStateHelper().put(PropertyKeys.widgetVar, _widgetVar);
160 }
161
162 public String getCaptionPing() {
163 return (String) getStateHelper().eval(PropertyKeys.captionPing, "Ping");
164 }
165
166 public void setCaptionPing(final String _captionPing) {
167 getStateHelper().put(PropertyKeys.captionPing, _captionPing);
168 }
169
170 public String getCaptionJitter() {
171 return (String) getStateHelper().eval(PropertyKeys.captionJitter, "Jitter");
172 }
173
174 public void setCaptionJitter(final String _captionJitter) {
175 getStateHelper().put(PropertyKeys.captionJitter, _captionJitter);
176 }
177
178 public String getCaptionDownload() {
179 return (String) getStateHelper().eval(PropertyKeys.captionDownload, "Download");
180 }
181
182 public void setCaptionDownload(final String _captionDownload) {
183 getStateHelper().put(PropertyKeys.captionDownload, _captionDownload);
184 }
185
186 public String getCaptionUpload() {
187 return (String) getStateHelper().eval(PropertyKeys.captionUpload, "Upload");
188 }
189
190 public void setCaptionUpload(final String _captionUpload) {
191 getStateHelper().put(PropertyKeys.captionUpload, _captionUpload);
192 }
193
194 public String getColorPing() {
195 return (String) getStateHelper().eval(PropertyKeys.colorPing, "#993333");
196 }
197
198 public void setColorPing(final String _colorPing) {
199 getStateHelper().put(PropertyKeys.colorPing, _colorPing);
200 }
201
202 public String getColorJitter() {
203 return (String) getStateHelper().eval(PropertyKeys.colorJitter, "#d2900a");
204 }
205
206 public void setColorJitter(final String _colorJitter) {
207 getStateHelper().put(PropertyKeys.colorJitter, _colorJitter);
208 }
209
210 public String getColorDownload() {
211 return (String) getStateHelper().eval(PropertyKeys.colorDownload, "#339933");
212 }
213
214 public void setColorDownload(final String _colorDownload) {
215 getStateHelper().put(PropertyKeys.colorDownload, _colorDownload);
216 }
217
218 public String getColorUpload() {
219 return (String) getStateHelper().eval(PropertyKeys.colorUpload, "#333399");
220 }
221
222 public void setColorUpload(final String _colorUpload) {
223 getStateHelper().put(PropertyKeys.colorUpload, _colorUpload);
224 }
225
226 public String getStyle() {
227 return (String) getStateHelper().eval(PropertyKeys.style, null);
228 }
229
230 public void setStyle(final String _style) {
231 getStateHelper().put(PropertyKeys.style, _style);
232 }
233
234 public String getStyleClass() {
235 return (String) getStateHelper().eval(PropertyKeys.styleClass, null);
236 }
237
238 public void setStyleClass(final String _styleClass) {
239 getStateHelper().put(PropertyKeys.styleClass, _styleClass);
240 }
241
242 public String getFile() {
243 return (String) getStateHelper().eval(PropertyKeys.file, null);
244 }
245
246 public void setFile(final String _File) {
247 getStateHelper().put(PropertyKeys.file, _File);
248 }
249 }