Skip to content

Commit 90a0e32

Browse files
committed
Refactor Format enum - added mediatype()
1 parent 64d627c commit 90a0e32

File tree

1 file changed

+40
-25
lines changed

1 file changed

+40
-25
lines changed

src/main/java/org/commonwl/view/WebConfig.java

Lines changed: 40 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121

2222
import static org.springframework.http.MediaType.parseMediaType;
2323

24+
import org.commonwl.view.workflow.Workflow;
25+
import org.commonwl.view.workflow.WorkflowPermalinkController;
2426
import org.springframework.context.annotation.Configuration;
2527
import org.springframework.http.MediaType;
2628
import org.springframework.web.servlet.config.annotation.ContentNegotiationConfigurer;
@@ -30,38 +32,51 @@
3032
public class WebConfig extends WebMvcConfigurerAdapter {
3133

3234
/**
33-
* Ordered list of formats as presented on Workflow page - must match the
34-
* .mediaType() strings below.
35+
* Ordered list of formats as presented on Workflow page and supported for
36+
* content negotiation.
37+
*
38+
* @see Workflow#getPermalink(Format)
39+
* @see WorkflowPermalinkController
3540
*
3641
*/
37-
public static enum formats {
38-
html, json, turtle, jsonld, rdfxml, svg, png, dot, zip, ro, yaml, raw
42+
public static enum Format {
43+
// Browser
44+
html(MediaType.TEXT_HTML),
45+
// API
46+
json(MediaType.APPLICATION_JSON),
47+
// RDF
48+
turtle("text/turtle"), jsonld("application/ld+json"), rdfxml("application/rdf+xml"),
49+
// Images
50+
svg("image/svg+xml"), png(MediaType.IMAGE_PNG), dot("text/vnd+graphviz"),
51+
// Archives
52+
zip("application/zip"), ro("application/vnd.wf4ever.robundle+zip"),
53+
// raw redirects
54+
yaml("text/x-yaml"), raw(MediaType.APPLICATION_OCTET_STREAM);
55+
56+
private final MediaType mediaType;
57+
58+
Format(MediaType mediaType) {
59+
this.mediaType = mediaType;
60+
}
61+
62+
Format(String mediaType) {
63+
this.mediaType = parseMediaType(mediaType);
64+
}
65+
66+
public MediaType mediaType() {
67+
return mediaType;
68+
}
3969
}
4070

4171
/**
42-
* Allows the use of the format query parameter to be used
43-
* instead of the Accept HTTP header
72+
* Allows the use of the format query parameter to be used instead of the Accept
73+
* HTTP header
4474
*/
4575
@Override
4676
public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
47-
configurer.favorParameter(true)
48-
// Browser
49-
.mediaType("html", MediaType.TEXT_HTML)
50-
// API
51-
.mediaType("json", MediaType.APPLICATION_JSON)
52-
// RDF
53-
.mediaType("turtle", parseMediaType("text/turtle"))
54-
.mediaType("jsonld", parseMediaType("application/ld+json"))
55-
.mediaType("rdfxml", parseMediaType("application/rdf+xml"))
56-
// Images
57-
.mediaType("svg", parseMediaType("image/svg+xml"))
58-
.mediaType("png", MediaType.IMAGE_PNG)
59-
.mediaType("dot", parseMediaType("text/vnd+graphviz"))
60-
// Archives
61-
.mediaType("zip", parseMediaType("application/zip"))
62-
.mediaType("ro", parseMediaType("application/vnd.wf4ever.robundle+zip"))
63-
// raw redirects
64-
.mediaType("yaml", parseMediaType("text/x-yaml"))
65-
.mediaType("raw", MediaType.APPLICATION_OCTET_STREAM);
77+
ContentNegotiationConfigurer c = configurer.favorParameter(true);
78+
for (Format f : Format.values()) {
79+
c = c.mediaType(f.name(), f.mediaType());
80+
}
6681
}
6782
}

0 commit comments

Comments
 (0)