package org.glassfish.jersey.message.internal;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import jakarta.ws.rs.core.Cookie;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.NewCookie;
public abstract class {
public static enum {
Token, QuotedString, Comment, Separator, Control
}
public abstract boolean ();
public abstract boolean (char separator, boolean skipWhiteSpace);
public abstract Event () throws ParseException;
public abstract Event (boolean skipWhiteSpace) throws ParseException;
protected abstract Event (boolean skipWhiteSpace, boolean preserveBackslash) throws ParseException;
protected abstract CharSequence (char startSeparator, char endSeparator) throws ParseException;
protected abstract Event ();
public abstract CharSequence ();
public abstract CharSequence getRemainder();
public abstract int ();
public final CharSequence () throws ParseException {
Event e = next(false);
if (e != Event.Token) {
throw new ParseException("Next event is not a Token", getIndex());
}
return getEventValue();
}
public final void (char c) throws ParseException {
Event e = next(false);
if (e != Event.Separator) {
throw new ParseException("Next event is not a Separator", getIndex());
}
if (c != getEventValue().charAt(0)) {
throw new ParseException("Expected separator '" + c + "' instead of '"
+ getEventValue().charAt(0) + "'", getIndex());
}
}
public final CharSequence () throws ParseException {
Event e = next(false);
if (e != Event.QuotedString) {
throw new ParseException("Next event is not a Quoted String", getIndex());
}
return getEventValue();
}
public final CharSequence () throws ParseException {
return nextTokenOrQuotedString(false);
}
private CharSequence (boolean preserveBackslash) throws ParseException {
Event e = next(false, preserveBackslash);
if (e != Event.Token && e != Event.QuotedString) {
throw new ParseException("Next event is not a Token or a Quoted String, "
+ getEventValue(), getIndex());
}
return getEventValue();
}
public static HttpHeaderReader (String header) {
return new HttpHeaderReaderImpl(header);
}
public static HttpHeaderReader (String header, boolean processComments) {
return new HttpHeaderReaderImpl(header, processComments);
}
public static Date (String date) throws ParseException {
return HttpDateFormat.readDate(date);
}
public static int (CharSequence q) throws ParseException {
if (q == null || q.length() == 0) {
throw new ParseException("Quality value cannot be null or an empty String", 0);
}
int index = 0;
final int length = q.length();
if (length > 5) {
throw new ParseException("Quality value is greater than the maximum length, 5", 0);
}
final char wholeNumber;
char c = wholeNumber = q.charAt(index++);
if (c == '0' || c == '1') {
if (index == length) {
return (c - '0') * 1000;
}
c = q.charAt(index++);
if (c != '.') {
throw new ParseException(
"Error parsing Quality value: a decimal place is expected rather than '" + c + "'", index);
}
if (index == length) {
return (c - '0') * 1000;
}
} else if (c == '.') {
if (index == length) {
throw new ParseException(
"Error parsing Quality value: a decimal numeral is expected after the decimal point", index);
}
} else {
throw new ParseException(
"Error parsing Quality value: a decimal numeral '0' or '1' is expected rather than '" + c + "'", index);
}
int value = 0;
int exponent = 100;
while (index < length) {
c = q.charAt(index++);
if (c >= '0' && c <= '9') {
value += (c - '0') * exponent;
exponent /= 10;
} else {
throw new ParseException(
"Error parsing Quality value: a decimal numeral is expected rather than '" + c + "'", index);
}
}
if (wholeNumber == '1') {
if (value > 0) {
throw new ParseException("The Quality value, " + q + ", is greater than 1", index);
}
return Quality.DEFAULT;
} else {
return value;
}
}
public static int (HttpHeaderReader reader) throws ParseException {
while (reader.hasNext()) {
reader.nextSeparator(';');
if (!reader.hasNext()) {
return Quality.DEFAULT;
}
CharSequence name = reader.nextToken();
reader.nextSeparator('=');
CharSequence value = reader.nextTokenOrQuotedString();
if (name.length() == 1 && (name.charAt(0) == 'q' || name.charAt(0) == 'Q')) {
return readQualityFactor(value);
}
}
return Quality.DEFAULT;
}
public static Map<String, String> (HttpHeaderReader reader) throws ParseException {
return readParameters(reader, false);
}
public static Map<String, String> (HttpHeaderReader reader, boolean fileNameFix) throws ParseException {
Map<String, String> m = null;
while (reader.hasNext()) {
reader.nextSeparator(';');
while (reader.hasNextSeparator(';', true)) {
reader.next();
}
if (!reader.hasNext()) {
break;
}
String name = reader.nextToken().toString().toLowerCase(Locale.ROOT);
reader.nextSeparator('=');
String value;
if ("filename".equals(name) && fileNameFix) {
value = reader.nextTokenOrQuotedString(true).toString();
value = value.substring(value.lastIndexOf('\\') + 1);
} else {
value = reader.nextTokenOrQuotedString(false).toString();
}
if (m == null) {
m = new LinkedHashMap<String, String>();
}
m.put(name, value);
}
return m;
}
public static Map<String, Cookie> (String header) {
return CookiesParser.parseCookies(header);
}
public static Cookie (String header) {
return CookiesParser.parseCookie(header);
}
public static NewCookie (String header) {
return CookiesParser.parseNewCookie(header);
}
private static interface <T> {
T (HttpHeaderReader reader) throws ParseException;
}
private static final ListElementCreator<MatchingEntityTag> =
new ListElementCreator<MatchingEntityTag>() {
@Override
public MatchingEntityTag (HttpHeaderReader reader) throws ParseException {
return MatchingEntityTag.valueOf(reader);
}
};
public static Set<MatchingEntityTag> (String header) throws ParseException {
if ("*".equals(header)) {
return MatchingEntityTag.ANY_MATCH;
}
HttpHeaderReader reader = new HttpHeaderReaderImpl(header);
Set<MatchingEntityTag> l = new HashSet<MatchingEntityTag>(1);
HttpHeaderListAdapter adapter = new HttpHeaderListAdapter(reader);
while (reader.hasNext()) {
l.add(MATCHING_ENTITY_TAG_CREATOR.create(adapter));
adapter.reset();
if (reader.hasNext()) {
reader.next();
}
}
return l;
}
private static final ListElementCreator<MediaType> =
new ListElementCreator<MediaType>() {
@Override
public MediaType (HttpHeaderReader reader) throws ParseException {
return MediaTypeProvider.valueOf(reader);
}
};
public static List<MediaType> (List<MediaType> l, String header) throws ParseException {
return HttpHeaderReader.readList(
l,
MEDIA_TYPE_CREATOR,
header);
}
private static final ListElementCreator<AcceptableMediaType> =
new ListElementCreator<AcceptableMediaType>() {
@Override
public AcceptableMediaType (HttpHeaderReader reader) throws ParseException {
return AcceptableMediaType.valueOf(reader);
}
};
public static List<AcceptableMediaType> (String header) throws ParseException {
return HttpHeaderReader.readQualifiedList(
AcceptableMediaType.COMPARATOR,
ACCEPTABLE_MEDIA_TYPE_CREATOR,
header);
}
private static final ListElementCreator<QualitySourceMediaType> =
new ListElementCreator<QualitySourceMediaType>() {
@Override
public QualitySourceMediaType (HttpHeaderReader reader) throws ParseException {
return QualitySourceMediaType.valueOf(reader);
}
};
public static List<QualitySourceMediaType> (String header) throws ParseException {
return HttpHeaderReader.readQualifiedList(
QualitySourceMediaType.COMPARATOR,
QUALITY_SOURCE_MEDIA_TYPE_CREATOR,
header);
}
public static List<QualitySourceMediaType> (String[] header) throws ParseException {
if (header.length < 2) {
return readQualitySourceMediaType(header[0]);
}
StringBuilder sb = new StringBuilder();
for (String h : header) {
if (sb.length() > 0) {
sb.append(",");
}
sb.append(h);
}
return readQualitySourceMediaType(sb.toString());
}
public static List<AcceptableMediaType> (
final String header, final List<QualitySourceMediaType> priorityMediaTypes) throws ParseException {
return HttpHeaderReader.readQualifiedList(
new Comparator<AcceptableMediaType>() {
@Override
public int (AcceptableMediaType o1, AcceptableMediaType o2) {
boolean q_o1_set = false;
int q_o1 = 0;
boolean q_o2_set = false;
int q_o2 = 0;
for (QualitySourceMediaType priorityType : priorityMediaTypes) {
if (!q_o1_set && MediaTypes.typeEqual(o1, priorityType)) {
q_o1 = o1.getQuality() * priorityType.getQuality();
q_o1_set = true;
} else if (!q_o2_set && MediaTypes.typeEqual(o2, priorityType)) {
q_o2 = o2.getQuality() * priorityType.getQuality();
q_o2_set = true;
}
}
int i = q_o2 - q_o1;
if (i != 0) {
return i;
}
i = o2.getQuality() - o1.getQuality();
if (i != 0) {
return i;
}
return MediaTypes.PARTIAL_ORDER_COMPARATOR.compare(o1, o2);
}
},
ACCEPTABLE_MEDIA_TYPE_CREATOR,
header);
}
private static final ListElementCreator<AcceptableToken> =
new ListElementCreator<AcceptableToken>() {
@Override
public AcceptableToken (HttpHeaderReader reader) throws ParseException {
return new AcceptableToken(reader);
}
};
public static List<AcceptableToken> (String header) throws ParseException {
return HttpHeaderReader.readQualifiedList(ACCEPTABLE_TOKEN_CREATOR, header);
}
private static final ListElementCreator<AcceptableLanguageTag> =
new ListElementCreator<AcceptableLanguageTag>() {
@Override
public AcceptableLanguageTag (HttpHeaderReader reader) throws ParseException {
return new AcceptableLanguageTag(reader);
}
};
public static List<AcceptableLanguageTag> (String header) throws ParseException {
return HttpHeaderReader.readQualifiedList(LANGUAGE_CREATOR, header);
}
private static <T extends Qualified> List<T> (ListElementCreator<T> c, String header)
throws ParseException {
List<T> l = readList(c, header);
Collections.sort(l, Quality.QUALIFIED_COMPARATOR);
return l;
}
private static <T> List<T> (final Comparator<T> comparator, ListElementCreator<T> c, String header)
throws ParseException {
List<T> l = readList(c, header);
Collections.sort(l, comparator);
return l;
}
public static List<String> (String header) throws ParseException {
return readList(new ListElementCreator<String>() {
@Override
public String (HttpHeaderReader reader) throws ParseException {
reader.hasNext();
return reader.nextToken().toString();
}
}, header);
}
private static <T> List<T> (final ListElementCreator<T> c, final String header) throws ParseException {
return readList(new ArrayList<T>(), c, header);
}
private static <T> List<T> (final List<T> l, final ListElementCreator<T> c, final String header)
throws ParseException {
HttpHeaderReader reader = new HttpHeaderReaderImpl(header);
HttpHeaderListAdapter adapter = new HttpHeaderListAdapter(reader);
while (reader.hasNext()) {
l.add(c.create(adapter));
adapter.reset();
if (reader.hasNext()) {
reader.next();
}
}
return l;
}
}