2323import com .atomgraph .linkeddatahub .resource .admin .Clear ;
2424import com .atomgraph .linkeddatahub .server .security .AgentContext ;
2525import com .atomgraph .linkeddatahub .server .util .UriPath ;
26- import com .atomgraph .linkeddatahub .server .util .XsltMasterUpdater ;
26+ import com .atomgraph .linkeddatahub .server .util .XSLTMasterUpdater ;
27+ import static com .atomgraph .server .status .UnprocessableEntityStatus .UNPROCESSABLE_ENTITY ;
2728import jakarta .inject .Inject ;
2829import jakarta .servlet .ServletContext ;
2930import jakarta .ws .rs .BadRequestException ;
3031import jakarta .ws .rs .Consumes ;
3132import jakarta .ws .rs .FormParam ;
3233import jakarta .ws .rs .HeaderParam ;
33- import jakarta .ws .rs .InternalServerErrorException ;
3434import jakarta .ws .rs .NotFoundException ;
3535import jakarta .ws .rs .POST ;
3636import jakarta .ws .rs .WebApplicationException ;
6161import java .util .List ;
6262import java .util .Optional ;
6363import java .util .Set ;
64+ import org .apache .jena .ontology .ConversionException ;
6465import org .apache .jena .util .FileManager ;
6566
6667/**
@@ -120,41 +121,47 @@ public Response post(@FormParam("package-uri") String packageURI, @HeaderParam("
120121 {
121122 if (packageURI == null ) throw new BadRequestException ("Package URI not specified" );
122123
123- try
124- {
125- EndUserApplication endUserApp = getApplication ().as (AdminApplication .class ).getEndUserApplication ();
124+ EndUserApplication endUserApp = getApplication ().as (AdminApplication .class ).getEndUserApplication ();
126125
127- if (log .isInfoEnabled ()) log .info ("Installing package: {}" , packageURI );
126+ if (log .isInfoEnabled ()) log .info ("Installing package: {}" , packageURI );
128127
129- // 1. Fetch package
130- com .atomgraph .linkeddatahub .apps .model .Package pkg = getPackage (packageURI );
131- if (pkg == null ) throw new BadRequestException ("Package not found: " + packageURI );
128+ // 1. Fetch package
129+ com .atomgraph .linkeddatahub .apps .model .Package pkg = getPackage (packageURI );
130+ if (pkg == null )
131+ throw new WebApplicationException ("Loading package failed" , UNPROCESSABLE_ENTITY .getStatusCode ()); // 422 Unprocessable Entity
132132
133- Resource ontology = pkg .getOntology ();
134- Resource stylesheet = pkg .getStylesheet ();
133+ Resource ontology = pkg .getOntology ();
134+ Resource stylesheet = pkg .getStylesheet ();
135135
136- if ( ontology == null ) throw new BadRequestException ( "Package ontology not found" );
137-
138- URI stylesheetURI = ( stylesheet != null ) ? URI . create ( stylesheet . getURI ()) : null ;
136+ // either ontology or stylesheet need to be specified, or both
137+ if ( ontology == null && stylesheet == null )
138+ throw new WebApplicationException ( "Package ontology and stylesheet are both unspecified" , UNPROCESSABLE_ENTITY . getStatusCode ()); // 422 Unprocessable Entity
139139
140+ try
141+ {
140142 String packagePath = UriPath .convert (packageURI );
141143
142- // 2. Download and install ontology
143- if (log .isDebugEnabled ()) log .debug ("Downloading package ontology from: {}" , ontology .getURI ());
144- Model ontologyModel = downloadOntology (ontology .getURI ());
145- installOntology (endUserApp , ontologyModel , ontology .getURI ());
144+ // 2. Download and install ontology if present
145+ if (ontology != null )
146+ {
147+ if (log .isDebugEnabled ()) log .debug ("Downloading package ontology from: {}" , ontology .getURI ());
148+ Model ontologyModel = downloadOntology (ontology .getURI ());
149+ installOntology (endUserApp , ontologyModel , ontology .getURI ());
150+ }
146151
147152 // 3. Download and install stylesheet if present
148- if (stylesheetURI != null )
153+ if (stylesheet != null )
149154 {
155+ URI stylesheetURI = URI .create (stylesheet .getURI ());
156+
150157 if (log .isDebugEnabled ()) log .debug ("Downloading package stylesheet from: {}" , stylesheetURI );
151158 String stylesheetContent = downloadStylesheet (stylesheetURI );
152159 installStylesheet (packagePath , stylesheetContent );
160+
161+ // 4. Regenerate master stylesheet
162+ regenerateMasterStylesheet (endUserApp , packagePath );
153163 }
154164
155- // 4. Regenerate master stylesheet
156- regenerateMasterStylesheet (endUserApp , packagePath );
157-
158165 // 5. Add ldh:import triple to application (in system.trig)
159166 addImportToApplication (endUserApp , packageURI );
160167
@@ -164,7 +171,7 @@ public Response post(@FormParam("package-uri") String packageURI, @HeaderParam("
164171 URI redirectURI = (referer != null ) ? referer : endUserApp .getBaseURI ();
165172 return Response .seeOther (redirectURI ).build ();
166173 }
167- catch (BadRequestException | IOException e )
174+ catch (IOException e )
168175 {
169176 log .error ("Failed to install package: {}" , packageURI , e );
170177 throw new WebApplicationException ("Package installation failed: " + e .getMessage (), e );
@@ -178,49 +185,41 @@ public Response post(@FormParam("package-uri") String packageURI, @HeaderParam("
178185 * @param packageURI the package URI (e.g., https://packages.linkeddatahub.com/skos/#this)
179186 * @return Package instance
180187 * @throws NotFoundException if package cannot be found (404)
181- * @throws InternalServerErrorException if package cannot be loaded for other reasons
182188 */
183189 private com .atomgraph .linkeddatahub .apps .model .Package getPackage (String packageURI )
184190 {
185- try
186- {
187- if (log .isDebugEnabled ()) log .debug ("Loading package from: {}" , packageURI );
191+ if (log .isDebugEnabled ()) log .debug ("Loading package from: {}" , packageURI );
188192
189- final Model model ;
190-
191- // check if we have the model in the cache first and if yes, return it from there instead making an HTTP request
192- if (((FileManager )getDataManager ()).hasCachedModel (packageURI ) ||
193- (getDataManager ().isResolvingMapped () && getDataManager ().isMapped (packageURI ))) // read mapped URIs (such as system ontologies) from a file
194- {
195- if (log .isDebugEnabled ()) log .debug ("hasCachedModel({}): {}" , packageURI , ((FileManager )getDataManager ()).hasCachedModel (packageURI ));
196- if (log .isDebugEnabled ()) log .debug ("isMapped({}): {}" , packageURI , getDataManager ().isMapped (packageURI ));
197- model = getDataManager ().loadModel (packageURI );
198- }
199- else
200- {
201- LinkedDataClient ldc = LinkedDataClient .create (getSystem ().getClient (), getSystem ().getMediaTypes ());
202- model = ldc .getModel (packageURI );
203- }
193+ final Model model ;
204194
205- return model .getResource (packageURI ).as (com .atomgraph .linkeddatahub .apps .model .Package .class );
195+ // check if we have the model in the cache first and if yes, return it from there instead making an HTTP request
196+ if (((FileManager )getDataManager ()).hasCachedModel (packageURI ) ||
197+ (getDataManager ().isResolvingMapped () && getDataManager ().isMapped (packageURI ))) // read mapped URIs (such as system ontologies) from a file
198+ {
199+ if (log .isDebugEnabled ()) log .debug ("hasCachedModel({}): {}" , packageURI , ((FileManager )getDataManager ()).hasCachedModel (packageURI ));
200+ if (log .isDebugEnabled ()) log .debug ("isMapped({}): {}" , packageURI , getDataManager ().isMapped (packageURI ));
201+ model = getDataManager ().loadModel (packageURI );
202+ }
203+ else
204+ {
205+ LinkedDataClient ldc = LinkedDataClient .create (getSystem ().getClient (), getSystem ().getMediaTypes ());
206+ model = ldc .getModel (packageURI );
206207 }
207- catch (WebApplicationException e )
208+
209+ try
208210 {
209- // Re-throw HTTP client errors from LinkedDataClient as-is (404, 403, etc.)
210- log .error ("HTTP error loading package from: {}" , packageURI , e );
211- throw e ;
211+ return model .getResource (packageURI ).as (com .atomgraph .linkeddatahub .apps .model .Package .class );
212212 }
213- catch (Exception e )
213+ catch (ConversionException ex )
214214 {
215- log .error ("Failed to load package from: {}" , packageURI , e );
216- throw new InternalServerErrorException ("Failed to load package from: " + packageURI , e );
215+ return null ;
217216 }
218217 }
219218
220219 /**
221220 * Downloads RDF from a URI using LinkedDataClient.
222221 */
223- private Model downloadOntology (String uri ) throws IOException
222+ private Model downloadOntology (String uri )
224223 {
225224 if (log .isDebugEnabled ()) log .debug ("Downloading ontology from: {}" , uri );
226225
@@ -355,7 +354,7 @@ private void regenerateMasterStylesheet(EndUserApplication app, String newPackag
355354 packagePaths .add (newPackagePath );
356355
357356 // Regenerate master stylesheet
358- XsltMasterUpdater updater = new XsltMasterUpdater (getServletContext ());
357+ XSLTMasterUpdater updater = new XSLTMasterUpdater (getServletContext ());
359358 updater .regenerateMasterStylesheet (packagePaths );
360359 }
361360
0 commit comments