com.nike.riposte.server.http.header.AcceptHeaderParser Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of riposte-spi Show documentation
Show all versions of riposte-spi Show documentation
Riposte module riposte-spi
package com.nike.riposte.server.http.header;
import com.nike.internal.util.Pair;
import com.nike.riposte.server.http.header.accept.MediaRange;
import com.nike.riposte.server.http.header.accept.MediaRangeSubType;
import com.nike.riposte.server.http.header.accept.MediaRangeType;
import com.nike.riposte.server.http.header.accept.MimeMediaRangeSubType;
import com.nike.riposte.server.http.header.accept.MimeMediaRangeType;
import com.nike.riposte.util.text.parsercombinator.Parser;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;
import static com.nike.riposte.server.http.mimetype.MimeTypeParser.parameterParser;
import static com.nike.riposte.server.http.mimetype.MimeTypeParser.semicolon;
import static com.nike.riposte.server.http.mimetype.MimeTypeParser.subTypeParser;
import static com.nike.riposte.server.http.mimetype.MimeTypeParser.token;
import static com.nike.riposte.server.http.mimetype.MimeTypeParser.typeParser;
import static com.nike.riposte.util.text.parsercombinator.Parser.Apply.match;
import static com.nike.riposte.util.text.parsercombinator.Parser.Apply.test;
import static com.nike.riposte.util.text.parsercombinator.Parser.Parsers.filter;
import static com.nike.riposte.util.text.parsercombinator.Parser.Parsers.floatNumber;
import static com.nike.riposte.util.text.parsercombinator.Parser.Parsers.oneOf;
import static com.nike.riposte.util.text.parsercombinator.Parser.Parsers.regex;
import static com.nike.riposte.util.text.parsercombinator.Parser.Parsers.skip;
import static com.nike.riposte.util.text.parsercombinator.Parser.Parsers.string;
import static com.nike.riposte.util.text.parsercombinator.Parser.Parsers.zeroOrMore;
/**
* Offers a set of Parser instances for parsing the components of an RFC-2616 14.1 Accept Header.
*
* @author Kirk Peterson
*/
@SuppressWarnings("WeakerAccess")
public class AcceptHeaderParser {
/**
* A Parser that extracts the quality-factor from a key-value pair.
*/
public static Parser qualityFactorParser =
skip(regex("\\s*;?\\s*q\\s*=\\s*")).thenParse(filter(floatNumber(), q -> q > 0.0f && q <= 1.0f));
/**
* A Parser that extracts a key-value pair that preceed the quality-factor header.
*/
public static Parser> mediaRangeParamaterParser =
filter(token, t -> !"q".equals(t)).thenSkip(regex("\\s*q?\\s*=\\s*")).thenParse(token);
/**
* A Parser that extracts a list of key-value pairs which preceed the quality-factor header, named media-params.
*/
public static final Parser