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();
}
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;
}
Aggregations