public static String selectPreferredMIMEType(Iterator<String> mimeTypes,HttpServletRequest request){
  List<HeaderElement> acceptElements=getHeaderElements(request,"Accept");
  if (acceptElements.isEmpty()) {
    if (mimeTypes.hasNext()) {
      return mimeTypes.next();
    }
 else {
      return null;
    }
  }
  String result=null;
  HeaderElement matchingAcceptType=null;
  double highestQuality=0.0;
  while (mimeTypes.hasNext()) {
    String mimeType=mimeTypes.next();
    HeaderElement acceptType=matchAcceptHeader(mimeType,acceptElements);
    if (acceptType != null) {
      double quality=1.0;
      String qualityStr=acceptType.getParameterValue("q");
      if (qualityStr != null) {
        try {
          quality=Double.parseDouble(qualityStr);
        }
 catch (        NumberFormatException e) {
        }
      }
      if (quality > highestQuality) {
        result=mimeType;
        matchingAcceptType=acceptType;
        highestQuality=quality;
      }
 else       if (quality == highestQuality) {
        if (isMoreSpecificType(acceptType,matchingAcceptType)) {
          result=mimeType;
          matchingAcceptType=acceptType;
        }
      }
    }
  }
  return result;
}

public static String selectPreferredMIMEType ( Iterator < String > mimeTypes , HttpServletRequest request ) { List < HeaderElement > acceptElements = getHeaderElements ( request , "Accept" ) ; if ( acceptElements . isEmpty ( ) ) { if ( mimeTypes . hasNext ( ) ) { return mimeTypes . next ( ) ; } else { return null ; } } String result = null ; HeaderElement matchingAcceptType = null ; double highestQuality = 0.0 ; while ( mimeTypes . hasNext ( ) ) { String mimeType = mimeTypes . next ( ) ; HeaderElement acceptType = matchAcceptHeader ( mimeType , acceptElements ) ; if ( acceptType != null ) { double quality = 1.0 ; String qualityStr = acceptType . getParameterValue ( "q" ) ; if ( qualityStr != null ) { try { quality = Double . parseDouble ( qualityStr ) ; } catch ( NumberFormatException e ) { } } if ( quality > highestQuality ) { result = mimeType ; matchingAcceptType = acceptType ; highestQuality = quality ; } else if ( quality == highestQuality ) { if ( isMoreSpecificType ( acceptType , matchingAcceptType ) ) { result = mimeType ; matchingAcceptType = acceptType ; } } } } return result ; }
selects from a set of mime types , the mime type that has the highest quality score when matched with the accept headers in the supplied request .
