1. import java.io.File;
  2. import java.util.HashMap;
  3. import java.util.Map;
  4. import java.util.Scanner;
  5. public class ResponseHeaderValidator {
  6. /**
  7. * Validates response headers against a configuration file.
  8. * @param responseHeaders A map of response headers.
  9. * @param configFilePath The path to the configuration file.
  10. * @return true if all headers are valid, false otherwise.
  11. */
  12. public static boolean validateHeaders(Map<String, String> responseHeaders, String configFilePath) {
  13. try {
  14. // Read the configuration file.
  15. File configFile = new File(configFilePath);
  16. Scanner scanner = new Scanner(configFile);
  17. // Parse the configuration file.
  18. Map<String, Map<String, String>> config = parseConfig(scanner);
  19. scanner.close();
  20. // Validate each header against the configuration.
  21. for (Map.Entry<String, String> entry : responseHeaders.entrySet()) {
  22. String headerName = entry.getKey();
  23. String headerValue = entry.getValue();
  24. if (config.containsKey(headerName)) {
  25. String expectedValue = config.get(headerName).get("value");
  26. if (!headerValue.equals(expectedValue)) {
  27. System.out.println("Header " + headerName + " is invalid. Expected: " + expectedValue + ", Actual: " + headerValue);
  28. return false; // Header validation failed.
  29. }
  30. } else {
  31. System.out.println("Header " + headerName + " is missing in configuration.");
  32. return false; // Header not found in config.
  33. }
  34. }
  35. return true; // All headers are valid.
  36. } catch (Exception e) {
  37. System.err.println("Error validating headers: " + e.getMessage());
  38. return false; // Error during validation.
  39. }
  40. }
  41. /**
  42. * Parses the configuration file.
  43. * @param scanner The scanner for reading the configuration file.
  44. * @return A map representing the configuration.
  45. */
  46. private static Map<String, Map<String, String>> parseConfig(Scanner scanner) throws Exception {
  47. Map<String, Map<String, String>> config = new HashMap<>();
  48. String line;
  49. while ((line = scanner.nextLine()) != null) {
  50. String[] parts = line.split(":", 2); // Split into header and value.
  51. if (parts.length == 2) {
  52. String headerName = parts[0].trim();
  53. String value = parts[1].trim();
  54. config.put(headerName, new HashMap<>());
  55. config.get(headerName).put("value", value);
  56. }
  57. }
  58. return config;
  59. }
  60. public static void main(String[] args) {
  61. //Example Usage
  62. Map<String, String> responseHeaders = new HashMap<>();
  63. responseHeaders.put("Content-Type", "application/json");
  64. responseHeaders.put("X-Custom-Header", "example_value");
  65. String configFilePath = "config.txt"; // Replace with your config file path
  66. boolean isValid = validateHeaders(responseHeaders, configFilePath);
  67. if (isValid) {
  68. System.out.println("Response headers are valid.");
  69. } else {
  70. System.out.println("Response headers are invalid.");
  71. }
  72. }
  73. }

Add your comment