@@ -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 /**
0 commit comments