Search in sources :

Example 1 with SearchRequestType

use of org.codelibs.fess.entity.SearchRequestParams.SearchRequestType in project fess by codelibs.

the class PopularWordHelper method getWordList.

public List<String> getWordList(final SearchRequestType searchRequestType, final String seed, final String[] tags, final String[] roles, final String[] fields, final String[] excludes) {
    final String baseSeed = seed != null ? seed : fessConfig.getSuggestPopularWordSeed();
    final String[] baseTags = tags != null ? tags : fessConfig.getSuggestPopularWordTagsAsArray();
    final String[] baseRoles = roles != null ? roles : ComponentUtil.getRoleQueryHelper().build(searchRequestType).stream().filter(StringUtil::isNotBlank).toArray(n -> new String[n]);
    final String[] baseFields = fields != null ? fields : fessConfig.getSuggestPopularWordFieldsAsArray();
    final String[] baseExcludes = excludes != null ? excludes : fessConfig.getSuggestPopularWordExcludesAsArray();
    try {
        return cache.get(getCacheKey(baseSeed, baseTags, baseRoles, baseFields, baseExcludes), () -> {
            final List<String> wordList = new ArrayList<>();
            final SuggestHelper suggestHelper = ComponentUtil.getSuggestHelper();
            final PopularWordsRequestBuilder popularWordsRequestBuilder = suggestHelper.suggester().popularWords().setSize(fessConfig.getSuggestPopularWordSizeAsInteger()).setWindowSize(fessConfig.getSuggestPopularWordWindowSizeAsInteger()).setQueryFreqThreshold(fessConfig.getSuggestPopularWordQueryFreqAsInteger());
            popularWordsRequestBuilder.setSeed(baseSeed);
            stream(baseTags).of(stream -> stream.forEach(tag -> popularWordsRequestBuilder.addTag(tag)));
            stream(baseRoles).of(stream -> stream.forEach(role -> popularWordsRequestBuilder.addRole(role)));
            stream(baseFields).of(stream -> stream.forEach(field -> popularWordsRequestBuilder.addField(field)));
            stream(baseExcludes).of(stream -> stream.forEach(exclude -> popularWordsRequestBuilder.addExcludeWord(exclude)));
            try {
                popularWordsRequestBuilder.execute().getResponse().getItems().stream().forEach(item -> wordList.add(item.getText()));
            } catch (final SuggesterException e) {
                logger.warn("Failed to generate popular words.", e);
            }
            return wordList;
        });
    } catch (final ExecutionException e) {
        logger.warn("Failed to load popular words.", e);
    }
    return Collections.emptyList();
}
Also used : StreamUtil.stream(org.codelibs.core.stream.StreamUtil.stream) StringUtil(org.codelibs.core.lang.StringUtil) PopularWordsRequestBuilder(org.codelibs.fess.suggest.request.popularwords.PopularWordsRequestBuilder) ArrayList(java.util.ArrayList) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) SearchRequestType(org.codelibs.fess.entity.SearchRequestParams.SearchRequestType) FessConfig(org.codelibs.fess.mylasta.direction.FessConfig) Logger(org.apache.logging.log4j.Logger) SuggesterException(org.codelibs.fess.suggest.exception.SuggesterException) ComponentUtil(org.codelibs.fess.util.ComponentUtil) PostConstruct(javax.annotation.PostConstruct) CacheBuilder(com.google.common.cache.CacheBuilder) Cache(com.google.common.cache.Cache) Collections(java.util.Collections) LogManager(org.apache.logging.log4j.LogManager) ArrayList(java.util.ArrayList) PopularWordsRequestBuilder(org.codelibs.fess.suggest.request.popularwords.PopularWordsRequestBuilder) StringUtil(org.codelibs.core.lang.StringUtil) ExecutionException(java.util.concurrent.ExecutionException) SuggesterException(org.codelibs.fess.suggest.exception.SuggesterException)

Example 2 with SearchRequestType

use of org.codelibs.fess.entity.SearchRequestParams.SearchRequestType in project fess by codelibs.

the class RoleQueryHelper method build.

public Set<String> build(final SearchRequestType searchRequestType) {
    final Set<String> roleSet = new HashSet<>();
    final HttpServletRequest request = LaRequestUtil.getOptionalRequest().orElse(null);
    final FessConfig fessConfig = ComponentUtil.getFessConfig();
    final boolean isApiRequest = !SearchRequestType.SEARCH.equals(searchRequestType) && !SearchRequestType.ADMIN_SEARCH.equals(searchRequestType);
    if (request != null) {
        @SuppressWarnings("unchecked") final Set<String> list = (Set<String>) request.getAttribute(USER_ROLES);
        if (list != null) {
            return list;
        }
        // request parameter
        if (StringUtil.isNotBlank(parameterKey)) {
            processParameter(request, roleSet);
        }
        // request header
        if (StringUtil.isNotBlank(headerKey)) {
            processHeader(request, roleSet);
        }
        // cookie
        if (StringUtil.isNotBlank(cookieKey)) {
            processCookie(request, roleSet);
        }
        // cookie mapping
        if (cookieNameMap != null) {
            buildByCookieNameMapping(request, roleSet);
        }
        final boolean hasAccessToken = processAccessToken(request, roleSet, isApiRequest);
        final RequestManager requestManager = ComponentUtil.getRequestManager();
        try {
            requestManager.findUserBean(FessUserBean.class).ifPresent(fessUserBean -> stream(fessUserBean.getPermissions()).of(stream -> stream.forEach(roleSet::add))).orElse(() -> {
                if (isApiRequest && ComponentUtil.getFessConfig().getApiAccessTokenRequiredAsBoolean()) {
                    throw new InvalidAccessTokenException("invalid_token", "Access token is requried.");
                }
                if (!hasAccessToken || roleSet.isEmpty()) {
                    roleSet.addAll(fessConfig.getSearchGuestPermissionList());
                }
            });
        } catch (final RuntimeException e) {
            try {
                requestManager.findLoginManager(FessUserBean.class).ifPresent(LoginManager::logout);
            } catch (final Exception e1) {
            // ignore
            }
            throw e;
        }
    }
    if (defaultRoleList != null) {
        roleSet.addAll(defaultRoleList);
    }
    if (logger.isDebugEnabled()) {
        logger.debug("roleSet: {}", roleSet);
    }
    if (request != null) {
        request.setAttribute(USER_ROLES, roleSet);
    }
    return roleSet;
}
Also used : StreamUtil.stream(org.codelibs.core.stream.StreamUtil.stream) AccessTokenService(org.codelibs.fess.app.service.AccessTokenService) StringUtil(org.codelibs.core.lang.StringUtil) Set(java.util.Set) HashMap(java.util.HashMap) InvalidAccessTokenException(org.codelibs.fess.exception.InvalidAccessTokenException) LaRequestUtil(org.lastaflute.web.util.LaRequestUtil) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) FessUserBean(org.codelibs.fess.mylasta.action.FessUserBean) List(java.util.List) HttpServletRequest(javax.servlet.http.HttpServletRequest) SearchRequestType(org.codelibs.fess.entity.SearchRequestParams.SearchRequestType) FessConfig(org.codelibs.fess.mylasta.direction.FessConfig) Logger(org.apache.logging.log4j.Logger) RequestManager(org.lastaflute.web.servlet.request.RequestManager) ComponentUtil(org.codelibs.fess.util.ComponentUtil) CachedCipher(org.codelibs.core.crypto.CachedCipher) LoginManager(org.lastaflute.web.login.LoginManager) Map(java.util.Map) PostConstruct(javax.annotation.PostConstruct) Cookie(javax.servlet.http.Cookie) LogManager(org.apache.logging.log4j.LogManager) Set(java.util.Set) HashSet(java.util.HashSet) FessConfig(org.codelibs.fess.mylasta.direction.FessConfig) InvalidAccessTokenException(org.codelibs.fess.exception.InvalidAccessTokenException) HttpServletRequest(javax.servlet.http.HttpServletRequest) InvalidAccessTokenException(org.codelibs.fess.exception.InvalidAccessTokenException) RequestManager(org.lastaflute.web.servlet.request.RequestManager) HashSet(java.util.HashSet)

Aggregations

ArrayList (java.util.ArrayList)2 List (java.util.List)2 PostConstruct (javax.annotation.PostConstruct)2 LogManager (org.apache.logging.log4j.LogManager)2 Logger (org.apache.logging.log4j.Logger)2 StringUtil (org.codelibs.core.lang.StringUtil)2 StreamUtil.stream (org.codelibs.core.stream.StreamUtil.stream)2 SearchRequestType (org.codelibs.fess.entity.SearchRequestParams.SearchRequestType)2 FessConfig (org.codelibs.fess.mylasta.direction.FessConfig)2 ComponentUtil (org.codelibs.fess.util.ComponentUtil)2 Cache (com.google.common.cache.Cache)1 CacheBuilder (com.google.common.cache.CacheBuilder)1 Collections (java.util.Collections)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1 Set (java.util.Set)1 ExecutionException (java.util.concurrent.ExecutionException)1 TimeUnit (java.util.concurrent.TimeUnit)1 Cookie (javax.servlet.http.Cookie)1