Skip to content

Commit 9bc6165

Browse files
ahmedababnehgeotaba90
authored andcommitted
[geoserver#219] GeoFence - Inserting multiple GeoFence rules together
1 parent fd84705 commit 9bc6165

File tree

24 files changed

+629
-19
lines changed

24 files changed

+629
-19
lines changed

src/gui/web/pom.xml

+2-2
Original file line numberDiff line numberDiff line change
@@ -276,8 +276,8 @@
276276
<version>3.1</version>
277277
<configuration>
278278
<encoding>utf8</encoding>
279-
<source>1.5</source>
280-
<target>1.5</target>
279+
<source>1.8</source>
280+
<target>1.8</target>
281281
</configuration>
282282
</plugin>
283283

src/services/core/persistence/src/main/java/org/geoserver/geofence/core/dao/impl/AdminRuleDAOImpl.java

+2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import org.apache.log4j.LogManager;
2121
import org.apache.log4j.Logger;
2222

23+
import org.springframework.transaction.annotation.Propagation;
2324
import org.springframework.transaction.annotation.Transactional;
2425

2526
/**
@@ -66,6 +67,7 @@ public int shift(long priorityStart, long offset) {
6667
}
6768

6869
@Override
70+
@Transactional(propagation = Propagation.REQUIRED, value = "geofenceTransactionManager")
6971
public long persist(AdminRule entity, InsertPosition position) {
7072

7173
return super.persist(AdminRule.class, entity, position);

src/services/core/persistence/src/main/java/org/geoserver/geofence/core/dao/impl/RuleDAOImpl.java

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import org.apache.log4j.Logger;
2222
import org.geoserver.geofence.core.dao.DuplicateKeyException;
2323

24+
import org.springframework.transaction.annotation.Propagation;
2425
import org.springframework.transaction.annotation.Transactional;
2526

2627
/**

src/services/core/services-api/pom.xml

+8-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@
2525
<dependencies>
2626

2727
<!-- APACHE COMMONS -->
28+
<dependency>
29+
<groupId>commons-lang</groupId>
30+
<artifactId>commons-lang</artifactId>
31+
</dependency>
2832
<!-- GeoFence -->
2933
<dependency>
3034
<groupId>org.geoserver.geofence</groupId>
@@ -70,7 +74,10 @@
7074
<artifactId>junit</artifactId>
7175
<scope>test</scope>
7276
</dependency>
73-
77+
<dependency>
78+
<groupId>org.javatuples</groupId>
79+
<artifactId>javatuples</artifactId>
80+
</dependency>
7481
</dependencies>
7582

7683
<build>

src/services/core/services-api/src/main/java/org/geoserver/geofence/services/AdminRuleAdminService.java

+18-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@
1111
import org.geoserver.geofence.services.dto.ShortAdminRule;
1212
import org.geoserver.geofence.services.exception.BadRequestServiceEx;
1313
import org.geoserver.geofence.services.exception.NotFoundServiceEx;
14+
import org.javatuples.Pair;
15+
import org.javatuples.Tuple;
1416

17+
import java.util.ArrayList;
1518
import java.util.List;
1619

1720

@@ -28,6 +31,20 @@ public interface AdminRuleAdminService
2831

2932
long insert(AdminRule rule);
3033

34+
/**
35+
* Inserts a list of admin rules
36+
*
37+
* @param rules list of pair of admin rules to be inserted along with the position.
38+
*/
39+
default List<Long> insert(List<Pair<AdminRule,InsertPosition>> rules) {
40+
List<Long> results=new ArrayList<>(rules.size());
41+
for (Pair<AdminRule,InsertPosition> rule: rules) {
42+
long id=insert(rule.getValue0(),rule.getValue1());
43+
results.add(id);
44+
}
45+
return results;
46+
}
47+
3148
long insert(AdminRule rule, InsertPosition position);
3249

3350
long update(AdminRule rule) throws NotFoundServiceEx;
@@ -128,7 +145,7 @@ public interface AdminRuleAdminService
128145
* @throws BadRequestServiceEx if a wildcard type is used in filter
129146
*/
130147
ShortAdminRule getRule(RuleFilter filter) throws BadRequestServiceEx;
131-
148+
132149
/**
133150
* Return the Rules according to the filter.
134151
* Rules will be enriched with all their joined data, so this method may be heavy to execute.

src/services/core/services-api/src/main/java/org/geoserver/geofence/services/RuleAdminService.java

+23-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55

66
package org.geoserver.geofence.services;
77

8+
import java.util.ArrayList;
9+
import java.util.HashMap;
10+
import java.util.Map;
811
import org.geoserver.geofence.core.model.LayerDetails;
912
import org.geoserver.geofence.core.model.Rule;
1013
import org.geoserver.geofence.core.model.RuleLimits;
@@ -13,7 +16,8 @@
1316
import org.geoserver.geofence.services.dto.ShortRule;
1417
import org.geoserver.geofence.services.exception.BadRequestServiceEx;
1518
import org.geoserver.geofence.services.exception.NotFoundServiceEx;
16-
19+
import org.javatuples.Quartet;
20+
import org.javatuples.Triplet;
1721
import java.util.List;
1822
import java.util.Set;
1923

@@ -33,6 +37,23 @@ public interface RuleAdminService
3337

3438
long insert(Rule rule, InsertPosition position);
3539

40+
/**
41+
* @param rules to be inserted
42+
* @return map of inserted rules along with their ids.
43+
*/
44+
default List<Long> insert(List<Quartet<Rule,RuleLimits,LayerDetails,InsertPosition>> rules) {
45+
List<Long> ids = new ArrayList<>();
46+
for (Quartet<Rule,RuleLimits,LayerDetails,InsertPosition> rule: rules) {
47+
long id = insert(rule.getValue0(),rule.getValue3());
48+
RuleLimits limits=rule.getValue1();
49+
if (limits!=null) setLimits(id,limits);
50+
LayerDetails details=rule.getValue2();
51+
if (details!=null) setDetails(id,details);
52+
ids.add(id);
53+
}
54+
return ids;
55+
}
56+
3657
long update(Rule rule) throws NotFoundServiceEx;
3758

3859
/**
@@ -134,7 +155,7 @@ public interface RuleAdminService
134155
* @throws BadRequestServiceEx if a wildcard type is used in filter
135156
*/
136157
ShortRule getRule(RuleFilter filter) throws BadRequestServiceEx;
137-
158+
138159
/**
139160
* Return the Rules according to the filter.
140161
* Rules will be enriched with all their joined data, so this method may be heavy to execute.

src/services/core/services-impl/pom.xml

+5
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,11 @@
193193
<scope>test</scope>
194194
</dependency>
195195

196+
<dependency>
197+
<groupId>org.javatuples</groupId>
198+
<artifactId>javatuples</artifactId>
199+
</dependency>
200+
196201
</dependencies>
197202

198203
<build>

src/services/core/services-impl/src/main/java/org/geoserver/geofence/services/AdminRuleAdminServiceImpl.java

+11
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@
2626

2727
import org.apache.log4j.LogManager;
2828
import org.apache.log4j.Logger;
29+
import org.javatuples.Pair;
30+
import org.javatuples.Tuple;
31+
import org.springframework.transaction.annotation.Propagation;
32+
import org.springframework.transaction.annotation.Transactional;
2933

3034
/**
3135
*
@@ -47,6 +51,13 @@ public long insert(AdminRule rule) {
4751
return rule.getId();
4852
}
4953

54+
55+
@Override
56+
@Transactional(propagation = Propagation.REQUIRED, value = "geofenceTransactionManager")
57+
public List<Long> insert(List<Pair<AdminRule,InsertPosition>> rules) {
58+
return AdminRuleAdminService.super.insert(rules);
59+
}
60+
5061
@Override
5162
public long insert(AdminRule rule, InsertPosition position) {
5263
ruleDAO.persist(rule, position);

src/services/core/services-impl/src/main/java/org/geoserver/geofence/services/RuleAdminServiceImpl.java

+18
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77

88
import com.googlecode.genericdao.search.Filter;
99
import com.googlecode.genericdao.search.Search;
10+
import java.util.HashMap;
11+
import java.util.Map;
12+
13+
import org.geoserver.geofence.core.dao.DuplicateKeyException;
1014
import org.geoserver.geofence.core.dao.LayerDetailsDAO;
1115
import org.geoserver.geofence.core.dao.RuleDAO;
1216
import org.geoserver.geofence.core.dao.RuleLimitsDAO;
@@ -29,6 +33,10 @@
2933

3034
import org.geoserver.geofence.services.exception.BadRequestServiceEx;
3135
import org.geoserver.geofence.services.exception.NotFoundServiceEx;
36+
import org.javatuples.Quartet;
37+
import org.javatuples.Triplet;
38+
import org.springframework.transaction.annotation.Propagation;
39+
import org.springframework.transaction.annotation.Transactional;
3240

3341
import static org.geoserver.geofence.services.util.FilterUtils.addCriteria;
3442
import static org.geoserver.geofence.services.util.FilterUtils.addFixedCriteria;
@@ -64,6 +72,16 @@ public long insert(Rule rule) {
6472
return rule.getId();
6573
}
6674

75+
/**
76+
* @param rules to be inserted
77+
* @return map of inserted rules along with their ids.
78+
*/
79+
@Override
80+
@Transactional(propagation = Propagation.REQUIRED, value = "geofenceTransactionManager")
81+
public List<Long> insert(List<Quartet<Rule,RuleLimits,LayerDetails,InsertPosition>> rules) {
82+
return RuleAdminService.super.insert(rules);
83+
}
84+
6785
@Override
6886
public long insert(Rule rule, InsertPosition position) {
6987
sanitizeFields(rule);

src/services/modules/rest/api/pom.xml

+2-2
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,8 @@
7575
<groupId>org.apache.maven.plugins</groupId>
7676
<artifactId>maven-compiler-plugin</artifactId>
7777
<configuration>
78-
<source>1.6</source>
79-
<target>1.6</target>
78+
<source>1.8</source>
79+
<target>1.8</target>
8080
</configuration>
8181
</plugin>
8282

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

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

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

8+
import java.util.List;
89
import org.geoserver.geofence.services.rest.exception.BadRequestRestEx;
910
import org.geoserver.geofence.services.rest.exception.InternalErrorRestEx;
1011
import org.geoserver.geofence.services.rest.exception.NotFoundRestEx;
@@ -23,6 +24,7 @@
2324

2425
import org.apache.cxf.jaxrs.ext.multipart.Multipart;
2526
import org.geoserver.geofence.services.rest.model.RESTInputAdminRule;
27+
import org.geoserver.geofence.services.rest.model.RESTInputAdminRuleList;
2628
import org.geoserver.geofence.services.rest.model.RESTOutputAdminRule;
2729
import org.geoserver.geofence.services.rest.model.RESTOutputAdminRuleList;
2830

@@ -40,6 +42,11 @@ public interface RESTAdminRuleService
4042
@Produces(MediaType.APPLICATION_XML)
4143
Response insert(@Multipart("rule") RESTInputAdminRule rule) throws BadRequestRestEx, NotFoundRestEx;
4244

45+
@POST
46+
@Path("/many")
47+
@Produces(MediaType.APPLICATION_XML)
48+
Response insertMany(@Multipart("AdminRuleList") RESTInputAdminRuleList rules) throws BadRequestRestEx, NotFoundRestEx;
49+
4350
@GET
4451
@Path("/id/{id}")
4552
@Produces(MediaType.APPLICATION_XML)

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

+12
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@
55

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

8+
import java.util.List;
89
import org.geoserver.geofence.services.rest.exception.BadRequestRestEx;
910
import org.geoserver.geofence.services.rest.exception.InternalErrorRestEx;
1011
import org.geoserver.geofence.services.rest.exception.NotFoundRestEx;
1112
import org.geoserver.geofence.services.rest.model.RESTInputRule;
13+
import org.geoserver.geofence.services.rest.model.RESTInputRuleList;
1214
import org.geoserver.geofence.services.rest.model.RESTOutputRule;
1315
import org.geoserver.geofence.services.rest.model.RESTOutputRuleList;
1416

@@ -40,6 +42,16 @@ public interface RESTRuleService
4042
@Produces(MediaType.TEXT_PLAIN)
4143
Response insert(@Multipart("rule") RESTInputRule rule) throws BadRequestRestEx, NotFoundRestEx;
4244

45+
@POST
46+
@Path("/many")
47+
@Produces(MediaType.TEXT_PLAIN)
48+
default Response insertMany(@Multipart("RuleList") RESTInputRuleList rules) throws BadRequestRestEx, NotFoundRestEx {
49+
for (RESTInputRule r: rules){
50+
insert(r);
51+
}
52+
return Response.status(Response.Status.CREATED).build();
53+
}
54+
4355
@GET
4456
@Path("/id/{id}")
4557
@Produces(MediaType.APPLICATION_XML)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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 javax.xml.bind.annotation.XmlElement;
8+
import javax.xml.bind.annotation.XmlRootElement;
9+
import java.util.Iterator;
10+
import java.util.List;
11+
12+
@XmlRootElement(name = "AdminRuleList")
13+
public class RESTInputAdminRuleList implements Iterable<RESTInputAdminRule> {
14+
15+
private List<RESTInputAdminRule> list;
16+
17+
18+
19+
@XmlElement(name = "adminrule")
20+
public List<RESTInputAdminRule> getList() {
21+
return list;
22+
}
23+
24+
public void setList(List<RESTInputAdminRule> userList) {
25+
this.list = userList;
26+
}
27+
28+
@Override
29+
public String toString() {
30+
return getClass().getSimpleName() + "[" + list.size() + " users]";
31+
}
32+
33+
@Override
34+
public Iterator<RESTInputAdminRule> iterator() {
35+
return list.iterator();
36+
}
37+
}

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;

0 commit comments

Comments
 (0)