Search in sources :

Example 6 with ActionResponse

use of org.lastaflute.web.response.ActionResponse in project fess by codelibs.

the class AdminEsreqAction method upload.

@Execute
public ActionResponse upload(final UploadForm form) {
    validate(form, messages -> {
    }, () -> asListHtml(null));
    verifyToken(() -> asListHtml(() -> saveToken()));
    String header = null;
    final StringBuilder buf = new StringBuilder(1000);
    try (final BufferedReader reader = new BufferedReader(new InputStreamReader(form.requestFile.getInputStream(), Constants.UTF_8))) {
        header = ReaderUtil.readLine(reader);
        String line;
        while ((line = ReaderUtil.readLine(reader)) != null) {
            buf.append(line);
        }
    } catch (final Exception e) {
        throwValidationError(messages -> messages.addErrorsFailedToReadRequestFile(GLOBAL, e.getMessage()), () -> {
            return asListHtml(() -> saveToken());
        });
    }
    final CurlRequest curlRequest = getCurlRequest(header);
    if (curlRequest == null) {
        final String msg = header;
        throwValidationError(messages -> messages.addErrorsInvalidHeaderForRequestFile(GLOBAL, msg), () -> {
            return asListHtml(() -> saveToken());
        });
    } else {
        try (final CurlResponse response = curlRequest.header("Content-Type", "application/json").body(buf.toString()).execute()) {
            final File tempFile = File.createTempFile("esreq_", ".json");
            try (final InputStream in = response.getContentAsStream()) {
                CopyUtil.copy(in, tempFile);
            } catch (final Exception e1) {
                if (tempFile != null && tempFile.exists() && !tempFile.delete()) {
                    logger.warn("Failed to delete " + tempFile.getAbsolutePath());
                }
                throw e1;
            }
            return asStream("es_" + System.currentTimeMillis() + ".json").contentTypeOctetStream().stream(out -> {
                try (final InputStream in = new FileInputStream(tempFile)) {
                    out.write(in);
                } finally {
                    if (tempFile.exists() && !tempFile.delete()) {
                        logger.warn("Failed to delete " + tempFile.getAbsolutePath());
                    }
                }
            });
        } catch (final Exception e) {
            logger.warn("Failed to process request file: " + form.requestFile.getFileName(), e);
            throwValidationError(messages -> messages.addErrorsInvalidHeaderForRequestFile(GLOBAL, e.getMessage()), () -> {
                return asListHtml(() -> saveToken());
            });
        }
    }
    // no-op
    return redirect(getClass());
}
Also used : Constants(org.codelibs.fess.Constants) Logger(org.slf4j.Logger) StringUtil(org.codelibs.core.lang.StringUtil) LoggerFactory(org.slf4j.LoggerFactory) FileInputStream(java.io.FileInputStream) InputStreamReader(java.io.InputStreamReader) ActionRuntime(org.lastaflute.web.ruts.process.ActionRuntime) File(java.io.File) Curl(org.codelibs.elasticsearch.runner.net.Curl) ResourceUtil(org.codelibs.fess.util.ResourceUtil) ActionResponse(org.lastaflute.web.response.ActionResponse) Locale(java.util.Locale) FessAdminAction(org.codelibs.fess.app.web.base.FessAdminAction) Execute(org.lastaflute.web.Execute) CurlResponse(org.codelibs.elasticsearch.runner.net.CurlResponse) BufferedReader(java.io.BufferedReader) CopyUtil(org.codelibs.core.io.CopyUtil) HtmlResponse(org.lastaflute.web.response.HtmlResponse) ReaderUtil(org.codelibs.core.io.ReaderUtil) CurlRequest(org.codelibs.elasticsearch.runner.net.CurlRequest) InputStream(java.io.InputStream) InputStreamReader(java.io.InputStreamReader) CurlResponse(org.codelibs.elasticsearch.runner.net.CurlResponse) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) BufferedReader(java.io.BufferedReader) CurlRequest(org.codelibs.elasticsearch.runner.net.CurlRequest) File(java.io.File) FileInputStream(java.io.FileInputStream) Execute(org.lastaflute.web.Execute)

Example 7 with ActionResponse

use of org.lastaflute.web.response.ActionResponse in project fess by codelibs.

the class AdminDictKuromojiAction method download.

@Execute
public ActionResponse download(final DownloadForm form) {
    validate(form, messages -> {
    }, () -> downloadpage(form.dictId));
    verifyTokenKeep(() -> downloadpage(form.dictId));
    return kuromojiService.getKuromojiFile(form.dictId).map(file -> {
        return asStream(new File(file.getPath()).getName()).contentTypeOctetStream().stream(out -> {
            try (InputStream inputStream = file.getInputStream()) {
                out.write(inputStream);
            }
        });
    }).orElseGet(() -> {
        throwValidationError(messages -> messages.addErrorsFailedToDownloadKuromojiFile(GLOBAL), () -> downloadpage(form.dictId));
        return null;
    });
}
Also used : BeanUtil(org.codelibs.core.beans.util.BeanUtil) Constants(org.codelibs.fess.Constants) OptionalEntity(org.dbflute.optional.OptionalEntity) OptionalThing(org.dbflute.optional.OptionalThing) Resource(javax.annotation.Resource) StringUtil(org.codelibs.core.lang.StringUtil) IOException(java.io.IOException) ActionRuntime(org.lastaflute.web.ruts.process.ActionRuntime) File(java.io.File) KuromojiItem(org.codelibs.fess.dict.kuromoji.KuromojiItem) RenderDataUtil(org.codelibs.fess.util.RenderDataUtil) ActionResponse(org.lastaflute.web.response.ActionResponse) KuromojiPager(org.codelibs.fess.app.pager.KuromojiPager) CrudMode(org.codelibs.fess.app.web.CrudMode) ComponentUtil(org.codelibs.fess.util.ComponentUtil) FessAdminAction(org.codelibs.fess.app.web.base.FessAdminAction) Execute(org.lastaflute.web.Execute) RenderData(org.lastaflute.web.response.render.RenderData) AdminDictAction(org.codelibs.fess.app.web.admin.dict.AdminDictAction) HtmlResponse(org.lastaflute.web.response.HtmlResponse) KuromojiService(org.codelibs.fess.app.service.KuromojiService) InputStream(java.io.InputStream) InputStream(java.io.InputStream) File(java.io.File) Execute(org.lastaflute.web.Execute)

Example 8 with ActionResponse

use of org.lastaflute.web.response.ActionResponse in project fess by codelibs.

the class AdminDictMappingAction method download.

@Execute
public ActionResponse download(final DownloadForm form) {
    validate(form, messages -> {
    }, () -> downloadpage(form.dictId));
    verifyTokenKeep(() -> downloadpage(form.dictId));
    return charMappingService.getCharMappingFile(form.dictId).map(file -> {
        return asStream(new File(file.getPath()).getName()).contentTypeOctetStream().stream(out -> {
            try (InputStream inputStream = file.getInputStream()) {
                out.write(inputStream);
            }
        });
    }).orElseGet(() -> {
        throwValidationError(messages -> messages.addErrorsFailedToDownloadMappingFile(GLOBAL), () -> downloadpage(form.dictId));
        return null;
    });
}
Also used : BeanUtil(org.codelibs.core.beans.util.BeanUtil) Constants(org.codelibs.fess.Constants) OptionalThing(org.dbflute.optional.OptionalThing) VaErrorHook(org.lastaflute.web.validation.VaErrorHook) ActionRuntime(org.lastaflute.web.ruts.process.ActionRuntime) RenderDataUtil(org.codelibs.fess.util.RenderDataUtil) ArrayList(java.util.ArrayList) CrudMode(org.codelibs.fess.app.web.CrudMode) CharMappingService(org.codelibs.fess.app.service.CharMappingService) FessAdminAction(org.codelibs.fess.app.web.base.FessAdminAction) RenderData(org.lastaflute.web.response.render.RenderData) HtmlResponse(org.lastaflute.web.response.HtmlResponse) OptionalEntity(org.dbflute.optional.OptionalEntity) Resource(javax.annotation.Resource) StringUtil(org.codelibs.core.lang.StringUtil) IOException(java.io.IOException) CharMappingPager(org.codelibs.fess.app.pager.CharMappingPager) CharMappingItem(org.codelibs.fess.dict.mapping.CharMappingItem) File(java.io.File) ActionResponse(org.lastaflute.web.response.ActionResponse) List(java.util.List) ComponentUtil(org.codelibs.fess.util.ComponentUtil) Execute(org.lastaflute.web.Execute) AdminDictAction(org.codelibs.fess.app.web.admin.dict.AdminDictAction) InputStream(java.io.InputStream) InputStream(java.io.InputStream) File(java.io.File) Execute(org.lastaflute.web.Execute)

Aggregations

Execute (org.lastaflute.web.Execute)8 ActionResponse (org.lastaflute.web.response.ActionResponse)8 StringUtil (org.codelibs.core.lang.StringUtil)7 Constants (org.codelibs.fess.Constants)7 ComponentUtil (org.codelibs.fess.util.ComponentUtil)7 HtmlResponse (org.lastaflute.web.response.HtmlResponse)7 File (java.io.File)6 IOException (java.io.IOException)6 InputStream (java.io.InputStream)6 Resource (javax.annotation.Resource)6 FessAdminAction (org.codelibs.fess.app.web.base.FessAdminAction)6 ActionRuntime (org.lastaflute.web.ruts.process.ActionRuntime)6 BeanUtil (org.codelibs.core.beans.util.BeanUtil)5 CrudMode (org.codelibs.fess.app.web.CrudMode)5 AdminDictAction (org.codelibs.fess.app.web.admin.dict.AdminDictAction)5 RenderDataUtil (org.codelibs.fess.util.RenderDataUtil)5 OptionalEntity (org.dbflute.optional.OptionalEntity)5 OptionalThing (org.dbflute.optional.OptionalThing)5 RenderData (org.lastaflute.web.response.render.RenderData)5 VaErrorHook (org.lastaflute.web.validation.VaErrorHook)3