Search in sources :

Example 1 with Artifact

use of org.codelibs.fess.helper.PluginHelper.Artifact in project fess by codelibs.

the class ApiAdminPluginAction method delete$index.

// DELETE /api/admin/plugin
@Execute
public JsonResponse<ApiResult> delete$index(final DeleteBody body) {
    validateApi(body, messages -> {
    });
    deleteArtifact(new Artifact(body.name, body.version));
    return asJson(new ApiResult.ApiResponse().status(ApiResult.Status.OK).result());
}
Also used : ApiResult(org.codelibs.fess.app.web.api.ApiResult) Artifact(org.codelibs.fess.helper.PluginHelper.Artifact) AdminPluginAction.installArtifact(org.codelibs.fess.app.web.admin.plugin.AdminPluginAction.installArtifact) AdminPluginAction.deleteArtifact(org.codelibs.fess.app.web.admin.plugin.AdminPluginAction.deleteArtifact) Execute(org.lastaflute.web.Execute)

Example 2 with Artifact

use of org.codelibs.fess.helper.PluginHelper.Artifact in project fess by codelibs.

the class ApiAdminPluginAction method put$index.

// PUT /api/admin/plugin
@Execute
public JsonResponse<ApiResult> put$index(final InstallBody body) {
    validateApi(body, messages -> {
    });
    final Artifact artifact = ComponentUtil.getPluginHelper().getArtifact(body.name, body.version);
    if (artifact == null) {
        return asJson(new ApiResult.ApiErrorResponse().message("invalid name or version").status(ApiResult.Status.BAD_REQUEST).result());
    }
    installArtifact(artifact);
    return asJson(new ApiResult.ApiResponse().status(ApiResult.Status.OK).result());
}
Also used : ApiResult(org.codelibs.fess.app.web.api.ApiResult) Artifact(org.codelibs.fess.helper.PluginHelper.Artifact) AdminPluginAction.installArtifact(org.codelibs.fess.app.web.admin.plugin.AdminPluginAction.installArtifact) AdminPluginAction.deleteArtifact(org.codelibs.fess.app.web.admin.plugin.AdminPluginAction.deleteArtifact) Execute(org.lastaflute.web.Execute)

Example 3 with Artifact

use of org.codelibs.fess.helper.PluginHelper.Artifact in project fess by codelibs.

the class PluginTests method testCRUD.

@Test
void testCRUD() throws Exception {
    List<Map<String, Object>> available = checkGetMethod(Collections.emptyMap(), getAvailableEndpointSuffix() + "/").body().jsonPath().get("response.plugins");
    final Map<String, Object> targetMap = available.get(0);
    final Artifact target = getArtifactFromMap(targetMap);
    // Install
    {
        checkPutMethod(targetMap, getInstallEndpointSuffix()).then().body("response.status", equalTo(0));
        boolean done = false;
        for (int i = 0; i < 60; i++) {
            final List<Map<String, Object>> installed = checkGetMethod(Collections.emptyMap(), getInstalledEndpointSuffix() + "/").body().jsonPath().get("response.plugins");
            boolean exists = installed.stream().map(this::getArtifactFromMap).anyMatch(a -> a.getName().equals(target.getName()) && a.getVersion().equals(target.getVersion()));
            if (!exists) {
                ThreadUtil.sleep(500);
                continue;
            }
            assertTrue(exists);
            done = true;
            break;
        }
        assertTrue(done);
    }
    // Delete
    {
        checkDeleteMethod(targetMap).then().body("response.status", equalTo(0));
        boolean done = false;
        for (int i = 0; i < 60; i++) {
            final List<Map<String, Object>> installed = checkGetMethod(Collections.emptyMap(), getInstalledEndpointSuffix() + "/").body().jsonPath().get("response.plugins");
            boolean exists = installed.stream().map(this::getArtifactFromMap).anyMatch(a -> a.getName().equals(target.getName()) && a.getVersion().equals(target.getVersion()));
            if (exists) {
                ThreadUtil.sleep(500);
                continue;
            }
            assertFalse(exists);
            done = true;
            break;
        }
        assertTrue(done);
    }
}
Also used : ThreadUtil(org.codelibs.core.lang.ThreadUtil) Artifact(org.codelibs.fess.helper.PluginHelper.Artifact) CrudTestBase(org.codelibs.fess.it.CrudTestBase) Assert.assertTrue(org.junit.Assert.assertTrue) HashMap(java.util.HashMap) Test(org.junit.jupiter.api.Test) List(java.util.List) AfterEach(org.junit.jupiter.api.AfterEach) Response(io.restassured.response.Response) Assert.assertFalse(org.junit.Assert.assertFalse) Matchers.equalTo(org.hamcrest.Matchers.equalTo) Map(java.util.Map) Tag(org.junit.jupiter.api.Tag) Collections(java.util.Collections) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map) Artifact(org.codelibs.fess.helper.PluginHelper.Artifact) Test(org.junit.jupiter.api.Test)

Example 4 with Artifact

use of org.codelibs.fess.helper.PluginHelper.Artifact in project fess by codelibs.

the class AdminPluginAction method install.

@Execute
@Secured({ ROLE })
public HtmlResponse install(final InstallForm form) {
    validate(form, messages -> {
    }, () -> asHtml(path_AdminPlugin_AdminPluginInstallpluginJsp));
    verifyToken(() -> asHtml(path_AdminPlugin_AdminPluginInstallpluginJsp));
    try {
        if (UPLOAD.equals(form.id)) {
            if (form.jarFile == null) {
                throwValidationError(messages -> messages.addErrorsPluginFileIsNotFound(GLOBAL, form.id), this::asListHtml);
            }
            if (!form.jarFile.getFileName().endsWith(".jar")) {
                throwValidationError(messages -> messages.addErrorsFileIsNotSupported(GLOBAL, form.jarFile.getFileName()), this::asListHtml);
            }
            final String filename = form.jarFile.getFileName();
            final File tempFile = ComponentUtil.getSystemHelper().createTempFile("tmp-adminplugin-", ".jar");
            try (final InputStream is = form.jarFile.getInputStream();
                final OutputStream os = new FileOutputStream(tempFile)) {
                CopyUtil.copy(is, os);
            } catch (final Exception e) {
                if (tempFile.exists() && !tempFile.delete()) {
                    logger.warn("Failed to delete {}.", tempFile.getAbsolutePath());
                }
                logger.debug("Failed to copy {}", filename, e);
                throwValidationError(messages -> messages.addErrorsFailedToInstallPlugin(GLOBAL, filename), this::asListHtml);
            }
            new Thread(() -> {
                try {
                    final PluginHelper pluginHelper = ComponentUtil.getPluginHelper();
                    final Artifact artifact = pluginHelper.getArtifactFromFileName(ArtifactType.UNKNOWN, filename, tempFile.getAbsolutePath());
                    pluginHelper.installArtifact(artifact);
                } catch (final Exception e) {
                    logger.warn("Failed to install {}", filename, e);
                } finally {
                    if (tempFile.exists() && !tempFile.delete()) {
                        logger.warn("Failed to delete {}.", tempFile.getAbsolutePath());
                    }
                }
            }).start();
            saveInfo(messages -> messages.addSuccessInstallPlugin(GLOBAL, form.jarFile.getFileName()));
        } else {
            final Artifact artifact = getArtifactFromInstallForm(form);
            if (artifact == null) {
                throwValidationError(messages -> messages.addErrorsCrudCouldNotFindCrudTable(GLOBAL, form.id), this::asListHtml);
            }
            installArtifact(artifact);
            saveInfo(messages -> messages.addSuccessInstallPlugin(GLOBAL, artifact.getFileName()));
        }
    } catch (final ValidationErrorException e) {
        throw e;
    } catch (final Exception e) {
        throwValidationError(messages -> messages.addErrorsFailedToInstallPlugin(GLOBAL, form.id), this::asListHtml);
    }
    return redirect(getClass());
}
Also used : OutputStream(java.io.OutputStream) Arrays(java.util.Arrays) Secured(org.codelibs.fess.annotation.Secured) Artifact(org.codelibs.fess.helper.PluginHelper.Artifact) PluginHelper(org.codelibs.fess.helper.PluginHelper) FileOutputStream(java.io.FileOutputStream) HashMap(java.util.HashMap) Collectors(java.util.stream.Collectors) ActionRuntime(org.lastaflute.web.ruts.process.ActionRuntime) File(java.io.File) RenderDataUtil(org.codelibs.fess.util.RenderDataUtil) ArrayList(java.util.ArrayList) List(java.util.List) Logger(org.apache.logging.log4j.Logger) ArtifactType(org.codelibs.fess.helper.PluginHelper.ArtifactType) ComponentUtil(org.codelibs.fess.util.ComponentUtil) FessAdminAction(org.codelibs.fess.app.web.base.FessAdminAction) Map(java.util.Map) ValidationErrorException(org.lastaflute.web.validation.exception.ValidationErrorException) Execute(org.lastaflute.web.Execute) CopyUtil(org.codelibs.core.io.CopyUtil) HtmlResponse(org.lastaflute.web.response.HtmlResponse) LogManager(org.apache.logging.log4j.LogManager) InputStream(java.io.InputStream) PluginHelper(org.codelibs.fess.helper.PluginHelper) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) ValidationErrorException(org.lastaflute.web.validation.exception.ValidationErrorException) File(java.io.File) ValidationErrorException(org.lastaflute.web.validation.exception.ValidationErrorException) Artifact(org.codelibs.fess.helper.PluginHelper.Artifact) Execute(org.lastaflute.web.Execute) Secured(org.codelibs.fess.annotation.Secured)

Example 5 with Artifact

use of org.codelibs.fess.helper.PluginHelper.Artifact in project fess by codelibs.

the class ThemeHelper method install.

public void install(final Artifact artifact) {
    final Path jarPath = getJarFile(artifact);
    final String themeName = getThemeName(artifact);
    if (logger.isDebugEnabled()) {
        logger.debug("Theme: {}", themeName);
    }
    try (ZipInputStream zis = new ZipInputStream(Files.newInputStream(jarPath))) {
        ZipEntry entry;
        while ((entry = zis.getNextEntry()) != null) {
            if (!entry.isDirectory()) {
                final String[] names = StreamUtil.split(entry.getName(), "/").get(stream -> stream.filter(s -> !"..".equals(s)).toArray(n -> new String[n]));
                if (names.length < 2) {
                    continue;
                }
                if (logger.isDebugEnabled()) {
                    logger.debug("Loading {}", entry.getName());
                }
                if ("view".equals(names[0])) {
                    names[0] = themeName;
                    final Path path = ResourceUtil.getViewTemplatePath(names);
                    Files.createDirectories(path.getParent());
                    Files.copy(zis, path, StandardCopyOption.REPLACE_EXISTING);
                } else if ("css".equals(names[0])) {
                    names[0] = themeName;
                    final Path path = ResourceUtil.getCssPath(names);
                    Files.createDirectories(path.getParent());
                    Files.copy(zis, path, StandardCopyOption.REPLACE_EXISTING);
                } else if ("js".equals(names[0])) {
                    names[0] = themeName;
                    final Path path = ResourceUtil.getJavaScriptPath(names);
                    Files.createDirectories(path.getParent());
                    Files.copy(zis, path, StandardCopyOption.REPLACE_EXISTING);
                } else if ("images".equals(names[0])) {
                    names[0] = themeName;
                    final Path path = ResourceUtil.getImagePath(names);
                    Files.createDirectories(path.getParent());
                    Files.copy(zis, path, StandardCopyOption.REPLACE_EXISTING);
                }
            }
        }
    } catch (final IOException e) {
        throw new ThemeException("Failed to install " + artifact, e);
    }
}
Also used : Path(java.nio.file.Path) ZipInputStream(java.util.zip.ZipInputStream) Artifact(org.codelibs.fess.helper.PluginHelper.Artifact) Files(java.nio.file.Files) ThemeException(org.codelibs.fess.exception.ThemeException) StringUtil(org.codelibs.core.lang.StringUtil) IOException(java.io.IOException) StandardCopyOption(java.nio.file.StandardCopyOption) ResourceUtil(org.codelibs.fess.util.ResourceUtil) Stream(java.util.stream.Stream) Logger(org.apache.logging.log4j.Logger) StreamUtil(org.codelibs.core.stream.StreamUtil) ArtifactType(org.codelibs.fess.helper.PluginHelper.ArtifactType) FileVisitOption(java.nio.file.FileVisitOption) Comparator(java.util.Comparator) Path(java.nio.file.Path) ZipEntry(java.util.zip.ZipEntry) LogManager(org.apache.logging.log4j.LogManager) ZipInputStream(java.util.zip.ZipInputStream) ThemeException(org.codelibs.fess.exception.ThemeException) ZipEntry(java.util.zip.ZipEntry) IOException(java.io.IOException)

Aggregations

Artifact (org.codelibs.fess.helper.PluginHelper.Artifact)9 Execute (org.lastaflute.web.Execute)4 HashMap (java.util.HashMap)2 List (java.util.List)2 Map (java.util.Map)2 LogManager (org.apache.logging.log4j.LogManager)2 Logger (org.apache.logging.log4j.Logger)2 Secured (org.codelibs.fess.annotation.Secured)2 AdminPluginAction.deleteArtifact (org.codelibs.fess.app.web.admin.plugin.AdminPluginAction.deleteArtifact)2 AdminPluginAction.installArtifact (org.codelibs.fess.app.web.admin.plugin.AdminPluginAction.installArtifact)2 ApiResult (org.codelibs.fess.app.web.api.ApiResult)2 ArtifactType (org.codelibs.fess.helper.PluginHelper.ArtifactType)2 Response (io.restassured.response.Response)1 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 FileVisitOption (java.nio.file.FileVisitOption)1 Files (java.nio.file.Files)1