-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Changes for including charset in api response
- Loading branch information
Showing
5 changed files
with
68 additions
and
43 deletions.
There are no files selected for viewing
89 changes: 52 additions & 37 deletions
89
record-api-common/src/main/java/eu/europeana/api/format/RdfFormat.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters