VariantsHandler { public Variant handleVariants(ContainerRequest request, List<Variant> variants) { List<AcceptMediaType> acceptMediaTypes = createAcceptMediaTypeList(convertToString(request.getRequestHeader(ACCEPT))); List<AcceptLanguage> acceptLanguages = createAcceptedLanguageList(convertToString(request.getRequestHeader(ACCEPT_LANGUAGE))); List<AcceptToken> acceptCharset = createAcceptedCharsetList(convertToString(request.getRequestHeader(ACCEPT_CHARSET))); List<AcceptToken> acceptEncoding = createAcceptedEncodingList(convertToString(request.getRequestHeader(ACCEPT_ENCODING))); List<Variant> filteredVariants = new ArrayList<>(variants); filteredVariants = filterByMediaType(acceptMediaTypes, filteredVariants); filteredVariants = filterByLanguage(acceptLanguages, filteredVariants); filteredVariants = filterByCharset(acceptCharset, filteredVariants); filteredVariants = filterByEncoding(acceptEncoding, filteredVariants); return filteredVariants.isEmpty() ? null : filteredVariants.get(0); }  Variant handleVariants(ContainerRequest request, List<Variant> variants);  }
@Test public void selectsVariant() throws Exception { List<Variant> variantList = Variant.mediaTypes(MediaType.valueOf("image/jpeg")).add() .mediaTypes(MediaType.valueOf("text/xml")).languages(new Locale("en", "gb")).add() .mediaTypes(MediaType.valueOf("text/xml;charset=utf-8")).languages(new Locale("en", "us")).add() .mediaTypes(MediaType.valueOf("text/xml;charset=utf-8")).languages(new Locale("en", "us")).encodings("gzip").add() .build(); MultivaluedMap<String, String> headers = new MultivaluedMapImpl(); headers.putSingle(ACCEPT, Joiner.on(',').join("text/xml", "image/png", "text/html;q=0.9", "text/plain;q=0.8", "application/xml", "*/*;q=0.5")); headers.putSingle(ACCEPT_LANGUAGE, "en-us,en;q=0.5"); headers.putSingle(ACCEPT_CHARSET, "utf-8,koi8;q=0.7"); headers.putSingle(ACCEPT_ENCODING, "gzip"); ContainerRequest request = new ContainerRequest("GET", null, null, null, headers, null); Variant variant = variantsHandler.handleVariants(request, variantList); assertNotNull(variant); assertEquals(new MediaType("text", "xml", ImmutableMap.of("charset", "utf-8")), variant.getMediaType()); assertEquals(new Locale("en", "us"), variant.getLanguage()); assertEquals("gzip", variant.getEncoding()); }