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());
}
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());
}
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);
}
}
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());
}
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);
}
}
Aggregations