org.restlet.data.Preference.getMetadata()方法的使用及代码示例

x33g5p2x  于2022-01-26 转载在 其他  
字(11.5k)|赞(0)|评价(0)|浏览(84)

本文整理了Java中org.restlet.data.Preference.getMetadata()方法的一些代码示例,展示了Preference.getMetadata()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Preference.getMetadata()方法的具体详情如下:
包路径:org.restlet.data.Preference
类名称:Preference
方法名:getMetadata

Preference.getMetadata介绍

[英]Returns the metadata associated with this preference.
[中]返回与此首选项关联的元数据。

代码示例

代码示例来源:origin: internetarchive/heritrix3

/**
 * If client can accept text/html, always prefer it. WebKit-based browsers
 * claim to want application/xml, but we don't want to give it to them. See
 * <a href="https://webarchive.jira.com/browse/HER-1603">https://webarchive.jira.com/browse/HER-1603</a>
 */
public Variant getPreferredVariant() {
  boolean addExplicitTextHtmlPreference = false;
  for (Preference<MediaType> mediaTypePreference: getRequest().getClientInfo().getAcceptedMediaTypes()) {
    if (mediaTypePreference.getMetadata().equals(MediaType.TEXT_HTML)) {
      mediaTypePreference.setQuality(Float.MAX_VALUE);
      addExplicitTextHtmlPreference = false;
      break;
    } else if (mediaTypePreference.getMetadata().includes(MediaType.TEXT_HTML)) {
      addExplicitTextHtmlPreference = true;
    }
  }
  
  if (addExplicitTextHtmlPreference) {
    List<Preference<MediaType>> acceptedMediaTypes = getRequest().getClientInfo().getAcceptedMediaTypes();
    acceptedMediaTypes.add(new Preference<MediaType>(MediaType.TEXT_HTML, Float.MAX_VALUE));
    getRequest().getClientInfo().setAcceptedMediaTypes(acceptedMediaTypes);
  }
  
  
  return super.getPreferredVariant();
}

代码示例来源:origin: org.restlet/org.restlet

@Override
  public String toString() {
    return (getMetadata() == null) ? ""
        : (getMetadata().getName() + ":" + getQuality());
  }
}

代码示例来源:origin: DeviceConnect/DeviceConnect-Android

@Override
  public String toString() {
    return (getMetadata() == null) ? ""
        : (getMetadata().getName() + ":" + getQuality());
  }
}

代码示例来源:origin: org.restlet.osgi/org.restlet

@Override
  public String toString() {
    return (getMetadata() == null) ? ""
        : (getMetadata().getName() + ":" + getQuality());
  }
}

代码示例来源:origin: org.restlet.jee/org.restlet.ext.jaxrs

/**
 * Creates a new SortedMetadata from the given Metadata.
 * 
 * @param preferences
 */
@SuppressWarnings({ "unchecked", "rawtypes" })
private SortedMetadata(Collection<Preference<T>> preferences) {
  SortedMap<Float, Collection<T>> map = new TreeMap<Float, Collection<T>>(
      Collections.reverseOrder());
  for (Preference<T> preference : preferences) {
    Float quality = preference.getQuality();
    Collection<T> metadatas = map.get(quality);
    if (metadatas == null) {
      metadatas = new ArrayList<T>(2);
      map.put(quality, metadatas);
    }
    metadatas.add(preference.getMetadata());
  }
  Collection<Collection<T>> values = map.values();
  this.metadatas = Collections.unmodifiableList(new ArrayList(values));
}

代码示例来源:origin: ontopia/ontopia

/**
 * Blocks the use of specified mime types for this resource, as it is known that the converter for that mime type
 * cannot produce the representation for the Resource's target class.
 * @param types The mime types to block
 */
protected void blockMimeType(MediaType... types) {
  List<Preference<MediaType>> acceptedMediaTypes = getClientInfo().getAcceptedMediaTypes();
  if (acceptedMediaTypes.size() > types.length) {
    return;
  }    

  Set<MediaType> accepted = new HashSet<>(acceptedMediaTypes.size());
  for (Preference<MediaType> p : acceptedMediaTypes) {
    accepted.add(p.getMetadata());
  }
  
  accepted.removeAll(Arrays.asList(types));
  
  if (accepted.isEmpty()) {
    throw OntopiaRestErrors.UNSUPPORTED_MIME_TYPE.build(getClass().getName(), Arrays.toString(types));
  }
}

代码示例来源:origin: apache/attic-polygene-java

protected Locale getLocale()
{
  Request request = Request.getCurrent();
  List<Preference<Language>> preferenceList = request.getClientInfo().getAcceptedLanguages();
  if( preferenceList.isEmpty() )
  {
    return Locale.getDefault();
  }
  Language language = preferenceList
    .get( 0 ).getMetadata();
  String[] localeStr = language.getName().split( "-" );
  Locale locale;
  switch( localeStr.length )
  {
  case 1:
    locale = new Locale( localeStr[ 0 ] );
    break;
  case 2:
    locale = new Locale( localeStr[ 0 ], localeStr[ 1 ] );
    break;
  case 3:
    locale = new Locale( localeStr[ 0 ], localeStr[ 1 ], localeStr[ 2 ] );
    break;
  default:
    locale = Locale.getDefault();
  }
  return locale;
}

代码示例来源:origin: org.apache.polygene.libraries/org.apache.polygene.library.rest-server

protected Locale getLocale()
{
  Request request = Request.getCurrent();
  List<Preference<Language>> preferenceList = request.getClientInfo().getAcceptedLanguages();
  if( preferenceList.isEmpty() )
  {
    return Locale.getDefault();
  }
  Language language = preferenceList
    .get( 0 ).getMetadata();
  String[] localeStr = language.getName().split( "-" );
  Locale locale;
  switch( localeStr.length )
  {
  case 1:
    locale = new Locale( localeStr[ 0 ] );
    break;
  case 2:
    locale = new Locale( localeStr[ 0 ], localeStr[ 1 ] );
    break;
  case 3:
    locale = new Locale( localeStr[ 0 ], localeStr[ 1 ], localeStr[ 2 ] );
    break;
  default:
    locale = Locale.getDefault();
  }
  return locale;
}

代码示例来源:origin: org.qi4j.library/org.qi4j.library.rest-server

protected Locale getLocale()
{
  Request request = Request.getCurrent();
  List<Preference<Language>> preferenceList = request.getClientInfo().getAcceptedLanguages();
  if( preferenceList.isEmpty() )
  {
    return Locale.getDefault();
  }
  Language language = preferenceList
    .get( 0 ).getMetadata();
  String[] localeStr = language.getName().split( "-" );
  Locale locale;
  switch( localeStr.length )
  {
  case 1:
    locale = new Locale( localeStr[ 0 ] );
    break;
  case 2:
    locale = new Locale( localeStr[ 0 ], localeStr[ 1 ] );
    break;
  case 3:
    locale = new Locale( localeStr[ 0 ], localeStr[ 1 ], localeStr[ 2 ] );
    break;
  default:
    locale = Locale.getDefault();
  }
  return locale;
}

代码示例来源:origin: ontopia/ontopia

@Override
protected void doError(Status status) {
  if (status.equals(Status.CLIENT_ERROR_NOT_ACCEPTABLE) && (status.getThrowable() == null)) {
    // we have an ontopia error for this
    List<Preference<MediaType>> acceptedMediaTypes = getClientInfo().getAcceptedMediaTypes();
    List<MediaType> accepted = new ArrayList<>(acceptedMediaTypes.size());
    for (Preference<MediaType> mt : acceptedMediaTypes) {
      accepted.add(mt.getMetadata());
    }
    
    super.doError(OntopiaRestErrors.UNSUPPORTED_MIME_TYPE.build(getClass().getName(), Arrays.toString(accepted.toArray())).getStatus());
  } else {
    super.doError(status);
  }
}

代码示例来源:origin: org.restlet.osgi/org.restlet

/**
 * Scores a list of metadata relatively to enriched client preferences.
 * 
 * @param metadataList
 *            The list of metadata to score.
 * @return The score.
 */
protected <T extends Metadata> float scoreMetadata(List<T> metadataList,
    List<Preference<T>> prefs) {
  float result = -1.0F;
  float current;
  if ((metadataList != null) && !metadataList.isEmpty()) {
    for (Preference<T> pref : prefs) {
      for (T metadata : metadataList) {
        if (pref.getMetadata().includes(metadata)) {
          current = pref.getQuality();
        } else {
          current = -1.0F;
        }
        if (current > result) {
          result = current;
        }
      }
    }
  } else {
    result = 0.0F;
  }
  return result;
}

代码示例来源:origin: org.restlet.osgi/org.restlet

/**
 * Scores a metadata relatively to enriched client preferences.
 * 
 * @param metadata
 *            The metadata to score.
 * @return The score.
 */
protected <T extends Metadata> float scoreMetadata(T metadata,
    List<Preference<T>> prefs) {
  float result = -1.0F;
  float current;
  if (metadata != null) {
    for (Preference<? extends Metadata> pref : prefs) {
      if (pref.getMetadata().includes(metadata)) {
        current = pref.getQuality();
      } else {
        current = -1.0F;
      }
      if (current > result) {
        result = current;
      }
    }
  } else {
    result = 0.0F;
  }
  return result;
}

代码示例来源:origin: org.restlet.osgi/org.restlet

/**
 * Returns the best supported encoding for a given client.
 * 
 * @param client
 *            The client preferences to use.
 * @return The best supported encoding for the given call.
 */
public Encoding getBestEncoding(ClientInfo client) {
  Encoding bestEncoding = null;
  Encoding currentEncoding = null;
  Preference<Encoding> currentPref = null;
  float bestScore = 0F;
  for (Iterator<Encoding> iter = getSupportedEncodings().iterator(); iter
      .hasNext();) {
    currentEncoding = iter.next();
    for (Iterator<Preference<Encoding>> iter2 = client
        .getAcceptedEncodings().iterator(); iter2.hasNext();) {
      currentPref = iter2.next();
      if (currentPref.getMetadata().equals(Encoding.ALL)
          || currentPref.getMetadata().equals(currentEncoding)) {
        // A match was found, compute its score
        if (currentPref.getQuality() > bestScore) {
          bestScore = currentPref.getQuality();
          bestEncoding = currentEncoding;
        }
      }
    }
  }
  return bestEncoding;
}

代码示例来源:origin: org.restlet.osgi/org.restlet

preference = preferences.get(i);
if (preference.getMetadata().equals(mediaType)
    && (preference.getQuality() < score)) {
  preference.setQuality(score);

代码示例来源:origin: org.restlet.osgi/org.restlet

undesired = new ArrayList<T>();
    undesired.add(pref.getMetadata());
for (int i = 0; i < result.size(); i++) {
  Preference<T> userPref = result.get(i);
  parent = (T) userPref.getMetadata().getParent();
  if (result.get(i).getMetadata().equals(allValue)) {
    result.remove(i);

代码示例来源:origin: org.restlet.osgi/org.restlet

/**
 * Returns the preferred metadata taking into account both metadata
 * supported by the server and client preferences.
 * 
 * @param supported
 *            The metadata supported by the server.
 * @param preferences
 *            The client preferences.
 * @return The preferred metadata.
 */
public static <T extends Metadata> T getPreferredMetadata(
    List<T> supported, List<Preference<T>> preferences) {
  T result = null;
  float maxQuality = 0;
  if (supported != null) {
    for (Preference<T> pref : preferences) {
      for (T metadata : supported) {
        if (pref.getMetadata().isCompatible(metadata)
            && (pref.getQuality() > maxQuality)) {
          result = metadata;
          maxQuality = pref.getQuality();
        }
      }
    }
  }
  return result;
}

代码示例来源:origin: org.restlet.osgi/org.restlet

/**
 * Scores a media type relatively to enriched client preferences.
 * 
 * @param mediaType
 *            The media type to score.
 * @return The score.
 */
public float scoreMediaType(MediaType mediaType) {
  float result = -1.0F;
  float current;
  if (mediaType != null) {
    for (Preference<MediaType> pref : getMediaTypePrefs()) {
      if (pref.getMetadata().includes(mediaType, false)) {
        current = pref.getQuality();
      } else {
        current = -1.0F;
      }
      if (current > result) {
        result = current;
      }
    }
  } else {
    result = 0.0F;
  }
  return result;
}

代码示例来源:origin: org.geoserver/rest

for ( Iterator<Preference<MediaType>> i = accepts.iterator(); i.hasNext(); ) {
  Preference<MediaType> pref = i.next();
  if ( pref.getMetadata().equals( MediaType.ALL ) ) {
    acceptsAll = true;
    continue;
  df = lookupFormat( pref.getMetadata() ); 
  if ( df != null ) {
    break;

代码示例来源:origin: org.archive.heritrix/heritrix-engine

/**
 * If client can accept text/html, always prefer it. WebKit-based browsers
 * claim to want application/xml, but we don't want to give it to them. See
 * <a href="https://webarchive.jira.com/browse/HER-1603">https://webarchive.jira.com/browse/HER-1603</a>
 */
public Variant getPreferredVariant() {
  boolean addExplicitTextHtmlPreference = false;
  for (Preference<MediaType> mediaTypePreference: getRequest().getClientInfo().getAcceptedMediaTypes()) {
    if (mediaTypePreference.getMetadata().equals(MediaType.TEXT_HTML)) {
      mediaTypePreference.setQuality(Float.MAX_VALUE);
      addExplicitTextHtmlPreference = false;
      break;
    } else if (mediaTypePreference.getMetadata().includes(MediaType.TEXT_HTML)) {
      addExplicitTextHtmlPreference = true;
    }
  }
  
  if (addExplicitTextHtmlPreference) {
    List<Preference<MediaType>> acceptedMediaTypes = getRequest().getClientInfo().getAcceptedMediaTypes();
    acceptedMediaTypes.add(new Preference<MediaType>(MediaType.TEXT_HTML, Float.MAX_VALUE));
    getRequest().getClientInfo().setAcceptedMediaTypes(acceptedMediaTypes);
  }
  
  
  return super.getPreferredVariant();
}

代码示例来源:origin: org.restlet.osgi/org.restlet

@Override
public PreferenceWriter append(Preference<?> pref) {
  append(pref.getMetadata().getName());
  if (pref.getQuality() < 1F) {
    append(";q=");
    appendQuality(pref.getQuality());
  }
  if (pref.getParameters() != null) {
    Parameter param;
    for (Iterator<Parameter> iter = pref.getParameters().iterator(); iter
        .hasNext();) {
      param = iter.next();
      if (param.getName() != null) {
        append(';').append(param.getName());
        if ((param.getValue() != null)
            && (param.getValue().length() > 0)) {
          append('=').append(param.getValue());
        }
      }
    }
  }
  return this;
}

相关文章