Search in sources :

Example 41 with Execute

use of org.lastaflute.web.Execute in project fess by codelibs.

the class ApiAdminSearchlistAction method docs.

// ===================================================================================
//                                                                      Search Execute
//                                                                      ==============
// GET /api/admin/searchlist
// POST /api/admin/searchlist
@Execute
public JsonResponse<ApiResult> docs(final SearchBody body) {
    validateApi(body, messages -> {
    });
    if (StringUtil.isBlank(body.q)) {
        // query matches on all documents.
        body.q = Constants.MATCHES_ALL_QUERY;
    }
    final SearchRenderData renderData = new SearchRenderData();
    body.initialize();
    try {
        searchService.search(body, renderData, getUserBean());
        return asJson(new ApiDocsResponse().renderData(renderData).status(Status.OK).result());
    } catch (final InvalidQueryException e) {
        if (logger.isDebugEnabled()) {
            logger.debug(e.getMessage(), e);
        }
        throwValidationErrorApi(e.getMessageCode());
    } catch (final ResultOffsetExceededException e) {
        if (logger.isDebugEnabled()) {
            logger.debug(e.getMessage(), e);
        }
        throwValidationErrorApi(messages -> messages.addErrorsResultSizeExceeded(GLOBAL));
    }
    throwValidationErrorApi(messages -> messages.addErrorsInvalidQueryUnknown(GLOBAL));
    // ignore
    return null;
}
Also used : Constants(org.codelibs.fess.Constants) AdminSearchlistAction.validateUpdateFields(org.codelibs.fess.app.web.admin.searchlist.AdminSearchlistAction.validateUpdateFields) AdminSearchlistAction.getDoc(org.codelibs.fess.app.web.admin.searchlist.AdminSearchlistAction.getDoc) LoggerFactory(org.slf4j.LoggerFactory) ApiDeleteResponse(org.codelibs.fess.app.web.api.ApiResult.ApiDeleteResponse) ResultOffsetExceededException(org.codelibs.fess.exception.ResultOffsetExceededException) QueryBuilders(org.elasticsearch.index.query.QueryBuilders) ApiResult(org.codelibs.fess.app.web.api.ApiResult) Status(org.codelibs.fess.app.web.api.ApiResult.Status) CrudMode(org.codelibs.fess.app.web.CrudMode) Map(java.util.Map) ApiResponse(org.codelibs.fess.app.web.api.ApiResult.ApiResponse) SearchRenderData(org.codelibs.fess.entity.SearchRenderData) QueryBuilder(org.elasticsearch.index.query.QueryBuilder) AdminSearchlistAction.validateCreateFields(org.codelibs.fess.app.web.admin.searchlist.AdminSearchlistAction.validateCreateFields) Logger(org.slf4j.Logger) FessApiAdminAction(org.codelibs.fess.app.web.api.admin.FessApiAdminAction) FessEsClient(org.codelibs.fess.es.client.FessEsClient) ApiDocResponse(org.codelibs.fess.app.web.api.ApiResult.ApiDocResponse) Resource(javax.annotation.Resource) StringUtil(org.codelibs.core.lang.StringUtil) JsonResponse(org.lastaflute.web.response.JsonResponse) ApiUpdateResponse(org.codelibs.fess.app.web.api.ApiResult.ApiUpdateResponse) SearchService(org.codelibs.fess.app.service.SearchService) ComponentUtil(org.codelibs.fess.util.ComponentUtil) ApiDocsResponse(org.codelibs.fess.app.web.api.ApiResult.ApiDocsResponse) Execute(org.lastaflute.web.Execute) InvalidQueryException(org.codelibs.fess.exception.InvalidQueryException) SearchRenderData(org.codelibs.fess.entity.SearchRenderData) ResultOffsetExceededException(org.codelibs.fess.exception.ResultOffsetExceededException) ApiDocsResponse(org.codelibs.fess.app.web.api.ApiResult.ApiDocsResponse) InvalidQueryException(org.codelibs.fess.exception.InvalidQueryException) Execute(org.lastaflute.web.Execute)

Example 42 with Execute

use of org.lastaflute.web.Execute in project fess by codelibs.

the class ApiAdminSearchlistAction method put$doc.

// PUT /api/admin/searchlist/doc
@Execute
public JsonResponse<ApiResult> put$doc(final CreateBody body) {
    validateApi(body, messages -> {
    });
    validateCreateFields(body, v -> throwValidationErrorApi(v));
    body.crudMode = CrudMode.CREATE;
    final Map<String, Object> doc = getDoc(body).map(entity -> {
        try {
            entity.putAll(fessConfig.convertToStorableDoc(body.doc));
            final String newId = ComponentUtil.getCrawlingInfoHelper().generateId(entity);
            entity.put(fessConfig.getIndexFieldId(), newId);
            final String index = fessConfig.getIndexDocumentUpdateIndex();
            final String type = fessConfig.getIndexDocumentType();
            fessEsClient.store(index, type, entity);
            saveInfo(messages -> messages.addSuccessCrudCreateCrudTable(GLOBAL));
        } catch (final Exception e) {
            logger.error("Failed to add " + entity, e);
            throwValidationErrorApi(messages -> messages.addErrorsCrudFailedToCreateCrudTable(GLOBAL, buildThrowableMessage(e)));
        }
        return entity;
    }).orElseGet(() -> {
        throwValidationErrorApi(messages -> messages.addErrorsCrudFailedToCreateInstance(GLOBAL));
        return null;
    });
    return asJson(new ApiUpdateResponse().id(doc.get(fessConfig.getIndexFieldDocId()).toString()).created(true).status(Status.OK).result());
}
Also used : Constants(org.codelibs.fess.Constants) AdminSearchlistAction.validateUpdateFields(org.codelibs.fess.app.web.admin.searchlist.AdminSearchlistAction.validateUpdateFields) AdminSearchlistAction.getDoc(org.codelibs.fess.app.web.admin.searchlist.AdminSearchlistAction.getDoc) LoggerFactory(org.slf4j.LoggerFactory) ApiDeleteResponse(org.codelibs.fess.app.web.api.ApiResult.ApiDeleteResponse) ResultOffsetExceededException(org.codelibs.fess.exception.ResultOffsetExceededException) QueryBuilders(org.elasticsearch.index.query.QueryBuilders) ApiResult(org.codelibs.fess.app.web.api.ApiResult) Status(org.codelibs.fess.app.web.api.ApiResult.Status) CrudMode(org.codelibs.fess.app.web.CrudMode) Map(java.util.Map) ApiResponse(org.codelibs.fess.app.web.api.ApiResult.ApiResponse) SearchRenderData(org.codelibs.fess.entity.SearchRenderData) QueryBuilder(org.elasticsearch.index.query.QueryBuilder) AdminSearchlistAction.validateCreateFields(org.codelibs.fess.app.web.admin.searchlist.AdminSearchlistAction.validateCreateFields) Logger(org.slf4j.Logger) FessApiAdminAction(org.codelibs.fess.app.web.api.admin.FessApiAdminAction) FessEsClient(org.codelibs.fess.es.client.FessEsClient) ApiDocResponse(org.codelibs.fess.app.web.api.ApiResult.ApiDocResponse) Resource(javax.annotation.Resource) StringUtil(org.codelibs.core.lang.StringUtil) JsonResponse(org.lastaflute.web.response.JsonResponse) ApiUpdateResponse(org.codelibs.fess.app.web.api.ApiResult.ApiUpdateResponse) SearchService(org.codelibs.fess.app.service.SearchService) ComponentUtil(org.codelibs.fess.util.ComponentUtil) ApiDocsResponse(org.codelibs.fess.app.web.api.ApiResult.ApiDocsResponse) Execute(org.lastaflute.web.Execute) InvalidQueryException(org.codelibs.fess.exception.InvalidQueryException) ApiUpdateResponse(org.codelibs.fess.app.web.api.ApiResult.ApiUpdateResponse) ResultOffsetExceededException(org.codelibs.fess.exception.ResultOffsetExceededException) InvalidQueryException(org.codelibs.fess.exception.InvalidQueryException) Execute(org.lastaflute.web.Execute)

Example 43 with Execute

use of org.lastaflute.web.Execute in project fess by codelibs.

the class ApiAdminSearchlistAction method post$doc.

// POST /api/admin/searchlist/doc
@Execute
public JsonResponse<ApiResult> post$doc(final EditBody body) {
    validateApi(body, messages -> {
    });
    validateUpdateFields(body, v -> throwValidationErrorApi(v));
    body.crudMode = CrudMode.EDIT;
    final Map<String, Object> doc = getDoc(body).map(entity -> {
        try {
            entity.putAll(fessConfig.convertToStorableDoc(body.doc));
            final String newId = ComponentUtil.getCrawlingInfoHelper().generateId(entity);
            String oldId = null;
            if (newId.equals(body.id)) {
                entity.put(fessConfig.getIndexFieldId(), body.id);
                entity.put(fessConfig.getIndexFieldVersion(), body.version);
            } else {
                oldId = body.id;
                entity.put(fessConfig.getIndexFieldId(), newId);
                entity.remove(fessConfig.getIndexFieldVersion());
            }
            final String index = fessConfig.getIndexDocumentUpdateIndex();
            final String type = fessConfig.getIndexDocumentType();
            fessEsClient.store(index, type, entity);
            if (oldId != null) {
                fessEsClient.delete(index, type, oldId, body.version);
            }
            saveInfo(messages -> messages.addSuccessCrudUpdateCrudTable(GLOBAL));
        } catch (final Exception e) {
            logger.error("Failed to update " + entity, e);
            throwValidationErrorApi(messages -> messages.addErrorsCrudFailedToUpdateCrudTable(GLOBAL, buildThrowableMessage(e)));
        }
        return entity;
    }).orElseGet(() -> {
        throwValidationErrorApi(messages -> messages.addErrorsCrudCouldNotFindCrudTable(GLOBAL, body.id));
        return null;
    });
    return asJson(new ApiUpdateResponse().id(doc.get(fessConfig.getIndexFieldDocId()).toString()).created(false).status(Status.OK).result());
}
Also used : Constants(org.codelibs.fess.Constants) AdminSearchlistAction.validateUpdateFields(org.codelibs.fess.app.web.admin.searchlist.AdminSearchlistAction.validateUpdateFields) AdminSearchlistAction.getDoc(org.codelibs.fess.app.web.admin.searchlist.AdminSearchlistAction.getDoc) LoggerFactory(org.slf4j.LoggerFactory) ApiDeleteResponse(org.codelibs.fess.app.web.api.ApiResult.ApiDeleteResponse) ResultOffsetExceededException(org.codelibs.fess.exception.ResultOffsetExceededException) QueryBuilders(org.elasticsearch.index.query.QueryBuilders) ApiResult(org.codelibs.fess.app.web.api.ApiResult) Status(org.codelibs.fess.app.web.api.ApiResult.Status) CrudMode(org.codelibs.fess.app.web.CrudMode) Map(java.util.Map) ApiResponse(org.codelibs.fess.app.web.api.ApiResult.ApiResponse) SearchRenderData(org.codelibs.fess.entity.SearchRenderData) QueryBuilder(org.elasticsearch.index.query.QueryBuilder) AdminSearchlistAction.validateCreateFields(org.codelibs.fess.app.web.admin.searchlist.AdminSearchlistAction.validateCreateFields) Logger(org.slf4j.Logger) FessApiAdminAction(org.codelibs.fess.app.web.api.admin.FessApiAdminAction) FessEsClient(org.codelibs.fess.es.client.FessEsClient) ApiDocResponse(org.codelibs.fess.app.web.api.ApiResult.ApiDocResponse) Resource(javax.annotation.Resource) StringUtil(org.codelibs.core.lang.StringUtil) JsonResponse(org.lastaflute.web.response.JsonResponse) ApiUpdateResponse(org.codelibs.fess.app.web.api.ApiResult.ApiUpdateResponse) SearchService(org.codelibs.fess.app.service.SearchService) ComponentUtil(org.codelibs.fess.util.ComponentUtil) ApiDocsResponse(org.codelibs.fess.app.web.api.ApiResult.ApiDocsResponse) Execute(org.lastaflute.web.Execute) InvalidQueryException(org.codelibs.fess.exception.InvalidQueryException) ApiUpdateResponse(org.codelibs.fess.app.web.api.ApiResult.ApiUpdateResponse) ResultOffsetExceededException(org.codelibs.fess.exception.ResultOffsetExceededException) InvalidQueryException(org.codelibs.fess.exception.InvalidQueryException) Execute(org.lastaflute.web.Execute)

Example 44 with Execute

use of org.lastaflute.web.Execute in project fess by codelibs.

the class ApiAdminWebconfigAction method settings.

// ===================================================================================
//                                                                      Search Execute
//                                                                      ==============
// GET /api/admin/webconfig/settings
// POST /api/admin/webconfig/settings
@Execute
public JsonResponse<ApiResult> settings(final SearchBody body) {
    validateApi(body, messages -> {
    });
    final WebConfigPager pager = copyBeanToNewBean(body, WebConfigPager.class);
    final List<WebConfig> list = webConfigService.getWebConfigList(pager);
    return asJson(new ApiResult.ApiConfigsResponse<EditBody>().settings(list.stream().map(entity -> createEditBody(entity)).collect(Collectors.toList())).total(pager.getAllRecordCount()).status(ApiResult.Status.OK).result());
}
Also used : ApiResult(org.codelibs.fess.app.web.api.ApiResult) WebConfigPager(org.codelibs.fess.app.pager.WebConfigPager) AdminWebconfigAction.getWebConfig(org.codelibs.fess.app.web.admin.webconfig.AdminWebconfigAction.getWebConfig) WebConfig(org.codelibs.fess.es.config.exentity.WebConfig) Execute(org.lastaflute.web.Execute)

Example 45 with Execute

use of org.lastaflute.web.Execute in project fess by codelibs.

the class GoAction method index.

// ===================================================================================
//                                                                           Attribute
//
// ===================================================================================
//                                                                               Hook
//                                                                              ======
// ===================================================================================
//                                                                      Search Execute
//                                                                      ==============
@Execute
public ActionResponse index(final GoForm form) throws IOException {
    validate(form, messages -> {
    }, () -> asHtml(virtualHost(path_Error_ErrorJsp)));
    if (isLoginRequired()) {
        return redirectToLogin();
    }
    Map<String, Object> doc = null;
    try {
        doc = searchService.getDocumentByDocId(form.docId, new String[] { fessConfig.getIndexFieldUrl(), fessConfig.getIndexFieldConfigId() }, 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 url = DocumentUtil.getValue(doc, fessConfig.getIndexFieldUrl(), String.class);
    if (url == null) {
        saveError(messages -> messages.addErrorsDocumentNotFound(GLOBAL, form.docId));
        return redirect(ErrorAction.class);
    }
    if (fessConfig.isSearchLog()) {
        final String userSessionId = userInfoHelper.getUserCode();
        if (userSessionId != null) {
            final SearchLogHelper searchLogHelper = ComponentUtil.getSearchLogHelper();
            final ClickLog clickLog = new ClickLog();
            clickLog.setUrl(url);
            clickLog.setRequestedAt(systemHelper.getCurrentTimeAsLocalDateTime());
            clickLog.setQueryRequestedAt(DfTypeUtil.toLocalDateTime(Long.parseLong(form.rt)));
            clickLog.setUserSessionId(userSessionId);
            clickLog.setDocId(form.docId);
            clickLog.setQueryId(form.queryId);
            if (form.order != null) {
                clickLog.setOrder(form.order);
            }
            searchLogHelper.addClickLog(clickLog);
        }
    }
    String hash;
    if (StringUtil.isNotBlank(form.hash)) {
        final String value = URLUtil.decode(form.hash, Constants.UTF_8);
        final StringBuilder buf = new StringBuilder(value.length() + 100);
        for (final char c : value.toCharArray()) {
            if (CharUtil.isUrlChar(c) || c == ' ') {
                buf.append(c);
            } else {
                try {
                    buf.append(URLEncoder.encode(String.valueOf(c), Constants.UTF_8));
                } catch (final UnsupportedEncodingException e) {
                // NOP
                }
            }
        }
        hash = buf.toString();
    } else {
        hash = StringUtil.EMPTY;
    }
    final String targetUrl = pathMappingHelper.replaceUrl(url);
    if (isFileSystemPath(targetUrl)) {
        if (fessConfig.isSearchFileProxyEnabled()) {
            final ViewHelper viewHelper = ComponentUtil.getViewHelper();
            try {
                final StreamResponse response = viewHelper.asContentResponse(doc);
                if (response.getHttpStatus().orElse(200) == 404) {
                    logger.debug("Not found: " + targetUrl);
                    saveError(messages -> messages.addErrorsNotFoundOnFileSystem(GLOBAL, targetUrl));
                    return redirect(ErrorAction.class);
                }
                return response;
            } catch (final Exception e) {
                logger.warn("Failed to load: " + doc, e);
                saveError(messages -> messages.addErrorsNotLoadFromServer(GLOBAL, targetUrl));
                return redirect(ErrorAction.class);
            }
        } else {
            return redirect(targetUrl + hash);
        }
    } else {
        return redirect(targetUrl + hash);
    }
}
Also used : FessSearchAction(org.codelibs.fess.app.web.base.FessSearchAction) Constants(org.codelibs.fess.Constants) DocumentUtil(org.codelibs.fess.util.DocumentUtil) LoggerFactory(org.slf4j.LoggerFactory) ClickLog(org.codelibs.fess.es.log.exentity.ClickLog) LaRequestUtil(org.lastaflute.web.util.LaRequestUtil) HttpServletRequest(javax.servlet.http.HttpServletRequest) ErrorAction(org.codelibs.fess.app.web.error.ErrorAction) StreamResponse(org.lastaflute.web.response.StreamResponse) Map(java.util.Map) CharUtil(org.codelibs.fess.crawler.util.CharUtil) HtmlResponse(org.lastaflute.web.response.HtmlResponse) Logger(org.slf4j.Logger) Resource(javax.annotation.Resource) StringUtil(org.codelibs.core.lang.StringUtil) IOException(java.io.IOException) URLUtil(org.codelibs.core.net.URLUtil) ActionResponse(org.lastaflute.web.response.ActionResponse) URLEncoder(java.net.URLEncoder) SearchLogHelper(org.codelibs.fess.helper.SearchLogHelper) ComponentUtil(org.codelibs.fess.util.ComponentUtil) DfTypeUtil(org.dbflute.util.DfTypeUtil) PathMappingHelper(org.codelibs.fess.helper.PathMappingHelper) Execute(org.lastaflute.web.Execute) ViewHelper(org.codelibs.fess.helper.ViewHelper) UnsupportedEncodingException(java.io.UnsupportedEncodingException) StreamResponse(org.lastaflute.web.response.StreamResponse) UnsupportedEncodingException(java.io.UnsupportedEncodingException) SearchLogHelper(org.codelibs.fess.helper.SearchLogHelper) ViewHelper(org.codelibs.fess.helper.ViewHelper) IOException(java.io.IOException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ErrorAction(org.codelibs.fess.app.web.error.ErrorAction) ClickLog(org.codelibs.fess.es.log.exentity.ClickLog) Execute(org.lastaflute.web.Execute)

Aggregations

Execute (org.lastaflute.web.Execute)125 ApiResult (org.codelibs.fess.app.web.api.ApiResult)94 Resource (javax.annotation.Resource)83 CrudMode (org.codelibs.fess.app.web.CrudMode)77 FessApiAdminAction (org.codelibs.fess.app.web.api.admin.FessApiAdminAction)71 JsonResponse (org.lastaflute.web.response.JsonResponse)71 Collectors (java.util.stream.Collectors)70 List (java.util.List)51 InputStream (java.io.InputStream)39 File (java.io.File)35 StreamResponse (org.lastaflute.web.response.StreamResponse)34 ApiResponse (org.codelibs.fess.app.web.api.ApiResult.ApiResponse)32 Status (org.codelibs.fess.app.web.api.ApiResult.Status)31 Constants (org.codelibs.fess.Constants)26 ApiUpdateResponse (org.codelibs.fess.app.web.api.ApiResult.ApiUpdateResponse)26 StringUtil (org.codelibs.core.lang.StringUtil)24 ComponentUtil (org.codelibs.fess.util.ComponentUtil)23 ApiConfigResponse (org.codelibs.fess.app.web.api.ApiResult.ApiConfigResponse)22 HtmlResponse (org.lastaflute.web.response.HtmlResponse)20 FessAdminAction (org.codelibs.fess.app.web.base.FessAdminAction)14