Skip to content

Commit

Permalink
Update API default response in browsers to force application/json (#360)
Browse files Browse the repository at this point in the history
* force the content-type

Part of the problem with the PDS product is that when in a browser it is better viewed using a plugin. To this end, force the return type for text/html of a PDS product or list of products to be application/json. Lovely, but if you are accessig a product directly, it is kind of what you deserve and probably desire.

Found how to set the content-type for the output. It can be overridden but required a rework of the list in WebMVCConfig. Turns out is is very order dependent. The current order seems to have everything working as desired while maybe not correctly.

It also turns out that setting the content-type is also order dependent with requesting the body. Yikes so much hidden order requirements in the bowels of spring. Anyway, marked all of the converters with comments to hopefully keep their order correct.

* Update PdsProductTextHtmlSerializer.java

put pretty print back in

* Update PdsProductTextHtmlSerializer.java

in the right place

* add text/html return as JSon for plural products responses

---------

Co-authored-by: Al Niessner <[email protected]>
Co-authored-by: thomas loubrieu <[email protected]>
  • Loading branch information
3 people authored Aug 9, 2023
1 parent dbe8263 commit b32f015
Show file tree
Hide file tree
Showing 22 changed files with 41 additions and 84 deletions.
9 changes: 0 additions & 9 deletions .github/release.yml

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public static void main(String[] args) throws Exception {
ctx.refresh();

new SpringApplication(SpringBootMain.class).run(args);
ctx.close();
}

class ExitException extends RuntimeException implements ExitCodeGenerator {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
import gov.nasa.pds.api.registry.view.Pds4JsonProductsSerializer;
import gov.nasa.pds.api.registry.view.Pds4XmlProductSerializer;
import gov.nasa.pds.api.registry.view.Pds4XmlProductsSerializer;
import gov.nasa.pds.api.registry.view.PdsProductTextHtmlSerializer;
import gov.nasa.pds.api.registry.view.PdsProductsTextHtmlSerializer;
import gov.nasa.pds.api.registry.view.PdsProductXMLSerializer;
import gov.nasa.pds.api.registry.view.PdsProductsXMLSerializer;
import gov.nasa.pds.api.registry.view.XmlErrorMessageSerializer;
Expand Down Expand Up @@ -67,14 +69,8 @@ public void configureContentNegotiation(ContentNegotiationConfigurer configurer)

@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
WebMVCConfig.log.info("Number of converters available " + Integer.toString(converters.size()));

// basic converter for /api-docs end-point, new with springdoc 2
converters.add(new ByteArrayHttpMessageConverter());

// basic converter for swagger-ui resources
converters.add(new StringHttpMessageConverter());

WebMVCConfig.log.info("Number of converters available at start " + Integer.toString(converters.size()));

// csv converters
converters.add(new CsvErrorMessageSerializer());
converters.add(new CsvPluralSerializer());
Expand All @@ -97,8 +93,22 @@ public void configureMessageConverters(List<HttpMessageConverter<?>> converters)
converters.add(new PdsProductsXMLSerializer());
converters.add(new XmlErrorMessageSerializer());

// text converters for PdsProduct(s)
converters.add(new PdsProductTextHtmlSerializer());
converters.add(new PdsProductsTextHtmlSerializer());

// Introduce basics that are not PDS related but may overload MediaType
// Put them after the PDS so that it always has priority except JSON below because it has */*
// basic converter for /api-docs end-point, new with springdoc 2
converters.add(new ByteArrayHttpMessageConverter());

// basic converter for swagger-ui resources
converters.add(new StringHttpMessageConverter());

// default json converters
converters.add(new JsonProductSerializer());
converters.add(new JsonErrorMessageSerializer());
converters.add(new JsonProductSerializer()); // this one must be last because it contains */*

WebMVCConfig.log.info("Number of converters available after adding locals " + Integer.toString(converters.size()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ private List<String> add_output_needs(List<String> given) throws ApplicationType
private String find_match(String from_user) {
String match = from_user;
StringTokenizer mimes = new StringTokenizer(from_user, ",");

while (mimes.hasMoreTokens()) {
/* separate the mime_type/mime_subtype from ;* stuff */
String mime = mimes.nextToken();
Expand Down Expand Up @@ -296,7 +296,6 @@ public Object getResponse() throws NothingFoundException {
throw new NothingFoundException();
}
return response;

}

public void setResponse(HitIterator hits, int real_total) {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ protected ErrorMessage readInternal(Class<? extends ErrorMessage> clazz,
@Override
protected void writeInternal(ErrorMessage t, HttpOutputMessage outputMessage)
throws IOException, HttpMessageNotWritableException {
outputMessage.getHeaders().setContentType(MediaType.TEXT_PLAIN); // must be before body is fetched
OutputStreamWriter osw = new OutputStreamWriter(outputMessage.getBody(), "UTF-8");
try {
osw.write("request,message\n");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ protected void writeInternal(WyriwygProducts 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);
Utilities.fix(t.getSummary());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
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;
Expand Down Expand Up @@ -37,7 +36,7 @@ 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ protected ErrorMessage readInternal(Class<? extends ErrorMessage> clazz,
@Override
protected void writeInternal(ErrorMessage t, HttpOutputMessage outputMessage)
throws IOException, HttpMessageNotWritableException {
outputMessage.getHeaders().setContentType(MediaType.TEXT_HTML); // must be before body is fetched
OutputStreamWriter osw = new OutputStreamWriter(outputMessage.getBody(), "UTF-8");
try {
osw.write("<html><body><h1>Error Message</h1><h2>From Request</h2><p>");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@
public class JsonErrorMessageSerializer extends AbstractHttpMessageConverter<ErrorMessage> {
public JsonErrorMessageSerializer() {
super(MediaType.APPLICATION_JSON, new MediaType("application", "kvp+json"),
new MediaType("application", "vnd.nasa.pds.pds4+json"), MediaType.ALL, new MediaType("*"),
MediaType.TEXT_HTML);
new MediaType("application", "vnd.nasa.pds.pds4+json"), MediaType.ALL, new MediaType("*"));
}

@Override
Expand All @@ -31,10 +30,8 @@ protected ErrorMessage readInternal(Class<? extends ErrorMessage> clazz,
@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");

// force content type to JSON
// outputMessage.getHeaders().setContentType(MediaType.APPLICATION_JSON);
try {
osw.write("{\"request\":\"");
osw.write(t.getRequest());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,8 @@ protected void writeInternal(WyriwygProducts t, HttpOutputMessage outputMessage)
throws IOException, HttpMessageNotWritableException {
ObjectMapper mapper = new ObjectMapper();
mapper.setSerializationInclusion(Include.NON_NULL);

outputMessage.getHeaders().setContentType(MediaType.APPLICATION_JSON); // must be before body is fetched
OutputStream os = outputMessage.getBody();

OutputStreamWriter wr = new OutputStreamWriter(os);
Utilities.fix(t.getSummary());
WyriwygSerializer.writeJSON(t, wr, mapper);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.springframework.http.ContentDisposition;
import org.springframework.http.HttpOutputMessage;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageNotWritableException;
Expand All @@ -16,7 +17,6 @@ public JsonProductSerializer() {

List<MediaType> supportMediaTypes = new ArrayList<MediaType>();
supportMediaTypes.add(MediaType.APPLICATION_JSON);
supportMediaTypes.add(MediaType.TEXT_HTML);
supportMediaTypes.add(MediaType.ALL);
supportMediaTypes.add(new MediaType("*"));

Expand All @@ -30,8 +30,8 @@ public JsonProductSerializer() {
@Override
protected void writeInternal(Object object, HttpOutputMessage outputMessage)
throws IOException, HttpMessageNotWritableException {
outputMessage.getHeaders().setContentType(MediaType.APPLICATION_JSON);

outputMessage.getHeaders().setContentType(MediaType.APPLICATION_JSON); // must be before body is fetched
outputMessage.getHeaders().setContentDisposition(ContentDisposition.empty());
super.writeInternal(object, outputMessage);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
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;
Expand Down Expand Up @@ -37,7 +36,7 @@ protected void writeInternal(WyriwygProduct t, HttpOutputMessage outputMessage)
throws IOException, HttpMessageNotWritableException {
ObjectMapper mapper = new ObjectMapper();
mapper.setSerializationInclusion(Include.NON_NULL);

outputMessage.getHeaders().setContentType(MediaType.APPLICATION_JSON); // must be before body is fetched
OutputStream os = outputMessage.getBody();
OutputStreamWriter wr = new OutputStreamWriter(os);
WyriwygSerializer.writeJSON(t, wr, mapper);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public void writeInternal(Pds4Product product, HttpOutputMessage msg)
throws IOException, HttpMessageNotWritableException {
ObjectMapper mapper = new ObjectMapper();
mapper.setSerializationInclusion(Include.NON_NULL);

msg.getHeaders().setContentType(MediaType.APPLICATION_JSON); // must be before body is fetched
OutputStream os = msg.getBody();
OutputStreamWriter wr = new OutputStreamWriter(os);
writeProduct(product, wr, mapper);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public void writeInternal(Pds4Products products, HttpOutputMessage msg)
throws IOException, HttpMessageNotWritableException {
ObjectMapper mapper = new ObjectMapper();
mapper.setSerializationInclusion(Include.NON_NULL);
msg.getHeaders().setContentType(MediaType.APPLICATION_JSON); // must be before body is fetched

OutputStream os = msg.getBody();
OutputStreamWriter wr = new OutputStreamWriter(os);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ protected Pds4Product readInternal(Class<? extends Pds4Product> clazz,
protected void writeInternal(Pds4Product product, HttpOutputMessage outputMessage)
throws IOException, HttpMessageNotWritableException {
try {
outputMessage.getHeaders().setContentType(MediaType.TEXT_XML); // must be before body is fetched
OutputStream outputStream = outputMessage.getBody();
XMLOutputFactory outputFactory = XMLOutputFactory.newFactory();
outputFactory.setProperty("javax.xml.stream.isRepairingNamespaces", true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ protected Pds4Products readInternal(Class<? extends Pds4Products> clazz,
protected void writeInternal(Pds4Products products, HttpOutputMessage outputMessage)
throws IOException, HttpMessageNotWritableException {
try {
outputMessage.getHeaders().setContentType(MediaType.TEXT_XML); // must be before body is fetched
OutputStream outputStream = outputMessage.getBody();
XMLOutputFactory outputFactory = XMLOutputFactory.newFactory();
outputFactory.setProperty("javax.xml.stream.isRepairingNamespaces", true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.nio.charset.Charset;

import org.springframework.http.HttpInputMessage;
import org.springframework.http.HttpOutputMessage;
import org.springframework.http.MediaType;
Expand Down Expand Up @@ -37,12 +36,11 @@ protected PdsProduct readInternal(Class<? extends PdsProduct> clazz,
protected void writeInternal(PdsProduct t, HttpOutputMessage outputMessage)
throws IOException, HttpMessageNotWritableException {
ObjectMapper mapper = new ObjectMapper();
outputMessage.getHeaders().setContentType(MediaType.APPLICATION_JSON); // must be before body is fetched
OutputStream os = outputMessage.getBody();
OutputStreamWriter wr = new OutputStreamWriter(os, Charset.defaultCharset());
mapper.setSerializationInclusion(Include.NON_NULL);
wr.write("<html><body><h1>JSON as text</h1><p><pre>");
wr.write(mapper.writerWithDefaultPrettyPrinter().writeValueAsString(t));
wr.write("</pre></p></body></html>");
wr.close();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ protected PdsProduct readInternal(Class<? extends PdsProduct> clazz,
protected void writeInternal(PdsProduct product, HttpOutputMessage outputMessage)
throws IOException, HttpMessageNotWritableException {
try {
outputMessage.getHeaders().setContentType(MediaType.TEXT_XML); // must be before body is fetched
OutputStream outputStream = outputMessage.getBody();
XMLOutputFactory outputFactory = XMLOutputFactory.newFactory();
outputFactory.setProperty("javax.xml.stream.isRepairingNamespaces", true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,12 @@ protected PdsProducts readInternal(Class<? extends PdsProducts> clazz,
protected void writeInternal(PdsProducts t, HttpOutputMessage outputMessage)
throws IOException, HttpMessageNotWritableException {
ObjectMapper mapper = new ObjectMapper();
outputMessage.getHeaders().setContentType(MediaType.APPLICATION_JSON); // must be before body is fetched
OutputStream os = outputMessage.getBody();
OutputStreamWriter wr = new OutputStreamWriter(os, Charset.defaultCharset());
mapper.setSerializationInclusion(Include.NON_NULL);
Utilities.fix(t.getSummary());
wr.write("<html><body><h1>JSON as text</h1><p><pre>");
wr.write(mapper.writerWithDefaultPrettyPrinter().writeValueAsString(t));
wr.write("</pre></p></body></html>");
wr.close();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ protected PdsProducts readInternal(Class<? extends PdsProducts> clazz,
protected void writeInternal(PdsProducts products, HttpOutputMessage outputMessage)
throws IOException, HttpMessageNotWritableException {
try {
outputMessage.getHeaders().setContentType(MediaType.TEXT_XML); // must be before body is fetched
OutputStream outputStream = outputMessage.getBody();
XmlMapper mapper = new XmlMapper();
XMLOutputFactory outputFactory = XMLOutputFactory.newFactory();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ protected ErrorMessage readInternal(Class<? extends ErrorMessage> clazz,
@Override
protected void writeInternal(ErrorMessage t, HttpOutputMessage outputMessage)
throws IOException, HttpMessageNotWritableException {
outputMessage.getHeaders().setContentType(MediaType.TEXT_XML); // must be before body is fetched
OutputStreamWriter osw = new OutputStreamWriter(outputMessage.getBody(), "UTF-8");
try {
osw.write("<error><request>");
Expand Down

0 comments on commit b32f015

Please sign in to comment.