use of java.text.ParseException in project jersey by jersey.
the class EntityTagProvider method fromString.
@Override
public EntityTag fromString(String header) {
throwIllegalArgumentExceptionIfNull(header, LocalizationMessages.ENTITY_TAG_IS_NULL());
try {
HttpHeaderReader reader = HttpHeaderReader.newInstance(header);
Event e = reader.next(false);
if (e == Event.QuotedString) {
return new EntityTag(reader.getEventValue().toString());
} else if (e == Event.Token) {
final CharSequence ev = reader.getEventValue();
if (ev != null && ev.length() > 0 && ev.charAt(0) == 'W') {
reader.nextSeparator('/');
return new EntityTag(reader.nextQuotedString().toString(), true);
}
}
} catch (ParseException ex) {
throw new IllegalArgumentException("Error parsing entity tag '" + header + "'", ex);
}
throw new IllegalArgumentException("Error parsing entity tag '" + header + "'");
}
use of java.text.ParseException in project jersey by jersey.
the class HttpDateFormat method readDate.
/**
* Read a date.
*
* @param date the date as a string.
*
* @return the date
* @throws java.text.ParseException in case the date string cannot be parsed.
*/
public static Date readDate(final String date) throws ParseException {
ParseException pe = null;
for (final SimpleDateFormat f : HttpDateFormat.getDateFormats()) {
try {
Date result = f.parse(date);
// parse can change time zone -> set it back to GMT
f.setTimeZone(GMT_TIME_ZONE);
return result;
} catch (final ParseException e) {
pe = (pe == null) ? e : pe;
}
}
throw pe;
}
use of java.text.ParseException in project jersey by jersey.
the class HttpHeaderReader method readQualityFactor.
/**
* TODO javadoc.
*/
public static int readQualityFactor(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);
}
// Parse the whole number and decimal point
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 == '.') {
// do this, for example HttpURLConnection.
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);
}
// Parse the fraction
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;
}
}
use of java.text.ParseException in project jersey by jersey.
the class HttpHeaderReaderImpl method nextSeparatedString.
@Override
public String nextSeparatedString(char startSeparator, char endSeparator) throws ParseException {
nextSeparator(startSeparator);
final int start = index;
for (; index < length; index++) {
if (header.charAt(index) == endSeparator) {
break;
}
}
if (start == index) {
// no token between separators
throw new ParseException(LocalizationMessages.HTTP_HEADER_NO_CHARS_BETWEEN_SEPARATORS(startSeparator, endSeparator), index);
} else if (index == length) {
// no end separator
throw new ParseException(LocalizationMessages.HTTP_HEADER_NO_END_SEPARATOR(endSeparator), index);
}
event = Event.Token;
value = header.subSequence(start, index++);
return value.toString();
}
use of java.text.ParseException in project jersey by jersey.
the class CacheControlProvider method fromString.
@Override
public CacheControl fromString(String header) {
throwIllegalArgumentExceptionIfNull(header, LocalizationMessages.CACHE_CONTROL_IS_NULL());
try {
HttpHeaderReader reader = HttpHeaderReader.newInstance(header);
CacheControl cacheControl = new CacheControl();
// defaults to true
cacheControl.setNoTransform(false);
while (reader.hasNext()) {
readDirective(cacheControl, reader);
if (reader.hasNextSeparator(',', true)) {
reader.nextSeparator(',');
}
}
return cacheControl;
} catch (ParseException pe) {
throw new IllegalArgumentException("Error parsing cache control '" + header + "'", pe);
}
}
Aggregations