Пример #1
0
  /**
   * Returns the list of variants that can be converted from a given object class.
   *
   * @param sourceClass The source class.
   * @param targetVariant The expected representation metadata.
   * @return The list of variants that can be converted.
   */
  public static List<VariantInfo> getVariants(Class<?> sourceClass, Variant targetVariant) {
    List<VariantInfo> result = null;
    List<VariantInfo> helperVariants = null;

    for (ConverterHelper ch : Engine.getInstance().getRegisteredConverters()) {
      // List of variants that can be converted from the source class
      helperVariants = ch.getVariants(sourceClass);

      if (helperVariants != null) {
        // Loop over the variants list
        for (VariantInfo helperVariant : helperVariants) {
          if (targetVariant == null) {
            result = addVariant(result, helperVariant);
          } else if (helperVariant.includes(targetVariant)) {
            // Detected a more generic variant, but still consider
            // the conversion is possible to the target variant.
            result = addVariant(result, new VariantInfo(targetVariant.getMediaType()));
          } else if (targetVariant.includes(helperVariant)) {
            // Detected a more specific variant, but still consider
            // the conversion is possible to the target variant.
            result = addVariant(result, helperVariant);
          }
        }
      }
    }

    return result;
  }
 @Override
 public List<Class<?>> getObjectClasses(Variant source) {
   List<Class<?>> result = null;
   if (VARIANT_JSON.isCompatible(source)) {
     result = addObjectClass(result, Object.class);
     result = addObjectClass(result, JacksonRepresentation.class);
   }
   return result;
 }
  @Override
  public <T> float score(Representation source, Class<T> target, Resource resource) {
    float result = -1.0F;

    if (source instanceof JacksonRepresentation<?>) {
      result = 1.0F;
    } else if ((target != null) && JacksonRepresentation.class.isAssignableFrom(target)) {
      result = 1.0F;
    } else if (VARIANT_JSON.isCompatible(source)) {
      result = 0.8F;
    }

    return result;
  }
  private String presentations(SkysailResponse<List<?>> skysailResponse) {
    Set<String> mediaTypes = new HashSet<String>();
    List<ConverterHelper> registeredConverters = Engine.getInstance().getRegisteredConverters();
    for (ConverterHelper ch : registeredConverters) {
      List<VariantInfo> variants = ch.getVariants(skysailResponse.getClass());
      if (variants == null) {
        continue;
      }
      for (VariantInfo variantInfo : variants) {
        mediaTypes.add(variantInfo.getMediaType().getSubType());
      }
    }

    StringBuilder sb = new StringBuilder();
    sb.append("<ul>\n");
    for (String mediaType : mediaTypes) {
      sb.append("<li><a href='?media=").append(mediaType).append("'>");
      sb.append(mediaType).append("</a></li>\n");
    }

    // sb.append("<li><a href='?media=json'>Json</a></li>\n");
    sb.append("</ul>\n");
    return sb.toString();
  }