Source:
<code>public SourceState poll(Emitter<StructuredRecord> writer, SourceState currentState) throws Exception { URL url = new URL(config.getUrl()); String response; URLConnection rawConnection = url.openConnection(); try { HttpURLConnection connection = (HttpURLConnection) rawConnection; try { if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) { response = Bytes.toString(ByteStreams.toByteArray(connection.getInputStream())); } else if (connection.getResponseCode() == HttpURLConnection.HTTP_BAD_REQUEST) { response = Bytes.toString(ByteStreams.toByteArray(connection.getErrorStream())); } else { throw new Exception("Invalid response code returned: " + connection.getResponseCode()); } } finally { connection.disconnect(); } } catch (ClassCastException e) { <START> if (e.getMessage().contains("FileURLConnection cannot be cast to java.net.HttpURLConnection")) { <END> response = Bytes.toString(ByteStreams.toByteArray(rawConnection.getInputStream())); } else { throw e; } } Map<String, List<String>> headers = rawConnection.getHeaderFields(); Map<String, String> flattenedHeaders = new HashMap<>(); for (Map.Entry<String, List<String>> entry : headers.entrySet()) { if (!Strings.isNullOrEmpty(entry.getKey())) { flattenedHeaders.put(entry.getKey(), Joiner.on(',').skipNulls().join(entry.getValue())); } } writeDefaultRecords(writer, response, flattenedHeaders); TimeUnit.SECONDS.sleep(config.getIntervalInSeconds()); return currentState; }</code><technical_language>this work (it cleaner): if (rawConnection instanceof FileURLConnection) { ... do check beforehand, instead of catch ClassCastException</technical_language>

Target:
public SourceState poll(Emitter<StructuredRecord> writer, SourceState currentState) throws Exception { byte[] lastPollTimeBytes = currentState.getState(POLL_TIME_STATE_KEY); if (lastPollTimeBytes != null) { long lastPollTime = Bytes.toLong(lastPollTimeBytes); long currentPollTime = System.currentTimeMillis(); long diffInSeconds = (currentPollTime - lastPollTime) / 1000; if (config.getIntervalInSeconds() - diffInSeconds > 0) { TimeUnit.SECONDS.sleep(1L); return currentState; } } try { URL url = new URL(config.getUrl()); String response = ""; HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod(METHOD); connection.setConnectTimeout(config.getConnectTimeout()); connection.setReadTimeout(config.getReadTimeout()); connection.setInstanceFollowRedirects(config.shouldFollowRedirects()); if (config.hasCustomRequestHeaders()) { for (Map.Entry<String, String> requestHeader : config.getRequestHeadersMap().entrySet()) { connection.setRequestProperty(requestHeader.getKey(), requestHeader.getValue()); } } int responseCode = connection.getResponseCode(); try { if (connection.getErrorStream() != null) { try (Reader reader = new InputStreamReader(connection.getErrorStream(), config.getCharset())) { response = CharStreams.toString(reader); } } else if (connection.getInputStream() != null) { try (Reader reader = new InputStreamReader(connection.getInputStream(), config.getCharset())) { response = CharStreams.toString(reader); } } } finally { connection.disconnect(); } Map<String, List<String>> headers = connection.getHeaderFields(); Map<String, String> flattenedHeaders = new HashMap<>(); for (Map.Entry<String, List<String>> entry : headers.entrySet()) { if (!Strings.isNullOrEmpty(entry.getKey())) { flattenedHeaders.put(entry.getKey(), Joiner.on(',').skipNulls().join(entry.getValue())); } } writer.emit(createStructuredRecord(response, flattenedHeaders, responseCode)); } finally { currentState.setState(POLL_TIME_STATE_KEY, Bytes.toBytes(System.currentTimeMillis())); } return currentState; }