import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;
public class MetadataStripper {
public static void main(String[] args) {
try {
// Read from stdin
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String line;
StringBuilder sb = new StringBuilder();
while ((line = reader.readLine()) != null) {
sb.append(line).append("\n");
}
String formData = sb.toString();
// Parse form data (assuming key=value pairs)
Map<String, String> formDataMap = parseFormData(formData);
// Strip metadata
Map<String, String> strippedFormData = stripMetadata(formDataMap);
// Output stripped form data
for (Map.Entry<String, String> entry : strippedFormData.entrySet()) {
System.out.println(entry.getKey() + "=" + entry.getValue());
}
} catch (IOException e) {
System.err.println("Error reading input: " + e.getMessage());
}
}
private static Map<String, String> parseFormData(String formData) {
Map<String, String> formData = new HashMap<>();
if (formData == null || formData.isEmpty()) return formData;
for (String line : formData.split("\n")) {
if (line.contains("=")) {
String[] parts = line.split("=", 2); // Split only at the first '='
if (parts.length == 2) {
String key = parts[0].trim();
String value = parts[1].trim();
formData.put(key, value);
}
}
}
return formData;
}
private static Map<String, String> stripMetadata(Map<String, String> formData) {
Map<String, String> strippedFormData = new HashMap<>();
for (Map.Entry<String, String> entry : formData.entrySet()) {
String key = entry.getKey();
String value = entry.getValue();
//Remove metadata prefix "Content-Type:" or "Content-Length:"
if (key.startsWith("Content-Type:") || key.startsWith("Content-Length:")) {
continue; // Skip metadata fields
}
// Remove any leading/trailing whitespace from the value
strippedFormData.put(key, value.trim());
}
return strippedFormData;
}
}
Add your comment