Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CXF-9054 do not use Date and SimpleDateFormat when parsing LocalDate,… #2056

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ private Temporal convertToTemporal(Class<? extends Temporal> valueType, String v
if (LocalTime.class.isAssignableFrom(valueType)) {
return LocalTime.parse(value);
} else if (LocalDate.class.isAssignableFrom(valueType)) {
return LocalDate.from(convertToDefaultDate(value).toInstant().atZone(ZoneId.systemDefault()));
return LocalDate.parse(value, SearchUtils.getLocalDateFormat(contextProperties));
} else if (LocalDateTime.class.isAssignableFrom(valueType)) {
return convertTo(value, SearchUtils.DEFAULT_DATETIME_FORMAT, Boolean.FALSE, LocalDateTime::parse);
} else if (OffsetTime.class.isAssignableFrom(valueType)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.logging.Level;
import java.util.logging.Logger;

Expand All @@ -47,6 +49,7 @@ public final class SearchUtils {
public static final String SEARCH_VISITOR_PROPERTY = "search.visitor";
public static final String DECODE_QUERY_VALUES = "search.decode.values";
public static final String ESCAPE_UNDERSCORE_CHAR = "search.escape.underscore.char";
private static final Map<String, DateTimeFormatter> FORMATTER_CACHE = new ConcurrentHashMap<>();

private static final Logger LOG = LogUtils.getL7dLogger(SearchUtils.class);

Expand Down Expand Up @@ -88,6 +91,11 @@ public static Date dateFromStringWithContextProperties(String value) {
return dateFromStringWithDefaultFormats(value);
}

public static DateTimeFormatter getLocalDateFormat(Map<String, String> properties) {
String format = properties.getOrDefault(DATE_FORMAT_PROPERTY, DEFAULT_DATE_FORMAT);
return FORMATTER_CACHE.computeIfAbsent(format, DateTimeFormatter::ofPattern);
}

public static SimpleDateFormat getDateFormatOrDefault(Map<String, String> properties, String pattern) {
return getDateFormatOrDefault(properties.get(DATE_FORMAT_PROPERTY), pattern);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,16 @@ public void testParseLocalDateWithDefaultFormat() throws SearchParseException, P
assertFalse(filter.isMet(new Condition(null, 123, null, LocalDate.parse("2010-03-12", df))));
}

@Test
public void testParseOldLocalDateWithDefaultFormat() throws SearchParseException {
DateTimeFormatter df = DateTimeFormatter.ofPattern(SearchUtils.DEFAULT_DATE_FORMAT);
SearchCondition<Condition> filter = parser.parse("localDate==1893-04-02");
assertTrue(filter.isMet(new Condition("whatever", 15, null, LocalDate.parse("1893-04-02", df))));

filter = parser.parse("localDate==1893-04-01");
assertTrue(filter.isMet(new Condition("whatever", 15, null, LocalDate.parse("1893-04-01", df))));
}

@Test
public void testParseInstantWithDefaultFormat() throws SearchParseException, ParseException {
SearchCondition<Condition> filter = parser.parse("instant=le=2010-03-11");
Expand Down