Search in sources :

Example 6 with CurlResponse

use of org.codelibs.curl.CurlResponse in project fess by codelibs.

the class AdminEsreqAction method upload.

@Execute
@Secured({ ROLE })
public ActionResponse upload(final UploadForm form) {
    validate(form, messages -> {
    }, () -> asListHtml(null));
    verifyTokenKeep(() -> asListHtml(this::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);
        if (header == null) {
            throwValidationError(messages -> messages.addErrorsInvalidHeaderForRequestFile(GLOBAL, "no header"), () -> asListHtml(this::saveToken));
            // no-op
            return redirect(getClass());
        }
        String line;
        while ((line = ReaderUtil.readLine(reader)) != null) {
            buf.append(line);
        }
    } catch (final Exception e) {
        throwValidationError(messages -> messages.addErrorsFailedToReadRequestFile(GLOBAL, e.getMessage()), () -> asListHtml(this::saveToken));
    }
    final CurlRequest curlRequest = getCurlRequest(header);
    if (curlRequest == null) {
        final String msg = header;
        throwValidationError(messages -> messages.addErrorsInvalidHeaderForRequestFile(GLOBAL, msg), () -> asListHtml(this::saveToken));
    } else {
        try (final CurlResponse response = curlRequest.body(buf.toString()).execute()) {
            final File tempFile = ComponentUtil.getSystemHelper().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()), () -> asListHtml(this::saveToken));
        }
    }
    // no-op
    return redirect(getClass());
}
Also used : Constants(org.codelibs.fess.Constants) Secured(org.codelibs.fess.annotation.Secured) StringUtil(org.codelibs.core.lang.StringUtil) CurlRequest(org.codelibs.curl.CurlRequest) FileInputStream(java.io.FileInputStream) InputStreamReader(java.io.InputStreamReader) ActionRuntime(org.lastaflute.web.ruts.process.ActionRuntime) File(java.io.File) ActionResponse(org.lastaflute.web.response.ActionResponse) Logger(org.apache.logging.log4j.Logger) CurlHelper(org.codelibs.fess.helper.CurlHelper) ComponentUtil(org.codelibs.fess.util.ComponentUtil) Locale(java.util.Locale) FessAdminAction(org.codelibs.fess.app.web.base.FessAdminAction) Execute(org.lastaflute.web.Execute) BufferedReader(java.io.BufferedReader) CopyUtil(org.codelibs.core.io.CopyUtil) HtmlResponse(org.lastaflute.web.response.HtmlResponse) LogManager(org.apache.logging.log4j.LogManager) ReaderUtil(org.codelibs.core.io.ReaderUtil) CurlResponse(org.codelibs.curl.CurlResponse) InputStream(java.io.InputStream) InputStreamReader(java.io.InputStreamReader) CurlResponse(org.codelibs.curl.CurlResponse) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) BufferedReader(java.io.BufferedReader) CurlRequest(org.codelibs.curl.CurlRequest) File(java.io.File) FileInputStream(java.io.FileInputStream) Execute(org.lastaflute.web.Execute) Secured(org.codelibs.fess.annotation.Secured)

Aggregations

CurlResponse (org.codelibs.curl.CurlResponse)6 IOException (java.io.IOException)5 LogManager (org.apache.logging.log4j.LogManager)5 Logger (org.apache.logging.log4j.Logger)5 ComponentUtil (org.codelibs.fess.util.ComponentUtil)5 StringUtil (org.codelibs.core.lang.StringUtil)4 InputStream (java.io.InputStream)3 Constants (org.codelibs.fess.Constants)3 File (java.io.File)2 FileInputStream (java.io.FileInputStream)2 Locale (java.util.Locale)2 PostConstruct (javax.annotation.PostConstruct)2 StringEscapeUtils (org.apache.commons.text.StringEscapeUtils)2 CopyUtil (org.codelibs.core.io.CopyUtil)2 StreamUtil (org.codelibs.core.stream.StreamUtil)2 Curl (org.codelibs.curl.Curl)2 CurlRequest (org.codelibs.curl.CurlRequest)2 FessConfig (org.codelibs.fess.mylasta.direction.FessConfig)2 CardView (org.dbflute.mail.CardView)2 SMailPostingDiscloser (org.dbflute.mail.send.supplement.SMailPostingDiscloser)2