use of org.haiku.haikudepotserver.dataobjects.PkgSupplement in project haikudepotserver by haiku.
the class PkgIconController method handleHeadOrGetPkgIcon.
private void handleHeadOrGetPkgIcon(RequestMethod requestMethod, HttpServletResponse response, Integer size, String format, String pkgName, Boolean fallback) throws IOException {
if (null == format) {
throw new MissingOrBadFormat();
}
if (Strings.isNullOrEmpty(pkgName) || !Pkg.PATTERN_NAME.matcher(pkgName).matches()) {
throw new MissingPkgName();
}
ObjectContext context = serverRuntime.newContext();
// cached
Optional<Pkg> pkg = Pkg.tryGetByName(context, pkgName);
if (!pkg.isPresent()) {
LOGGER.debug("request for icon for package '{}', but no such package was able to be found", pkgName);
throw new PkgNotFound();
}
PkgSupplement pkgSupplement = pkg.get().getPkgSupplement();
switch(format) {
case org.haiku.haikudepotserver.dataobjects.MediaType.EXTENSION_HAIKUVECTORICONFILE:
Optional<PkgIcon> hvifPkgIcon = pkg.get().getPkgSupplement().getPkgIcon(org.haiku.haikudepotserver.dataobjects.MediaType.getByExtension(context, format).get(), null);
if (hvifPkgIcon.isPresent()) {
byte[] data = hvifPkgIcon.get().getPkgIconImage().getData();
response.setContentType(org.haiku.haikudepotserver.dataobjects.MediaType.MEDIATYPE_HAIKUVECTORICONFILE);
outputToResponse(response, pkgSupplement, data, requestMethod == RequestMethod.GET);
} else {
throw new PkgIconNotFound();
}
break;
case org.haiku.haikudepotserver.dataobjects.MediaType.EXTENSION_PNG:
if (null == size) {
throw new IllegalArgumentException("the size must be provided when requesting a PNG");
}
size = normalizeSize(size);
Optional<byte[]> pngImageData = renderedPkgIconRepository.render(size, context, pkg.get().getPkgSupplement());
if (!pngImageData.isPresent()) {
if ((null == fallback) || !fallback) {
throw new PkgIconNotFound();
}
handleGenericHeadOrGet(requestMethod, response, size, true);
} else {
byte[] data = pngImageData.get();
response.setContentType(MediaType.PNG.toString());
outputToResponse(response, pkgSupplement, data, requestMethod == RequestMethod.GET);
}
break;
default:
throw new IllegalStateException("unexpected format; " + format);
}
}
use of org.haiku.haikudepotserver.dataobjects.PkgSupplement in project haikudepotserver by haiku.
the class PkgApiIT method testGetPkgScreenshots.
/**
* <p>This test depends on the sample package pkg1 having some screenshots associated with it.</p>
*/
@Test
public void testGetPkgScreenshots() {
IntegrationTestSupportService.StandardTestData data = integrationTestSupportService.createStandardTestData();
// ------------------------------------
GetPkgScreenshotsResult result = pkgApi.getPkgScreenshots(new GetPkgScreenshotsRequest(data.pkg1.getName()));
// ------------------------------------
PkgSupplement pkgSupplement = data.pkg1.getPkgSupplement();
Assertions.assertThat(result.items.size()).isEqualTo(pkgSupplement.getPkgScreenshots().size());
List<org.haiku.haikudepotserver.dataobjects.PkgScreenshot> sortedScreenshots = pkgSupplement.getSortedPkgScreenshots();
Assertions.assertThat(sortedScreenshots).hasSize(3);
int[] widths = { 320, 240, 320 };
int[] heights = { 240, 320, 240 };
int[] lengths = { 41296, 28303, 33201 };
for (int i = 0; i < sortedScreenshots.size(); i++) {
org.haiku.haikudepotserver.dataobjects.PkgScreenshot pkgScreenshot = sortedScreenshots.get(i);
PkgScreenshot apiPkgScreenshot = result.items.get(i);
Assertions.assertThat(pkgScreenshot.getCode()).isEqualTo(apiPkgScreenshot.code);
Assertions.assertThat(pkgScreenshot.getWidth()).isEqualTo(apiPkgScreenshot.width);
Assertions.assertThat(pkgScreenshot.getHeight()).isEqualTo(apiPkgScreenshot.height);
Assertions.assertThat(pkgScreenshot.getLength()).isEqualTo(apiPkgScreenshot.length);
Assertions.assertThat(apiPkgScreenshot.width).isEqualTo(widths[i]);
Assertions.assertThat(apiPkgScreenshot.height).isEqualTo(heights[i]);
Assertions.assertThat(apiPkgScreenshot.length).isEqualTo(lengths[i]);
}
}
use of org.haiku.haikudepotserver.dataobjects.PkgSupplement in project haikudepotserver by haiku.
the class PkgApiIT method testConfigurePkgIcon_ok_bitmap.
/**
* <p>This test will configure the icons for the package.</p>
*/
@Test
public void testConfigurePkgIcon_ok_bitmap() throws Exception {
setAuthenticatedUserToRoot();
integrationTestSupportService.createStandardTestData();
byte[] sample16 = getResourceData("sample-16x16.png");
byte[] sample32 = getResourceData("sample-32x32.png");
byte[] sample64 = getResourceData("sample-64x64.png");
ConfigurePkgIconRequest request = new ConfigurePkgIconRequest();
request.pkgName = "pkg1";
request.pkgIcons = ImmutableList.of(new ConfigurePkgIconRequest.PkgIcon(MediaType.PNG.toString(), 16, Base64.getEncoder().encodeToString(sample16)), new ConfigurePkgIconRequest.PkgIcon(MediaType.PNG.toString(), 32, Base64.getEncoder().encodeToString(sample32)), new ConfigurePkgIconRequest.PkgIcon(MediaType.PNG.toString(), 64, Base64.getEncoder().encodeToString(sample64)));
// ------------------------------------
pkgApi.configurePkgIcon(request);
// ------------------------------------
{
ObjectContext objectContext = serverRuntime.newContext();
Pkg pkgAfter = Pkg.getByName(objectContext, "pkg1");
PkgSupplement pkgSupplementAfter = pkgAfter.getPkgSupplement();
org.haiku.haikudepotserver.dataobjects.MediaType mediaTypePng = org.haiku.haikudepotserver.dataobjects.MediaType.getByCode(objectContext, MediaType.PNG.toString());
Assertions.assertThat(pkgSupplementAfter.getPkgIcons().size()).isEqualTo(3);
Optional<PkgIcon> pkgIcon16Optional = pkgSupplementAfter.getPkgIcon(mediaTypePng, 16);
Assertions.assertThat(pkgIcon16Optional.get().getPkgIconImage().getData()).isEqualTo(sample16);
Optional<PkgIcon> pkgIcon32Optional = pkgSupplementAfter.getPkgIcon(mediaTypePng, 32);
Assertions.assertThat(pkgIcon32Optional.get().getPkgIconImage().getData()).isEqualTo(sample32);
Optional<PkgIcon> pkgIcon64Optional = pkgSupplementAfter.getPkgIcon(mediaTypePng, 64);
Assertions.assertThat(pkgIcon64Optional.get().getPkgIconImage().getData()).isEqualTo(sample64);
}
}
use of org.haiku.haikudepotserver.dataobjects.PkgSupplement in project haikudepotserver by haiku.
the class PkgApiIT method testUpdatePkgChangelog_withNoContent.
/**
* <p>Writing in no content will mean that the change log that was there is now removed.</p>
*/
@Test
public void testUpdatePkgChangelog_withNoContent() {
integrationTestSupportService.createStandardTestData();
setAuthenticatedUserToRoot();
UpdatePkgChangelogRequest request = new UpdatePkgChangelogRequest();
request.pkgName = "pkg1";
request.content = "";
// ------------------------------------
pkgApi.updatePkgChangelog(request);
// ------------------------------------
{
ObjectContext context = serverRuntime.newContext();
PkgSupplement pkgSupplementAfter = Pkg.getByName(context, "pkg1").getPkgSupplement();
Assertions.assertThat(pkgSupplementAfter.getPkgChangelog().isPresent()).isFalse();
}
}
use of org.haiku.haikudepotserver.dataobjects.PkgSupplement in project haikudepotserver by haiku.
the class PkgApiIT method testReorderPkgScreenshots.
/**
* <p>This test assumes that the test data has a pkg1 with three screenshots associated with it.</p>
*/
@Test
public void testReorderPkgScreenshots() {
setAuthenticatedUserToRoot();
IntegrationTestSupportService.StandardTestData data = integrationTestSupportService.createStandardTestData();
List<org.haiku.haikudepotserver.dataobjects.PkgScreenshot> sortedScreenshotsBefore = data.pkg1.getPkgSupplement().getSortedPkgScreenshots();
if (3 != sortedScreenshotsBefore.size()) {
throw new IllegalStateException("the test requires that pkg1 has three screenshots associated with it");
}
// ------------------------------------
pkgApi.reorderPkgScreenshots(new ReorderPkgScreenshotsRequest(data.pkg1.getName(), ImmutableList.of(sortedScreenshotsBefore.get(2).getCode(), sortedScreenshotsBefore.get(0).getCode())));
// ------------------------------------
ObjectContext context = serverRuntime.newContext();
PkgSupplement pkgSupplement = Pkg.getByName(context, data.pkg1.getName()).getPkgSupplement();
List<org.haiku.haikudepotserver.dataobjects.PkgScreenshot> sortedScreenshotsAfter = pkgSupplement.getSortedPkgScreenshots();
Assertions.assertThat(sortedScreenshotsAfter.size()).isEqualTo(3);
Assertions.assertThat(sortedScreenshotsAfter.get(0).getCode()).isEqualTo(sortedScreenshotsBefore.get(2).getCode());
Assertions.assertThat(sortedScreenshotsAfter.get(1).getCode()).isEqualTo(sortedScreenshotsBefore.get(0).getCode());
Assertions.assertThat(sortedScreenshotsAfter.get(2).getCode()).isEqualTo(sortedScreenshotsBefore.get(1).getCode());
}
Aggregations