Skip to content

Commit

Permalink
add rule limits to rest
Browse files Browse the repository at this point in the history
  • Loading branch information
taba90 committed Jul 15, 2022
1 parent fd84705 commit 7559b57
Show file tree
Hide file tree
Showing 5 changed files with 170 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
* @author Etj (etj at geo-solutions.it)
*/
@XmlRootElement(name = "rule")
@XmlType(name="Rule", propOrder={"position","grant","username","rolename","instance","ipaddress","service","request","workspace","layer","constraints"})
@XmlType(name="Rule", propOrder={"position","grant","username","rolename","instance","ipaddress","service","request","workspace","layer","limits","constraints"})
public class RESTInputRule extends AbstractRESTPayload {

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

private GrantType grant;

private RESTRuleLimits limits;

private RESTLayerConstraints constraints;

public RESTInputRule() {
Expand Down Expand Up @@ -118,6 +120,13 @@ public void setWorkspace(String workspace) {
}


public RESTRuleLimits getLimits() {
return limits;
}

public void setLimits(RESTRuleLimits limits) {
this.limits = limits;
}

public RESTLayerConstraints getConstraints() {
return constraints;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
* @author Etj (etj at geo-solutions.it)
*/
@XmlRootElement(name = "Rule")
@XmlType(propOrder={"id", "priority","grant","username","rolename","instance","ipaddress","service","request","workspace","layer","constraints"})
@XmlType(propOrder={"id", "priority","grant","username","rolename","instance","ipaddress","service","request","workspace","layer","limits","constraints"})
public class RESTOutputRule implements Serializable {

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

private RESTLayerConstraints constraints;

private RESTRuleLimits limits;

public RESTOutputRule() {
}

Expand Down Expand Up @@ -126,10 +128,18 @@ public RESTLayerConstraints getConstraints() {
return constraints;
}

public RESTRuleLimits getLimits() {
return limits;
}

public void setConstraints(RESTLayerConstraints constraints) {
this.constraints = constraints;
}

public void setLimits(RESTRuleLimits limits) {
this.limits = limits;
}

public Long getPriority() {
return priority;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/* (c) 2022 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.geofence.services.rest.model;

import org.geoserver.geofence.core.model.enums.CatalogMode;
import org.geoserver.geofence.core.model.enums.SpatialFilterType;

import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;

@XmlRootElement(name = "Limits")
@XmlType(propOrder={"restrictedAreaWkt","spatialFilterType","catalogMode"})
public class RESTRuleLimits {

private String restrictedAreaWkt;
private SpatialFilterType spatialFilterType;
private CatalogMode catalogMode;

public String getRestrictedAreaWkt() {
return restrictedAreaWkt;
}

public void setRestrictedAreaWkt(String restrictedAreaWkt) {
this.restrictedAreaWkt = restrictedAreaWkt;
}

public SpatialFilterType getSpatialFilterType() {
return spatialFilterType;
}

public void setSpatialFilterType(SpatialFilterType spatialFilterType) {
this.spatialFilterType = spatialFilterType;
}

public CatalogMode getCatalogMode() {
return catalogMode;
}

public void setCatalogMode(CatalogMode catalogMode) {
this.catalogMode = catalogMode;
}

@Override
public String toString() {
return "RESTRuleLimits{" +
"restrictedAreaWkt='" + restrictedAreaWkt + '\'' +
", spatialFilterType=" + spatialFilterType +
", catalogMode=" + catalogMode +
'}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,19 @@

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

import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;


import org.apache.commons.lang.StringUtils;

import org.geoserver.geofence.core.model.RuleLimits;
import org.geoserver.geofence.services.rest.model.RESTRuleLimits;
import org.locationtech.jts.geom.Geometry;
import org.locationtech.jts.geom.MultiPolygon;
import org.locationtech.jts.io.ParseException;
Expand Down Expand Up @@ -55,6 +59,7 @@
import java.util.Arrays;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.stream.Collectors;

/**
*
Expand Down Expand Up @@ -88,7 +93,6 @@ public RESTOutputRule get(Long id) throws BadRequestRestEx, NotFoundRestEx, Inte
}

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

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

RuleLimits limits=limitsFromInput(inputRule.getLimits());
if (limits!=null) {
ruleAdminService.setLimits(id,limits);
}

return Response.status(Status.CREATED).tag(id.toString()).entity(id).build();
} catch (BadRequestServiceEx ex) {
LOGGER.error(ex.getMessage());
Expand All @@ -125,6 +134,13 @@ public Response insert(RESTInputRule inputRule) throws NotFoundRestEx, BadReques
}
}

private InsertPosition insertPosition(RESTInputRule inputRule){
return
inputRule.getPosition().getPosition() == RulePosition.fixedPriority ? InsertPosition.FIXED
: inputRule.getPosition().getPosition() == RulePosition.offsetFromBottom ? InsertPosition.FROM_END
: inputRule.getPosition().getPosition() == RulePosition.offsetFromTop ? InsertPosition.FROM_START : null;
}

@Override
public void update(Long id, RESTInputRule rule) throws BadRequestRestEx, NotFoundRestEx, InternalErrorRestEx {

Expand Down Expand Up @@ -579,6 +595,25 @@ protected RESTOutputRule toOutput(Rule rule) {
out.setConstraints(constraints);
}

if (rule.getRuleLimits()!=null){
RESTRuleLimits ruleLimits=new RESTRuleLimits();
RuleLimits limits=rule.getRuleLimits();
if (limits.getSpatialFilterType()!=null)
ruleLimits.setSpatialFilterType(limits.getSpatialFilterType());
if (limits.getCatalogMode()!=null)
ruleLimits.setCatalogMode(limits.getCatalogMode());
if (limits.getAllowedArea()!=null) {
MultiPolygon area = limits.getAllowedArea();
if (area != null) {
String areaWKT = "SRID=" + area.getSRID() + ";" + area.toText();
ruleLimits.setRestrictedAreaWkt(areaWKT);
}
if (limits.getSpatialFilterType()!=null)
ruleLimits.setSpatialFilterType(limits.getSpatialFilterType());
}
out.setLimits(ruleLimits);
}

return out;
}

Expand Down Expand Up @@ -608,6 +643,28 @@ protected Rule fromInput(RESTInputRule in) {
return rule;
}

protected RuleLimits limitsFromInput(RESTRuleLimits in){
RuleLimits limits=null;
if (in!=null && (in.getCatalogMode()!=null || in.getRestrictedAreaWkt()!=null)){
limits=new RuleLimits();
limits.setCatalogMode(in.getCatalogMode());
limits.setSpatialFilterType(in.getSpatialFilterType());
if (StringUtils.isNotBlank(in.getRestrictedAreaWkt())) {
if (in.getRestrictedAreaWkt() != null) {
Geometry g;
try {
g = toGeometryAllowedArea(in.getRestrictedAreaWkt());
} catch (ParseException ex) {
throw new BadRequestRestEx("Error parsing WKT:" + ex.getMessage());
}
limits.setAllowedArea((MultiPolygon) g);
}
}
}
return limits;
}


protected LayerDetails detailsFromInput(RESTInputRule in) {
RESTLayerConstraints constraints = in.getConstraints();
if (constraints != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@

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

import java.util.List;

import org.geoserver.geofence.core.model.Rule;
import org.geoserver.geofence.core.model.enums.CatalogMode;
import org.geoserver.geofence.core.model.enums.SpatialFilterType;
import org.geoserver.geofence.services.RuleAdminService;
import org.geoserver.geofence.services.rest.model.RESTInputUser;
import org.geoserver.geofence.services.rest.model.RESTInputRule;
Expand All @@ -16,6 +20,7 @@
import org.geoserver.geofence.core.model.enums.AccessType;
import org.geoserver.geofence.core.model.enums.GrantType;
import org.geoserver.geofence.services.rest.exception.BadRequestRestEx;
import org.geoserver.geofence.services.rest.model.RESTRuleLimits;
import org.geoserver.geofence.services.rest.model.util.IdName;
import java.util.ArrayList;
import java.util.Arrays;
Expand Down Expand Up @@ -331,6 +336,39 @@ public void testAllowedAreaSRID() {


}

@Test
public void testLimits() {

RESTInputRule rule = new RESTInputRule();
rule.setPosition(new RESTRulePosition(RESTRulePosition.RulePosition.fixedPriority, 42));
rule.setGrant(GrantType.ALLOW);
rule.setWorkspace("wLimits0");
rule.setLayer("lLimits0");
restRuleService.insert(rule);
rule = new RESTInputRule();
rule.setPosition(new RESTRulePosition(RESTRulePosition.RulePosition.offsetFromTop, 0));
rule.setGrant(GrantType.LIMIT);
rule.setWorkspace("wLimits0");
rule.setLayer("lLimits0");

RESTRuleLimits limits = new RESTRuleLimits();
String allowedArea = "MULTIPOLYGON (((4146.5 1301.4, 4147.5 1301.1, 4147.8 1301.4, 4146.5 1301.4)))";
limits.setRestrictedAreaWkt(allowedArea);
limits.setCatalogMode(CatalogMode.HIDE);
limits.setSpatialFilterType(SpatialFilterType.CLIP);
rule.setLimits(limits);

Long rid = (Long) restRuleService.insert(rule).getEntity();
assertNotNull(rid);
RESTOutputRule out = restRuleService.get(rid);
assertNotNull(out);
limits = out.getLimits();
assertEquals(CatalogMode.HIDE, limits.getCatalogMode());
assertEquals(SpatialFilterType.CLIP, limits.getSpatialFilterType());
assertEquals("SRID=4326;" + allowedArea, limits.getRestrictedAreaWkt());
}



}

0 comments on commit 7559b57

Please sign in to comment.