1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. import java.util.HashMap;
  5. import java.util.Map;
  6. public class MetadataStripper {
  7. public static void main(String[] args) {
  8. try {
  9. // Read from stdin
  10. BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
  11. String line;
  12. StringBuilder sb = new StringBuilder();
  13. while ((line = reader.readLine()) != null) {
  14. sb.append(line).append("\n");
  15. }
  16. String formData = sb.toString();
  17. // Parse form data (assuming key=value pairs)
  18. Map<String, String> formDataMap = parseFormData(formData);
  19. // Strip metadata
  20. Map<String, String> strippedFormData = stripMetadata(formDataMap);
  21. // Output stripped form data
  22. for (Map.Entry<String, String> entry : strippedFormData.entrySet()) {
  23. System.out.println(entry.getKey() + "=" + entry.getValue());
  24. }
  25. } catch (IOException e) {
  26. System.err.println("Error reading input: " + e.getMessage());
  27. }
  28. }
  29. private static Map<String, String> parseFormData(String formData) {
  30. Map<String, String> formData = new HashMap<>();
  31. if (formData == null || formData.isEmpty()) return formData;
  32. for (String line : formData.split("\n")) {
  33. if (line.contains("=")) {
  34. String[] parts = line.split("=", 2); // Split only at the first '='
  35. if (parts.length == 2) {
  36. String key = parts[0].trim();
  37. String value = parts[1].trim();
  38. formData.put(key, value);
  39. }
  40. }
  41. }
  42. return formData;
  43. }
  44. private static Map<String, String> stripMetadata(Map<String, String> formData) {
  45. Map<String, String> strippedFormData = new HashMap<>();
  46. for (Map.Entry<String, String> entry : formData.entrySet()) {
  47. String key = entry.getKey();
  48. String value = entry.getValue();
  49. //Remove metadata prefix "Content-Type:" or "Content-Length:"
  50. if (key.startsWith("Content-Type:") || key.startsWith("Content-Length:")) {
  51. continue; // Skip metadata fields
  52. }
  53. // Remove any leading/trailing whitespace from the value
  54. strippedFormData.put(key, value.trim());
  55. }
  56. return strippedFormData;
  57. }
  58. }

Add your comment