<!-- SPEED TEST --> <pe:speedtest id="speedtest" widgetVar="speedtest" bgColor="#65ed28" captionDownload="Down" captionUpload="Up" captionJitter="Jitter" captionPing="Ping" file="#{request.contextPath}/resources/other/dummy.payload"> <p:ajax listener="#{speedTestController.saveResults}" process="@this" update="growl" /> </pe:speedtest> <div class="ui-g"> <p:commandButton id="btnStart" value="Start Speedtest" icon="pi pi-play" style="width:100%;" onclick="PF('speedtest').start();" /> </div>
@Named @ViewScoped public class SpeedTestController implements Serializable { private static final long serialVersionUID = -5297177555931103300L; private static final Integer ONEMB = 1024 * 1024; // Size of Download-File (Default=5) private Integer sizeInMB = 5; public static void saveResults(final SpeedTestEvent speedTestEvent) { final String msg = "PingTimeMS ='" + String.valueOf(speedTestEvent.getPingTimeMS()) + "', \n" + "Jitter ='" + String.valueOf(speedTestEvent.getJitterTimeMS()) + "', \n" + "SpeedMbpsDownload='" + String.valueOf(speedTestEvent.getSpeedMbpsDownload()) + "', \n" + "SpeedMbpsUpload='" + String.valueOf(speedTestEvent.getSpeedMbpsUpload()) + "'"; final FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, "Saved your speed results:", msg); FacesContext.getCurrentInstance().addMessage(null, message); } private String generatePayloadFile() { // Generate random test string final Integer size2Generate = ONEMB * sizeInMB; return RandomStringUtils.randomAlphanumeric(size2Generate); } public void downloadPayloadFile() { final String payloadString = generatePayloadFile(); try { final FacesContext facesContext = FacesContext.getCurrentInstance(); final HttpServletResponse response = (HttpServletResponse) facesContext.getExternalContext().getResponse(); response.reset(); response.setHeader("Content-Type", "application/octet-stream"); response.setHeader("Content-Disposition", "attachment;filename=dummy.payload"); response.getWriter().write(payloadString); response.getWriter().flush(); facesContext.responseComplete(); } catch (final Exception e) { // ignore } } public void updateSizeInMB() { // Dummy for updating the size on the server... } public Integer getSizeInMB() { return sizeInMB; } public void setSizeInMB(final Integer sizeInMB) { this.sizeInMB = sizeInMB; } }