import java.util.HashMap;
import java.util.Map;
public class HeaderValidator {
/**
* Validates request headers for data migration.
*
* @param headers A map of request headers.
* @return true if all conditions are met, false otherwise.
*/
public static boolean validateHeaders(Map<String, String> headers) {
// Check if 'Content-Type' header is present and is 'application/json'
if (!headers.containsKey("Content-Type") || !headers.get("Content-Type").equalsIgnoreCase("application/json")) {
System.err.println("Invalid Content-Type: Expected application/json");
return false;
}
// Check if 'Authorization' header is present
if (!headers.containsKey("Authorization")) {
System.err.println("Missing Authorization header");
return false;
}
// Check if 'Migration-Type' header is present and is 'full' or 'incremental'
if (!headers.containsKey("Migration-Type") ||
!"full".equalsIgnoreCase(headers.get("Migration-Type")) &&
!"incremental".equalsIgnoreCase(headers.get("Migration-Type"))) {
System.err.println("Invalid Migration-Type: Expected full or incremental");
return false;
}
// Check if 'Source-System' header is present
if (!headers.containsKey("Source-System")) {
System.err.println("Missing Source-System header");
return false;
}
// Check if 'Destination-System' header is present
if (!headers.containsKey("Destination-System")) {
System.err.println("Missing Destination-System header");
return false;
}
return true; // All conditions met
}
public static void main(String[] args) {
// Example Usage
Map<String, String> validHeaders = new HashMap<>();
validHeaders.put("Content-Type", "application/json");
validHeaders.put("Authorization", "Bearer <token>");
validHeaders.put("Migration-Type", "full");
validHeaders.put("Source-System", "SystemA");
validHeaders.put("Destination-System", "SystemB");
Map<String, String> invalidHeaders = new HashMap<>();
invalidHeaders.put("Content-Type", "text/plain");
invalidHeaders.put("Authorization", "Bearer <token>");
invalidHeaders.put("Migration-Type", "partial");
System.out.println("Valid Headers: " + validateHeaders(validHeaders));
System.out.println("Invalid Headers: " + validateHeaders(invalidHeaders));
}
}
Add your comment