Skip to content

Commit

Permalink
[GEOS-8181] KML reflector attempts to query all records in table
Browse files Browse the repository at this point in the history
  • Loading branch information
aaime committed Jul 31, 2017
1 parent 1e1e920 commit 2ddc43e
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import java.util.EmptyStackException;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;

import org.geoserver.kml.KmlEncodingContext;
import org.geoserver.kml.decorator.KmlDecoratorFactory.KmlDecorator;
Expand All @@ -18,6 +19,7 @@
import org.geotools.filter.function.EnvFunction;
import org.geotools.map.FeatureLayer;
import org.geotools.map.Layer;
import org.geotools.styling.AbstractStyleVisitor;
import org.geotools.styling.Style;
import org.geotools.styling.Symbolizer;
import org.opengis.feature.simple.SimpleFeature;
Expand All @@ -44,6 +46,8 @@ public class FeatureSequenceFactory implements SequenceFactory<Feature> {
private Style simplified;

private KmlEncodingContext context;

private boolean hasActiveRules;

public FeatureSequenceFactory(KmlEncodingContext context, FeatureLayer layer) {
this.context = context;
Expand All @@ -59,10 +63,22 @@ public FeatureSequenceFactory(KmlEncodingContext context, FeatureLayer layer) {

// prepare the style for this layer
simplified = getSimplifiedStyle(mapContent, layer);

AtomicBoolean rulesFound = new AtomicBoolean(false);
if(simplified != null) {
simplified.accept(new AbstractStyleVisitor() {
public void visit(org.geotools.styling.Rule rule) {
rulesFound.set(true);
// no need to scan inside rules
}
});
}
this.hasActiveRules = rulesFound.get();
}

private Style getSimplifiedStyle(WMSMapContent mc, Layer layer) {
ScaleStyleVisitor visitor = new ScaleStyleVisitor(mc.getScaleDenominator(),
final double scaleDenominator = mc.getScaleDenominator();
ScaleStyleVisitor visitor = new ScaleStyleVisitor(scaleDenominator,
(SimpleFeatureType) layer.getFeatureSource().getSchema());
try {
layer.getStyle().accept(visitor);
Expand All @@ -74,7 +90,7 @@ private Style getSimplifiedStyle(WMSMapContent mc, Layer layer) {

@Override
public Sequence<Feature> newSequence() {
return new FeatureGenerator(simplified != null ? context.openIterator(features) : null);
return new FeatureGenerator(hasActiveRules ? context.openIterator(features) : null);
}

public class FeatureGenerator implements Sequence<Feature> {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/* (c) 2017 Open Source Geospatial Foundation - all rights reserved
* This code is licensed under the GPL 2.0 license, available at the root
* application directory.
*/
package org.geoserver.kml.sequence;

import static org.easymock.EasyMock.*;
import static org.junit.Assert.*;

import org.geoserver.kml.KmlEncodingContext;
import org.geoserver.wms.WMSMapContent;
import org.geotools.data.simple.SimpleFeatureCollection;
import org.geotools.feature.DefaultFeatureCollection;
import org.geotools.map.FeatureLayer;
import org.geotools.styling.Style;
import org.geotools.styling.StyleBuilder;
import org.junit.Test;

import de.micromata.opengis.kml.v_2_2_0.Feature;
import junit.framework.AssertionFailedError;

public class FeatureSequenceFactoryTest {

@Test
public void testNoScanOnEmptyStyle() {
// a style with scale dependency
StyleBuilder sb = new StyleBuilder();
Style style = sb.createStyle(sb.createPolygonSymbolizer());
style.featureTypeStyles().get(0).rules().get(0).setMaxScaleDenominator(1000);

// the layer
final DefaultFeatureCollection fc = new DefaultFeatureCollection();
FeatureLayer layer = new FeatureLayer(fc, style);

// a max context with a scale outside the rule range
WMSMapContent mc = createNiceMock(WMSMapContent.class);
expect(mc.getScaleDenominator()).andReturn(2000d).anyTimes();
replay(mc);

// and the context wiring everything toghether
KmlEncodingContext context = createNiceMock(KmlEncodingContext.class);
expect(context.openIterator(anyObject(SimpleFeatureCollection.class)))
.andThrow(new AssertionFailedError("Should not have called openIterator")).anyTimes();
expect(context.getCurrentFeatureCollection()).andReturn(fc).anyTimes();
expect(context.getMapContent()).andReturn(mc).anyTimes();
replay(context);

// check no features have been read
FeatureSequenceFactory sf = new FeatureSequenceFactory(context, layer);
Sequence<Feature> sequence = sf.newSequence();
assertNull(sequence.next());
}
}

0 comments on commit 2ddc43e

Please sign in to comment.