-
Notifications
You must be signed in to change notification settings - Fork 115
feat: add http headers parameter to cli and local app #2162
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
Open
davidgamez
wants to merge
3
commits into
master
Choose a base branch
from
custom_user_agent
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,6 +15,7 @@ | |
| */ | ||
| package org.mobilitydata.gtfsvalidator.app.gui; | ||
|
|
||
| import com.google.common.collect.ImmutableMap; | ||
| import com.google.common.flogger.FluentLogger; | ||
| import java.awt.Color; | ||
| import java.awt.Component; | ||
|
|
@@ -45,7 +46,9 @@ | |
| import javax.swing.JFrame; | ||
| import javax.swing.JLabel; | ||
| import javax.swing.JPanel; | ||
| import javax.swing.JScrollPane; | ||
| import javax.swing.JSpinner; | ||
| import javax.swing.JTextArea; | ||
| import javax.swing.JTextField; | ||
| import javax.swing.event.DocumentEvent; | ||
| import javax.swing.event.DocumentListener; | ||
|
|
@@ -78,6 +81,8 @@ public class GtfsValidatorApp extends JFrame { | |
|
|
||
| private final JSpinner numThreadsSpinner = new JSpinner(); | ||
| private final JTextField countryCodeField = new JTextField("", 3); | ||
| private final JTextArea httpHeadersField = new JTextArea(4, TEXT_FIELD_COLUMN_WIDTH); | ||
| private final JLabel httpHeadersErrorLabel = new JLabel(); | ||
|
|
||
| private final MonitoredValidationRunner validationRunner; | ||
| private final ValidationDisplay validationDisplay; | ||
|
|
@@ -129,6 +134,17 @@ public String getCountryCode() { | |
| return countryCodeField.getText(); | ||
| } | ||
|
|
||
| public void setHttpHeaders(String httpHeaders) { | ||
| httpHeadersField.setText(httpHeaders); | ||
| } | ||
|
|
||
| /** | ||
| * Returns the raw text from the HTTP headers field (newline-separated {@code Name: Value} lines). | ||
| */ | ||
| public String getHttpHeaders() { | ||
| return httpHeadersField.getText(); | ||
| } | ||
|
|
||
| void addPreValidationCallback(Runnable callback) { | ||
| preValidationCallbacks.add(callback); | ||
| } | ||
|
|
@@ -291,6 +307,34 @@ private void constructAdvancedOptionsPanel(JPanel parent) { | |
| fieldConstraints.gridy = 1; | ||
| panel.add(countryCodeField, fieldConstraints); | ||
|
|
||
| // HTTP Headers — spans both columns so the text area gets full width | ||
| GridBagConstraints fullRowConstraints = new GridBagConstraints(); | ||
| fullRowConstraints.gridx = 0; | ||
| fullRowConstraints.gridwidth = 2; | ||
| fullRowConstraints.anchor = GridBagConstraints.NORTHWEST; | ||
| fullRowConstraints.fill = GridBagConstraints.HORIZONTAL; | ||
| fullRowConstraints.weightx = 1.0; | ||
|
|
||
| fullRowConstraints.gridy = 2; | ||
| panel.add(new JLabel(bundle.getString("http_headers")), fullRowConstraints); | ||
|
|
||
| httpHeadersField.setLineWrap(false); | ||
| JScrollPane headersScrollPane = new JScrollPane(httpHeadersField); | ||
| fullRowConstraints.gridy = 3; | ||
| panel.add(headersScrollPane, fullRowConstraints); | ||
|
|
||
| httpHeadersErrorLabel.setForeground(Color.RED); | ||
| httpHeadersErrorLabel.setVisible(false); | ||
| fullRowConstraints.gridy = 4; | ||
| panel.add(httpHeadersErrorLabel, fullRowConstraints); | ||
|
|
||
| fullRowConstraints.gridy = 5; | ||
| panel.add(new JLabel(bundle.getString("http_headers_description")), fullRowConstraints); | ||
|
|
||
| httpHeadersField | ||
| .getDocument() | ||
| .addDocumentListener(documentChangeListener(this::updateValidationButtonStatus)); | ||
|
|
||
| advancedOptionsPanel.setVisible(false); | ||
| } | ||
|
|
||
|
|
@@ -342,6 +386,13 @@ private void constructValidateButton(JPanel panel) { | |
| } | ||
|
|
||
| private void updateValidationButtonStatus() { | ||
| String headerError = validateHttpHeadersText(httpHeadersField.getText()); | ||
| if (headerError != null) { | ||
| httpHeadersErrorLabel.setText(headerError); | ||
| httpHeadersErrorLabel.setVisible(true); | ||
| } else { | ||
| httpHeadersErrorLabel.setVisible(false); | ||
| } | ||
| validateButton.setEnabled(isValidationReadyToRun()); | ||
| } | ||
|
|
||
|
|
@@ -352,6 +403,9 @@ private boolean isValidationReadyToRun() { | |
| if (outputDirectoryField.getText().isBlank()) { | ||
| return false; | ||
| } | ||
| if (validateHttpHeadersText(httpHeadersField.getText()) != null) { | ||
| return false; | ||
| } | ||
| return true; | ||
| } | ||
|
|
||
|
|
@@ -373,7 +427,7 @@ private ValidationRunnerConfig buildConfig() throws URISyntaxException { | |
| config.setPrettyJson(true); | ||
| config.setStdoutOutput(false); | ||
|
|
||
| String gtfsInput = gtfsInputField.getText(); | ||
| String gtfsInput = gtfsInputField.getText().strip(); | ||
| if (gtfsInput.isBlank()) { | ||
| throw new IllegalStateException("gtfsInputField is blank"); | ||
| } | ||
|
|
@@ -383,7 +437,7 @@ private ValidationRunnerConfig buildConfig() throws URISyntaxException { | |
| config.setGtfsSource(Path.of(gtfsInput).toUri()); | ||
| } | ||
|
|
||
| String outputDirectory = outputDirectoryField.getText(); | ||
| String outputDirectory = outputDirectoryField.getText().strip(); | ||
|
Member
Author
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. Not quite related to the PR's main purpose. However, this fixes a critical error when the output directory contains extra spaces. |
||
| if (outputDirectory.isBlank()) { | ||
| throw new IllegalStateException("outputDirectoryField is blank"); | ||
| } | ||
|
|
@@ -399,9 +453,50 @@ private ValidationRunnerConfig buildConfig() throws URISyntaxException { | |
| config.setCountryCode(CountryCode.forStringOrUnknown(countryCode)); | ||
| } | ||
|
|
||
| config.setHttpHeaders(parseHttpHeaders(httpHeadersField.getText())); | ||
|
|
||
| return config.build(); | ||
| } | ||
|
|
||
| /** | ||
| * Validates newline-separated {@code Name: Value} header lines. | ||
| * | ||
| * @return {@code null} if all lines are valid, or a human-readable error message for the first | ||
| * invalid line. | ||
| */ | ||
| static String validateHttpHeadersText(String text) { | ||
| for (String line : text.split("\n")) { | ||
| if (line.isBlank()) { | ||
| continue; | ||
| } | ||
| int colon = line.indexOf(':'); | ||
| if (colon <= 0) { | ||
| return "Invalid header (expected \u201cName: Value\u201d): " + line.trim(); | ||
| } | ||
| } | ||
| return null; | ||
| } | ||
|
|
||
| /** | ||
| * Parses newline-separated {@code Name: Value} header lines into an {@link ImmutableMap}. Blank | ||
| * lines are skipped. Colons inside the value are preserved. | ||
| * | ||
| * <p>Callers must ensure the text is valid via {@link #validateHttpHeadersText} first. | ||
| */ | ||
| static ImmutableMap<String, String> parseHttpHeaders(String text) { | ||
| // Use LinkedHashMap so duplicate header names keep the last value (last-wins semantics) | ||
| // rather than throwing, which is a more forgiving user experience. | ||
| java.util.LinkedHashMap<String, String> map = new java.util.LinkedHashMap<>(); | ||
| for (String line : text.split("\n")) { | ||
| if (line.isBlank()) { | ||
| continue; | ||
| } | ||
| int colon = line.indexOf(':'); | ||
| map.put(line.substring(0, colon).trim(), line.substring(colon + 1).trim()); | ||
|
davidgamez marked this conversation as resolved.
|
||
| } | ||
| return ImmutableMap.copyOf(map); | ||
| } | ||
|
|
||
| private static Font createBoldFont() { | ||
| JLabel label = new JLabel(); | ||
| Font baseFont = label.getFont(); | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Not quite related to the PR's main purpose. However, this fixes a critical error when the URL contains extra spaces.