Skip to content

Commit e4af21b

Browse files
committed
isOwner improvements
1 parent e522435 commit e4af21b

2 files changed

Lines changed: 14 additions & 59 deletions

File tree

src/main/java/com/atomgraph/linkeddatahub/resource/acl/Access.java

Lines changed: 6 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@
4646
import org.apache.jena.ontology.Ontology;
4747
import org.apache.jena.query.ParameterizedSparqlString;
4848
import org.apache.jena.query.Query;
49-
import org.apache.jena.query.QuerySolution;
5049
import org.apache.jena.query.QuerySolutionMap;
5150
import org.apache.jena.query.ResultSetRewindable;
5251
import org.apache.jena.rdf.model.Model;
@@ -162,48 +161,25 @@ public Response get(@QueryParam(QUERY) Query unused,
162161
*/
163162
protected boolean isOwner(Resource accessTo, Resource agent)
164163
{
164+
if (agent == null) return false;
165+
165166
QuerySolutionMap qsm = new QuerySolutionMap();
166167
qsm.add(SPIN.THIS_VAR_NAME, accessTo);
168+
167169
ParameterizedSparqlString pss = getDocumentOwnerQuery();
168170
pss.setParams(qsm);
169171

170-
ResultSetRewindable docOwnerResult = getEndpointAccessor().select(pss.asQuery(), List.of(), List.of());
171-
//loadResultSet(getApplication().getService(), getDocumentOwnerQuery(), qsm); // could use ASK query in principle
172+
ResultSetRewindable ownerResult = getEndpointAccessor().select(pss.asQuery(), List.of(), List.of());
172173
try
173174
{
174-
return isOwner(docOwnerResult, agent);
175+
return ownerResult.hasNext() && agent.equals(ownerResult.next().getResource("owner"));
175176
}
176177
finally
177178
{
178-
docOwnerResult.close();
179+
ownerResult.close();
179180
}
180181
}
181182

182-
/**
183-
* Checks if the given agent is the <code>acl:owner</code> of the document.
184-
*
185-
* @param docOwnerResult the result set containing document metadata
186-
* @param agent the agent whose ownership is checked
187-
* @return true if the agent is the owner, false otherwise.
188-
*/
189-
protected boolean isOwner(ResultSetRewindable docOwnerResult, Resource agent)
190-
{
191-
if (docOwnerResult == null) throw new IllegalArgumentException("ResultSet cannot be null");
192-
if (agent == null) throw new IllegalArgumentException("Agent resource cannot be null");
193-
194-
Resource owner = null;
195-
196-
while (docOwnerResult.hasNext())
197-
{
198-
QuerySolution qs = docOwnerResult.next();
199-
if (owner == null && qs.contains("owner")) owner = qs.getResource("owner");
200-
}
201-
202-
docOwnerResult.reset();
203-
204-
return owner != null && owner.equals(agent);
205-
}
206-
207183
/**
208184
* Creates a special <code>acl:Authorization</code> resource for an owner.
209185
* @param accessTo requested URI

src/main/java/com/atomgraph/linkeddatahub/server/filter/request/AuthorizationFilter.java

Lines changed: 8 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@
4848
import java.util.Set;
4949
import org.apache.jena.query.ParameterizedSparqlString;
5050
import org.apache.jena.query.Query;
51-
import org.apache.jena.query.QuerySolution;
5251
import org.apache.jena.query.QuerySolutionMap;
5352
import org.apache.jena.query.ResultSet;
5453
import org.apache.jena.query.ResultSetRewindable;
@@ -246,44 +245,24 @@ public Resource getAuthorizationByMode(Model authModel, Resource accessMode)
246245
*/
247246
protected boolean isOwner(Resource accessTo, Resource agent)
248247
{
248+
if (agent == null) return false;
249+
249250
QuerySolutionMap qsm = new QuerySolutionMap();
250251
qsm.add(SPIN.THIS_VAR_NAME, accessTo);
251252

252-
ResultSetRewindable docOwnerResult = loadResultSet(getApplication().getService(), getDocumentOwnerQuery(), qsm); // could use ASK query in principle
253+
ParameterizedSparqlString pss = getDocumentOwnerQuery();
254+
pss.setParams(qsm);
255+
256+
ResultSetRewindable ownerResult = loadResultSet(getApplication().getService(), getDocumentOwnerQuery(), qsm); // could use ASK query in principle
253257
try
254258
{
255-
return isOwner(docOwnerResult, agent);
259+
return ownerResult.hasNext() && agent.equals(ownerResult.next().getResource("owner"));
256260
}
257261
finally
258262
{
259-
docOwnerResult.close();
263+
ownerResult.close();
260264
}
261265
}
262-
263-
/**
264-
* Checks if the given agent is the <code>acl:owner</code> of the document.
265-
*
266-
* @param docOwnerResult the result set containing document metadata
267-
* @param agent the agent whose ownership is checked
268-
* @return true if the agent is the owner, false otherwise.
269-
*/
270-
protected boolean isOwner(ResultSetRewindable docOwnerResult, Resource agent)
271-
{
272-
if (docOwnerResult == null) throw new IllegalArgumentException("ResultSet cannot be null");
273-
if (agent == null) throw new IllegalArgumentException("Agent resource cannot be null");
274-
275-
Resource owner = null;
276-
277-
while (docOwnerResult.hasNext())
278-
{
279-
QuerySolution qs = docOwnerResult.next();
280-
if (owner == null && qs.contains("owner")) owner = qs.getResource("owner");
281-
}
282-
283-
docOwnerResult.reset();
284-
285-
return owner != null && owner.equals(agent);
286-
}
287266

288267
/**
289268
* Loads RDF graph from a service.

0 commit comments

Comments
 (0)