Search in sources :

Example 91 with ParseException

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 + "'");
}
Also used : Event(org.glassfish.jersey.message.internal.HttpHeaderReader.Event) EntityTag(javax.ws.rs.core.EntityTag) ParseException(java.text.ParseException)

Example 92 with ParseException

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;
}
Also used : ParseException(java.text.ParseException) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date)

Example 93 with ParseException

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;
    }
}
Also used : ParseException(java.text.ParseException)

Example 94 with ParseException

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();
}
Also used : ParseException(java.text.ParseException)

Example 95 with ParseException

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);
    }
}
Also used : CacheControl(javax.ws.rs.core.CacheControl) ParseException(java.text.ParseException)

Aggregations

ParseException (java.text.ParseException)1141 Date (java.util.Date)435 SimpleDateFormat (java.text.SimpleDateFormat)387 IOException (java.io.IOException)118 DateFormat (java.text.DateFormat)117 Calendar (java.util.Calendar)104 ArrayList (java.util.ArrayList)98 Test (org.junit.Test)58 HashMap (java.util.HashMap)45 Matcher (java.util.regex.Matcher)39 File (java.io.File)38 GregorianCalendar (java.util.GregorianCalendar)34 Map (java.util.Map)28 BigDecimal (java.math.BigDecimal)23 Timestamp (java.sql.Timestamp)21 List (java.util.List)21 Locale (java.util.Locale)20 SipException (javax.sip.SipException)20 InputStream (java.io.InputStream)19 TimeZone (java.util.TimeZone)19