Skip to content

Commit 674f467

Browse files
PKIService: add more general variants of resolveFormat
PKIService.resolveFormat is used to check a media type, or list of media types (possibly including wildcards) against the default list of content types understood and produced or processed by Dogtag. The current variants compare the parameter against a hardcoded list of supported content types. However, it would be useful to also provide general variants that allow the caller to specify both the candidate type(s) and the valid/accepted types. This commit adds those variants. Related: dogtagpki#3297 Signed-off-by: Fraser Tweedale <ftweedal@redhat.com>
1 parent 5618b62 commit 674f467

File tree

1 file changed

+43
-7
lines changed

1 file changed

+43
-7
lines changed

base/server/src/main/java/com/netscape/cms/servlet/base/PKIService.java

Lines changed: 43 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -102,24 +102,60 @@ public static String getBanner() throws IOException {
102102
return new String(Files.readAllBytes(bannerFile), "UTF-8").trim();
103103
}
104104

105+
/**
106+
* Return a match for a candidate media type (which may be a wildcard)
107+
* against the default list of valid media types.
108+
*
109+
* @return the matching MediaType or null if no match
110+
*/
105111
public static MediaType resolveFormat(MediaType format) {
112+
return resolveFormat(format, MESSAGE_FORMATS);
113+
}
106114

107-
if (format == null) return null;
115+
/**
116+
* Return a match for a candidate media type (which may be a wildcard)
117+
* against a list of valid media types.
118+
*
119+
* @return the matching MediaType or null if no match
120+
*/
121+
public static MediaType resolveFormat(MediaType candidate, List<MediaType> validTypes) {
122+
if (candidate == null) return null;
108123

109-
for (MediaType supportedFormat : MESSAGE_FORMATS) {
110-
if (format.isCompatible(supportedFormat)) return supportedFormat;
124+
for (MediaType validType : validTypes) {
125+
if (candidate.isCompatible(validType)) return validType;
111126
}
112127

113128
return null;
114129
}
115130

131+
/**
132+
* Find a match from a list of candidate media types (which may be wildcards)
133+
* against the default list of valid media types.
134+
*
135+
* Candidates are checked in list order. Quality values ("q" parameter)
136+
* are ignored.
137+
*
138+
* @return the matching MediaType or null if no match
139+
*/
116140
public static MediaType resolveFormat(List<MediaType> formats) {
141+
return resolveFormat(formats, MESSAGE_FORMATS);
142+
}
117143

118-
if (formats == null) return null;
144+
/**
145+
* Find a match from a list of candidate media types (which may be wildcards)
146+
* against a list of valid media types.
147+
*
148+
* Candidates are checked in list order. Quality values ("q" parameter)
149+
* are ignored.
150+
*
151+
* @return the matching MediaType or null if no match
152+
*/
153+
public static MediaType resolveFormat(List<MediaType> candidates, List<MediaType> validTypes) {
154+
if (candidates == null) return null;
119155

120-
for (MediaType acceptableFormat : formats) {
121-
MediaType supportedFormat = resolveFormat(acceptableFormat);
122-
if (supportedFormat != null) return supportedFormat;
156+
for (MediaType candidate : candidates) {
157+
MediaType match = resolveFormat(candidate, validTypes);
158+
if (match != null) return match;
123159
}
124160

125161
return null;

0 commit comments

Comments
 (0)