Search in sources :

Example 6 with StreamResponse

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

the class ViewHelper method writeContent.

protected StreamResponse writeContent(final String configId, final String url, final CrawlerClient client) {
    final ResponseData responseData = client.execute(RequestDataBuilder.newRequestData().get().url(url).build());
    final StreamResponse response = new StreamResponse(StringUtil.EMPTY);
    writeFileName(response, responseData);
    writeContentType(response, responseData);
    writeNoCache(response, responseData);
    if (responseData.getHttpStatusCode() == 404) {
        response.httpStatus(responseData.getHttpStatusCode());
        return response;
    }
    response.stream(out -> {
        try (final InputStream is = new BufferedInputStream(responseData.getResponseBody())) {
            out.write(is);
        } catch (final IOException e) {
            if (!(e.getCause() instanceof ClientAbortException)) {
                throw new FessSystemException("Failed to write a content. configId: " + configId + ", url: " + url, e);
            }
        } finally {
            responseData.close();
        }
        if (logger.isDebugEnabled()) {
            logger.debug("Finished to write " + url);
        }
    });
    return response;
}
Also used : BufferedInputStream(java.io.BufferedInputStream) BufferedInputStream(java.io.BufferedInputStream) InputStream(java.io.InputStream) ResponseData(org.codelibs.fess.crawler.entity.ResponseData) StreamResponse(org.lastaflute.web.response.StreamResponse) ClientAbortException(org.apache.catalina.connector.ClientAbortException) IOException(java.io.IOException) FessSystemException(org.codelibs.fess.exception.FessSystemException)

Example 7 with StreamResponse

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

the class CacheAction method index.

// ===================================================================================
//                                                                           Attribute
//
// ===================================================================================
//                                                                               Hook
//                                                                              ======
// ===================================================================================
//                                                                      Search Execute
//                                                                      ==============
@Execute
public ActionResponse index(final CacheForm form) {
    validate(form, messages -> {
    }, () -> asHtml(virtualHost(path_Error_ErrorJsp)));
    if (isLoginRequired()) {
        return redirectToLogin();
    }
    Map<String, Object> doc = null;
    try {
        doc = searchService.getDocumentByDocId(form.docId, queryHelper.getCacheResponseFields(), getUserBean()).orElse(null);
    } catch (final Exception e) {
        logger.warn("Failed to request: " + form.docId, e);
    }
    if (doc == null) {
        saveError(messages -> messages.addErrorsDocidNotFound(GLOBAL, form.docId));
        return redirect(ErrorAction.class);
    }
    final String content = viewHelper.createCacheContent(doc, form.hq);
    if (content == null) {
        saveError(messages -> messages.addErrorsDocidNotFound(GLOBAL, form.docId));
        return redirect(ErrorAction.class);
    }
    final StreamResponse response = asStream(DocumentUtil.getValue(doc, fessConfig.getIndexFieldDocId(), String.class)).contentType("text/html; charset=UTF-8").data(content.getBytes(Constants.CHARSET_UTF_8));
    // TODO will be fixed in lastaflute
    response.headerContentDispositionInline();
    return response;
}
Also used : StreamResponse(org.lastaflute.web.response.StreamResponse) Execute(org.lastaflute.web.Execute)

Example 8 with StreamResponse

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

the class ApiAdminBackupAction method get$file.

// GET /api/admin/backup/file/{id}
@Execute
public StreamResponse get$file(final String id) {
    FessConfig fessConfig = ComponentUtil.getFessConfig();
    if (stream(fessConfig.getIndexBackupAllTargets()).get(stream -> stream.anyMatch(s -> s.equals(id)))) {
        if (id.equals("system.properties")) {
            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);
                    }
                }
            });
        } else if (id.endsWith(CSV_EXTENTION)) {
            final String name = id.substring(0, id.length() - CSV_EXTENTION.length());
            if ("search_log".equals(name)) {
                return writeCsvResponse(id, getSearchLogCsvWriteCall());
            } else if ("search_field_log".equals(name)) {
                return writeCsvResponse(id, getSearchFieldLogCsvWriteCall());
            } else if ("user_info".equals(name)) {
                return writeCsvResponse(id, getUserInfoCsvWriteCall());
            } else if ("click_log".equals(name)) {
                return writeCsvResponse(id, getClickLogCsvWriteCall());
            } else if ("favorite_log".equals(name)) {
                return writeCsvResponse(id, getFavoriteLogCsvWriteCall());
            }
        } 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 (CurlResponse response = Curl.get(ResourceUtil.getElasticsearchHttpUrl() + "/" + index + "/_data").header("Content-Type", "application/json").param("format", "json").execute()) {
                    out.write(response.getContentAsStream());
                }
            });
        }
    }
    throwValidationErrorApi(messages -> messages.addErrorsCouldNotFindBackupIndex(GLOBAL));
    // no-op
    return StreamResponse.asEmptyBody();
}
Also used : AdminBackupAction.getClickLogCsvWriteCall(org.codelibs.fess.app.web.admin.backup.AdminBackupAction.getClickLogCsvWriteCall) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ApiBackupFilesResponse(org.codelibs.fess.app.web.api.ApiResult.ApiBackupFilesResponse) ApiResult(org.codelibs.fess.app.web.api.ApiResult) Curl(org.codelibs.elasticsearch.runner.net.Curl) ResourceUtil(org.codelibs.fess.util.ResourceUtil) FessConfig(org.codelibs.fess.mylasta.direction.FessConfig) StreamResponse(org.lastaflute.web.response.StreamResponse) ByteArrayInputStream(java.io.ByteArrayInputStream) Map(java.util.Map) OutputStreamWriter(java.io.OutputStreamWriter) CurlResponse(org.codelibs.elasticsearch.runner.net.CurlResponse) AdminBackupAction.getSearchLogCsvWriteCall(org.codelibs.fess.app.web.admin.backup.AdminBackupAction.getSearchLogCsvWriteCall) StreamUtil.stream(org.codelibs.core.stream.StreamUtil.stream) FessApiAdminAction(org.codelibs.fess.app.web.api.admin.FessApiAdminAction) BufferedWriter(java.io.BufferedWriter) AdminBackupAction.getBackupItems(org.codelibs.fess.app.web.admin.backup.AdminBackupAction.getBackupItems) CsvWriter(com.orangesignal.csv.CsvWriter) JsonResponse(org.lastaflute.web.response.JsonResponse) Consumer(java.util.function.Consumer) CsvConfig(com.orangesignal.csv.CsvConfig) AdminBackupAction.getFavoriteLogCsvWriteCall(org.codelibs.fess.app.web.admin.backup.AdminBackupAction.getFavoriteLogCsvWriteCall) List(java.util.List) CSV_EXTENTION(org.codelibs.fess.app.web.admin.backup.AdminBackupAction.CSV_EXTENTION) ComponentUtil(org.codelibs.fess.util.ComponentUtil) AdminBackupAction.getUserInfoCsvWriteCall(org.codelibs.fess.app.web.admin.backup.AdminBackupAction.getUserInfoCsvWriteCall) Execute(org.lastaflute.web.Execute) AdminBackupAction.getSearchFieldLogCsvWriteCall(org.codelibs.fess.app.web.admin.backup.AdminBackupAction.getSearchFieldLogCsvWriteCall) InputStream(java.io.InputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) CurlResponse(org.codelibs.elasticsearch.runner.net.CurlResponse) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) FessConfig(org.codelibs.fess.mylasta.direction.FessConfig) Execute(org.lastaflute.web.Execute)

Example 9 with StreamResponse

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

the class ApiAdminDictSeunjeonAction method get$download.

// GET /api/admin/dict/seunjeon/download/{dictId}
@Execute
public StreamResponse get$download(final String dictId, final DownloadBody body) {
    body.dictId = dictId;
    validateApi(body, messages -> {
    });
    return seunjeonService.getSeunjeonFile(body.dictId).map(file -> {
        return asStream(new File(file.getPath()).getName()).contentTypeOctetStream().stream(out -> {
            try (InputStream inputStream = file.getInputStream()) {
                out.write(inputStream);
            }
        });
    }).orElseGet(() -> {
        throwValidationErrorApi(messages -> messages.addErrorsFailedToDownloadProtwordsFile(GLOBAL));
        return null;
    });
}
Also used : SeunjeonItem(org.codelibs.fess.dict.seunjeon.SeunjeonItem) FessApiAdminAction(org.codelibs.fess.app.web.api.admin.FessApiAdminAction) SeunjeonService(org.codelibs.fess.app.service.SeunjeonService) Resource(javax.annotation.Resource) JsonResponse(org.lastaflute.web.response.JsonResponse) Collectors(java.util.stream.Collectors) ApiResult(org.codelibs.fess.app.web.api.ApiResult) File(java.io.File) UploadForm(org.codelibs.fess.app.web.admin.dict.seunjeon.UploadForm) StreamResponse(org.lastaflute.web.response.StreamResponse) CrudMode(org.codelibs.fess.app.web.CrudMode) SeunjeonFile(org.codelibs.fess.dict.seunjeon.SeunjeonFile) AdminDictSeunjeonAction(org.codelibs.fess.app.web.admin.dict.seunjeon.AdminDictSeunjeonAction) Execute(org.lastaflute.web.Execute) InputStream(java.io.InputStream) SeunjeonPager(org.codelibs.fess.app.pager.SeunjeonPager) InputStream(java.io.InputStream) File(java.io.File) SeunjeonFile(org.codelibs.fess.dict.seunjeon.SeunjeonFile) Execute(org.lastaflute.web.Execute)

Aggregations

StreamResponse (org.lastaflute.web.response.StreamResponse)9 Execute (org.lastaflute.web.Execute)8 InputStream (java.io.InputStream)7 Resource (javax.annotation.Resource)6 ApiResult (org.codelibs.fess.app.web.api.ApiResult)6 FessApiAdminAction (org.codelibs.fess.app.web.api.admin.FessApiAdminAction)6 JsonResponse (org.lastaflute.web.response.JsonResponse)6 File (java.io.File)5 Collectors (java.util.stream.Collectors)5 CrudMode (org.codelibs.fess.app.web.CrudMode)5 IOException (java.io.IOException)2 Map (java.util.Map)2 CsvConfig (com.orangesignal.csv.CsvConfig)1 CsvWriter (com.orangesignal.csv.CsvWriter)1 BufferedInputStream (java.io.BufferedInputStream)1 BufferedWriter (java.io.BufferedWriter)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 OutputStreamWriter (java.io.OutputStreamWriter)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1