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

gov.nasa.pds.api.registry.view.CsvSingularSerializer 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.OutputStream;
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 com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.databind.ObjectMapper;

import gov.nasa.pds.model.WyriwygProduct;

public class CsvSingularSerializer extends AbstractHttpMessageConverter {
  public CsvSingularSerializer() {
    super(new MediaType("application", "csv"), new MediaType("text", "csv"));
  }

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

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

  @Override
  protected void writeInternal(WyriwygProduct t, HttpOutputMessage outputMessage)
      throws IOException, HttpMessageNotWritableException {
    ObjectMapper mapper = new ObjectMapper();
    mapper.setSerializationInclusion(Include.NON_NULL);
    outputMessage.getHeaders().setContentType(MediaType.TEXT_PLAIN); // must be before body is fetched
    OutputStream os = outputMessage.getBody();
    OutputStreamWriter wr = new OutputStreamWriter(os);
    WyriwygSerializer.writeCSV(t, wr, mapper);
    wr.close();
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy