From a298d3053ec4f121223857d8607335d20a8cd5a1 Mon Sep 17 00:00:00 2001 From: Gethin James Date: Thu, 1 Jun 2017 11:11:11 +0000 Subject: [PATCH] Merged searchrep (5.2.1) to 5.2.N (5.2.1) 136814 gjames: SEARCH-348: Better timezone handling git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/BRANCHES/DEV/5.2.N/root@137061 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261 --- .../rest/api/search/impl/SearchMapper.java | 65 +++++++++++++------ .../rest/api/search/SearchMapperTests.java | 22 +++++++ 2 files changed, 66 insertions(+), 21 deletions(-) diff --git a/source/java/org/alfresco/rest/api/search/impl/SearchMapper.java b/source/java/org/alfresco/rest/api/search/impl/SearchMapper.java index c5ccc96284..93b2779ca4 100644 --- a/source/java/org/alfresco/rest/api/search/impl/SearchMapper.java +++ b/source/java/org/alfresco/rest/api/search/impl/SearchMapper.java @@ -37,20 +37,6 @@ import static org.alfresco.rest.api.Nodes.PARAM_INCLUDE_PROPERTIES; import static org.alfresco.service.cmr.search.SearchService.LANGUAGE_CMIS_ALFRESCO; import static org.alfresco.service.cmr.search.SearchService.LANGUAGE_FTS_ALFRESCO; import static org.alfresco.service.cmr.search.SearchService.LANGUAGE_LUCENE; - -import java.time.ZoneId; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; -import java.util.ListIterator; -import java.util.Map; -import java.util.Optional; -import java.util.Set; -import java.util.TimeZone; -import java.util.function.Function; -import java.util.regex.Matcher; -import java.util.stream.Collectors; - import org.alfresco.error.AlfrescoRuntimeException; import org.alfresco.repo.search.impl.lucene.LuceneQueryLanguageSPI; import org.alfresco.rest.api.search.context.SearchRequestContext; @@ -87,6 +73,19 @@ import org.alfresco.service.cmr.search.SearchParameters.SortDefinition.SortType; import org.alfresco.service.cmr.search.StatsRequestParameters; import org.alfresco.util.ParameterCheck; +import java.time.ZoneId; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.ListIterator; +import java.util.Map; +import java.util.Optional; +import java.util.Set; +import java.util.TimeZone; +import java.util.function.Function; +import java.util.regex.Matcher; +import java.util.stream.Collectors; + /** * Maps from a json request and a solr SearchParameters object. * @@ -120,6 +119,7 @@ public class SearchMapper fromPaging(sp, params.getPaging()); fromSort(sp, searchQuery.getSort()); fromTemplate(sp, searchQuery.getTemplates()); + fromTimezone(sp, searchQuery.getTimezone()); validateInclude(searchQuery.getInclude()); fromDefault(sp, searchQuery.getDefaults()); fromFilterQuery(sp, searchQuery.getFilterQueries()); @@ -133,7 +133,6 @@ public class SearchMapper fromRange(sp, searchQuery.getFacetRanges()); fromScope(sp, searchQuery.getScope(), searchRequestContext); fromLimits(sp, searchQuery.getLimits()); - fromTimezone(sp, searchQuery.getTimezone()); return sp; } @@ -690,22 +689,46 @@ public class SearchMapper /** * Validates and sets the timezone * @param sp SearchParameters - * @param timezone a valid java.time.ZoneId + * @param timezoneId a valid java.time.ZoneId */ - public void fromTimezone(SearchParameters sp, String timezone) + public void fromTimezone(SearchParameters sp, String timezoneId) { - if (timezone!= null && !timezone.isEmpty()) + /* + * java.util.TimeZone will not error if you set an invalid timezone + * it just falls back to GMT without telling you. + * + * So I am using java.time.ZoneId because that throws an error, + * if I then convert a ZoneId to Timezone I have the same problem (silently uses GMT) + * so + * I am converting using both methods: + * If a timezoneId is invalid then an Invalid error is thrown + * If its not possible to take a java.time.ZoneId and convert it to a java.util.TimeZone then an Incompatible error is thrown + * + */ + if (timezoneId!= null && !timezoneId.isEmpty()) { ZoneId validZoneId = null; + TimeZone timeZone = null; + try { - validZoneId = ZoneId.of(timezone); - sp.setTimezone(validZoneId.toString()); + validZoneId = ZoneId.of(timezoneId); + timeZone = TimeZone.getTimeZone(timezoneId); } catch (Exception e) { - throw new IllegalArgumentException("Invalid timezone "+timezone); + throw new IllegalArgumentException("Invalid timezoneId "+timezoneId); } + + if (validZoneId.getId().equals(timeZone.getID())) + { + sp.setTimezone(validZoneId.getId()); + } + else + { + throw new IllegalArgumentException("Incompatible timezoneId "+timezoneId); + } + } } diff --git a/source/test-java/org/alfresco/rest/api/search/SearchMapperTests.java b/source/test-java/org/alfresco/rest/api/search/SearchMapperTests.java index bfcf1e8391..4ced0f4666 100644 --- a/source/test-java/org/alfresco/rest/api/search/SearchMapperTests.java +++ b/source/test-java/org/alfresco/rest/api/search/SearchMapperTests.java @@ -549,12 +549,34 @@ public class SearchMapperTests assertEquals("Europe/Madrid", searchParameters.getTimezone()); searchMapper.fromTimezone(searchParameters, "GMT+1"); assertEquals("GMT+01:00", searchParameters.getTimezone()); + searchMapper.fromTimezone(searchParameters, "GMT+01:00"); + assertEquals("GMT+01:00", searchParameters.getTimezone()); searchMapper.fromTimezone(searchParameters, "GMT-9"); assertEquals("GMT-09:00", searchParameters.getTimezone()); searchMapper.fromTimezone(searchParameters, "GMT+08:00"); assertEquals("GMT+08:00", searchParameters.getTimezone()); searchMapper.fromTimezone(searchParameters, "GMT-12:00"); assertEquals("GMT-12:00", searchParameters.getTimezone()); + + try + { + searchMapper.fromTimezone(searchParameters, "UTC+5"); + fail(); + } + catch (IllegalArgumentException iae) + { + assertTrue("UTC is not support by java.util.timezone",iae.getLocalizedMessage().contains("Incompatible timezoneId")); + } + + try + { + searchMapper.fromTimezone(searchParameters, "UTC+06:00"); + fail(); + } + catch (IllegalArgumentException iae) + { + assertTrue(iae.getLocalizedMessage().contains("Incompatible timezoneId")); + } } @Test