org.mule.module.apikit
Class RestContentTypeParser

java.lang.Object
  extended by org.mule.module.apikit.RestContentTypeParser

public final class RestContentTypeParser
extends Object

MIME-Type Parser

This class provides basic functions for handling mime-types. It can handle matching mime-types against a list of media-ranges. See section 14.1 of the HTTP specification [RFC 2616] for a complete explanation.

http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.1

A port to Java of Joe Gregorio's MIME-Type Parser:

http://code.google.com/p/mimeparse/


Nested Class Summary
protected static class RestContentTypeParser.FitnessAndQuality
          Structure for holding a fitness/quality combo
protected static class RestContentTypeParser.ParseResults
          Parse results container
 
Method Summary
static com.google.common.net.MediaType bestMatch(List<org.raml.model.MimeType> supportedRepresentations, String header)
          Takes a list of supportedRepresentations mime-types and finds the best match for all the media-ranges listed in header.
protected static RestContentTypeParser.FitnessAndQuality fitnessAndQualityParsed(com.google.common.net.MediaType target, List<com.google.common.net.MediaType> parsedRanges)
          Deprecated. used by apikit1 only
protected static RestContentTypeParser.FitnessAndQuality fitnessAndQualityParsed(String mimeType, Collection<RestContentTypeParser.ParseResults> parsedRanges)
          Find the best match for a given mimeType against a list of media_ranges that have already been parsed by MimeParse.parseMediaRange().
static boolean isMediaTypeAcceptable(List<com.google.common.net.MediaType> acceptContentTypes, com.google.common.net.MediaType mediaType)
           
protected static RestContentTypeParser.ParseResults parseMediaRange(String range)
          Carves up a media range and returns a ParseResults.
protected static RestContentTypeParser.ParseResults parseMimeType(String mimeType)
          Carves up a mime-type and returns a ParseResults object

For example, the media range 'application/xhtml;q=0.5' would get parsed into:

('application', 'xhtml', {'q', '0.5'})

static float quality(com.google.common.net.MediaType mimeType, List<com.google.common.net.MediaType> ranges)
          Returns the quality 'q' of a mime-type when compared against the mediaRanges in ranges.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

parseMimeType

protected static RestContentTypeParser.ParseResults parseMimeType(String mimeType)
Carves up a mime-type and returns a ParseResults object

For example, the media range 'application/xhtml;q=0.5' would get parsed into:

('application', 'xhtml', {'q', '0.5'})


parseMediaRange

protected static RestContentTypeParser.ParseResults parseMediaRange(String range)
Carves up a media range and returns a ParseResults.

For example, the media range 'application/*;q=0.5' would get parsed into:

('application', '*', {'q', '0.5'})

In addition this function also guarantees that there is a value for 'q' in the params dictionary, filling it in with a proper default if necessary.

Parameters:
range -

fitnessAndQualityParsed

protected static RestContentTypeParser.FitnessAndQuality fitnessAndQualityParsed(com.google.common.net.MediaType target,
                                                                                 List<com.google.common.net.MediaType> parsedRanges)
Deprecated. used by apikit1 only

Find the best match for a given mimeType against a list of media_ranges that have already been parsed by MimeParse.parseMediaRange(). Returns a tuple of the fitness value and the value of the 'q' quality parameter of the best match, or (-1, 0) if no match was found. Just as for quality_parsed(), 'parsed_ranges' must be a list of parsed media ranges.

Parameters:
target -
parsedRanges -

fitnessAndQualityParsed

protected static RestContentTypeParser.FitnessAndQuality fitnessAndQualityParsed(String mimeType,
                                                                                 Collection<RestContentTypeParser.ParseResults> parsedRanges)
Find the best match for a given mimeType against a list of media_ranges that have already been parsed by MimeParse.parseMediaRange(). Returns a tuple of the fitness value and the value of the 'q' quality parameter of the best match, or (-1, 0) if no match was found. Just as for quality_parsed(), 'parsed_ranges' must be a list of parsed media ranges.

Parameters:
mimeType -
parsedRanges -

quality

public static float quality(com.google.common.net.MediaType mimeType,
                            List<com.google.common.net.MediaType> ranges)
Returns the quality 'q' of a mime-type when compared against the mediaRanges in ranges. For example:

Parameters:
mimeType -

bestMatch

public static com.google.common.net.MediaType bestMatch(List<org.raml.model.MimeType> supportedRepresentations,
                                                        String header)
Takes a list of supportedRepresentations mime-types and finds the best match for all the media-ranges listed in header. The value of header must be a string that conforms to the format of the HTTP Accept: header. The value of 'supportedRepresentations' is a list of mime-types.

MimeParse.bestMatch(Arrays.asList(new String[]{"application/xbel+xml", "text/xml"}), "text/*;q=0.5,*; q=0.1") 'text/xml'

Parameters:
supportedRepresentations -
header -
Returns:

isMediaTypeAcceptable

public static boolean isMediaTypeAcceptable(List<com.google.common.net.MediaType> acceptContentTypes,
                                            com.google.common.net.MediaType mediaType)


Copyright © 2013. All rights reserved.