use of org.codelibs.fess.annotation.Secured in project fess by codelibs.
the class AdminDataconfigAction method createnew.
// ===================================================================================
// Edit Execute
// ============
// -----------------------------------------------------
// Entry Page
// ----------
@Execute
@Secured({ ROLE })
public HtmlResponse createnew() {
saveToken();
return asEditHtml().useForm(CreateForm.class, op -> {
op.setup(form -> {
form.initialize();
ComponentUtil.getCrawlingConfigHelper().getDefaultConfig(ConfigType.DATA).ifPresent(entity -> {
copyBeanToBean(entity, form, copyOp -> {
copyOp.excludeNull();
copyOp.exclude(Stream.concat(Stream.of(Constants.COMMON_CONVERSION_RULE), Stream.of(Constants.PERMISSIONS, Constants.VIRTUAL_HOSTS)).toArray(n -> new String[n]));
});
final PermissionHelper permissionHelper = ComponentUtil.getPermissionHelper();
form.permissions = stream(entity.getPermissions()).get(stream -> stream.map(s -> permissionHelper.decode(s)).filter(StringUtil::isNotBlank).distinct().collect(Collectors.joining("\n")));
form.virtualHosts = stream(entity.getVirtualHosts()).get(stream -> stream.filter(StringUtil::isNotBlank).map(String::trim).collect(Collectors.joining("\n")));
form.name = null;
});
form.crudMode = CrudMode.CREATE;
});
});
}
use of org.codelibs.fess.annotation.Secured in project fess by codelibs.
the class AdminDesignAction method upload.
@Execute
@Secured({ ROLE })
public HtmlResponse upload(final UploadForm form) {
validate(form, messages -> {
}, () -> asListHtml(form));
verifyToken(this::asListHtml);
final String uploadedFileName = form.designFile.getFileName();
String fileName = form.designFileName;
if (StringUtil.isBlank(fileName)) {
fileName = uploadedFileName;
try {
int pos = fileName.indexOf('/');
if (pos >= 0) {
fileName = fileName.substring(pos + 1);
}
pos = fileName.indexOf('\\');
if (pos >= 0) {
fileName = fileName.substring(pos + 1);
}
} catch (final Exception e) {
throwValidationError(messages -> messages.addErrorsDesignFileNameIsInvalid("designFile"), this::asListHtml);
}
}
if (StringUtil.isBlank(fileName)) {
throwValidationError(messages -> messages.addErrorsDesignFileNameIsNotFound("designFile"), this::asListHtml);
}
File uploadFile;
// normalize filename
if (checkFileType(fileName, fessConfig.getSupportedUploadedMediaExtentionsAsArray()) && checkFileType(uploadedFileName, fessConfig.getSupportedUploadedMediaExtentionsAsArray())) {
uploadFile = new File(getServletContext().getRealPath("/images/" + fileName));
} else if (checkFileType(fileName, fessConfig.getSupportedUploadedCssExtentionsAsArray()) && checkFileType(uploadedFileName, fessConfig.getSupportedUploadedCssExtentionsAsArray())) {
uploadFile = new File(getServletContext().getRealPath("/css/" + fileName));
} else if (checkFileType(fileName, fessConfig.getSupportedUploadedJsExtentionsAsArray()) && checkFileType(uploadedFileName, fessConfig.getSupportedUploadedJsExtentionsAsArray())) {
uploadFile = new File(getServletContext().getRealPath("/js/" + fileName));
} else if (fessConfig.isSupportedUploadedFile(fileName) || fessConfig.isSupportedUploadedFile(uploadedFileName)) {
uploadFile = ResourceUtil.getResourceAsFileNoException(fileName);
if (uploadFile == null) {
throwValidationError(messages -> messages.addErrorsDesignFileNameIsNotFound("designFileName"), this::asListHtml);
return null;
}
} else {
throwValidationError(messages -> messages.addErrorsDesignFileIsUnsupportedType("designFileName"), this::asListHtml);
return null;
}
final File parentFile = uploadFile.getParentFile();
if (!parentFile.exists() && !parentFile.mkdirs()) {
logger.warn("Could not create {}", parentFile.getAbsolutePath());
}
try {
write(uploadFile.getAbsolutePath(), form.designFile.getFileData());
final String currentFileName = fileName;
saveInfo(messages -> messages.addSuccessUploadDesignFile(GLOBAL, currentFileName));
} catch (final Exception e) {
logger.error("Failed to write an image file: {}", fileName, e);
throwValidationError(messages -> messages.addErrorsFailedToWriteDesignImageFile(GLOBAL), this::asListHtml);
}
return redirect(getClass());
}
use of org.codelibs.fess.annotation.Secured in project fess by codelibs.
the class AdminDesignAction method editAsUseDefault.
@Execute
@Secured({ ROLE })
public HtmlResponse editAsUseDefault(final EditForm form) {
final String jspType = "orig/view";
final File jspFile = getJspFile(form.fileName, jspType);
try {
form.content = new String(FileUtil.readBytes(jspFile), Constants.UTF_8);
} catch (final UnsupportedEncodingException e) {
throw new FessSystemException("Invalid encoding", e);
}
saveToken();
return asEditHtml(form);
}
use of org.codelibs.fess.annotation.Secured in project fess by codelibs.
the class AdminDesignAction method download.
@Execute
@Secured({ ROLE, ROLE + VIEW })
public StreamResponse download(final FileAccessForm form) {
final File file = getTargetFile(form.fileName).get();
if (file == null) {
throwValidationError(messages -> messages.addErrorsTargetFileDoesNotExist(GLOBAL, form.fileName), this::asListHtml);
return null;
}
validate(form, messages -> {
}, this::asListHtml);
verifyTokenKeep(this::asListHtml);
return asStream(file.getName()).contentTypeOctetStream().stream(out -> {
try (FileInputStream fis = new FileInputStream(file)) {
out.write(fis);
}
});
}
use of org.codelibs.fess.annotation.Secured in project fess by codelibs.
the class AdminBackupAction method download.
@Execute
@Secured({ ROLE, ROLE + VIEW })
public ActionResponse download(final String id) {
if (stream(fessConfig.getIndexBackupAllTargets()).get(stream -> stream.anyMatch(s -> s.equals(id)))) {
if ("system.properties".equals(id)) {
return asStream(id).contentTypeOctetStream().stream(out -> {
try (final ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
ComponentUtil.getSystemProperties().store(baos, id);
try (final InputStream in = new ByteArrayInputStream(baos.toByteArray())) {
out.write(in);
}
}
});
}
if (id.endsWith(NDJSON_EXTENTION)) {
final String name = id.substring(0, id.length() - NDJSON_EXTENTION.length());
if ("search_log".equals(name)) {
return writeNdjsonResponse(id, getSearchLogNdjsonWriteCall());
}
if ("user_info".equals(name)) {
return writeNdjsonResponse(id, getUserInfoNdjsonWriteCall());
}
if ("click_log".equals(name)) {
return writeNdjsonResponse(id, getClickLogNdjsonWriteCall());
} else if ("favorite_log".equals(name)) {
return writeNdjsonResponse(id, getFavoriteLogNdjsonWriteCall());
}
} else if ("fess.json".equals(id)) {
return asStream(id).contentTypeOctetStream().stream(out -> {
final Path fessJsonPath = getFessJsonPath();
try (final InputStream in = Files.newInputStream(fessJsonPath)) {
out.write(in);
}
});
} else if ("doc.json".equals(id)) {
return asStream(id).contentTypeOctetStream().stream(out -> {
final Path fessJsonPath = getDocJsonPath();
try (final InputStream in = Files.newInputStream(fessJsonPath)) {
out.write(in);
}
});
} else {
final String index;
final String filename;
if (id.endsWith(".bulk")) {
index = id.substring(0, id.length() - 5);
filename = id;
} else {
index = id;
filename = id + ".bulk";
}
return asStream(filename).contentTypeOctetStream().stream(out -> {
try (final BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(out.stream(), Constants.CHARSET_UTF_8))) {
SearchEngineUtil.scroll(index, hit -> {
try {
writer.write("{\"index\":{\"_index\":\"" + hit.getIndex() + "\",\"_id\":\"" + StringEscapeUtils.escapeJson(hit.getId()) + "\"}}\n");
writer.write(hit.getSourceAsString());
writer.write("\n");
} catch (final IOException e) {
throw new IORuntimeException(e);
}
return true;
});
writer.flush();
}
});
}
}
throwValidationError(messages -> messages.addErrorsCouldNotFindBackupIndex(GLOBAL), this::asListHtml);
// no-op
return redirect(getClass());
}
Aggregations