Package javax.ws.rs.core

Examples of javax.ws.rs.core.Variant



   public Variant getBestMatch(List<Variant> available)
   {
      BigDecimal bestQuality = BigDecimal.ZERO;
      Variant bestOption = null;
      for (Variant option : available)
      {
         VariantQuality quality = new VariantQuality();
         if (!applyMediaType(option, quality))
            continue;
View Full Code Here


      MediaType mediaType = MediaType.valueOf("text/plain");
      String encoding = "gzip";
      Locale locale = Locale.UK;

      List<Variant> available = new ArrayList<Variant>();
      available.add(new Variant(mediaTypeWithCharset, null, null));
      available.add(new Variant(mediaTypeWithCharset, locale, null));
      available.add(new Variant(mediaTypeWithCharset, null, encoding));
      available.add(new Variant(mediaTypeWithCharset, locale, encoding));
      available.add(new Variant(mediaType, null, null));
      available.add(new Variant(mediaType, locale, null));
      available.add(new Variant(mediaType, null, encoding));
      available.add(new Variant(mediaType, locale, encoding));
      available.add(new Variant(null, locale, null));
      available.add(new Variant(null, locale, encoding));
      available.add(new Variant(null, null, encoding));

      // Assert all acceptable:
      for (Variant variant : available)
         assertEquals(variant, negotiation.getBestMatch(Arrays.asList(variant)));

      Variant best = negotiation.getBestMatch(available);
      assertNotNull(best);
      assertEquals(mediaTypeWithCharset, best.getMediaType());
      assertEquals(encoding, best.getEncoding());
      assertEquals(locale, best.getLanguage());
   }
View Full Code Here

   {
      String header = "text/*, text/html, text/html;level=1, */*";
      ServerDrivenNegotiation negotiation = new ServerDrivenNegotiation();
      negotiation.setAcceptHeaders(Arrays.asList(header));

      Variant o1 = new Variant(MediaType.valueOf("text/html;level=1"), null, null);
      Variant o2 = new Variant(MediaType.valueOf("text/html"), null, null);
      Variant o3 = new Variant(MediaType.valueOf("text/*"), null, null);
      Variant o4 = new Variant(MediaType.valueOf("*/*"), null, null);

      List<Variant> available = new ArrayList<Variant>();
      available.add(o4);
      assertEquals(o4, negotiation.getBestMatch(available));
      available.add(o3);
View Full Code Here

      String header2 = "text/html;level=2;q=0.4, */*;q=0.5";
      ServerDrivenNegotiation negotiation = new ServerDrivenNegotiation();
      negotiation.setAcceptHeaders(Arrays.asList(header1, header2));
      negotiation.setAcceptLanguageHeaders(Arrays.asList("en"));

      Variant q03 = new Variant(MediaType.valueOf("text/plain"), null, null);
      Variant q04 = new Variant(MediaType.valueOf("text/html;level=2"), null, null);
      Variant q05 = new Variant(MediaType.valueOf("image/jpeg"), null, null);
      Variant q07 = new Variant(MediaType.valueOf("text/html"), null, null);
      Variant q07plus = new Variant(MediaType.valueOf("text/html;level=3"), null, null);
      Variant q10 = new Variant(MediaType.valueOf("text/html;level=1"), null, null);

      List<Variant> available = new ArrayList<Variant>();
      available.add(q03);
      assertEquals(q03, negotiation.getBestMatch(available));
      available.add(q04);
View Full Code Here

      public Response toResponse(MyException exception)
      {
         System.out.println("Method: " + request.getMethod());

         ArrayList<Variant> list = new ArrayList<Variant>();
         list.add(new Variant(MediaType.APPLICATION_JSON_TYPE, null, null));
         request.selectVariant(list);
         return Response.status(Response.Status.PRECONDITION_FAILED).build();
      }
View Full Code Here

      public Response doGet(@Context Request r)
      {
         List<Variant> vs = Variant.VariantListBuilder.newInstance().languages(new Locale("zh")).languages(
                 new Locale("fr")).languages(new Locale("en")).add().build();

         Variant v = r.selectVariant(vs);
         if (v == null)
            return Response.notAcceptable(vs).build();
         else
            return Response.ok(v.getLanguage(), v).build();
      }
View Full Code Here

         List<Variant> vs = Variant.VariantListBuilder.newInstance().mediaTypes(MediaType.valueOf("image/jpeg")).add()
                 .mediaTypes(MediaType.valueOf("application/xml")).languages(new Locale("en", "us")).add().mediaTypes(
                         MediaType.valueOf("text/xml")).languages(new Locale("en")).add().mediaTypes(
                         MediaType.valueOf("text/xml")).languages(new Locale("en", "us")).add().build();

         Variant v = r.selectVariant(vs);
         if (v == null)
            return Response.notAcceptable(vs).build();
         else
            return Response.ok("GET", v).build();
      }
View Full Code Here

   {
      @GET
      public Response doGet(@Context Request r)
      {
         List<Variant> vs = Variant.VariantListBuilder.newInstance().encodings("enc1", "enc2", "enc3").add().build();
         Variant v = r.selectVariant(vs);
         if (v == null)
            return Response.notAcceptable(vs).build();
         else
            return Response.ok(v.getEncoding(), v).build();
      }
View Full Code Here

    int i = characterSets.length;
    MediaType[] mediaTypes = new MediaType[i];
    while (--i >= 0)
      mediaTypes[i] = MediaType.valueOf("application/xml;charset=" + characterSets[i]);
    List<Variant> variants = Variant.mediaTypes(mediaTypes).build();
    Variant variant = request.selectVariant(variants);
    if (variant == null)
      return Response.notAcceptable(variants).build();
    return Response.ok(new TestData(), variant).build();
  }
View Full Code Here

        VariantQChecked bestVariant = null;
        boolean isIdentityEncodingChecked = false;

        for (Iterator<Variant> iter = variants.iterator(); iter.hasNext();) {
            double acceptQFactor = -1.0d;
            Variant v = iter.next();
            logger.debug("Variant being evaluated is: {}", v);
            MediaType vMediaType = v.getMediaType();
            if (vMediaType != null && acceptableMediaTypes != null) {
                boolean isCompatible = false;
                boolean isAcceptable = true; // explicitly denied by the client
                for (MediaType mt : acceptableMediaTypes) {
                    logger.debug("Checking variant media type {} against Accept media type {}",
                                 vMediaType,
                                 mt);
                    if (mt.isCompatible(vMediaType)) {
                        Map<String, String> params = mt.getParameters();
                        String q = params.get("q");
                        if (q != null) {
                            try {
                                Double qAsDouble = Double.valueOf(q);
                                if (qAsDouble.equals(0.0)) {
                                    isAcceptable = false;
                                    logger
                                        .debug("Accept Media Type: {} is NOT compatible with q-factor {}",
                                               mt,
                                               qAsDouble);
                                    break;
                                }
                                acceptQFactor = qAsDouble;
                            } catch (NumberFormatException e) {
                                logger
                                    .debug("NumberFormatException during MediaType q-factor evaluation: {}",
                                           e);
                            }
                        } else {
                            acceptQFactor = 1.0d;
                        }

                        isCompatible = true;
                        logger.debug("Accept Media Type: {} is compatible with q-factor {}",
                                     mt,
                                     acceptQFactor);
                    }
                }
                if (!isCompatible || !isAcceptable) {
                    logger.debug("Variant {} is not compatible or not acceptable", vMediaType);
                    continue;
                }
            }

            if (bestVariant != null) {
                if (acceptQFactor < bestVariant.acceptMediaTypeQFactor) {
                    logger
                        .debug("Best variant's media type {} q-factor {} is greater than current variant {} q-factor {}",
                               new Object[] {bestVariant.variant,
                                   bestVariant.acceptMediaTypeQFactor, vMediaType, acceptQFactor});
                    continue;
                }
            }

            double acceptLanguageQFactor = -1.0d;
            Locale vLocale = v.getLanguage();
            if (vLocale != null && languages != null) {
                boolean isCompatible = false;
                logger.debug("Checking variant locale {}", vLocale);
                if (languages.getBannedLanguages().contains(vLocale)) {
                    logger.debug("Variant locale {} was in unacceptable languages", vLocale);
                    continue;
                }
                for (AcceptLanguage.ValuedLocale locale : languages.getValuedLocales()) {
                    logger
                        .debug("Checking against Accept-Language locale {} with quality factor {}",
                               locale.locale,
                               locale.qValue);
                    if (locale.isWildcard() || vLocale.equals(locale.locale)) {
                        logger.debug("Locale is compatible {}", locale.locale);
                        isCompatible = true;
                        acceptLanguageQFactor = locale.qValue;
                        break;
                    }
                }
                if (!isCompatible) {
                    logger.debug("Variant locale is not compatible {}", vLocale);
                    continue;
                }
            }

            if (bestVariant != null) {
                if (acceptLanguageQFactor < bestVariant.acceptLanguageQFactor) {
                    logger
                        .debug("Best variant's language {} q-factor {} is greater than current variant {} q-factor {}",
                               new Object[] {bestVariant.variant,
                                   bestVariant.acceptLanguageQFactor, v, acceptLanguageQFactor});
                    continue;
                }
            }

            double acceptCharsetQFactor = -1.0d;
            String vCharset = ProviderUtils.getCharsetOrNull(v.getMediaType());
            boolean hasCharSet = true;

            if (vCharset == null) {
                hasCharSet = false;
            } else if (vCharset != null && charsets != null) {
                boolean isCompatible = false;
                logger.debug("Checking variant charset: {}", vCharset);
                if (charsets.getBannedCharsets().contains(vCharset)) {
                    logger.debug("Variant charset {} was in unacceptable charsets", vCharset);
                    continue;
                }
                for (AcceptCharset.ValuedCharset charset : charsets.getValuedCharsets()) {
                    logger
                        .debug("Checking against Accept-Charset charset {} with quality factor {}",
                               charset.charset,
                               charset.qValue);
                    if (charset.isWildcard() || vCharset.equalsIgnoreCase(charset.charset)) {
                        logger.debug("Charset is compatible with {}", charset.charset);
                        isCompatible = true;
                        acceptCharsetQFactor = charset.qValue;
                        break;
                    }
                }

                if (!isCompatible) {
                    logger.debug("Variant charset is not compatible {}", vCharset);
                    /*
                     * do not remove this from the acceptable list even if not
                     * compatible but set to -1.0d for now. according to HTTP
                     * spec, it is "ok" to send
                     */
                }
            }

            if (bestVariant != null) {
                if (acceptCharsetQFactor < bestVariant.acceptCharsetQFactor && hasCharSet) {
                    logger
                        .debug("Best variant's charset {} q-factor {} is greater than current variant {} q-factor {}",
                               new Object[] {bestVariant.variant, bestVariant.acceptCharsetQFactor,
                                   v, acceptCharsetQFactor});
                    continue;
                }
            }

            double acceptEncodingQFactor = -1.0d;
            String vEncoding = v.getEncoding();
            if (vEncoding != null) {
                logger.debug("Checking variant encoding {}", vEncoding);
                if (encodings == null || encodings.isAnyEncodingAllowed()) {
                    logger.debug("Accept-Encoding is null or wildcard");
                    if (!v.getEncoding().equalsIgnoreCase("identity")) {
                        logger
                            .debug("Variant encoding {} does not equal identity so not acceptable",
                                   vEncoding);
                        // if there is no Accept Encoding, only identity is
                        // acceptable
View Full Code Here

TOP

Related Classes of javax.ws.rs.core.Variant

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.