1. import java.io.ByteArrayOutputStream;
  2. import java.io.IOException;
  3. import java.io.ObjectOutputStream;
  4. import java.util.HashMap;
  5. import java.util.Map;
  6. public class HttpRequestSerializer {
  7. /**
  8. * Serializes an HTTP request object for monitoring. Handles edge cases like null requests.
  9. * @param request The HTTP request object to serialize.
  10. * @return A byte array representing the serialized HTTP request, or null if serialization fails.
  11. */
  12. public static byte[] serializeRequest(HttpRequest request) {
  13. if (request == null) {
  14. System.err.println("Warning: Null request provided. Returning null.");
  15. return null; // Handle null request
  16. }
  17. try (ByteArrayOutputStream baos = new ByteArrayOutputStream();
  18. ObjectOutputStream oos = new ObjectOutputStream(baos)) {
  19. oos.writeObject(request);
  20. return baos.toByteArray();
  21. } catch (IOException e) {
  22. System.err.println("Error serializing request: " + e.getMessage());
  23. e.printStackTrace();
  24. return null; // Handle serialization errors
  25. }
  26. }
  27. /**
  28. * Deserializes an HTTP request object from a byte array. Handles edge cases like null arrays.
  29. * @param serializedData The byte array containing the serialized HTTP request.
  30. * @return An HTTP request object, or null if deserialization fails.
  31. */
  32. public static HttpRequest deserializeRequest(byte[] serializedData) {
  33. if (serializedData == null) {
  34. System.err.println("Warning: Null serialized data provided. Returning null.");
  35. return null; // Handle null data
  36. }
  37. try (ByteArrayOutputStream baos = new ByteArrayOutputStream();
  38. ObjectInputStream ois = new ObjectInputStream(baos)) {
  39. ois.readBytes(serializedData); // Reset the byte array
  40. HttpRequest request = (HttpRequest) ois.readObject();
  41. return request;
  42. } catch (IOException | ClassNotFoundException e) {
  43. System.err.println("Error deserializing request: " + e.getMessage());
  44. e.printStackTrace();
  45. return null; // Handle deserialization errors
  46. }
  47. }
  48. //Example HttpRequest class for serialization/deserialization purposes
  49. public static class HttpRequest {
  50. private String method;
  51. private String url;
  52. private String headers;
  53. private String body;
  54. public HttpRequest(String method, String url, String headers, String body) {
  55. this.method = method;
  56. this.url = url;
  57. this.headers = headers;
  58. this.body = body;
  59. }
  60. public String getMethod() {
  61. return method;
  62. }
  63. public String getUrl() {
  64. return url;
  65. }
  66. public String getHeaders() {
  67. return headers;
  68. }
  69. public String getBody() {
  70. return body;
  71. }
  72. }
  73. public static void main(String[] args) {
  74. // Example Usage
  75. HttpRequest request = new HttpRequest("GET", "https://example.com", "Content-Type: application/json", "{\"key\": \"value\"}");
  76. byte[] serialized = serializeRequest(request);
  77. if (serialized != null) {
  78. System.out.println("Request serialized successfully.");
  79. HttpRequest deserialized = deserializeRequest(serialized);
  80. if (deserialized != null) {
  81. System.out.println("Request deserialized successfully.");
  82. System.out.println("Method: " + deserialized.getMethod());
  83. System.out.println("URL: " + deserialized.getUrl());
  84. } else {
  85. System.out.println("Deserialization failed.");
  86. }
  87. } else {
  88. System.out.println("Serialization failed.");
  89. }
  90. //Example with null request
  91. HttpRequest nullRequest = null;
  92. byte[] serializedNull = serializeRequest(nullRequest);
  93. if (serializedNull == null) {
  94. System.out.println("Serialization of null request handled.");
  95. }
  96. }
  97. }

Add your comment