All Downloads are FREE. Search and download functionalities are using the official Maven repository.

gov.nasa.pds.api.registry.view.JsonErrorMessageSerializer Maven / Gradle / Ivy

There is a newer version: 4.5.6
Show newest version
package gov.nasa.pds.api.registry.view;

import java.io.IOException;
import java.io.OutputStreamWriter;
import org.springframework.http.HttpInputMessage;
import org.springframework.http.HttpOutputMessage;
import org.springframework.http.MediaType;
import org.springframework.http.converter.AbstractHttpMessageConverter;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.http.converter.HttpMessageNotWritableException;
import gov.nasa.pds.model.ErrorMessage;

public class JsonErrorMessageSerializer extends AbstractHttpMessageConverter {
  public JsonErrorMessageSerializer() {
    super(MediaType.APPLICATION_JSON, new MediaType("application", "kvp+json"),
        new MediaType("application", "vnd.nasa.pds.pds4+json"), MediaType.ALL, new MediaType("*"));
  }

  @Override
  protected boolean supports(Class clazz) {
    return ErrorMessage.class.isAssignableFrom(clazz);
  }

  @Override
  protected ErrorMessage readInternal(Class clazz,
      HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException {
    return new ErrorMessage();
  }

  @Override
  protected void writeInternal(ErrorMessage t, HttpOutputMessage outputMessage)
      throws IOException, HttpMessageNotWritableException {
    outputMessage.getHeaders().setContentType(MediaType.APPLICATION_JSON); // must be before body is fetched
    OutputStreamWriter osw = new OutputStreamWriter(outputMessage.getBody(), "UTF-8");
    try {
      osw.write("{\"request\":\"");
      osw.write(t.getRequest());
      osw.write("\",\"message\":\"");
      osw.write(t.getMessage());
      osw.write("\"}");
    } finally {
      osw.close();
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy