Beispiel #1
0
  /**
   * Constructor.
   *
   * @param clientInfo
   * @param metadataService
   */
  public Conneg(ClientInfo clientInfo, MetadataService metadataService) {
    this.clientInfo = clientInfo;
    this.metadataService = metadataService;

    if (clientInfo != null) {
      // Get the enriched user preferences
      this.languagePrefs =
          getEnrichedPreferences(
              clientInfo.getAcceptedLanguages(),
              (metadataService == null) ? null : metadataService.getDefaultLanguage(),
              Language.ALL);
      this.mediaTypePrefs =
          getEnrichedPreferences(
              clientInfo.getAcceptedMediaTypes(),
              (metadataService == null) ? null : metadataService.getDefaultMediaType(),
              MediaType.ALL);
      this.characterSetPrefs =
          getEnrichedPreferences(
              clientInfo.getAcceptedCharacterSets(),
              (metadataService == null) ? null : metadataService.getDefaultCharacterSet(),
              CharacterSet.ALL);
      this.encodingPrefs =
          getEnrichedPreferences(
              clientInfo.getAcceptedEncodings(),
              (metadataService == null) ? null : metadataService.getDefaultEncoding(),
              Encoding.ALL);
    }
  }
  /**
   * Creates a {@link ClientInfo} instance with preferences matching exactly the current variant.
   *
   * @return The new {@link ClientInfo} instance.
   */
  public ClientInfo createClientInfo() {
    ClientInfo result = new ClientInfo();

    if (getCharacterSet() != null) {
      result.getAcceptedCharacterSets().add(new Preference<CharacterSet>(getCharacterSet()));
    }

    if (getEncodings() != null) {
      for (Encoding encoding : getEncodings()) {
        result.getAcceptedEncodings().add(new Preference<Encoding>(encoding));
      }
    }

    if (getLanguages() != null) {
      for (Language language : getLanguages()) {
        result.getAcceptedLanguages().add(new Preference<Language>(language));
      }
    }

    if (getMediaType() != null) {
      result.getAcceptedMediaTypes().add(new Preference<MediaType>(getMediaType()));
    }

    return result;
  }
  public void testAccMediaType() throws IOException {
    Response response = get("accMediaTypes", MediaType.TEXT_PLAIN);
    assertEquals(Status.SUCCESS_OK, response.getStatus());
    assertEquals("[" + MediaType.TEXT_PLAIN.toString() + "]", response.getEntity().getText());

    ClientInfo clientInfo = new ClientInfo();
    clientInfo.getAcceptedMediaTypes().add(new Preference<MediaType>(MediaType.TEXT_PLAIN, 0.5f));
    clientInfo.getAcceptedMediaTypes().add(new Preference<MediaType>(MediaType.TEXT_HTML, 0.8f));
    clientInfo.getAcceptedMediaTypes().add(new Preference<MediaType>(MediaType.TEXT_XML, 0.2f));
    response = get("accMediaTypes", clientInfo);
    assertEquals(Status.SUCCESS_OK, response.getStatus());
    assertEquals(
        "["
            + MediaType.TEXT_HTML.toString()
            + ", "
            + MediaType.TEXT_PLAIN.toString()
            + ", "
            + MediaType.TEXT_XML.toString()
            + "]",
        response.getEntity().getText());
  }
Beispiel #4
0
  /**
   * Handles an object entity. Automatically serializes the object using the {@link
   * org.restlet.service.ConverterService}.
   *
   * @param method The request method to use.
   * @param entity The object entity to post.
   * @param resultClass The class of the response entity.
   * @return The response object entity.
   * @throws ResourceException
   */
  private <T> T handle(Method method, Object entity, Class<T> resultClass)
      throws ResourceException {
    T result = null;
    org.restlet.service.ConverterService cs = getConverterService();

    ClientInfo clientInfo = getClientInfo();

    if (clientInfo.getAcceptedMediaTypes().isEmpty()) {
      cs.updatePreferences(clientInfo.getAcceptedMediaTypes(), resultClass);
    }

    Representation requestEntity = null;

    if (entity != null) {
      List<? extends Variant> variants = cs.getVariants(entity.getClass(), null);
      requestEntity =
          toRepresentation(entity, clientInfo.getPreferredVariant(variants, getMetadataService()));
    }

    result = toObject(handle(method, requestEntity, clientInfo), resultClass);
    return result;
  }