1616 */
1717package com .atomgraph .linkeddatahub .resource .admin .pkg ;
1818
19+ import com .atomgraph .client .util .DataManager ;
1920import com .atomgraph .linkeddatahub .apps .model .AdminApplication ;
2021import com .atomgraph .linkeddatahub .apps .model .EndUserApplication ;
2122import com .atomgraph .linkeddatahub .client .LinkedDataClient ;
4950import java .util .ArrayList ;
5051import java .util .List ;
5152import java .util .Set ;
53+ import org .apache .jena .util .FileManager ;
5254
5355/**
5456 * JAX-RS resource that installs a LinkedDataHub package.
@@ -67,6 +69,7 @@ public class Install
6769
6870 private final com .atomgraph .linkeddatahub .apps .model .Application application ;
6971 private final com .atomgraph .linkeddatahub .Application system ;
72+ private final DataManager dataManager ;
7073
7174 @ Context ServletContext servletContext ;
7275
@@ -75,13 +78,16 @@ public class Install
7578 *
7679 * @param application matched application (admin app)
7780 * @param system system application
81+ * @param dataManager data manager
7882 */
7983 @ Inject
8084 public Install (com .atomgraph .linkeddatahub .apps .model .Application application ,
81- com .atomgraph .linkeddatahub .Application system )
85+ com .atomgraph .linkeddatahub .Application system ,
86+ DataManager dataManager )
8287 {
8388 this .application = application ;
8489 this .system = system ;
90+ this .dataManager = dataManager ;
8591 }
8692
8793 /**
@@ -112,14 +118,13 @@ public Response post(@FormParam("package-uri") String packageURI, @HeaderParam("
112118
113119 if (ontology == null ) throw new BadRequestException ("Package ontology not found" );
114120
115- URI ontologyURI = URI .create (ontology .getURI ());
116121 URI stylesheetURI = (stylesheet != null ) ? URI .create (stylesheet .getURI ()) : null ;
117122
118123 String packagePath = UriPath .convert (packageURI );
119124
120125 // 2. Download and install ontology
121- if (log .isDebugEnabled ()) log .debug ("Downloading package ontology from: {}" , ontologyURI );
122- Model ontologyModel = downloadOntology (ontologyURI );
126+ if (log .isDebugEnabled ()) log .debug ("Downloading package ontology from: {}" , ontology . getURI () );
127+ Model ontologyModel = downloadOntology (ontology . getURI () );
123128 installOntology (endUserApp , ontologyModel );
124129
125130 // 3. Download and install stylesheet if present
@@ -164,8 +169,21 @@ private com.atomgraph.linkeddatahub.apps.model.Package getPackage(String package
164169 {
165170 if (log .isDebugEnabled ()) log .debug ("Loading package from: {}" , packageURI );
166171
167- LinkedDataClient ldc = LinkedDataClient .create (getSystem ().getClient (), getSystem ().getMediaTypes ());
168- Model model = ldc .getModel (packageURI );
172+ final Model model ;
173+
174+ // check if we have the model in the cache first and if yes, return it from there instead making an HTTP request
175+ if (((FileManager )getDataManager ()).hasCachedModel (packageURI ) ||
176+ (getDataManager ().isResolvingMapped () && getDataManager ().isMapped (packageURI ))) // read mapped URIs (such as system ontologies) from a file
177+ {
178+ if (log .isDebugEnabled ()) log .debug ("hasCachedModel({}): {}" , packageURI , ((FileManager )getDataManager ()).hasCachedModel (packageURI ));
179+ if (log .isDebugEnabled ()) log .debug ("isMapped({}): {}" , packageURI , getDataManager ().isMapped (packageURI ));
180+ model = getDataManager ().loadModel (packageURI );
181+ }
182+ else
183+ {
184+ LinkedDataClient ldc = LinkedDataClient .create (getSystem ().getClient (), getSystem ().getMediaTypes ());
185+ model = ldc .getModel (packageURI );
186+ }
169187
170188 return model .getResource (packageURI ).as (com .atomgraph .linkeddatahub .apps .model .Package .class );
171189 }
@@ -185,12 +203,23 @@ private com.atomgraph.linkeddatahub.apps.model.Package getPackage(String package
185203 /**
186204 * Downloads RDF from a URI using LinkedDataClient.
187205 */
188- private Model downloadOntology (URI uri ) throws IOException
206+ private Model downloadOntology (String uri ) throws IOException
189207 {
190- if (log .isDebugEnabled ()) log .debug ("Downloading RDF from: {}" , uri );
208+ if (log .isDebugEnabled ()) log .debug ("Downloading ontology from: {}" , uri );
191209
192- LinkedDataClient ldc = LinkedDataClient .create (getSystem ().getClient (), getSystem ().getMediaTypes ());
193- return ldc .getModel (uri .toString ());
210+ // check if we have the model in the cache first and if yes, return it from there instead making an HTTP request
211+ if (((FileManager )getDataManager ()).hasCachedModel (uri ) ||
212+ (getDataManager ().isResolvingMapped () && getDataManager ().isMapped (uri ))) // read mapped URIs (such as system ontologies) from a file
213+ {
214+ if (log .isDebugEnabled ()) log .debug ("hasCachedModel({}): {}" , uri , ((FileManager )getDataManager ()).hasCachedModel (uri ));
215+ if (log .isDebugEnabled ()) log .debug ("isMapped({}): {}" , uri , getDataManager ().isMapped (uri ));
216+ return getDataManager ().loadModel (uri );
217+ }
218+ else
219+ {
220+ LinkedDataClient ldc = LinkedDataClient .create (getSystem ().getClient (), getSystem ().getMediaTypes ());
221+ return ldc .getModel (uri );
222+ }
194223 }
195224
196225 /**
@@ -206,9 +235,7 @@ private String downloadStylesheet(URI uri) throws IOException
206235 try (Response response = target .request ("text/xsl" , "text/*;q=0.8" ).get ())
207236 {
208237 if (!response .getStatusInfo ().getFamily ().equals (Response .Status .Family .SUCCESSFUL ))
209- {
210238 throw new IOException ("Failed to download XSLT from " + uri + ": " + response .getStatus ());
211- }
212239
213240 return response .readEntity (String .class );
214241 }
@@ -320,4 +347,14 @@ public ServletContext getServletContext()
320347 return servletContext ;
321348 }
322349
350+ /**
351+ * Returns RDF data manager.
352+ *
353+ * @return RDF data manager
354+ */
355+ public DataManager getDataManager ()
356+ {
357+ return dataManager ;
358+ }
359+
323360}
0 commit comments