@@ -175,64 +175,19 @@ public static RestRequest requestWithoutParameters(NamedXContentRegistry xConten
175
175
requestIdGenerator .incrementAndGet ());
176
176
}
177
177
178
- /**
179
- * An http request can be accompanied with a compatible version indicating with what version a client is using.
180
- * Only a major Versions are supported. Internally we use Versions objects, but only use Version(major,0,0)
181
- * @return a version with what a client is compatible with.
182
- */
183
- public Version getCompatibleApiVersion (BiFunction <Map <String ,List <String >>,Boolean ,Boolean > isRequestingCompatibilityFunction ) {
184
- if (/*headersValidation &&*/ isRequestingCompatibilityFunction .apply (getHeaders (),hasContent ())) {
185
- return Version .fromString (Version .CURRENT .major -1 +".0.0" );
186
- } else {
187
- return Version .CURRENT ;
188
- }
178
+ public Version getRequestedCompatibility (BiFunction <String , String , Version > restCompatibleFunction ) {
179
+ return restCompatibleFunction .apply (getSingleHeader ("Accept" ), getSingleHeader ("Content-Type" ));
189
180
}
190
181
191
-
192
- private boolean isRequestingCompatibility () {
193
- /* String acceptHeader = header(CompatibleConstants.COMPATIBLE_ACCEPT_HEADER);
194
- String aVersion = XContentType.parseVersion(acceptHeader);
195
- byte acceptVersion = aVersion == null ? Version.CURRENT.major : Integer.valueOf(aVersion).byteValue();
196
- String contentTypeHeader = header(CompatibleConstants.COMPATIBLE_CONTENT_TYPE_HEADER);
197
- String cVersion = XContentType.parseVersion(contentTypeHeader);
198
- byte contentTypeVersion = cVersion == null ? Version.CURRENT.major : Integer.valueOf(cVersion).byteValue();
199
-
200
- if(Version.CURRENT.major < acceptVersion || Version.CURRENT.major - acceptVersion > 1 ){
201
- throw new CompatibleApiHeadersCombinationException(
202
- String.format(Locale.ROOT, "Unsupported version provided. " +
203
- "Accept=%s Content-Type=%s hasContent=%b path=%s params=%s method=%s", acceptHeader,
204
- contentTypeHeader, hasContent(), path(), params.toString(), method().toString()));
205
- }
206
- if (hasContent()) {
207
- if(Version.CURRENT.major < contentTypeVersion || Version.CURRENT.major - contentTypeVersion > 1 ){
208
- throw new CompatibleApiHeadersCombinationException(
209
- String.format(Locale.ROOT, "Unsupported version provided. " +
210
- "Accept=%s Content-Type=%s hasContent=%b path=%s params=%s method=%s", acceptHeader,
211
- contentTypeHeader, hasContent(), path(), params.toString(), method().toString()));
212
- }
213
-
214
- if (contentTypeVersion != acceptVersion) {
215
- throw new CompatibleApiHeadersCombinationException(
216
- String.format(Locale.ROOT, "Content-Type and Accept headers have to match when content is present. " +
217
- "Accept=%s Content-Type=%s hasContent=%b path=%s params=%s method=%s", acceptHeader,
218
- contentTypeHeader, hasContent(), path(), params.toString(), method().toString()));
219
- }
220
- // both headers should be versioned or none
221
- if ((cVersion == null && aVersion!=null) || (aVersion ==null && cVersion!=null) ){
222
- throw new CompatibleApiHeadersCombinationException(
223
- String.format(Locale.ROOT, "Versioning is required on both Content-Type and Accept headers. " +
224
- "Accept=%s Content-Type=%s hasContent=%b path=%s params=%s method=%s", acceptHeader,
225
- contentTypeHeader, hasContent(), path(), params.toString(), method().toString()));
226
- }
227
-
228
- return contentTypeVersion < Version.CURRENT.major;
182
+ private final String getSingleHeader (String name ) {
183
+ //TODO: is this case sensitive ?
184
+ List <String > values = headers .get (name );
185
+ if (values != null && values .isEmpty () == false ) {
186
+ return values .get (0 );
229
187
}
230
-
231
- return acceptVersion < Version.CURRENT.major;*/
232
- return true ;
188
+ return null ;
233
189
}
234
190
235
-
236
191
public enum Method {
237
192
GET , POST , PUT , DELETE , OPTIONS , HEAD , PATCH , TRACE , CONNECT
238
193
}
@@ -597,4 +552,5 @@ public static class BadParameterException extends RuntimeException {
597
552
}
598
553
599
554
}
555
+
600
556
}
0 commit comments