Skip to content

Commit cbdaf68

Browse files
committed
Varnish ban fix in ClearOntology
1 parent 06451cc commit cbdaf68

4 files changed

Lines changed: 51 additions & 48 deletions

File tree

http-tests/admin/packages/uninstall-package-ontology.sh

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,8 @@ curl -k -w "%{http_code}\n" -o /dev/null -f -s \
2222
| grep -q "$STATUS_SEE_OTHER"
2323

2424
# verify owl:imports triple exists before uninstall
25-
curl -k -s \
26-
-H "Accept: application/n-triples" \
27-
"${END_USER_BASE_URL}ns" \
28-
| grep -q "<${namespace_ontology_uri}> <http://www.w3.org/2002/07/owl#imports> <${package_ontology_uri}>"
25+
ns_before=$(curl -k -s -H "Accept: application/n-triples" "${END_USER_BASE_URL}ns")
26+
echo "$ns_before" | grep -q "<${namespace_ontology_uri}> <http://www.w3.org/2002/07/owl#imports> <${package_ontology_uri}>"
2927

3028
# uninstall package via POST to packages/uninstall endpoint
3129
curl -k -w "%{http_code}\n" -o /dev/null -f -s \
@@ -37,13 +35,14 @@ curl -k -w "%{http_code}\n" -o /dev/null -f -s \
3735
| grep -q "$STATUS_SEE_OTHER"
3836

3937
# verify owl:imports triple was removed from namespace graph
40-
curl -k -s \
41-
-H "Accept: application/n-triples" \
42-
"${END_USER_BASE_URL}ns" \
43-
| grep -v -q "<${namespace_ontology_uri}> <http://www.w3.org/2002/07/owl#imports> <${package_ontology_uri}>"
38+
ns_after=$(curl -k -s -H "Accept: application/n-triples" "${END_USER_BASE_URL}ns")
39+
if echo "$ns_after" | grep -q "<${namespace_ontology_uri}> <http://www.w3.org/2002/07/owl#imports> <${package_ontology_uri}>"; then
40+
exit 1
41+
fi
4442

4543
# verify package ontology document was deleted
4644
package_ontology_hash=$(echo -n "$package_ontology_uri" | shasum -a 1 | cut -d' ' -f1)
47-
curl -k -w "%{http_code}\n" -o /dev/null -s \
48-
"${ADMIN_BASE_URL}ontologies/${package_ontology_hash}/" \
49-
| grep -q "$STATUS_NOT_FOUND"
45+
ontology_status=$(curl -k -w "%{http_code}\n" -o /dev/null -s \
46+
-E "$OWNER_CERT_FILE":"$OWNER_CERT_PWD" \
47+
"${ADMIN_BASE_URL}ontologies/${package_ontology_hash}/")
48+
echo "$ontology_status" | grep -q "$STATUS_FORBIDDEN"

src/main/java/com/atomgraph/linkeddatahub/resource/admin/ClearOntology.java

Lines changed: 40 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -81,33 +81,40 @@ public Response post(@FormParam("uri") String ontologyURI, @HeaderParam("Referer
8181
{
8282
if (ontologyURI == null) throw new BadRequestException("Ontology URI not specified");
8383

84-
EndUserApplication app = getApplication().as(AdminApplication.class).getEndUserApplication(); // we're assuming the current app is admin
85-
OntModelSpec ontModelSpec = new OntModelSpec(getSystem().getOntModelSpec(app));
84+
EndUserApplication endUserApp = getApplication().as(AdminApplication.class).getEndUserApplication(); // we're assuming the current app is admin
85+
OntModelSpec ontModelSpec = new OntModelSpec(getSystem().getOntModelSpec(endUserApp));
8686
if (ontModelSpec.getDocumentManager().getFileManager().hasCachedModel(ontologyURI))
8787
{
8888
if (log.isDebugEnabled()) log.debug("Clearing ontology with URI '{}' from memory", ontologyURI);
8989
ontModelSpec.getDocumentManager().getFileManager().removeCacheModel(ontologyURI);
90+
91+
URI ontologyDocURI = UriBuilder.fromUri(ontologyURI).fragment(null).build(); // skip fragment from the ontology URI to get its graph URI
92+
// purge from admin cache
9093
if (getApplication().getFrontendProxy() != null)
9194
{
92-
URI ontologyDocURI = UriBuilder.fromUri(ontologyURI).fragment(null).build(); // skip fragment from the ontology URI to get its graph URI
9395
if (log.isDebugEnabled()) log.debug("Purge ontology document with URI '{}' from frontend proxy cache", ontologyDocURI);
94-
try (Response response = purge(getApplication().getFrontendProxy(), ontologyDocURI.toString()))
95-
{
96-
// Response automatically closed by try-with-resources
97-
}
96+
ban(getApplication().getFrontendProxy(), ontologyDocURI.toString(), false);
9897
}
9998
if (getApplication().getService().getBackendProxy() != null)
10099
{
101100
if (log.isDebugEnabled()) log.debug("Ban ontology with URI '{}' from backend proxy cache", ontologyURI);
102-
try (Response response = ban(getApplication().getService().getBackendProxy(), ontologyURI))
103-
{
104-
// Response automatically closed by try-with-resources
105-
}
101+
ban(getApplication().getService().getBackendProxy(), ontologyURI);
102+
}
103+
// purge from end-user cache
104+
if (endUserApp.getFrontendProxy() != null)
105+
{
106+
if (log.isDebugEnabled()) log.debug("Purge ontology document with URI '{}' from frontend proxy cache", ontologyDocURI);
107+
ban(endUserApp.getFrontendProxy(), ontologyDocURI.toString(), false);
108+
}
109+
if (endUserApp.getService().getBackendProxy() != null)
110+
{
111+
if (log.isDebugEnabled()) log.debug("Ban ontology with URI '{}' from backend proxy cache", ontologyURI);
112+
ban(endUserApp.getService().getBackendProxy(), ontologyURI);
106113
}
107114

108115
// !!! we need to reload the ontology model before returning a response, to make sure the next request already gets the new version !!!
109116
// same logic as in OntologyFilter. TO-DO: encapsulate?
110-
OntologyModelGetter modelGetter = new OntologyModelGetter(app, ontModelSpec, getSystem().getOntologyQuery());
117+
OntologyModelGetter modelGetter = new OntologyModelGetter(endUserApp, ontModelSpec, getSystem().getOntologyQuery());
111118
ontModelSpec.setImportModelGetter(modelGetter);
112119
if (log.isDebugEnabled()) log.debug("Started loading ontology with URI '{}' from the admin dataset", ontologyURI);
113120
Model baseModel = modelGetter.getModel(ontologyURI);
@@ -125,36 +132,36 @@ public Response post(@FormParam("uri") String ontologyURI, @HeaderParam("Referer
125132
else return Response.ok().build();
126133
}
127134

135+
public void ban(Resource proxy, String url)
136+
{
137+
ban(proxy, url, true);
138+
}
139+
128140
/**
129141
* Bans URL from the backend proxy cache.
130142
*
131143
* @param proxy proxy server URL
132144
* @param url banned URL
133-
* @return proxy server response
134-
*/
135-
public Response ban(Resource proxy, String url)
136-
{
137-
if (url == null) throw new IllegalArgumentException("Resource cannot be null");
138-
139-
return getSystem().getClient().target(proxy.getURI()).request().
140-
header(CacheInvalidationFilter.HEADER_NAME, UriComponent.encode(url, UriComponent.Type.UNRESERVED)). // the value has to be URL-encoded in order to match request URLs in Varnish
141-
method("BAN", Response.class);
142-
}
143-
144-
/**
145-
* Purges URL from proxy cache.
146-
*
147-
* @param proxy proxy resource
148-
* @param url URL to be banned
149-
* @return response from proxy
145+
* @param urlEncode if true, the banned URL value will be URL-encoded
150146
*/
151-
public Response purge(Resource proxy, String url)
147+
public void ban(Resource proxy, String url, boolean urlEncode)
152148
{
153149
if (url == null) throw new IllegalArgumentException("Resource cannot be null");
154150

155-
// create new Client instance, otherwise ApacheHttpClient reuses connection and Varnish ignores BAN request
156-
return getSystem().getClient().target(proxy.getURI()).request().
157-
method("PURGE", Response.class);
151+
// Extract path from URL - Varnish req.url only contains the path, not the full URL
152+
URI uri = URI.create(url);
153+
String path = uri.getPath();
154+
if (uri.getQuery() != null) path += "?" + uri.getQuery();
155+
156+
final String urlValue = urlEncode ? UriComponent.encode(path, UriComponent.Type.UNRESERVED) : path;
157+
158+
try (Response cr = getSystem().getClient().target(proxy.getURI()).
159+
request().
160+
header(CacheInvalidationFilter.HEADER_NAME, urlValue).
161+
method("BAN", Response.class))
162+
{
163+
// Response automatically closed by try-with-resources
164+
}
158165
}
159166

160167
/**

src/main/java/com/atomgraph/linkeddatahub/resource/admin/pkg/InstallPackage.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,6 @@ private void installOntology(EndUserApplication app, Model ontologyModel, String
330330
"INSERT { <%s> owl:imports <%s> } WHERE { }",
331331
namespaceOntologyURI, packageOntologyURI
332332
);
333-
334333
UpdateRequest updateRequest = UpdateFactory.create(updateString);
335334

336335
try (Response patchResponse = gsc.patch(namespaceGraphURI, updateRequest))

src/main/java/com/atomgraph/linkeddatahub/resource/admin/pkg/UninstallPackage.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -211,8 +211,7 @@ private void uninstallOntology(EndUserApplication app, String packageURI) throws
211211

212212
try (Response deleteResponse = gsc.delete(ontologyDocumentURI))
213213
{
214-
if (!deleteResponse.getStatusInfo().getFamily().equals(Response.Status.Family.SUCCESSFUL) &&
215-
deleteResponse.getStatus() != 404) // 404 is OK - document already deleted
214+
if (!deleteResponse.getStatusInfo().getFamily().equals(Response.Status.Family.SUCCESSFUL))
216215
{
217216
if (log.isErrorEnabled()) log.error("Failed to DELETE package ontology document {}: {}", ontologyDocumentURI, deleteResponse.getStatus());
218217
throw new IOException("Failed to DELETE package ontology document " + ontologyDocumentURI + ": " + deleteResponse.getStatus());
@@ -231,7 +230,6 @@ private void uninstallOntology(EndUserApplication app, String packageURI) throws
231230
"DELETE WHERE { <%s> owl:imports <%s> }",
232231
namespaceOntologyURI, packageOntologyURI
233232
);
234-
235233
UpdateRequest updateRequest = UpdateFactory.create(updateString);
236234

237235
try (Response patchResponse = gsc.patch(namespaceGraphURI, updateRequest))

0 commit comments

Comments
 (0)