Skip to content

Commit de357c7

Browse files
committed
Package test fixes
1 parent 25caef4 commit de357c7

6 files changed

Lines changed: 267 additions & 103 deletions

File tree

http-tests/admin/packages/install-package-404.sh renamed to http-tests/admin/packages/install-package-422.sh

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ purge_cache "$END_USER_VARNISH_SERVICE"
77
purge_cache "$ADMIN_VARNISH_SERVICE"
88
purge_cache "$FRONTEND_VARNISH_SERVICE"
99

10-
# Invalid/non-existent package URI should return 404 Not Found
11-
# (the HTTP client error from the remote package server is re-thrown)
10+
# Invalid/non-existent package URI should return 422 Unprocessable Entity
11+
# (package loading failed)
1212
curl -k -w "%{http_code}\n" -o /dev/null -s \
1313
-E "$OWNER_CERT_FILE":"$OWNER_CERT_PWD" \
1414
-X POST \
1515
-H "Content-Type: application/x-www-form-urlencoded" \
1616
--data-urlencode "package-uri=https://packages.linkeddatahub.com/nonexistent/#package" \
1717
"${ADMIN_BASE_URL}packages/install" \
18-
| grep -q "$STATUS_NOT_FOUND"
18+
| grep -q "$STATUS_UNPROCESSABLE_ENTITY"

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,5 @@ curl -k -w "%{http_code}\n" -o /dev/null -s \
1212
-E "$OWNER_CERT_FILE":"$OWNER_CERT_PWD" \
1313
-X POST \
1414
-H "Content-Type: application/x-www-form-urlencoded" \
15-
"{$ADMIN_BASE_URL}packages/uninstall" \
15+
"${ADMIN_BASE_URL}packages/uninstall" \
1616
| grep -q "$STATUS_BAD_REQUEST"

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

Lines changed: 51 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,14 @@
2323
import com.atomgraph.linkeddatahub.resource.admin.Clear;
2424
import com.atomgraph.linkeddatahub.server.security.AgentContext;
2525
import 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;
2728
import jakarta.inject.Inject;
2829
import jakarta.servlet.ServletContext;
2930
import jakarta.ws.rs.BadRequestException;
3031
import jakarta.ws.rs.Consumes;
3132
import jakarta.ws.rs.FormParam;
3233
import jakarta.ws.rs.HeaderParam;
33-
import jakarta.ws.rs.InternalServerErrorException;
3434
import jakarta.ws.rs.NotFoundException;
3535
import jakarta.ws.rs.POST;
3636
import jakarta.ws.rs.WebApplicationException;
@@ -61,6 +61,7 @@
6161
import java.util.List;
6262
import java.util.Optional;
6363
import java.util.Set;
64+
import org.apache.jena.ontology.ConversionException;
6465
import 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

Comments
 (0)