-
Notifications
You must be signed in to change notification settings - Fork 120
URL supports placeholders and can be dynamically replaced with msg ex… #535
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -48,13 +48,17 @@ | |
| import java.util.HashMap; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.Set; | ||
| import java.util.concurrent.CountDownLatch; | ||
| import java.util.concurrent.Executors; | ||
| import java.util.concurrent.ScheduledExecutorService; | ||
| import java.util.concurrent.TimeUnit; | ||
| import java.util.regex.Pattern; | ||
|
|
||
| public class HttpSinkTask extends SinkTask { | ||
| private static final Logger log = LoggerFactory.getLogger(HttpSinkTask.class); | ||
| // the Regex Pattern like '{number}' | ||
| protected static final Pattern PATTERN = Pattern.compile("\\{(\\w+)\\}"); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. PATTERN is declared protected but is only used inside this class, and the name is generic enough to collide with subclass fields. Prefer |
||
| protected static final int DEFAULT_CONSUMER_TIMEOUT_SECONDS = 30; | ||
| protected static final String DEFAULT_REQUEST_TIMEOUT_MILL_SECONDS = "3000"; | ||
| protected static final int DEFAULT_OAUTH_DELAY_SECONDS = 1; | ||
|
|
@@ -105,8 +109,10 @@ public void put(List<ConnectRecord> records) throws ConnectException { | |
| if (auth != null) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No unit tests are included in this diff. The PR checklist claims tests were written, but the diff contains no test file changes. Tests should cover: URL with single/multiple placeholders, missing extensions, empty extension values, values with special characters, and URLs with no placeholders. |
||
| headerMap.putAll(auth.auth()); | ||
| } | ||
| // replace the placeholder of url with extensions | ||
| String urlInsteadOfPlaceholder = formatUrl(url, connectRecord.getExtensions()); | ||
| // render query to url | ||
| String urlWithQueryParameters = renderQueryParametersToUrl(url, queryParameters, fixedQueryParameters); | ||
| String urlWithQueryParameters = renderQueryParametersToUrl(urlInsteadOfPlaceholder, queryParameters, fixedQueryParameters); | ||
| HttpRequest httpRequest = new HttpRequest(); | ||
| httpRequest.setUrl(urlWithQueryParameters); | ||
| httpRequest.setMethod(method); | ||
|
|
@@ -140,6 +146,32 @@ public void put(List<ConnectRecord> records) throws ConnectException { | |
| } | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The new |
||
| } | ||
|
|
||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. formatUrl uses |
||
| /** | ||
| * Get a formatted url that will replace a placeholder with Extension Values | ||
| * | ||
| * @param url the source url str | ||
| * @param extensions ConnectRecord Extension Values | ||
| * @return the formatted url | ||
| */ | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [Critical] Fix: Change to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The PR checklist claims >80% unit-test coverage, but no tests were added in this PR. formatUrl has several distinct branches worth covering: whole-URL placeholder, embedded placeholders, missing extension key, null/empty value, and values with special characters. Consider extracting the logic to a package-private static helper (or testing through put() with the HTTP call mocked) so it can be unit-tested. |
||
| private String formatUrl(String url, KeyValue extensions) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. PATTERN.matcher(url).matches() only returns true when the ENTIRE url is a single |
||
| if (!PATTERN.matcher(url).matches()) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Extension values are inserted into the URL via raw string replacement without URL encoding. Values containing reserved characters ( There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| return url; | ||
| } | ||
| if (extensions != null && extensions.keySet() != null) { | ||
| Set<String> keys = extensions.keySet(); | ||
| String template = url; | ||
| for (String key : keys) { | ||
| String value = extensions.getString(key); | ||
| if (StringUtils.isNotEmpty(value)) { | ||
| // simple replaced the placeholder | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Extension values are untrusted record data inserted into the URL with no encoding. A value containing a space, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Extension values are substituted into the URL with a raw There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If an extension key is missing or its value is empty, the corresponding |
||
| template = template.replace("{" + key + "}", value); | ||
| } | ||
| } | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If a placeholder has no matching extension key, or the value is null/empty (skipped at line 164), the raw |
||
| return template; | ||
| } | ||
| return url; | ||
| } | ||
|
|
||
| private Map<String, String> renderHeaderMap(String headerParameters, String fixedHeaderParameters, String token) { | ||
| Map<String, String> headerMap = new HashMap<>(); | ||
| if (headerParameters != null) { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The comment says the pattern matches
{number}, but the regex\{(\w+)\}actually matches any word-character sequence (letters, digits, underscore), not just digits. The comment is misleading — update it to say e.g.{placeholder}or{word}.