Skip to content

Commit

Permalink
Changes for including charset in api response
Browse files Browse the repository at this point in the history
  • Loading branch information
nshweta90 committed Apr 29, 2024
1 parent 0636b9d commit 04553d4
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 43 deletions.
Original file line number Diff line number Diff line change
@@ -1,56 +1,71 @@
package eu.europeana.api.format;

import java.nio.charset.Charset;
import org.apache.commons.lang3.StringUtils;

/**
* @author Hugo
* @since 13 Oct 2023
*/
public enum RdfFormat {
JSONLD("jsonld", null, "application/ld+json")
, JSON("json", null, "application/json")
, XML("rdf", "xml", "application/rdf+xml", "application/xml", "text/xml", "rdf/xml")
, TURTLE("ttl", null, "text/turtle", "application/turtle", "application/x-turtle")
, N3("n3", null, "text/n3", "text/rdf+n3", "application/n3")
, NT("nt", null, "application/n-triples", "application/ntriples", "text/nt");

public static RdfFormat getFormatByExtension(String extension) {
for ( RdfFormat format : RdfFormat.values() ) {
if ( format.acceptsExtension(extension) ) { return format; }
}
return null;
JSONLD("jsonld","json",null,"application/ld+json","application/json")
, XML("rdf","xml","utf-8","application/rdf+xml","application/xml","text/xml","rdf/xml")
, TURTLE("ttl",null,"utf-8","text/turtle","application/turtle","application/x-turtle")
, N3("n3",null,"utf-8","text/n3","text/rdf+n3","application/n3")
, NT("nt",null,null,"application/n-triples","application/ntriples","text/nt");

public static RdfFormat getFormatByExtension(String extension) {
for ( RdfFormat format : RdfFormat.values() ) {
if ( format.acceptsExtension(extension) ) { return format; }
}
return null;
}

public static RdfFormat getFormatByMediaType(String mediaType) {
for ( RdfFormat format : RdfFormat.values() ) {
if ( format.acceptsMediaType(mediaType) ) { return format; }
public static RdfFormat getFormatByMediaType(String mediaType) {
if(StringUtils.isNotEmpty(mediaType)) {
for (RdfFormat format : RdfFormat.values()) {
if (format.acceptsMediaType(mediaType)) {
return format;
}
return null;
}
}
return null;
}

private String extension;
private String alternative;
private String[] mediaTypes;
private String extension;
private String alternative;
private String charset;
private String[] mediaTypes;

RdfFormat(String extension, String alternative, String... mediaTypes) {
this.extension = extension;
this.alternative = alternative;
this.mediaTypes = mediaTypes;
}
RdfFormat(String extension, String alternative, String charset
, String... mediaTypes) {
this.extension = extension;
this.alternative = alternative;
this.charset = charset;
this.mediaTypes = mediaTypes;
}

public String getExtension() { return extension; }
public String getExtension() { return extension; }

public String getAlternative() { return alternative; }
public String getAlternative() { return alternative; }

public String getMediaType() { return mediaTypes[0]; }
public String getCharset() { return charset; }

public boolean acceptsExtension(String extension) {
return ( this.extension.equals(extension)
|| ( this.alternative != null && this.alternative.equals(extension)));
}
public String getMediaType() { return mediaTypes[0]; }

public boolean acceptsMediaType(String mediaType) {
for ( String mType : mediaTypes ) {
if ( mType.equals(mediaType) ) { return true; }
}
return false;
public Charset getCharsetObject(){
return StringUtils.isNotEmpty(charset)?Charset.forName(charset):null;
}

public boolean acceptsExtension(String extension) {
return ( this.extension.equals(extension)
|| ( this.alternative != null && this.alternative.equals(extension)));
}

public boolean acceptsMediaType(String mediaType) {
for ( String mType : mediaTypes ) {
if (mType.equals(mediaType)) { return true; }
}
}
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ public FormatHandlerRegistry(JsonLdWriter jsonLdWriter, XmlRecordWriter xmlRecor
this.jenaBasedNTWriter = jenaBasedNTWriter;

put(RdfFormat.JSONLD, this.jsonLdWriter);
put(RdfFormat.JSON, this.jsonLdWriter);
put(RdfFormat.XML, this.xmlRecordWriter);
put(RdfFormat.TURTLE, this.jenaBasedTurtleWriter);
put(RdfFormat.N3, this.jenaBasedN3Writer);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ public FormatHandlerRegistryV2(JsonV2Writer jsonV2Writer,
this.xmlRecordWriter = xmlRecordWriter;
this.jenaBasedTurtleWriter = jenaBasedTurtleWriter;

put(RdfFormat.JSON, this.jsonV2Writer);
put(RdfFormat.JSONLD, this.jsonLdWriter);
put(RdfFormat.XML, this.xmlRecordWriter);
put(RdfFormat.TURTLE, this.jenaBasedTurtleWriter);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import eu.europeana.api.format.RdfFormat;
import eu.europeana.api.record.model.RecordRequest;
import jakarta.servlet.http.HttpServletRequest;
import java.nio.charset.Charset;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
Expand Down Expand Up @@ -96,9 +97,10 @@ private static boolean idHasExtension(String localId) {
public static HttpHeaders getHeaders(HttpServletRequest request, RecordRequest recordRequest) {
HttpHeaders httpHeaders= new HttpHeaders();
if (!recordRequest.hasExtension() && isValidMediaType(request)) {
httpHeaders.setContentType(MediaType.valueOf(request.getHeader(HttpHeaders.ACCEPT)));
String header = request.getHeader(HttpHeaders.ACCEPT);
httpHeaders.setContentType(RecordUtils.getMediaTypeObject(RdfFormat.getFormatByMediaType(header)));
} else { // default content-type by default RDF Format
httpHeaders.setContentType(MediaType.valueOf(recordRequest.getRdfFormat().getMediaType()));
httpHeaders.setContentType(getMediaTypeObject(recordRequest.getRdfFormat()));
}
return httpHeaders;
}
Expand All @@ -107,5 +109,15 @@ private static boolean isValidMediaType(HttpServletRequest request) {
String acceptHeader = request.getHeader(HttpHeaders.ACCEPT);
return acceptHeader != null && RdfFormat.getFormatByMediaType(acceptHeader) != null;
}


public static MediaType getMediaTypeObject(RdfFormat format) {
Charset charset = format.getCharsetObject();
String mediaTypeVal = format.getMediaType();
if (format.getCharsetObject() != null) {
return new MediaType(MediaType.valueOf(mediaTypeVal), charset);
}
return new MediaType(MediaType.valueOf(mediaTypeVal));
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import eu.europeana.api.record.model.RecordRequest;
import eu.europeana.api.record.service.RecordService;
import eu.europeana.api.record.utils.RecordUtils;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import jakarta.servlet.http.HttpServletRequest;
Expand Down Expand Up @@ -152,7 +151,7 @@ private ResponseEntity<StreamingResponseBody> createResponseMultipleRecords(List

// create response headers
org.springframework.http.HttpHeaders httpHeaders= new org.springframework.http.HttpHeaders();
httpHeaders.setContentType(MediaType.valueOf(RdfFormat.JSONLD.getMediaType()));
httpHeaders.setContentType(RecordUtils.getMediaTypeObject(RdfFormat.JSONLD));
StreamingResponseBody responseBody = new StreamingResponseBody() {
@Override
public void writeTo(OutputStream out) throws IOException {
Expand All @@ -162,4 +161,5 @@ public void writeTo(OutputStream out) throws IOException {
};
return new ResponseEntity<>(responseBody, httpHeaders, HttpStatus.OK);
}

}

0 comments on commit 04553d4

Please sign in to comment.