@Test(dataProvider="testPowerData") public void testPower(String op, boolean throwExc) { MetaStbCache cache = new MetaStbCache(); MetaStb stb = new MetaStb(); stb.setMacAddress("00:00:41:B7:CD:52"); cache.putMetaStb(stb); final MockPowerClient client = new MockPowerClient(stb); PowerController controller = new PowerController(); DawgShowConfiguration config = new DawgShowConfiguration(); Wiring.autowire(controller, config); Assert.assertNotNull(controller.getPowerClient(stb, null)); controller = new PowerController() { protected PowerClient getPowerClient(MetaStb stb, Cookie cookie) { return client; } }; PowerException exc = null; Wiring.autowire(controller, cache); JwtDeviceAccessValidator validator = new JwtDeviceAccessValidator(null, null, false); Wiring.autowire(controller, validator); Assert.assertNotNull(controller.getPowerClient(stb, null)); try { controller.power(new String[] {"000041B7CD52"}, op, null, null, null); } catch (PowerException e) { exc = e; } Assert.assertEquals(exc != null, throwExc); if (!throwExc) { Assert.assertNotNull(client.lastOp); Assert.assertEquals(client.lastOp, PowerOperation.valueOf(op)); } }
@RequestMapping(method = { RequestMethod.POST }, value = "/power") @ResponseBody public void power(@RequestParam(value="deviceIds[]") String[] deviceIds, @RequestParam String command, HttpServletRequest req, HttpServletResponse resp, @CookieValue(name=DawgCookieUtils.COOKIE_NAME, required=false) String dawt) throws PowerException { accessValidator.validateUserHasAccessToDevices(req, resp, false, deviceIds); Cookie cookie = dawt == null ? null : cookieUtils.createApacheCookie(dawt, -1, req); PowerException exc = null; StringBuilder failedIds = new StringBuilder(); for (String deviceId : deviceIds) { MetaStb stb = metaStbCache.getMetaStb(deviceId); try { PowerClient client = getPowerClient(stb, cookie); try { PowerOperation op = PowerOperation.valueOf(command); client.performPowerOperation(op); } catch (IllegalArgumentException iae) { throw new PowerException("Invalid power command '" + command + "'", iae); } } catch (PowerException e) { failedIds.append((!failedIds.toString().isEmpty() ? ", " : "") + stb.getId()); exc = e; } } if (exc != null) { throw new PowerException("Failed to send power command '" + command + "' to " + failedIds.toString(), exc); } }
PowerController implements ViewConstants { @RequestMapping(method = { RequestMethod.POST }, value = "/power") @ResponseBody public void power(@RequestParam(value="deviceIds[]") String[] deviceIds, @RequestParam String command, HttpServletRequest req, HttpServletResponse resp, @CookieValue(name=DawgCookieUtils.COOKIE_NAME, required=false) String dawt) throws PowerException { accessValidator.validateUserHasAccessToDevices(req, resp, false, deviceIds); Cookie cookie = dawt == null ? null : cookieUtils.createApacheCookie(dawt, -1, req); PowerException exc = null; StringBuilder failedIds = new StringBuilder(); for (String deviceId : deviceIds) { MetaStb stb = metaStbCache.getMetaStb(deviceId); try { PowerClient client = getPowerClient(stb, cookie); try { PowerOperation op = PowerOperation.valueOf(command); client.performPowerOperation(op); } catch (IllegalArgumentException iae) { throw new PowerException("Invalid power command '" + command + "'", iae); } } catch (PowerException e) { failedIds.append((!failedIds.toString().isEmpty() ? ", " : "") + stb.getId()); exc = e; } } if (exc != null) { throw new PowerException("Failed to send power command '" + command + "' to " + failedIds.toString(), exc); } } }
PowerController implements ViewConstants { @RequestMapping(method = { RequestMethod.POST }, value = "/power") @ResponseBody public void power(@RequestParam(value="deviceIds[]") String[] deviceIds, @RequestParam String command, HttpServletRequest req, HttpServletResponse resp, @CookieValue(name=DawgCookieUtils.COOKIE_NAME, required=false) String dawt) throws PowerException { accessValidator.validateUserHasAccessToDevices(req, resp, false, deviceIds); Cookie cookie = dawt == null ? null : cookieUtils.createApacheCookie(dawt, -1, req); PowerException exc = null; StringBuilder failedIds = new StringBuilder(); for (String deviceId : deviceIds) { MetaStb stb = metaStbCache.getMetaStb(deviceId); try { PowerClient client = getPowerClient(stb, cookie); try { PowerOperation op = PowerOperation.valueOf(command); client.performPowerOperation(op); } catch (IllegalArgumentException iae) { throw new PowerException("Invalid power command '" + command + "'", iae); } } catch (PowerException e) { failedIds.append((!failedIds.toString().isEmpty() ? ", " : "") + stb.getId()); exc = e; } } if (exc != null) { throw new PowerException("Failed to send power command '" + command + "' to " + failedIds.toString(), exc); } }  }
PowerController implements ViewConstants { @RequestMapping(method = { RequestMethod.POST }, value = "/power") @ResponseBody public void power(@RequestParam(value="deviceIds[]") String[] deviceIds, @RequestParam String command, HttpServletRequest req, HttpServletResponse resp, @CookieValue(name=DawgCookieUtils.COOKIE_NAME, required=false) String dawt) throws PowerException { accessValidator.validateUserHasAccessToDevices(req, resp, false, deviceIds); Cookie cookie = dawt == null ? null : cookieUtils.createApacheCookie(dawt, -1, req); PowerException exc = null; StringBuilder failedIds = new StringBuilder(); for (String deviceId : deviceIds) { MetaStb stb = metaStbCache.getMetaStb(deviceId); try { PowerClient client = getPowerClient(stb, cookie); try { PowerOperation op = PowerOperation.valueOf(command); client.performPowerOperation(op); } catch (IllegalArgumentException iae) { throw new PowerException("Invalid power command '" + command + "'", iae); } } catch (PowerException e) { failedIds.append((!failedIds.toString().isEmpty() ? ", " : "") + stb.getId()); exc = e; } } if (exc != null) { throw new PowerException("Failed to send power command '" + command + "' to " + failedIds.toString(), exc); } }  @RequestMapping(method = { RequestMethod.POST }, value = "/power") @ResponseBody void power(@RequestParam(value="deviceIds[]") String[] deviceIds, @RequestParam String command,
            HttpServletRequest req, HttpServletResponse resp, @CookieValue(name=DawgCookieUtils.COOKIE_NAME, required=false) String dawt); }
PowerController implements ViewConstants { @RequestMapping(method = { RequestMethod.POST }, value = "/power") @ResponseBody public void power(@RequestParam(value="deviceIds[]") String[] deviceIds, @RequestParam String command, HttpServletRequest req, HttpServletResponse resp, @CookieValue(name=DawgCookieUtils.COOKIE_NAME, required=false) String dawt) throws PowerException { accessValidator.validateUserHasAccessToDevices(req, resp, false, deviceIds); Cookie cookie = dawt == null ? null : cookieUtils.createApacheCookie(dawt, -1, req); PowerException exc = null; StringBuilder failedIds = new StringBuilder(); for (String deviceId : deviceIds) { MetaStb stb = metaStbCache.getMetaStb(deviceId); try { PowerClient client = getPowerClient(stb, cookie); try { PowerOperation op = PowerOperation.valueOf(command); client.performPowerOperation(op); } catch (IllegalArgumentException iae) { throw new PowerException("Invalid power command '" + command + "'", iae); } } catch (PowerException e) { failedIds.append((!failedIds.toString().isEmpty() ? ", " : "") + stb.getId()); exc = e; } } if (exc != null) { throw new PowerException("Failed to send power command '" + command + "' to " + failedIds.toString(), exc); } }  @RequestMapping(method = { RequestMethod.POST }, value = "/power") @ResponseBody void power(@RequestParam(value="deviceIds[]") String[] deviceIds, @RequestParam String command,
            HttpServletRequest req, HttpServletResponse resp, @CookieValue(name=DawgCookieUtils.COOKIE_NAME, required=false) String dawt);  }