content: package com.fit2cloud.gateway.utils;
  
  import com.fasterxml.jackson.core.JsonProcessingException;
  import com.fasterxml.jackson.databind.ObjectMapper;
  import org.springframework.core.io.Resource;
  import org.springframework.core.io.buffer.DataBuffer;
  import org.springframework.http.HttpStatus;
  import org.springframework.http.MediaType;
  import org.springframework.http.server.reactive.ServerHttpRequest;
  import org.springframework.http.server.reactive.ServerHttpResponse;
  import reactor.core.publisher.Mono;
  
  import java.io.ByteArrayOutputStream;
  import java.io.IOException;
  import java.io.InputStream;
  import java.nio.charset.StandardCharsets;
  import java.util.Date;
  import java.util.LinkedHashMap;
  import java.util.Map;
  
  public class WriteMessageUtil {
  
      private static final ObjectMapper objectMapper = new ObjectMapper();
  
      public static Mono<Void> writeErrorMessage(ServerHttpRequest request, ServerHttpResponse response, HttpStatus status, String message) {
          response.getHeaders().setContentType(MediaType.APPLICATION_JSON);
          response.setStatusCode(status);
  
          Map<String, Object> errorAttributes = new LinkedHashMap<>();
          errorAttributes.put("timestamp", new Date());
          errorAttributes.put("path", request.getPath().value());
          errorAttributes.put("status", status.value());
          errorAttributes.put("error", status.getReasonPhrase());
          errorAttributes.put("message", message);
  
          String body;
          try {
              body = objectMapper.writeValueAsString(errorAttributes);
          } catch (JsonProcessingException e) {
              throw new RuntimeException(e);
          }
          DataBuffer dataBuffer = response.bufferFactory().wrap(body.getBytes(StandardCharsets.UTF_8));
          return response.writeWith(Mono.just(dataBuffer));
      }
  
      public static Mono<Void> write(ServerHttpResponse response, HttpStatus status, Resource html) throws IOException {
          response.getHeaders().setContentType(MediaType.TEXT_HTML);
          response.setStatusCode(status);
  
          DataBuffer dataBuffer = response.bufferFactory().wrap(toByteArray(html.getInputStream()));
          return response.writeWith(Mono.just(dataBuffer));
      }
  
      public static byte[] toByteArray(InputStream input) throws IOException {
          ByteArrayOutputStream output = new ByteArrayOutputStream();
          byte[] buffer = new byte[1024 * 4];
          int n = 0;
          while (-1 != (n = input.read(buffer))) {
              output.write(buffer, 0, n);
          }
          return output.toByteArray();
      }
  
  
  }
  