public String downloadPresentation(Long stationId, Date timestamp) { logger.debug("Downloading presentation..."); final Station station = stationDao.getStationById(stationId); if (station == null || station.getIpAddress() == null) { logger.error("Invalid station."); return null; } File presentationDir = getPresentationDir(); String sStationId = String.valueOf(stationId); String sTimestamp = String.valueOf(timestamp.getTime()); final String outputFileName = ExmUtil.concat(sStationId, separator, sTimestamp, presentationExt); File outputFile = new File(presentationDir, outputFileName); final Integer port = serverPropertyDao.getIntegerProperty(ConfigManager.MASTER_DUPLICATION_PORT); final String path = serverPropertyDao.getStringProperty(ConfigManager.MASTER_DUPLICATION_PRESENTATION_PATH); final String login = serverPropertyDao.getStringProperty(ConfigManager.MASTER_DUPLICATION_LOGIN); final String password = serverPropertyDao.getStringProperty(ConfigManager.MASTER_DUPLICATION_PASSWORD); final Integer timeout = serverPropertyDao.getIntegerProperty(ConfigManager.MASTER_DUPLICATION_PRESENTATION_TIMEOUT); InputStream input = null; OutputStream output = null; try { final URL url = new URL("http", station.getIpAddress(), port, path); final String loginPassword = ExmUtil.concat(login, ":", password); final String loginPasswordEnc = new String(Base64.encodeBase64(loginPassword.getBytes())); URLConnection connection = url.openConnection(); connection.setRequestProperty("Authorization", "Basic " + loginPasswordEnc); connection.setConnectTimeout(timeout); // connection.setReadTimeout(timeout); presentationDir.mkdirs(); input = connection.getInputStream(); output = new FileOutputStream(outputFile); IOUtils.copy(input, output); output.close(); input.close(); } catch (MalformedURLException e) { logger.error("Cannot connect to the station.", e); } catch (IOException e) { logger.error("Cannot connect to the station.", e); } finally { IOUtils.closeQuietly(input); IOUtils.closeQuietly(output); } return outputFileName; }
public void sendDownloadRequestsToRoom(Long sourceStationId, String presentationId) { final Integer port = serverPropertyDao.getIntegerProperty(ConfigManager.MASTER_DUPLICATION_PORT); final String presentationPath = serverPropertyDao.getStringProperty( ConfigManager.MASTER_DUPLICATION_PRESENTATION_REQUEST_PATH); final String propertiesPath = serverPropertyDao.getStringProperty( ConfigManager.MASTER_DUPLICATION_PROPERTIES_REQUEST_PATH); final String login = serverPropertyDao.getStringProperty(ConfigManager.MASTER_DUPLICATION_LOGIN); final String password = serverPropertyDao.getStringProperty(ConfigManager.MASTER_DUPLICATION_PASSWORD); final Integer presentationTimeout = serverPropertyDao.getIntegerProperty( ConfigManager.MASTER_DUPLICATION_PRESENTATION_REQUEST_TIMEOUT); final Integer propertiesTimeout = serverPropertyDao.getIntegerProperty( ConfigManager.MASTER_DUPLICATION_PROPERTIES_REQUEST_TIMEOUT); Set<Station> stations = stationDao.getStationsFromRoom(sourceStationId); URLConnection connection = null; InputStream input = null; Station sourceStation = stationDao.getStationById(sourceStationId); if (sourceStation == null) { logger.error("Invalid station: " + sourceStationId); return; } for (Station station : stations) { if (sourceStationId.equals(station.getId())) { // If it is a source station - skip. continue; } try { String fullPath = ExmUtil.concat( presentationPath, "?presentationId=", URLEncoder.encode(presentationId, "UTF-8")); URL url = new URL("http", station.getIpAddress(), port, fullPath); final String loginPassword = ExmUtil.concat(login, ":", password); final String loginPasswordEnc = new String(Base64.encodeBase64(loginPassword.getBytes())); if (logger.isDebugEnabled()) { logger.debug("Connecting to station: " + station.getIpAddress()); } connection = url.openConnection(); connection.setRequestProperty("Authorization", "Basic " + loginPasswordEnc); connection.setConnectTimeout(presentationTimeout); connection.setReadTimeout(presentationTimeout); input = connection.getInputStream(); input.close(); input = null; String sourceAddress = ExmUtil.concat(sourceStation.getIpAddress(), ":", String.valueOf(port)); fullPath = ExmUtil.concat( propertiesPath, "?sourceAddress=", URLEncoder.encode(sourceAddress, "UTF-8")); url = new URL("http", station.getIpAddress(), port, fullPath); if (logger.isDebugEnabled()) { logger.debug("Connecting to station: " + station.getIpAddress()); } connection = url.openConnection(); connection.setRequestProperty("Authorization", "Basic " + loginPasswordEnc); connection.setConnectTimeout(propertiesTimeout); connection.setReadTimeout(propertiesTimeout); input = connection.getInputStream(); input.close(); input = null; } catch (MalformedURLException e) { logger.warn("Cannot connect to the station: " + station.getIpAddress(), e); } catch (IOException e) { logger.warn("Cannot connect to the station: " + station.getIpAddress(), e); } finally { IOUtils.closeQuietly(input); } } }