Skip to content

Commit 9c44b00

Browse files
committed
[geoserver#221] Add RuleLimits handling to Rule REST endpoint
1 parent fd84705 commit 9c44b00

File tree

5 files changed

+170
-3
lines changed

5 files changed

+170
-3
lines changed

src/services/modules/rest/api/src/main/java/org/geoserver/geofence/services/rest/model/RESTInputRule.java

+10-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
* @author Etj (etj at geo-solutions.it)
1919
*/
2020
@XmlRootElement(name = "rule")
21-
@XmlType(name="Rule", propOrder={"position","grant","username","rolename","instance","ipaddress","service","request","workspace","layer","constraints"})
21+
@XmlType(name="Rule", propOrder={"position","grant","username","rolename","instance","ipaddress","service","request","workspace","layer","limits","constraints"})
2222
public class RESTInputRule extends AbstractRESTPayload {
2323

2424
private RESTRulePosition position;
@@ -38,6 +38,8 @@ public class RESTInputRule extends AbstractRESTPayload {
3838

3939
private GrantType grant;
4040

41+
private RESTRuleLimits limits;
42+
4143
private RESTLayerConstraints constraints;
4244

4345
public RESTInputRule() {
@@ -118,6 +120,13 @@ public void setWorkspace(String workspace) {
118120
}
119121

120122

123+
public RESTRuleLimits getLimits() {
124+
return limits;
125+
}
126+
127+
public void setLimits(RESTRuleLimits limits) {
128+
this.limits = limits;
129+
}
121130

122131
public RESTLayerConstraints getConstraints() {
123132
return constraints;

src/services/modules/rest/api/src/main/java/org/geoserver/geofence/services/rest/model/RESTOutputRule.java

+11-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
* @author Etj (etj at geo-solutions.it)
2121
*/
2222
@XmlRootElement(name = "Rule")
23-
@XmlType(propOrder={"id", "priority","grant","username","rolename","instance","ipaddress","service","request","workspace","layer","constraints"})
23+
@XmlType(propOrder={"id", "priority","grant","username","rolename","instance","ipaddress","service","request","workspace","layer","limits","constraints"})
2424
public class RESTOutputRule implements Serializable {
2525

2626
private Long id;
@@ -43,6 +43,8 @@ public class RESTOutputRule implements Serializable {
4343

4444
private RESTLayerConstraints constraints;
4545

46+
private RESTRuleLimits limits;
47+
4648
public RESTOutputRule() {
4749
}
4850

@@ -126,10 +128,18 @@ public RESTLayerConstraints getConstraints() {
126128
return constraints;
127129
}
128130

131+
public RESTRuleLimits getLimits() {
132+
return limits;
133+
}
134+
129135
public void setConstraints(RESTLayerConstraints constraints) {
130136
this.constraints = constraints;
131137
}
132138

139+
public void setLimits(RESTRuleLimits limits) {
140+
this.limits = limits;
141+
}
142+
133143
public Long getPriority() {
134144
return priority;
135145
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/* (c) 2022 Open Source Geospatial Foundation - all rights reserved
2+
* This code is licensed under the GPL 2.0 license, available at the root
3+
* application directory.
4+
*/
5+
package org.geoserver.geofence.services.rest.model;
6+
7+
import org.geoserver.geofence.core.model.enums.CatalogMode;
8+
import org.geoserver.geofence.core.model.enums.SpatialFilterType;
9+
10+
import javax.xml.bind.annotation.XmlRootElement;
11+
import javax.xml.bind.annotation.XmlType;
12+
13+
@XmlRootElement(name = "Limits")
14+
@XmlType(propOrder={"restrictedAreaWkt","spatialFilterType","catalogMode"})
15+
public class RESTRuleLimits {
16+
17+
private String restrictedAreaWkt;
18+
private SpatialFilterType spatialFilterType;
19+
private CatalogMode catalogMode;
20+
21+
public String getRestrictedAreaWkt() {
22+
return restrictedAreaWkt;
23+
}
24+
25+
public void setRestrictedAreaWkt(String restrictedAreaWkt) {
26+
this.restrictedAreaWkt = restrictedAreaWkt;
27+
}
28+
29+
public SpatialFilterType getSpatialFilterType() {
30+
return spatialFilterType;
31+
}
32+
33+
public void setSpatialFilterType(SpatialFilterType spatialFilterType) {
34+
this.spatialFilterType = spatialFilterType;
35+
}
36+
37+
public CatalogMode getCatalogMode() {
38+
return catalogMode;
39+
}
40+
41+
public void setCatalogMode(CatalogMode catalogMode) {
42+
this.catalogMode = catalogMode;
43+
}
44+
45+
@Override
46+
public String toString() {
47+
return "RESTRuleLimits{" +
48+
"restrictedAreaWkt='" + restrictedAreaWkt + '\'' +
49+
", spatialFilterType=" + spatialFilterType +
50+
", catalogMode=" + catalogMode +
51+
'}';
52+
}
53+
}

src/services/modules/rest/impl/src/main/java/org/geoserver/geofence/services/rest/impl/RESTRuleServiceImpl.java

+58-1
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,19 @@
55

66
package org.geoserver.geofence.services.rest.impl;
77

8+
import java.util.HashMap;
89
import java.util.HashSet;
910
import java.util.List;
11+
import java.util.Map;
1012
import java.util.Set;
1113
import javax.ws.rs.core.Response;
1214
import javax.ws.rs.core.Response.Status;
1315

1416

1517
import org.apache.commons.lang.StringUtils;
1618

19+
import org.geoserver.geofence.core.model.RuleLimits;
20+
import org.geoserver.geofence.services.rest.model.RESTRuleLimits;
1721
import org.locationtech.jts.geom.Geometry;
1822
import org.locationtech.jts.geom.MultiPolygon;
1923
import org.locationtech.jts.io.ParseException;
@@ -55,6 +59,7 @@
5559
import java.util.Arrays;
5660
import java.util.ArrayList;
5761
import java.util.Comparator;
62+
import java.util.stream.Collectors;
5863

5964
/**
6065
*
@@ -88,7 +93,6 @@ public RESTOutputRule get(Long id) throws BadRequestRestEx, NotFoundRestEx, Inte
8893
}
8994

9095
@Override
91-
@Transactional(propagation = Propagation.REQUIRED, value = "geofenceTransactionManager")
9296
public Response insert(RESTInputRule inputRule) throws NotFoundRestEx, BadRequestRestEx, InternalErrorRestEx {
9397

9498
if (inputRule.getPosition() == null || inputRule.getPosition().getPosition() == null) {
@@ -115,6 +119,11 @@ public Response insert(RESTInputRule inputRule) throws NotFoundRestEx, BadReques
115119
ruleAdminService.setDetails(id, details);
116120
}
117121

122+
RuleLimits limits=limitsFromInput(inputRule.getLimits());
123+
if (limits!=null) {
124+
ruleAdminService.setLimits(id,limits);
125+
}
126+
118127
return Response.status(Status.CREATED).tag(id.toString()).entity(id).build();
119128
} catch (BadRequestServiceEx ex) {
120129
LOGGER.error(ex.getMessage());
@@ -125,6 +134,13 @@ public Response insert(RESTInputRule inputRule) throws NotFoundRestEx, BadReques
125134
}
126135
}
127136

137+
private InsertPosition insertPosition(RESTInputRule inputRule){
138+
return
139+
inputRule.getPosition().getPosition() == RulePosition.fixedPriority ? InsertPosition.FIXED
140+
: inputRule.getPosition().getPosition() == RulePosition.offsetFromBottom ? InsertPosition.FROM_END
141+
: inputRule.getPosition().getPosition() == RulePosition.offsetFromTop ? InsertPosition.FROM_START : null;
142+
}
143+
128144
@Override
129145
public void update(Long id, RESTInputRule rule) throws BadRequestRestEx, NotFoundRestEx, InternalErrorRestEx {
130146

@@ -579,6 +595,25 @@ protected RESTOutputRule toOutput(Rule rule) {
579595
out.setConstraints(constraints);
580596
}
581597

598+
if (rule.getRuleLimits()!=null){
599+
RESTRuleLimits ruleLimits=new RESTRuleLimits();
600+
RuleLimits limits=rule.getRuleLimits();
601+
if (limits.getSpatialFilterType()!=null)
602+
ruleLimits.setSpatialFilterType(limits.getSpatialFilterType());
603+
if (limits.getCatalogMode()!=null)
604+
ruleLimits.setCatalogMode(limits.getCatalogMode());
605+
if (limits.getAllowedArea()!=null) {
606+
MultiPolygon area = limits.getAllowedArea();
607+
if (area != null) {
608+
String areaWKT = "SRID=" + area.getSRID() + ";" + area.toText();
609+
ruleLimits.setRestrictedAreaWkt(areaWKT);
610+
}
611+
if (limits.getSpatialFilterType()!=null)
612+
ruleLimits.setSpatialFilterType(limits.getSpatialFilterType());
613+
}
614+
out.setLimits(ruleLimits);
615+
}
616+
582617
return out;
583618
}
584619

@@ -608,6 +643,28 @@ protected Rule fromInput(RESTInputRule in) {
608643
return rule;
609644
}
610645

646+
protected RuleLimits limitsFromInput(RESTRuleLimits in){
647+
RuleLimits limits=null;
648+
if (in!=null && (in.getCatalogMode()!=null || in.getRestrictedAreaWkt()!=null)){
649+
limits=new RuleLimits();
650+
limits.setCatalogMode(in.getCatalogMode());
651+
limits.setSpatialFilterType(in.getSpatialFilterType());
652+
if (StringUtils.isNotBlank(in.getRestrictedAreaWkt())) {
653+
if (in.getRestrictedAreaWkt() != null) {
654+
Geometry g;
655+
try {
656+
g = toGeometryAllowedArea(in.getRestrictedAreaWkt());
657+
} catch (ParseException ex) {
658+
throw new BadRequestRestEx("Error parsing WKT:" + ex.getMessage());
659+
}
660+
limits.setAllowedArea((MultiPolygon) g);
661+
}
662+
}
663+
}
664+
return limits;
665+
}
666+
667+
611668
protected LayerDetails detailsFromInput(RESTInputRule in) {
612669
RESTLayerConstraints constraints = in.getConstraints();
613670
if (constraints != null) {

src/services/modules/rest/impl/src/test/java/org/geoserver/geofence/services/rest/impl/RESTRuleServiceImplTest.java

+38
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@
55

66
package org.geoserver.geofence.services.rest.impl;
77

8+
import java.util.List;
9+
810
import org.geoserver.geofence.core.model.Rule;
11+
import org.geoserver.geofence.core.model.enums.CatalogMode;
12+
import org.geoserver.geofence.core.model.enums.SpatialFilterType;
913
import org.geoserver.geofence.services.RuleAdminService;
1014
import org.geoserver.geofence.services.rest.model.RESTInputUser;
1115
import org.geoserver.geofence.services.rest.model.RESTInputRule;
@@ -16,6 +20,7 @@
1620
import org.geoserver.geofence.core.model.enums.AccessType;
1721
import org.geoserver.geofence.core.model.enums.GrantType;
1822
import org.geoserver.geofence.services.rest.exception.BadRequestRestEx;
23+
import org.geoserver.geofence.services.rest.model.RESTRuleLimits;
1924
import org.geoserver.geofence.services.rest.model.util.IdName;
2025
import java.util.ArrayList;
2126
import java.util.Arrays;
@@ -331,6 +336,39 @@ public void testAllowedAreaSRID() {
331336

332337

333338
}
339+
340+
@Test
341+
public void testLimits() {
342+
343+
RESTInputRule rule = new RESTInputRule();
344+
rule.setPosition(new RESTRulePosition(RESTRulePosition.RulePosition.fixedPriority, 42));
345+
rule.setGrant(GrantType.ALLOW);
346+
rule.setWorkspace("wLimits0");
347+
rule.setLayer("lLimits0");
348+
restRuleService.insert(rule);
349+
rule = new RESTInputRule();
350+
rule.setPosition(new RESTRulePosition(RESTRulePosition.RulePosition.offsetFromTop, 0));
351+
rule.setGrant(GrantType.LIMIT);
352+
rule.setWorkspace("wLimits0");
353+
rule.setLayer("lLimits0");
354+
355+
RESTRuleLimits limits = new RESTRuleLimits();
356+
String allowedArea = "MULTIPOLYGON (((4146.5 1301.4, 4147.5 1301.1, 4147.8 1301.4, 4146.5 1301.4)))";
357+
limits.setRestrictedAreaWkt(allowedArea);
358+
limits.setCatalogMode(CatalogMode.HIDE);
359+
limits.setSpatialFilterType(SpatialFilterType.CLIP);
360+
rule.setLimits(limits);
361+
362+
Long rid = (Long) restRuleService.insert(rule).getEntity();
363+
assertNotNull(rid);
364+
RESTOutputRule out = restRuleService.get(rid);
365+
assertNotNull(out);
366+
limits = out.getLimits();
367+
assertEquals(CatalogMode.HIDE, limits.getCatalogMode());
368+
assertEquals(SpatialFilterType.CLIP, limits.getSpatialFilterType());
369+
assertEquals("SRID=4326;" + allowedArea, limits.getRestrictedAreaWkt());
370+
}
371+
334372

335373

336374
}

0 commit comments

Comments
 (0)