コード例 #1
0
ファイル: Conneg.java プロジェクト: florhaf/ravent
  /**
   * Scores a variant relatively to enriched client preferences.
   *
   * @param variant The variant to score.
   * @return The enriched client preferences.
   */
  public float scoreVariant(Variant variant) {
    float result = -1.0F;
    float languageScore = scoreLanguages(variant.getLanguages());

    if (languageScore != -1.0F) {
      float mediaTypeScore = scoreMediaType(variant.getMediaType());

      if (mediaTypeScore != -1.0F) {
        float characterSetScore = scoreCharacterSet(variant.getCharacterSet());

        if (characterSetScore != -1.0F) {
          float encodingScore = scoreEncodings(variant.getEncodings());

          if (encodingScore != -1.0F) {
            // Return the weighted average score
            result =
                ((languageScore * 4.0F)
                        + (mediaTypeScore * 3.0F)
                        + (characterSetScore * 2.0F)
                        + encodingScore)
                    / 9.0F;
          }
        }
      }
    }

    return result;
  }
コード例 #2
0
  /**
   * Indicates if the current variant includes the given variant.
   *
   * @param other The other variant.
   * @return True if the current variant includes the other.
   */
  public boolean includes(Variant other) {
    boolean result = other != null;

    // Compare the character set
    if (result) {
      result = (getCharacterSet() == null) || getCharacterSet().includes(other.getCharacterSet());
    }

    // Compare the media type
    if (result) {
      result = (getMediaType() == null) || getMediaType().includes(other.getMediaType());
    }

    // Compare the languages
    if (result) {
      result =
          (getLanguages().isEmpty())
              || getLanguages().contains(Language.ALL)
              || getLanguages().containsAll(other.getLanguages());
    }

    // Compare the encodings
    if (result) {
      result =
          (getEncodings().isEmpty())
              || getEncodings().contains(Encoding.ALL)
              || getEncodings().containsAll(other.getEncodings());
    }

    return result;
  }
コード例 #3
0
ファイル: ConverterUtils.java プロジェクト: florhaf/ravent
  /**
   * 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;
  }
コード例 #4
0
  private static void completeOperationProduces(
      Operation operation,
      MethodAnnotationInfo mai,
      ServerResource sr,
      MetadataService metadataService) {
    if (metadataService != null) {
      try {
        List<Variant> responseVariants =
            mai.getResponseVariants(metadataService, sr.getConverterService());

        if (responseVariants == null || responseVariants.isEmpty()) {
          if (mai.getJavaMethod().getReturnType() != Void.TYPE) {
            LOGGER.warning("Method has no response variant: " + mai.getJavaMethod());
          }
          return;
        }

        // une representation per variant ?
        for (Variant variant : responseVariants) {

          if (variant.getMediaType() == null) {
            LOGGER.warning("Variant has no media type: " + variant);
            continue;
          }

          operation.getProduces().add(variant.getMediaType().getName());
        }
      } catch (IOException e) {
        throw new ResourceException(e);
      }
    }
  }
コード例 #5
0
 /**
  * Constructor from a variant.
  *
  * @param variant The variant to copy.
  * @param modificationDate The modification date.
  * @param tag The tag.
  */
 public Representation(Variant variant, Date modificationDate, Tag tag) {
   setCharacterSet(variant.getCharacterSet());
   setEncodings(variant.getEncodings());
   setLocationRef(variant.getLocationRef());
   setLanguages(variant.getLanguages());
   setMediaType(variant.getMediaType());
   setModificationDate(modificationDate);
   setTag(tag);
 }
コード例 #6
0
ファイル: UsersResource.java プロジェクト: alphaville/jaqpot3
  @Override
  protected Representation get(Variant variant) throws ResourceException {
    ListUsers lister = new ListUsers();
    lister.setMode(ListUsers.ListUsersMode.BY_UID);

    DbListStreamPublisher publisher = new DbListStreamPublisher();
    publisher.setMedia(variant.getMediaType());
    publisher.setBaseUri(Configuration.getBaseUri().augment("user"));
    try {
      return publisher.process(lister);
    } catch (JaqpotException ex) {
      return errorReport(
          ex, "DbError", "Error while getting data from the DB", variant.getMediaType(), false);
    }
  }
コード例 #7
0
 @Override
 public float score(Object source, Variant target, Resource resource) {
   float result = -1.0F;
   if (!(source instanceof de.twenty11.skysail.common.responses.SkysailResponse)) {
     return 0.0F;
   }
   if (target.getMediaType().equals(MediaType.TEXT_HTML)) {
     result = 1.0F;
   } else if (target.getMediaType().equals(SkysailApplication.SKYSAIL_HTMLFORM_MEDIATYPE)) {
     result = 1.0F;
   } else {
     result = 0.5F;
   }
   return result;
 }
コード例 #8
0
  @Override
  public Representation get(Variant variant) throws ResourceException {
    verifyStudySiteExists();

    if (variant.getMediaType().equals(MediaType.TEXT_XML)) {
      return createXmlRepresentation(studySite);
    } else {
      return null;
    }
  }
コード例 #9
0
  public Representation represent(Variant variant) throws ResourceException {
    if (plugin == null) return getListing(variant);
    else {
      File file = new File(root + File.separator + "log" + File.separator + plugin);
      if (!file.exists()) throw new ResourceException(Status.CLIENT_ERROR_NOT_FOUND);

      try {
        return new InputRepresentation(new FileInputStream(file), variant.getMediaType());
      } catch (IOException e) {
        throw new ResourceException(Status.SERVER_ERROR_INTERNAL, e);
      }
    }
  }
コード例 #10
0
  /**
   * Indicates if the current variant is equal to the given variant.
   *
   * @param other The other variant.
   * @return True if the current variant includes the other.
   */
  @Override
  public boolean equals(Object other) {
    boolean result = (other instanceof Variant);

    if (result && (other != this)) {
      Variant otherVariant = (Variant) other;

      // Compare the character set
      if (result) {
        result =
            ((getCharacterSet() == null) && (otherVariant.getCharacterSet() == null)
                || (getCharacterSet() != null)
                    && getCharacterSet().equals(otherVariant.getCharacterSet()));
      }

      // Compare the media type
      if (result) {
        result =
            ((getMediaType() == null) && (otherVariant.getMediaType() == null)
                || (getMediaType() != null) && getMediaType().equals(otherVariant.getMediaType()));
      }

      // Compare the languages
      if (result) {
        result = getLanguages().equals(otherVariant.getLanguages());
      }

      // Compare the encodings
      if (result) {
        result = getEncodings().equals(otherVariant.getEncodings());
      }

      // Compare the location URI
      if (result) {
        result =
            ((getLocationRef() == null) && (otherVariant.getLocationRef() == null)
                || (getLocationRef() != null)
                    && getLocationRef().equals(otherVariant.getLocationRef()));
      }
    }

    return result;
  }
コード例 #11
0
  private Representation getListing(Variant variant) throws ResourceException {
    final File folder = new File(root + File.separator + "log");
    final String[] list = folder.list();

    final Document document = BaseDocumentUtils.impl.newDocument();
    final Element root = document.createElement("root");

    if (list != null) {
      for (String file : list)
        root.appendChild(BaseDocumentUtils.impl.createElementWithText(document, "plugin", file));
    }

    document.appendChild(root);

    return new DomRepresentation(variant.getMediaType(), document);
  }
コード例 #12
0
  /**
   * Returns the list of representation variants associated to a given annotation value,
   * corresponding to either an input or output entity.
   *
   * @param metadataService The metadata service to use.
   * @param annotationValue The entity annotation value.
   * @return A list of variants.
   */
  private List<Variant> getVariants(MetadataService metadataService, String annotationValue) {
    List<Variant> result = null;

    if (annotationValue != null) {
      String[] variants = annotationValue.split("\\|");

      for (String variantValue : variants) {
        Variant variant = null;
        String[] extensions = variantValue.split("\\+");
        List<MediaType> mediaTypes = null;
        List<Language> languages = null;
        CharacterSet characterSet = null;

        for (String extension : extensions) {
          if (extension != null) {
            List<Metadata> metadataList = metadataService.getAllMetadata(extension);

            if (metadataList != null) {
              for (Metadata metadata : metadataList) {
                if (metadata instanceof MediaType) {
                  if (mediaTypes == null) {
                    mediaTypes = new ArrayList<MediaType>();
                  }

                  mediaTypes.add((MediaType) metadata);
                } else if (metadata instanceof Language) {
                  if (languages == null) {
                    languages = new ArrayList<Language>();
                  }

                  languages.add((Language) metadata);
                } else if (metadata instanceof CharacterSet) {
                  if (characterSet == null) {
                    characterSet = (CharacterSet) metadata;
                  } else {
                    Context.getCurrentLogger()
                        .warning(
                            "A representation variant can have only one character set. Please check your annotation value.");
                  }
                }
              }
            }
          }
        }

        // Now build the representation variants
        if (mediaTypes != null) {
          for (MediaType mediaType : mediaTypes) {
            if ((result == null) || (!result.contains(mediaType))) {
              if (result == null) {
                result = new ArrayList<Variant>();
              }

              variant = new Variant(mediaType);

              if (languages != null) {
                variant.getLanguages().addAll(languages);
              }

              if (characterSet != null) {
                variant.setCharacterSet(characterSet);
              }

              result.add(variant);
            }
          }
        }
      }
    }

    return result;
  }
コード例 #13
0
 /**
  * Indicates if the current variant is compatible with the given variant.
  *
  * @param other The other variant.
  * @return True if the current variant is compatible with the other.
  */
 public boolean isCompatible(Variant other) {
   return (other != null) && (includes(other) || other.includes(this));
 }
コード例 #14
0
 /**
  * Constructor with a variant.
  *
  * @param variant The variant to describe.
  */
 public RepresentationInfo(Variant variant) {
   setMediaType(variant.getMediaType());
 }
コード例 #15
0
  private static void completeOperationInput(
      CollectInfo collectInfo,
      Operation operation,
      MethodAnnotationInfo mai,
      ServerResource sr,
      List<? extends IntrospectionHelper> introspectionHelper,
      MetadataService metadataService) {
    Class<?>[] inputClasses = mai.getJavaMethod().getParameterTypes();
    if (inputClasses != null && inputClasses.length > 0) {

      // Input representation
      // Handles only the first method parameter
      TypeInfo inputTypeInfo;
      try {
        inputTypeInfo =
            Types.getTypeInfo(inputClasses[0], mai.getJavaMethod().getGenericParameterTypes()[0]);
      } catch (UnsupportedTypeException e) {
        LOGGER.warning(
            "Could not add input representation of method"
                + mai.getJavaMethod()
                + ". "
                + e.getMessage());
        return;
      }

      RepresentationCollector.addRepresentation(collectInfo, inputTypeInfo, introspectionHelper);

      PayLoad inputEntity = new PayLoad();
      inputEntity.setType(inputTypeInfo.getRepresentationName());
      inputEntity.setArray(inputTypeInfo.isList());
      operation.setInputPayLoad(inputEntity);

      // Consumes
      if (metadataService != null) {

        try {
          List<Variant> requestVariants =
              mai.getRequestVariants(metadataService, sr.getConverterService());

          if (requestVariants == null || requestVariants.isEmpty()) {
            LOGGER.warning(
                "Could not add consumes of method "
                    + mai.getJavaMethod()
                    + ". There is no requested variant");
            return;
          }

          // une representation per variant ?
          for (Variant variant : requestVariants) {

            if (variant.getMediaType() == null) {
              LOGGER.warning("Variant has no media type: " + variant);
              continue;
            }

            operation.getConsumes().add(variant.getMediaType().getName());
          }
        } catch (IOException e) {
          throw new ResourceException(e);
        }
      }
    }
  }