Search in sources :

Example 1 with PkgIcon

use of org.haiku.haikudepotserver.api1.model.pkg.PkgIcon 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);
    }
}
Also used : PkgIcon(org.haiku.haikudepotserver.dataobjects.PkgIcon) PkgSupplement(org.haiku.haikudepotserver.dataobjects.PkgSupplement) Optional(java.util.Optional) ConfigurePkgIconRequest(org.haiku.haikudepotserver.api1.model.pkg.ConfigurePkgIconRequest) MediaType(com.google.common.net.MediaType) ObjectContext(org.apache.cayenne.ObjectContext) Pkg(org.haiku.haikudepotserver.dataobjects.Pkg) AbstractIntegrationTest(org.haiku.haikudepotserver.AbstractIntegrationTest) Test(org.junit.jupiter.api.Test)

Example 2 with PkgIcon

use of org.haiku.haikudepotserver.api1.model.pkg.PkgIcon 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();
}
Also used : AccessDeniedException(org.springframework.security.access.AccessDeniedException) PkgIcon(org.haiku.haikudepotserver.api1.model.pkg.PkgIcon) org.haiku.haikudepotserver.pkg.model(org.haiku.haikudepotserver.pkg.model) ObjectContext(org.apache.cayenne.ObjectContext) IOException(java.io.IOException) BadPkgIconException(org.haiku.haikudepotserver.api1.support.BadPkgIconException) ByteArrayInputStream(java.io.ByteArrayInputStream) org.haiku.haikudepotserver.dataobjects(org.haiku.haikudepotserver.dataobjects)

Example 3 with PkgIcon

use of org.haiku.haikudepotserver.api1.model.pkg.PkgIcon in project haikudepotserver by haiku.

the class PkgApiImpl method getPkgIcons.

@Override
public GetPkgIconsResult getPkgIcons(GetPkgIconsRequest request) {
    Preconditions.checkNotNull(request);
    Preconditions.checkState(!Strings.isNullOrEmpty(request.pkgName), "a package name must be supplied to get the package's icons");
    final ObjectContext context = serverRuntime.newContext();
    Pkg pkg = getPkg(context, request.pkgName);
    GetPkgIconsResult result = new GetPkgIconsResult();
    result.pkgIcons = pkg.getPkgSupplement().getPkgIcons().stream().map(pi -> new PkgIcon(pi.getMediaType().getCode(), pi.getSize())).collect(Collectors.toList());
    return result;
}
Also used : PkgIcon(org.haiku.haikudepotserver.api1.model.pkg.PkgIcon) ObjectContext(org.apache.cayenne.ObjectContext)

Example 4 with PkgIcon

use of org.haiku.haikudepotserver.api1.model.pkg.PkgIcon in project haikudepotserver by haiku.

the class PkgApiIT method testConfigurePkgIcon_ok_hvif.

/**
 * <p>This test will configure the icons for the package.</p>
 */
@Test
public void testConfigurePkgIcon_ok_hvif() throws Exception {
    setAuthenticatedUserToRoot();
    integrationTestSupportService.createStandardTestData();
    byte[] sampleHvif = getResourceData("sample.hvif");
    ConfigurePkgIconRequest request = new ConfigurePkgIconRequest();
    request.pkgName = "pkg1";
    request.pkgIcons = Collections.singletonList(new ConfigurePkgIconRequest.PkgIcon(org.haiku.haikudepotserver.dataobjects.MediaType.MEDIATYPE_HAIKUVECTORICONFILE, null, Base64.getEncoder().encodeToString(sampleHvif)));
    // ------------------------------------
    pkgApi.configurePkgIcon(request);
    // ------------------------------------
    {
        ObjectContext objectContext = serverRuntime.newContext();
        PkgSupplement pkgSupplementAfter = Pkg.getByName(objectContext, "pkg1").getPkgSupplement();
        org.haiku.haikudepotserver.dataobjects.MediaType mediaTypeHvif = org.haiku.haikudepotserver.dataobjects.MediaType.getByCode(objectContext, org.haiku.haikudepotserver.dataobjects.MediaType.MEDIATYPE_HAIKUVECTORICONFILE);
        Assertions.assertThat(pkgSupplementAfter.getPkgIcons().size()).isEqualTo(1);
        Optional<PkgIcon> pkgIconHvifOptional = pkgSupplementAfter.getPkgIcon(mediaTypeHvif, null);
        Assertions.assertThat(pkgIconHvifOptional.get().getPkgIconImage().getData()).isEqualTo(sampleHvif);
    }
}
Also used : PkgIcon(org.haiku.haikudepotserver.dataobjects.PkgIcon) PkgSupplement(org.haiku.haikudepotserver.dataobjects.PkgSupplement) Optional(java.util.Optional) ConfigurePkgIconRequest(org.haiku.haikudepotserver.api1.model.pkg.ConfigurePkgIconRequest) MediaType(com.google.common.net.MediaType) ObjectContext(org.apache.cayenne.ObjectContext) AbstractIntegrationTest(org.haiku.haikudepotserver.AbstractIntegrationTest) Test(org.junit.jupiter.api.Test)

Aggregations

ObjectContext (org.apache.cayenne.ObjectContext)4 MediaType (com.google.common.net.MediaType)2 Optional (java.util.Optional)2 AbstractIntegrationTest (org.haiku.haikudepotserver.AbstractIntegrationTest)2 ConfigurePkgIconRequest (org.haiku.haikudepotserver.api1.model.pkg.ConfigurePkgIconRequest)2 PkgIcon (org.haiku.haikudepotserver.api1.model.pkg.PkgIcon)2 PkgIcon (org.haiku.haikudepotserver.dataobjects.PkgIcon)2 PkgSupplement (org.haiku.haikudepotserver.dataobjects.PkgSupplement)2 Test (org.junit.jupiter.api.Test)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 BadPkgIconException (org.haiku.haikudepotserver.api1.support.BadPkgIconException)1 org.haiku.haikudepotserver.dataobjects (org.haiku.haikudepotserver.dataobjects)1 Pkg (org.haiku.haikudepotserver.dataobjects.Pkg)1 org.haiku.haikudepotserver.pkg.model (org.haiku.haikudepotserver.pkg.model)1 AccessDeniedException (org.springframework.security.access.AccessDeniedException)1