Skip to content

Commit

Permalink
[geoserver#219] GeoFence - Inserting multiple GeoFence rules together
Browse files Browse the repository at this point in the history
  • Loading branch information
ahmedababnehgeo authored and taba90 committed Jul 14, 2022
1 parent fd84705 commit 9bc6165
Show file tree
Hide file tree
Showing 24 changed files with 629 additions and 19 deletions.
4 changes: 2 additions & 2 deletions src/gui/web/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -276,8 +276,8 @@
<version>3.1</version>
<configuration>
<encoding>utf8</encoding>
<source>1.5</source>
<target>1.5</target>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;

import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
Expand Down Expand Up @@ -66,6 +67,7 @@ public int shift(long priorityStart, long offset) {
}

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

return super.persist(AdminRule.class, entity, position);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.apache.log4j.Logger;
import org.geoserver.geofence.core.dao.DuplicateKeyException;

import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
Expand Down
9 changes: 8 additions & 1 deletion src/services/core/services-api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@
<dependencies>

<!-- APACHE COMMONS -->
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
</dependency>
<!-- GeoFence -->
<dependency>
<groupId>org.geoserver.geofence</groupId>
Expand Down Expand Up @@ -70,7 +74,10 @@
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.javatuples</groupId>
<artifactId>javatuples</artifactId>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@
import org.geoserver.geofence.services.dto.ShortAdminRule;
import org.geoserver.geofence.services.exception.BadRequestServiceEx;
import org.geoserver.geofence.services.exception.NotFoundServiceEx;
import org.javatuples.Pair;
import org.javatuples.Tuple;

import java.util.ArrayList;
import java.util.List;


Expand All @@ -28,6 +31,20 @@ public interface AdminRuleAdminService

long insert(AdminRule rule);

/**
* Inserts a list of admin rules
*
* @param rules list of pair of admin rules to be inserted along with the position.
*/
default List<Long> insert(List<Pair<AdminRule,InsertPosition>> rules) {
List<Long> results=new ArrayList<>(rules.size());
for (Pair<AdminRule,InsertPosition> rule: rules) {
long id=insert(rule.getValue0(),rule.getValue1());
results.add(id);
}
return results;
}

long insert(AdminRule rule, InsertPosition position);

long update(AdminRule rule) throws NotFoundServiceEx;
Expand Down Expand Up @@ -128,7 +145,7 @@ public interface AdminRuleAdminService
* @throws BadRequestServiceEx if a wildcard type is used in filter
*/
ShortAdminRule getRule(RuleFilter filter) throws BadRequestServiceEx;

/**
* Return the Rules according to the filter.
* Rules will be enriched with all their joined data, so this method may be heavy to execute.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@

package org.geoserver.geofence.services;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import org.geoserver.geofence.core.model.LayerDetails;
import org.geoserver.geofence.core.model.Rule;
import org.geoserver.geofence.core.model.RuleLimits;
Expand All @@ -13,7 +16,8 @@
import org.geoserver.geofence.services.dto.ShortRule;
import org.geoserver.geofence.services.exception.BadRequestServiceEx;
import org.geoserver.geofence.services.exception.NotFoundServiceEx;

import org.javatuples.Quartet;
import org.javatuples.Triplet;
import java.util.List;
import java.util.Set;

Expand All @@ -33,6 +37,23 @@ public interface RuleAdminService

long insert(Rule rule, InsertPosition position);

/**
* @param rules to be inserted
* @return map of inserted rules along with their ids.
*/
default List<Long> insert(List<Quartet<Rule,RuleLimits,LayerDetails,InsertPosition>> rules) {
List<Long> ids = new ArrayList<>();
for (Quartet<Rule,RuleLimits,LayerDetails,InsertPosition> rule: rules) {
long id = insert(rule.getValue0(),rule.getValue3());
RuleLimits limits=rule.getValue1();
if (limits!=null) setLimits(id,limits);
LayerDetails details=rule.getValue2();
if (details!=null) setDetails(id,details);
ids.add(id);
}
return ids;
}

long update(Rule rule) throws NotFoundServiceEx;

/**
Expand Down Expand Up @@ -134,7 +155,7 @@ public interface RuleAdminService
* @throws BadRequestServiceEx if a wildcard type is used in filter
*/
ShortRule getRule(RuleFilter filter) throws BadRequestServiceEx;

/**
* Return the Rules according to the filter.
* Rules will be enriched with all their joined data, so this method may be heavy to execute.
Expand Down
5 changes: 5 additions & 0 deletions src/services/core/services-impl/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,11 @@
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.javatuples</groupId>
<artifactId>javatuples</artifactId>
</dependency>

</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.javatuples.Pair;
import org.javatuples.Tuple;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
*
Expand All @@ -47,6 +51,13 @@ public long insert(AdminRule rule) {
return rule.getId();
}


@Override
@Transactional(propagation = Propagation.REQUIRED, value = "geofenceTransactionManager")
public List<Long> insert(List<Pair<AdminRule,InsertPosition>> rules) {
return AdminRuleAdminService.super.insert(rules);
}

@Override
public long insert(AdminRule rule, InsertPosition position) {
ruleDAO.persist(rule, position);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@

import com.googlecode.genericdao.search.Filter;
import com.googlecode.genericdao.search.Search;
import java.util.HashMap;
import java.util.Map;

import org.geoserver.geofence.core.dao.DuplicateKeyException;
import org.geoserver.geofence.core.dao.LayerDetailsDAO;
import org.geoserver.geofence.core.dao.RuleDAO;
import org.geoserver.geofence.core.dao.RuleLimitsDAO;
Expand All @@ -29,6 +33,10 @@

import org.geoserver.geofence.services.exception.BadRequestServiceEx;
import org.geoserver.geofence.services.exception.NotFoundServiceEx;
import org.javatuples.Quartet;
import org.javatuples.Triplet;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import static org.geoserver.geofence.services.util.FilterUtils.addCriteria;
import static org.geoserver.geofence.services.util.FilterUtils.addFixedCriteria;
Expand Down Expand Up @@ -64,6 +72,16 @@ public long insert(Rule rule) {
return rule.getId();
}

/**
* @param rules to be inserted
* @return map of inserted rules along with their ids.
*/
@Override
@Transactional(propagation = Propagation.REQUIRED, value = "geofenceTransactionManager")
public List<Long> insert(List<Quartet<Rule,RuleLimits,LayerDetails,InsertPosition>> rules) {
return RuleAdminService.super.insert(rules);
}

@Override
public long insert(Rule rule, InsertPosition position) {
sanitizeFields(rule);
Expand Down
4 changes: 2 additions & 2 deletions src/services/modules/rest/api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.6</target>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

package org.geoserver.geofence.services.rest;

import java.util.List;
import org.geoserver.geofence.services.rest.exception.BadRequestRestEx;
import org.geoserver.geofence.services.rest.exception.InternalErrorRestEx;
import org.geoserver.geofence.services.rest.exception.NotFoundRestEx;
Expand All @@ -23,6 +24,7 @@

import org.apache.cxf.jaxrs.ext.multipart.Multipart;
import org.geoserver.geofence.services.rest.model.RESTInputAdminRule;
import org.geoserver.geofence.services.rest.model.RESTInputAdminRuleList;
import org.geoserver.geofence.services.rest.model.RESTOutputAdminRule;
import org.geoserver.geofence.services.rest.model.RESTOutputAdminRuleList;

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

@POST
@Path("/many")
@Produces(MediaType.APPLICATION_XML)
Response insertMany(@Multipart("AdminRuleList") RESTInputAdminRuleList rules) throws BadRequestRestEx, NotFoundRestEx;

@GET
@Path("/id/{id}")
@Produces(MediaType.APPLICATION_XML)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@

package org.geoserver.geofence.services.rest;

import java.util.List;
import org.geoserver.geofence.services.rest.exception.BadRequestRestEx;
import org.geoserver.geofence.services.rest.exception.InternalErrorRestEx;
import org.geoserver.geofence.services.rest.exception.NotFoundRestEx;
import org.geoserver.geofence.services.rest.model.RESTInputRule;
import org.geoserver.geofence.services.rest.model.RESTInputRuleList;
import org.geoserver.geofence.services.rest.model.RESTOutputRule;
import org.geoserver.geofence.services.rest.model.RESTOutputRuleList;

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

@POST
@Path("/many")
@Produces(MediaType.TEXT_PLAIN)
default Response insertMany(@Multipart("RuleList") RESTInputRuleList rules) throws BadRequestRestEx, NotFoundRestEx {
for (RESTInputRule r: rules){
insert(r);
}
return Response.status(Response.Status.CREATED).build();
}

@GET
@Path("/id/{id}")
@Produces(MediaType.APPLICATION_XML)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/* (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 javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import java.util.Iterator;
import java.util.List;

@XmlRootElement(name = "AdminRuleList")
public class RESTInputAdminRuleList implements Iterable<RESTInputAdminRule> {

private List<RESTInputAdminRule> list;



@XmlElement(name = "adminrule")
public List<RESTInputAdminRule> getList() {
return list;
}

public void setList(List<RESTInputAdminRule> userList) {
this.list = userList;
}

@Override
public String toString() {
return getClass().getSimpleName() + "[" + list.size() + " users]";
}

@Override
public Iterator<RESTInputAdminRule> iterator() {
return list.iterator();
}
}
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

0 comments on commit 9bc6165

Please sign in to comment.