1. import java.util.HashMap;
  2. import java.util.Map;
  3. public class HeaderValidator {
  4. /**
  5. * Validates request headers for data migration.
  6. *
  7. * @param headers A map of request headers.
  8. * @return true if all conditions are met, false otherwise.
  9. */
  10. public static boolean validateHeaders(Map<String, String> headers) {
  11. // Check if 'Content-Type' header is present and is 'application/json'
  12. if (!headers.containsKey("Content-Type") || !headers.get("Content-Type").equalsIgnoreCase("application/json")) {
  13. System.err.println("Invalid Content-Type: Expected application/json");
  14. return false;
  15. }
  16. // Check if 'Authorization' header is present
  17. if (!headers.containsKey("Authorization")) {
  18. System.err.println("Missing Authorization header");
  19. return false;
  20. }
  21. // Check if 'Migration-Type' header is present and is 'full' or 'incremental'
  22. if (!headers.containsKey("Migration-Type") ||
  23. !"full".equalsIgnoreCase(headers.get("Migration-Type")) &&
  24. !"incremental".equalsIgnoreCase(headers.get("Migration-Type"))) {
  25. System.err.println("Invalid Migration-Type: Expected full or incremental");
  26. return false;
  27. }
  28. // Check if 'Source-System' header is present
  29. if (!headers.containsKey("Source-System")) {
  30. System.err.println("Missing Source-System header");
  31. return false;
  32. }
  33. // Check if 'Destination-System' header is present
  34. if (!headers.containsKey("Destination-System")) {
  35. System.err.println("Missing Destination-System header");
  36. return false;
  37. }
  38. return true; // All conditions met
  39. }
  40. public static void main(String[] args) {
  41. // Example Usage
  42. Map<String, String> validHeaders = new HashMap<>();
  43. validHeaders.put("Content-Type", "application/json");
  44. validHeaders.put("Authorization", "Bearer <token>");
  45. validHeaders.put("Migration-Type", "full");
  46. validHeaders.put("Source-System", "SystemA");
  47. validHeaders.put("Destination-System", "SystemB");
  48. Map<String, String> invalidHeaders = new HashMap<>();
  49. invalidHeaders.put("Content-Type", "text/plain");
  50. invalidHeaders.put("Authorization", "Bearer <token>");
  51. invalidHeaders.put("Migration-Type", "partial");
  52. System.out.println("Valid Headers: " + validateHeaders(validHeaders));
  53. System.out.println("Invalid Headers: " + validateHeaders(invalidHeaders));
  54. }
  55. }

Add your comment