Introduction

Pah is a Java library for handling the HTTP Accept header. It allows code that supports multiple content types to determine which, if any, is most appropriate in response to a particular Accept header, in accordance with RFC 2616. It is open source, and free for you to use.

The latest version of Pah available for download is 1.6. The javadoc is also available online.

Example

A piece of code that's capable of generating text/html and text/x-c can determine which of the two should be returned when it receives the accept header text/plain;q=0.3, text/html;q=0.7 as follows:

import net.sourceforge.pah.AcceptHeader; import static java.util.Arrays.asList; import static net.sourceforge.pah.MediaType.mediaType; ... List<MediaType> availableTypes = asList( mediaType("text", "html"), mediaType("text", "x-c") ); List<MediaType> suitableTypes = AcceptHeader.sortedByAcceptHeaderPreference( "text/plain;q=0.3, text/html;q=0.7", availableTypes );

This results in suitableTypes containing only text/html because this is the only available media type that satisfies the header.

Using the same collection of available media types, the code handles an accept header that cannot be satisfied, such as text/plain as follows:

List<MediaType> noSuitableTypes = AcceptHeader.sortedByAcceptHeaderPreference( "text/plain", availableTypes );

This results in an empty List of media types, indicating that none of the available media types is suitable

Finally, if multiple available media types are equally acceptable given a particular accept header, the order in which they are supplied to the method is used. For example, using the same collection of available media types again, the accept header */* is handled as follows:

List<MediaType> allSuitableTypes = AcceptHeader.sortedByAcceptHeaderPreference( "*/*", availableTypes );

The List allSuitableTypes contains text/html and text/x-c in that order, because both available types are equally acceptable responses to the accept header, so Pah has fallen back to ordering them in the order they were supplied - in other words, the order in which the available media types is supplied is considered to be the server's order of preference, but this is only taken into account if more than one available media type has the same preference according to the accept header.