Search in sources :

Example 1 with AcceptList

use of org.apache.jena.atlas.web.AcceptList in project jena by apache.

the class ConNeg method match.

/**
 * Match a single media type against a header string.
 *
 * @param headerString HTTP header string
 * @param mediaRangeStr Semi-colon separated list of media types
 * @return the matched media type or <code>null</code> if there was no match
 */
public static String match(String headerString, String mediaRangeStr) {
    AcceptList l = new AcceptList(headerString);
    // MediaType
    MediaRange aItem = new MediaRange(mediaRangeStr);
    MediaType m = l.match(aItem);
    if (m == null)
        return null;
    return m.toHeaderString();
}
Also used : MediaType(org.apache.jena.atlas.web.MediaType) MediaRange(org.apache.jena.atlas.web.MediaRange) AcceptList(org.apache.jena.atlas.web.AcceptList)

Example 2 with AcceptList

use of org.apache.jena.atlas.web.AcceptList in project jena by apache.

the class ConNeg method choose.

/**
 * <p>
 * This method receives a HTTP header string, an {@link AcceptList} and a default
 * {@link MediaType}.
 * </p>
 * <p>
 * If the header string is null, it returns the given default MediaType.
 * </p>
 * <p>
 * Otherwise it builds an {@link AcceptList} object with the header string and
 * uses it to match against the given MediaType.
 * </p>
 *
 * @param headerString HTTP header string
 * @param myPrefs accept list
 * @param defaultMediaType default media type
 * @return a media type or <code>null</code> if none matched or if the HTTP
 *     header string and the default media type are <code>null</code>.
 */
private static MediaType choose(String headerString, AcceptList myPrefs, MediaType defaultMediaType) {
    if (headerString == null)
        return defaultMediaType;
    AcceptList headerList = new AcceptList(headerString);
    if (myPrefs == null) {
        MediaType i = headerList.first();
        if (i == null)
            return defaultMediaType;
        return i;
    }
    MediaType i = AcceptList.match(headerList, myPrefs);
    if (i == null)
        return defaultMediaType;
    return i;
}
Also used : MediaType(org.apache.jena.atlas.web.MediaType) AcceptList(org.apache.jena.atlas.web.AcceptList)

Example 3 with AcceptList

use of org.apache.jena.atlas.web.AcceptList in project jena by apache.

the class ConNeg method choose.

/**
     * <p>This method receives a HTTP header string, an {@link AcceptList} and a
     * default {@link MediaType}.</p>
     *
     * <p>If the header string is null, it returns the given default MediaType.</p>
     *
     * <p>Otherwise it builds an {@link AcceptList} object with the header string
     * and uses it to match against the given MediaType.</p>
     *
     * @param headerString HTTP header string
     * @param myPrefs accept list
     * @param defaultMediaType default media type
     * @return a media type or <code>null</code> if none matched or if the
     *          HTTP header string and the default media type are <code>null</code>.
     */
private static MediaType choose(String headerString, AcceptList myPrefs, MediaType defaultMediaType) {
    if (headerString == null)
        return defaultMediaType;
    AcceptList headerList = new AcceptList(headerString);
    if (myPrefs == null) {
        MediaType i = headerList.first();
        if (i == null)
            return defaultMediaType;
        return i;
    }
    MediaType i = AcceptList.match(headerList, myPrefs);
    if (i == null)
        return defaultMediaType;
    return i;
}
Also used : MediaType(org.apache.jena.atlas.web.MediaType) AcceptList(org.apache.jena.atlas.web.AcceptList)

Example 4 with AcceptList

use of org.apache.jena.atlas.web.AcceptList in project jena by apache.

the class ConNeg method match.

/**
     * Match a single media type against a header string.
     *
     * @param headerString HTTP header string
     * @param mediaRangeStr Semi-colon separated list of media types
     * @return the matched media type or <code>null</code> if there was no match
     */
public static String match(String headerString, String mediaRangeStr) {
    AcceptList l = new AcceptList(headerString);
    // MediaType
    MediaRange aItem = new MediaRange(mediaRangeStr);
    MediaType m = l.match(aItem);
    if (m == null)
        return null;
    return m.toHeaderString();
}
Also used : MediaType(org.apache.jena.atlas.web.MediaType) MediaRange(org.apache.jena.atlas.web.MediaRange) AcceptList(org.apache.jena.atlas.web.AcceptList)

Example 5 with AcceptList

use of org.apache.jena.atlas.web.AcceptList in project jena by apache.

the class TestContentNegotiation method testMatch.

// HTTP: RDF
//See WebContent.defaultGraphAcceptHeader, defaultDatasetAcceptHeader, defaultRDFAcceptHeader
private void testMatch(String header, String offer, String result) {
    AcceptList list1 = new AcceptList(header);
    AcceptList list2 = new AcceptList(offer);
    MediaType matchItem = AcceptList.match(list1, list2);
    if (result == null) {
        assertNull("Match not null: from " + q(header) + " :: " + q(offer), matchItem);
        return;
    }
    assertNotNull("Match is null: expected " + q(result), matchItem);
    assertEquals("Match different", result, matchItem.toHeaderString());
}
Also used : MediaType(org.apache.jena.atlas.web.MediaType) AcceptList(org.apache.jena.atlas.web.AcceptList)

Aggregations

AcceptList (org.apache.jena.atlas.web.AcceptList)5 MediaType (org.apache.jena.atlas.web.MediaType)5 MediaRange (org.apache.jena.atlas.web.MediaRange)2