public static void updateStringParameterFilter(final GeoServerTileLayerInfo tileLayerInfo, final String paramKey, boolean createParam, final String defaultValue, final Set<String> allowedValues) { removeParameterFilter(tileLayerInfo, paramKey); if (createParam && allowedValues != null && allowedValues.size() > 0) { // make sure default value is among the list of allowed values Set<String> values = new TreeSet<String>(allowedValues); StringParameterFilter stringListFilter = new StringParameterFilter(); stringListFilter.setKey(paramKey); stringListFilter.setDefaultValue(defaultValue == null ? "" : defaultValue); stringListFilter.getValues().addAll(values); tileLayerInfo.getParameterFilters().add(stringListFilter); } }
@Override public String apply(@Nullable String str) throws ParameterException { if (str == null || str.length() == 0) { return getDefaultValue(); } str = getNormalize().apply(str); if (getLegalValues().contains(str)) { return str; } throw new ParameterException(str + " violates filter for parameter " + getKey()); }
@Override public StringParameterFilter clone() { StringParameterFilter clone = new StringParameterFilter(); clone.setDefaultValue(getDefaultValue()); clone.setKey(getKey()); if (values != null) { clone.values = new ArrayList<String>(values); } clone.setNormalize(getNormalize().clone()); return clone; } }
/** * Checks whether a given parameter value applies to this filter. * * @param parameterValue the value to check if applies to this parameter filter * @return {@code true} if {@code parameterValue} is valid according to this filter, {@code * false} otherwise */ @Override public boolean applies(@Nullable String parameterValue) { return getLegalValues().contains(getNormalize().apply(parameterValue)); }
List<ParameterFilter> filters = new LinkedList<>(); StringParameterFilter filter = new StringParameterFilter(); filter.setKey("FILTER1"); filter.setValues(Arrays.asList("foo", "bar")); filters.add(filter); StringParameterFilter filter = new StringParameterFilter(); filter.setKey("FILTER2"); filter.setValues(Arrays.asList("quux", "quam")); filters.add(filter); StringParameterFilter filter = new StringParameterFilter(); filter.setKey("FILTER3"); filter.setValues(Arrays.asList("quux", "quam")); filters.add(filter);
@Test public void testNormalizeUpper() throws Exception { filter.setNormalize(new CaseNormalizer(Case.UPPER, Locale.ENGLISH)); assertThat(filter.getLegalValues(), containsInAnyOrder("FOO", "BAR", "BAZ")); for (String test : Arrays.asList("foo", "Bar", "BAZ")) { assertThat(filter.applies(test), is(true)); assertThat(filter.apply(test), equalTo(test.toUpperCase(Locale.ENGLISH))); } for (String test : Arrays.asList("Foo", "FOO", "BaR", "bar", "BAR", "BAz", "baz")) { assertThat(filter.applies(test), is(true)); assertThat(filter.apply(test), equalTo(test.toUpperCase(Locale.ENGLISH))); } for (String test : Arrays.asList("fooo", "fo", "Ba", "BBarr", "BA", "BBAZ")) { assertThat(filter.applies(test), is(false)); try { filter.apply(test); fail(); } catch (Exception ex) { assertThat(ex, instanceOf(ParameterException.class)); } } assertThat(filter.apply(null), equalTo("Default")); assertThat(filter.apply(""), equalTo("Default")); }
public void testGetParameterFilters() { layerInfoTileLayer = new GeoServerTileLayer(layerInfo, defaults, gridSetBroker); List<ParameterFilter> parameterFilters = layerInfoTileLayer.getParameterFilters(); assertNotNull(parameterFilters); assertEquals(1, parameterFilters.size()); assertTrue(parameterFilters.get(0) instanceof StringParameterFilter); StringParameterFilter styleFilter = (StringParameterFilter) parameterFilters.get(0); assertEquals("STYLES", styleFilter.getKey()); assertEquals("default_style", styleFilter.getDefaultValue()); assertEquals(new HashSet<String>(Arrays.asList("alternateStyle-1", "alternateStyle-2")), new HashSet<String>(styleFilter.getLegalValues())); // layerInfoTileLayer.getInfo().getCachedStyles().add("alternateStyle-2"); }
@Test public void testCloneable() throws Exception { filter.setNormalize(new CaseNormalizer(Case.UPPER, Locale.ENGLISH)); StringParameterFilter clone = filter.clone(); assertThat(clone.getDefaultValue(), equalTo(filter.getDefaultValue())); assertThat(clone.getValues(), equalTo(filter.getValues())); assertThat( clone.getNormalize().getConfiguredLocale(), equalTo(filter.getNormalize().getConfiguredLocale())); assertThat(clone.getNormalize().getCase(), equalTo(filter.getNormalize().getCase())); } }
@Test public void testBasic() throws Exception { assertThat( filter.getNormalize(), allOf( hasProperty("case", equalTo(Case.NONE)), hasProperty("locale", equalTo(Locale.getDefault())))); assertThat(filter.getLegalValues(), containsInAnyOrder("foo", "Bar", "BAZ")); assertThat(filter.applies(test), is(true)); assertThat(filter.apply(test), equalTo(test)); assertThat(filter.applies(test), is(false)); try { filter.apply(test); fail(); } catch (Exception ex) { assertThat(filter.applies(test), is(false)); try { filter.apply(test); fail(); } catch (Exception ex) { assertThat(filter.apply(null), equalTo("Default")); assertThat(filter.apply(""), equalTo("Default"));
/** * @see org.geoserver.gwc.layer.GeoServerTileLayerInfo#cachedStyles() */ @Override public ImmutableSet<String> cachedStyles() { ParameterFilter styleQualifier = TileLayerInfoUtil.findParameterFilter("STYLES", getParameterFilters()); if (styleQualifier != null) { if (styleQualifier instanceof StringParameterFilter) { StringParameterFilter sp = (StringParameterFilter) styleQualifier; return ImmutableSet.copyOf(sp.getLegalValues()); } } return ImmutableSet.of(); }
public void testRenameAlternateStyle() throws Exception { Set<ParameterFilter> params = new HashSet<ParameterFilter>(); when(mockTileLayerInfo.getParameterFilters()).thenReturn(params); TileLayerInfoUtil.setCachedStyles(mockTileLayerInfo, null, ImmutableSet.of(STYLE_NAME)); assertEquals(1, params.size()); ImmutableSet<String> styles = ImmutableSet.of(STYLE_NAME); when(mockTileLayerInfo.cachedStyles()).thenReturn(styles); listener.handleModifyEvent(styleNameModifyEvent); assertEquals(1, params.size()); ParameterFilter updated = params.iterator().next(); assertTrue(updated instanceof StringParameterFilter); assertEquals(Lists.newArrayList(STYLE_NAME_MODIFIED), ((StringParameterFilter) updated).getValues()); verify(mockTileLayer, times(1)).resetParameterFilters(); verify(mockMediator, times(1)).truncateByLayerAndStyle(eq(PREFIXED_RESOURCE_NAME), eq(STYLE_NAME)); verify(mockMediator, times(1)).save(same(mockTileLayer)); }
StringParameterFilter stylesParameterFilter = new StringParameterFilter(); stylesParameterFilter.setKey("STYLES"); stylesParameterFilter.setValues( styles.stream().map(StyleImpl::getName).collect(Collectors.toList())); paramFilters.add(stylesParameterFilter);
@Test public void testNormalizeLower() throws Exception { filter.setNormalize(new CaseNormalizer(Case.LOWER, Locale.ENGLISH)); assertThat(filter.getLegalValues(), containsInAnyOrder("foo", "bar", "baz")); for (String test : Arrays.asList("foo", "Bar", "BAZ")) { assertThat(filter.applies(test), is(true)); assertThat(filter.apply(test), equalTo(test.toLowerCase(Locale.ENGLISH))); } for (String test : Arrays.asList("Foo", "FOO", "BaR", "bar", "BAR", "BAz", "baz")) { assertThat(filter.applies(test), is(true)); assertThat(filter.apply(test), equalTo(test.toLowerCase(Locale.ENGLISH))); } for (String test : Arrays.asList("fooo", "fo", "Ba", "BBarr", "BA", "BBAZ")) { assertThat(filter.applies(test), is(false)); try { filter.apply(test); fail(); } catch (Exception ex) { assertThat(ex, instanceOf(ParameterException.class)); } } assertThat(filter.apply(null), equalTo("Default")); assertThat(filter.apply(""), equalTo("Default")); }
floatFilter.getValues()); assertEquals("STYLES", stringFilter.getKey()); assertEquals("capitals", stringFilter.getDefaultValue()); assertEquals(ImmutableList.of("burg", "point"), stringFilter.getLegalValues());
/** * Add a {@link StringParameterFilter} to the layer, replacing any existing filter for the same * parameter. * * @param tileLayerInfo layer to update the filter on * @param paramKey key for the parameter * @param createParam create a new filter if there is none to replace for the specified key * @param defaultValue default value * @param allowedValues legal values for the parameter */ public static void updateStringParameterFilter( final GeoServerTileLayerInfo tileLayerInfo, final String paramKey, boolean createParam, final String defaultValue, final Set<String> allowedValues) { createParam |= tileLayerInfo.removeParameterFilter(paramKey); if (createParam && allowedValues != null && allowedValues.size() > 0) { // make sure default value is among the list of allowed values Set<String> values = new TreeSet<String>(allowedValues); StringParameterFilter stringListFilter = new StringParameterFilter(); stringListFilter.setKey(paramKey); stringListFilter.setDefaultValue(defaultValue == null ? "" : defaultValue); values.addAll(stringListFilter.getValues()); stringListFilter.setValues(new ArrayList<String>(values)); tileLayerInfo.addParameterFilter(stringListFilter); } }
TileLayer tileLayer = mock(TileLayer.class); StringParameterFilter styles = new StringParameterFilter(); styles.setKey("STYLES"); styles.setValues(Arrays.asList("style-a", "style-b")); StringParameterFilter time = new StringParameterFilter(); time.setKey("time"); time.setValues(Arrays.asList("2016-02-23T03:00:00.000Z")); StringParameterFilter elevation = new StringParameterFilter(); elevation.setKey("elevation"); elevation.setValues(Arrays.asList("500"));
@Test public void testNormalizeLocale() throws Exception { filter.setValues(Arrays.asList("f\u0131o", "B\u0131r", "BIZ")); filter.setNormalize(new CaseNormalizer(Case.LOWER, Locale.forLanguageTag("tr"))); assertThat(filter.getLegalValues(), containsInAnyOrder("f\u0131o", "b\u0131r", "b\u0131z")); for (String test : Arrays.asList("f\u0131o", "B\u0131r", "BIZ")) { assertThat(filter.applies(test), is(true)); assertThat(filter.apply(test), equalTo(test.toLowerCase(Locale.forLanguageTag("tr")))); } for (String test : Arrays.asList( "F\u0131o", "FIO", "B\u0131R", "b\u0131r", "BIR", "BIz", "b\u0131z")) { assertThat(filter.applies(test), is(true)); assertThat(filter.apply(test), equalTo(test.toLowerCase(Locale.forLanguageTag("tr")))); } for (String test : Arrays.asList("f\u0131oo", "f\u0131", "B\u0131", "BB\u0131rr", "BI", "BBIZ")) { assertThat(filter.applies(test), is(false)); try { filter.apply(test); fail(); } catch (Exception ex) { assertThat(ex, instanceOf(ParameterException.class)); } } assertThat(filter.apply(null), equalTo("Default")); assertThat(filter.apply(""), equalTo("Default")); }
floatFilter.getValues()); assertEquals("STYLES", stringFilter.getKey()); assertEquals("capitals", stringFilter.getDefaultValue()); assertEquals(ImmutableList.of("burg", "point"), stringFilter.getLegalValues());
@Before public void setUp() { filter = new StringParameterFilter(); filter.setKey("TEST"); filter.setValues(Arrays.asList("foo", "Bar", "BAZ")); filter.setDefaultValue("Default"); xs = new GeoWebCacheXStream(); xs = XMLConfiguration.getConfiguredXStream(xs, new StaticWebApplicationContext()); }
StringParameterFilter stylesParameterFilter = new StringParameterFilter(); stylesParameterFilter.setKey("STYLES"); stylesParameterFilter.setValues(Arrays.asList("style1", "style2"));