use of org.haiku.haikudepotserver.api1.support.BadPkgIconException in project haikudepotserver by haiku.
the class PkgApiImpl method configurePkgIcon.
@Override
public ConfigurePkgIconResult configurePkgIcon(ConfigurePkgIconRequest request) throws BadPkgIconException {
Preconditions.checkNotNull(request);
Preconditions.checkState(!Strings.isNullOrEmpty(request.pkgName));
final ObjectContext context = serverRuntime.newContext();
Pkg pkg = getPkg(context, request.pkgName);
User user = obtainAuthenticatedUser(context);
if (!permissionEvaluator.hasPermission(SecurityContextHolder.getContext().getAuthentication(), pkg, Permission.PKG_EDITICON)) {
throw new AccessDeniedException("attempt to configure the icon for package [" + pkg + "], but the user is not able to");
}
// insert or override the icons
int updated = 0;
int removed = 0;
Set<org.haiku.haikudepotserver.dataobjects.PkgIcon> createdOrUpdatedPkgIcons = new HashSet<>();
if (null != request.pkgIcons && !request.pkgIcons.isEmpty()) {
if (request.pkgIcons.stream().filter(pi -> pi.mediaTypeCode.equals(MediaType.MEDIATYPE_HAIKUVECTORICONFILE)).collect(SingleCollector.optional()).isPresent()) {
if (request.pkgIcons.size() > 1) {
throw new IllegalStateException("if an hvif icon is supplied then there should be no other variants.");
}
} else {
if (!contains(request.pkgIcons, com.google.common.net.MediaType.PNG.toString(), 16) || !contains(request.pkgIcons, com.google.common.net.MediaType.PNG.toString(), 32) || !contains(request.pkgIcons, com.google.common.net.MediaType.PNG.toString(), 64)) {
throw new IllegalStateException("there should be three bitmap icons supplied in sizes 16, 32 and 64");
}
}
for (ConfigurePkgIconRequest.PkgIcon pkgIconApi : request.pkgIcons) {
MediaType mediaType = MediaType.tryGetByCode(context, pkgIconApi.mediaTypeCode).orElseThrow(() -> new IllegalStateException("unknown media type; " + pkgIconApi.mediaTypeCode));
if (Strings.isNullOrEmpty(pkgIconApi.dataBase64)) {
throw new IllegalStateException("the base64 data must be supplied with the request to configure a pkg icon");
}
if (Strings.isNullOrEmpty(pkgIconApi.mediaTypeCode)) {
throw new IllegalStateException("the mediaTypeCode must be supplied to configure a pkg icon");
}
try {
byte[] data = Base64.getDecoder().decode(pkgIconApi.dataBase64);
ByteArrayInputStream dataInputStream = new ByteArrayInputStream(data);
createdOrUpdatedPkgIcons.add(pkgIconService.storePkgIconImage(dataInputStream, mediaType, pkgIconApi.size, context, pkg.getPkgSupplement()));
updated++;
} catch (IOException ioe) {
throw new RuntimeException("a problem has arisen storing the data for an icon", ioe);
} catch (org.haiku.haikudepotserver.pkg.model.BadPkgIconException bpie) {
throw new BadPkgIconException(pkgIconApi.mediaTypeCode, pkgIconApi.size, bpie);
}
}
}
for (org.haiku.haikudepotserver.dataobjects.PkgIcon pkgIcon : ImmutableList.copyOf(pkg.getPkgSupplement().getPkgIcons())) {
if (!createdOrUpdatedPkgIcons.contains(pkgIcon)) {
context.deleteObjects(pkgIcon.getPkgIconImage(), pkgIcon);
removed++;
}
}
pkg.setModifyTimestamp();
context.commitChanges();
LOGGER.info("did configure icons for pkg {} (updated {}, removed {})", pkg.getName(), updated, removed);
return new ConfigurePkgIconResult();
}
use of org.haiku.haikudepotserver.api1.support.BadPkgIconException in project haikudepotserver by haiku.
the class PkgApiIT method testConfigurePkgIcon_badData.
/**
* <p>Here we are trying to load the HVIF data in as PNG images.</p>
*/
@Test
public void testConfigurePkgIcon_badData() throws Exception {
setAuthenticatedUserToRoot();
integrationTestSupportService.createStandardTestData();
byte[] sampleHvif = getResourceData("sample.hvif");
ConfigurePkgIconRequest request = new ConfigurePkgIconRequest();
request.pkgName = "pkg1";
request.pkgIcons = ImmutableList.of(new ConfigurePkgIconRequest.PkgIcon(MediaType.PNG.toString(), 16, Base64.getEncoder().encodeToString(sampleHvif)), new ConfigurePkgIconRequest.PkgIcon(MediaType.PNG.toString(), 32, Base64.getEncoder().encodeToString(sampleHvif)), new ConfigurePkgIconRequest.PkgIcon(MediaType.PNG.toString(), 64, Base64.getEncoder().encodeToString(sampleHvif)));
try {
// ------------------------------------
pkgApi.configurePkgIcon(request);
// ------------------------------------
org.junit.jupiter.api.Assertions.fail("expected an instance of '" + BadPkgIconException.class.getSimpleName() + "' to have been thrown");
} catch (BadPkgIconException bpie) {
// This is the first one that failed so we should get this come up as the exception that was thrown.
Assertions.assertThat(bpie.getSize()).isEqualTo(16);
Assertions.assertThat(bpie.getMediaTypeCode()).isEqualTo(MediaType.PNG.toString());
}
}
Aggregations