Skip to content

Commit 3f4660e

Browse files
committed
Refactored Access endpoint to use getEndpointAccessor()
1 parent 693e9e3 commit 3f4660e

2 files changed

Lines changed: 21 additions & 53 deletions

File tree

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

Lines changed: 13 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import static com.atomgraph.core.model.SPARQLEndpoint.QUERY;
2323
import com.atomgraph.linkeddatahub.apps.model.AdminApplication;
2424
import com.atomgraph.linkeddatahub.apps.model.Application;
25-
import com.atomgraph.linkeddatahub.client.SesameProtocolClient;
2625
import com.atomgraph.linkeddatahub.model.Service;
2726
import com.atomgraph.linkeddatahub.model.auth.Agent;
2827
import com.atomgraph.linkeddatahub.server.security.AgentContext;
@@ -49,7 +48,6 @@
4948
import org.apache.jena.query.Query;
5049
import org.apache.jena.query.QuerySolution;
5150
import org.apache.jena.query.QuerySolutionMap;
52-
import org.apache.jena.query.ResultSet;
5351
import org.apache.jena.query.ResultSetRewindable;
5452
import org.apache.jena.rdf.model.Model;
5553
import org.apache.jena.rdf.model.ModelFactory;
@@ -104,7 +102,7 @@ public Access(@Context Request request, @Context UriInfo uriInfo,
104102

105103
@Override
106104
@GET
107-
public Response get(@QueryParam(QUERY) Query query,
105+
public Response get(@QueryParam(QUERY) Query unused,
108106
@QueryParam(DEFAULT_GRAPH_URI) List<URI> defaultGraphUris, @QueryParam(NAMED_GRAPH_URI) List<URI> namedGraphUris)
109107
{
110108
final Agent agent = getAgentContext().map(AgentContext::getAgent).orElse(null);
@@ -113,9 +111,7 @@ public Response get(@QueryParam(QUERY) Query query,
113111
// addProperty(RDF.type, FOAF.Agent).
114112
// as(Agent.class);
115113

116-
//final ParameterizedSparqlString pss = getApplication().canAs(EndUserApplication.class) ? getACLQuery() : getOwnerACLQuery();
117-
final ParameterizedSparqlString authPss = getACLQuery();
118-
114+
//final ParameterizedSparqlString pss = getApplication().canAs(EndUserApplication.class) ? getACLQuery() : getOwnerACLQuery();
119115
try
120116
{
121117
if (!getUriInfo().getQueryParameters().containsKey(SPIN.THIS_VAR_NAME)) throw new BadRequestException("?this query param is not provided");
@@ -125,15 +121,18 @@ public Response get(@QueryParam(QUERY) Query query,
125121

126122
QuerySolutionMap thisQsm = new QuerySolutionMap();
127123
thisQsm.add(SPIN.THIS_VAR_NAME, accessTo);
128-
ResultSetRewindable docTypesResult = loadResultSet(getEndUserService(), getDocumentTypeQuery(), thisQsm);
124+
ParameterizedSparqlString typePss = getDocumentTypeQuery();
125+
typePss.setParams(thisQsm);
129126

127+
ResultSetRewindable docTypesResult = getEndpointAccessor().select(typePss.asQuery(), null, null);
130128
try
131129
{
130+
final ParameterizedSparqlString authPss = getACLQuery();
132131
authPss.setParams(new AuthorizationParams(getApplication().getBase(), accessTo, agent).get());
133-
query = new SetResultSetValues().apply(authPss.asQuery(), docTypesResult);
134-
assert query.toString().contains("VALUES");
132+
Query authQuery = new SetResultSetValues().apply(authPss.asQuery(), docTypesResult);
133+
assert authQuery.toString().contains("VALUES");
135134

136-
Model authModel = getEndpointAccessor().loadModel(query, defaultGraphUris, namedGraphUris);
135+
Model authModel = getEndpointAccessor().loadModel(authQuery, defaultGraphUris, namedGraphUris);
137136
// special case where the agent is the owner of the requested document - automatically grant acl:Read/acl:Append/acl:Write access
138137
if (isOwner(accessTo, agent))
139138
{
@@ -154,38 +153,6 @@ public Response get(@QueryParam(QUERY) Query query,
154153
}
155154
}
156155

157-
/**
158-
* Loads SPARQL result set from a service.
159-
*
160-
* @param service SPARQL service
161-
* @param pss auth query string
162-
* @param qsm query solution map (applied to the query string or sent as request params, depending on the protocol)
163-
* @return authorization graph (can be empty)
164-
* @see com.atomgraph.linkeddatahub.vocabulary.LDHC#authQuery
165-
*/
166-
protected ResultSetRewindable loadResultSet(com.atomgraph.linkeddatahub.model.Service service, ParameterizedSparqlString pss, QuerySolutionMap qsm)
167-
{
168-
if (service == null) throw new IllegalArgumentException("Service cannot be null");
169-
if (pss == null) throw new IllegalArgumentException("ParameterizedSparqlString cannot be null");
170-
if (qsm == null) throw new IllegalArgumentException("QuerySolutionMap cannot be null");
171-
172-
// send query bindings separately from the query if the service supports the Sesame protocol
173-
if (service.getSPARQLClient() instanceof SesameProtocolClient sesameProtocolClient)
174-
try (Response cr = sesameProtocolClient.query(pss.asQuery(), ResultSet.class, qsm)) // register(new CacheControlFilter(CacheControl.valueOf("no-cache"))). // add Cache-Control: no-cache to request
175-
{
176-
return cr.readEntity(ResultSetRewindable.class);
177-
}
178-
else
179-
{
180-
pss.setParams(qsm);
181-
try (Response cr = service.getSPARQLClient(). // register(new CacheControlFilter(CacheControl.valueOf("no-cache"))). // add Cache-Control: no-cache to request
182-
query(pss.asQuery(), ResultSet.class))
183-
{
184-
return cr.readEntity(ResultSetRewindable.class);
185-
}
186-
}
187-
}
188-
189156
/**
190157
* Checks if the given agent is the <code>acl:owner</code> of the document.
191158
*
@@ -197,8 +164,11 @@ protected boolean isOwner(Resource accessTo, Resource agent)
197164
{
198165
QuerySolutionMap qsm = new QuerySolutionMap();
199166
qsm.add(SPIN.THIS_VAR_NAME, accessTo);
167+
ParameterizedSparqlString pss = getDocumentOwnerQuery();
168+
pss.setParams(qsm);
200169

201-
ResultSetRewindable docOwnerResult = loadResultSet(getApplication().getService(), getDocumentOwnerQuery(), qsm); // could use ASK query in principle
170+
ResultSetRewindable docOwnerResult = getEndpointAccessor().select(pss.asQuery(), null, null);
171+
//loadResultSet(getApplication().getService(), getDocumentOwnerQuery(), qsm); // could use ASK query in principle
202172
try
203173
{
204174
return isOwner(docOwnerResult, agent);

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

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -191,24 +191,22 @@ public Resource authorize(ContainerRequestContext request, Resource agent, Resou
191191
thisQsm = new QuerySolutionMap();
192192
thisQsm.add(SPIN.THIS_VAR_NAME, accessTo);
193193

194-
// special case where the agent is the owner of the requested document - automatically grant acl:Read/acl:Append/acl:Write access
195-
if (agent != null && isOwner(accessTo, agent))
196-
{
197-
log.debug("Agent <{}> is the owner of <{}>, granting acl:Read/acl:Append/acl:Write access", agent, accessTo);
198-
return createOwnerAuthorization(accessTo, agent);
199-
}
200-
201194
docTypesResult.close();
202195
docTypesResult = loadResultSet(getApplication().getService(), getDocumentTypeQuery(), thisQsm);
203196
try
204197
{
205198
Set<Resource> parentTypes = new HashSet<>();
206199
docTypesResult.forEachRemaining(qs -> parentTypes.add(qs.getResource("Type")));
207200

208-
// only root and containers allow child documents
201+
// only root and containers allow child documents. This needs to be checked before checking ownership
209202
if (Collections.disjoint(parentTypes, Set.of(Default.Root, DH.Container))) return null;
210-
211-
docTypesResult.reset(); // rewind result set to the beginning
203+
204+
// special case where the agent is the owner of the requested document - automatically grant acl:Read/acl:Append/acl:Write access
205+
if (agent != null && isOwner(accessTo, agent))
206+
{
207+
log.debug("Agent <{}> is the owner of <{}>, granting acl:Read/acl:Append/acl:Write access", agent, accessTo);
208+
return createOwnerAuthorization(accessTo, agent);
209+
}
212210
}
213211
finally
214212
{

0 commit comments

Comments
 (0)