Example usage for org.apache.commons.lang3 StringUtils abbreviate

List of usage examples for org.apache.commons.lang3 StringUtils abbreviate

Introduction

In this page you can find the example usage for org.apache.commons.lang3 StringUtils abbreviate.

Prototype

public static String abbreviate(final String str, final int maxWidth) 

Source Link

Document

Abbreviates a String using ellipses.

Usage

From source file:org.codelibs.fess.app.service.FailureUrlService.java

public void store(final CrawlingConfig crawlingConfig, final String errorName, final String url,
        final Throwable e) {
    if (e instanceof ContainerNotAvailableException) {
        return;//ww w  . j a  va  2s  .c om
    }

    final FailureUrlBhv bhv = ComponentUtil.getComponent(FailureUrlBhv.class);
    final FailureUrl failureUrl = bhv.selectEntity(cb -> {
        cb.query().setUrl_Equal(url);
        if (crawlingConfig != null) {
            cb.query().setConfigId_Equal(crawlingConfig.getConfigId());
        }
    }).map(entity -> {
        entity.setErrorCount(entity.getErrorCount() + 1);
        return entity;
    }).orElseGet(() -> {
        final FailureUrl entity = new FailureUrl();
        entity.setErrorCount(1);
        entity.setUrl(url);
        if (crawlingConfig != null) {
            entity.setConfigId(crawlingConfig.getConfigId());
        }
        return entity;
    });

    failureUrl.setErrorName(errorName);
    failureUrl.setErrorLog(StringUtils.abbreviate(getStackTrace(e), 4000));
    failureUrl.setLastAccessTime(ComponentUtil.getSystemHelper().getCurrentTimeAsLong());
    failureUrl.setThreadName(Thread.currentThread().getName());

    bhv.insertOrUpdate(failureUrl, op -> {
        op.setRefreshPolicy(Constants.TRUE);
    });
}

From source file:org.codelibs.fess.app.web.admin.wizard.AdminWizardAction.java

protected String crawlingConfigInternal(final CrawlingConfigForm form) {

    String configName = form.crawlingConfigName;
    String configPath = form.crawlingConfigPath.trim();
    if (StringUtil.isBlank(configName)) {
        configName = StringUtils.abbreviate(configPath, 30);
    }//from   www. j  a  v a 2  s.  c  o m

    // normalize
    final StringBuilder buf = new StringBuilder(1000);
    for (int i = 0; i < configPath.length(); i++) {
        final char c = configPath.charAt(i);
        if (c == '\\') {
            buf.append('/');
        } else if (c == ' ') {
            buf.append("%20");
        } else if (CharUtil.isUrlChar(c)) {
            buf.append(c);
        } else {
            try {
                buf.append(URLEncoder.encode(String.valueOf(c), Constants.UTF_8));
            } catch (final UnsupportedEncodingException e) {
            }
        }
    }
    configPath = convertCrawlingPath(buf.toString());

    final String username = systemHelper.getUsername();
    final long now = systemHelper.getCurrentTimeAsLong();

    try {
        if (isWebCrawlingPath(configPath)) {
            // web
            final WebConfig wConfig = new WebConfig();
            wConfig.setAvailable(Constants.T);
            wConfig.setBoost(1.0f);
            wConfig.setCreatedBy(username);
            wConfig.setCreatedTime(now);
            if (form.depth != null) {
                wConfig.setDepth(form.depth);
            }
            wConfig.setExcludedDocUrls(
                    getDefaultString("default.config.web.excludedDocUrls", StringUtil.EMPTY));
            wConfig.setExcludedUrls(getDefaultString("default.config.web.excludedUrls", StringUtil.EMPTY));
            wConfig.setIncludedDocUrls(
                    getDefaultString("default.config.web.includedDocUrls", StringUtil.EMPTY));
            wConfig.setIncludedUrls(getDefaultString("default.config.web.includedUrls", StringUtil.EMPTY));
            wConfig.setIntervalTime(getDefaultInteger("default.config.web.intervalTime",
                    Constants.DEFAULT_INTERVAL_TIME_FOR_WEB));
            if (form.maxAccessCount != null) {
                wConfig.setMaxAccessCount(form.maxAccessCount);
            }
            wConfig.setName(configName);
            wConfig.setNumOfThread(getDefaultInteger("default.config.web.numOfThread",
                    Constants.DEFAULT_NUM_OF_THREAD_FOR_WEB));
            wConfig.setSortOrder(getDefaultInteger("default.config.web.sortOrder", 1));
            wConfig.setUpdatedBy(username);
            wConfig.setUpdatedTime(now);
            wConfig.setUrls(configPath);
            wConfig.setUserAgent(
                    getDefaultString("default.config.web.userAgent", fessConfig.getUserAgentName()));
            wConfig.setPermissions(ComponentUtil.getFessConfig().getSearchDefaultDisplayEncodedPermissions());

            webConfigService.store(wConfig);

        } else {
            // file
            final FileConfig fConfig = new FileConfig();
            fConfig.setAvailable(Constants.T);
            fConfig.setBoost(1.0f);
            fConfig.setCreatedBy(username);
            fConfig.setCreatedTime(now);
            if (form.depth != null) {
                fConfig.setDepth(form.depth);
            }
            fConfig.setExcludedDocPaths(
                    getDefaultString("default.config.file.excludedDocPaths", StringUtil.EMPTY));
            fConfig.setExcludedPaths(getDefaultString("default.config.file.excludedPaths", StringUtil.EMPTY));
            fConfig.setIncludedDocPaths(
                    getDefaultString("default.config.file.includedDocPaths", StringUtil.EMPTY));
            fConfig.setIncludedPaths(getDefaultString("default.config.file.includedPaths", StringUtil.EMPTY));
            fConfig.setIntervalTime(getDefaultInteger("default.config.file.intervalTime",
                    Constants.DEFAULT_INTERVAL_TIME_FOR_FS));
            if (form.maxAccessCount != null) {
                fConfig.setMaxAccessCount(form.maxAccessCount);
            }
            fConfig.setName(configName);
            fConfig.setNumOfThread(getDefaultInteger("default.config.file.numOfThread",
                    Constants.DEFAULT_NUM_OF_THREAD_FOR_FS));
            fConfig.setSortOrder(getDefaultInteger("default.config.file.sortOrder", 1));
            fConfig.setUpdatedBy(username);
            fConfig.setUpdatedTime(now);
            fConfig.setPaths(configPath);
            fConfig.setPermissions(ComponentUtil.getFessConfig().getSearchDefaultDisplayEncodedPermissions());

            fileConfigService.store(fConfig);
        }
        return configName;
    } catch (final Exception e) {
        logger.error("Failed to create crawling config: " + form.crawlingConfigPath, e);
        throwValidationError(messages -> messages.addErrorsFailedToCreateCrawlingConfigAtWizard(GLOBAL), () -> {
            return asHtml(path_AdminWizard_AdminWizardConfigJsp);
        });
        return null;
    }
}

From source file:org.codelibs.fess.crawler.transformer.FessTransformer.java

default String abbreviateSite(final String value) {
    final int maxSiteLength = getMaxSiteLength();
    if (maxSiteLength > -1) {
        return StringUtils.abbreviate(value, maxSiteLength);
    } else {/*from w w  w  .ja v a  2 s.c  om*/
        return value;
    }
}

From source file:org.codelibs.fess.helper.DocumentHelper.java

public String getDigest(final ResponseData responseData, final String content,
        final Map<String, Object> dataMap, final int maxWidth) {
    if (content == null) {
        return StringUtil.EMPTY; // empty
    }// w  w w. j a v  a 2 s  . c o  m

    String subContent;
    if (content.length() < maxWidth * 2) {
        subContent = content;
    } else {
        subContent = content.substring(0, maxWidth * 2);
    }

    final int[] spaceChars = getSpaceChars();
    try (final Reader reader = new StringReader(subContent)) {
        final String originalStr = TextUtil.normalizeText(reader).initialCapacity(content.length())
                .spaceChars(spaceChars).execute();
        return StringUtils.abbreviate(originalStr, maxWidth);
    } catch (final IOException e) {
        return StringUtil.EMPTY; // empty
    }
}

From source file:org.codelibs.fess.helper.SearchLogHelper.java

public void addSearchLog(final SearchRequestParams params, final LocalDateTime requestedTime,
        final String queryId, final String query, final int pageStart, final int pageSize,
        final QueryResponseList queryResponseList) {

    final RoleQueryHelper roleQueryHelper = ComponentUtil.getRoleQueryHelper();
    final UserInfoHelper userInfoHelper = ComponentUtil.getUserInfoHelper();
    final SearchLog searchLog = new SearchLog();

    if (ComponentUtil.getFessConfig().isUserInfo()) {
        final String userCode = userInfoHelper.getUserCode();
        if (userCode != null) {
            searchLog.setUserSessionId(userCode);
            searchLog.setUserInfo(getUserInfo(userCode));
        }/*from   w  w  w  .j ava 2s.co  m*/
    }

    searchLog.setRoles(roleQueryHelper.build(params.getType()).stream().toArray(n -> new String[n]));
    searchLog.setQueryId(queryId);
    searchLog.setHitCount(queryResponseList.getAllRecordCount());
    searchLog.setResponseTime(queryResponseList.getExecTime());
    searchLog.setQueryTime(queryResponseList.getQueryTime());
    searchLog.setSearchWord(StringUtils.abbreviate(query, 1000));
    searchLog.setRequestedAt(requestedTime);
    searchLog.setSearchQuery(StringUtils.abbreviate(queryResponseList.getSearchQuery(), 1000));
    searchLog.setQueryOffset(pageStart);
    searchLog.setQueryPageSize(pageSize);
    ComponentUtil.getRequestManager().findUserBean(FessUserBean.class).ifPresent(user -> {
        searchLog.setUser(user.getUserId());
    });

    final HttpServletRequest request = LaRequestUtil.getRequest();
    searchLog.setClientIp(StringUtils.abbreviate(ComponentUtil.getViewHelper().getClientIp(request), 100));
    searchLog.setReferer(StringUtils.abbreviate(request.getHeader("referer"), 1000));
    searchLog.setUserAgent(StringUtils.abbreviate(request.getHeader("user-agent"), 255));
    final Object accessType = request.getAttribute(Constants.SEARCH_LOG_ACCESS_TYPE);
    if (Constants.SEARCH_LOG_ACCESS_TYPE_JSON.equals(accessType)) {
        searchLog.setAccessType(Constants.SEARCH_LOG_ACCESS_TYPE_JSON);
    } else if (Constants.SEARCH_LOG_ACCESS_TYPE_GSA.equals(accessType)) {
        searchLog.setAccessType(Constants.SEARCH_LOG_ACCESS_TYPE_GSA);
    } else if (Constants.SEARCH_LOG_ACCESS_TYPE_OTHER.equals(accessType)) {
        searchLog.setAccessType(Constants.SEARCH_LOG_ACCESS_TYPE_OTHER);
    } else if (Constants.SEARCH_LOG_ACCESS_TYPE_ADMIN.equals(accessType)) {
        searchLog.setAccessType(Constants.SEARCH_LOG_ACCESS_TYPE_ADMIN);
    } else {
        searchLog.setAccessType(Constants.SEARCH_LOG_ACCESS_TYPE_WEB);
    }
    final Object languages = request.getAttribute(Constants.REQUEST_LANGUAGES);
    if (languages != null) {
        searchLog.setLanguages(StringUtils.join((String[]) languages, ","));
    } else {
        searchLog.setLanguages(StringUtil.EMPTY);
    }
    final String virtualHostKey = ComponentUtil.getVirtualHostHelper().getVirtualHostKey();
    if (StringUtil.isNotBlank(virtualHostKey)) {
        searchLog.setVirtualHost(virtualHostKey);
    } else {
        searchLog.setVirtualHost(StringUtil.EMPTY);
    }

    @SuppressWarnings("unchecked")
    final Map<String, List<String>> fieldLogMap = (Map<String, List<String>>) request
            .getAttribute(Constants.FIELD_LOGS);
    if (fieldLogMap != null) {
        final int queryMaxLength = ComponentUtil.getFessConfig().getQueryMaxLengthAsInteger();
        for (final Map.Entry<String, List<String>> logEntry : fieldLogMap.entrySet()) {
            for (final String value : logEntry.getValue()) {
                searchLog.addSearchFieldLogValue(logEntry.getKey(),
                        StringUtils.abbreviate(value, queryMaxLength));
            }
        }
    }

    addDocumentsInResponse(queryResponseList, searchLog);

    searchLogQueue.add(searchLog);
}

From source file:org.codelibs.fess.helper.SystemHelper.java

public String abbreviateLongText(final String str) {
    return StringUtils.abbreviate(str,
            ComponentUtil.getFessConfig().getMaxLogOutputLengthAsInteger().intValue());
}

From source file:org.codelibs.fess.helper.ViewHelper.java

public String getContentTitle(final Map<String, Object> document) {
    final FessConfig fessConfig = ComponentUtil.getFessConfig();
    String title = DocumentUtil.getValue(document, fessConfig.getIndexFieldTitle(), String.class);
    if (StringUtil.isBlank(title)) {
        title = DocumentUtil.getValue(document, fessConfig.getIndexFieldFilename(), String.class);
        if (StringUtil.isBlank(title)) {
            title = DocumentUtil.getValue(document, fessConfig.getIndexFieldUrl(), String.class);
        }//from   w w w.  j a  va  2 s .  com
    }
    final int size = fessConfig.getResponseMaxTitleLengthAsInteger();
    if (size > -1) {
        title = StringUtils.abbreviate(title, size);
    }
    final String value = LaFunctions.h(title);
    if (!fessConfig.isResponseHighlightContentTitleEnabled()) {
        return value;
    }
    return getQuerySet().map(querySet -> {
        final Matcher matcher = Pattern
                .compile(querySet.stream().map(LaFunctions::h).map(Pattern::quote)
                        .collect(Collectors.joining("|")), Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE)
                .matcher(value);
        final StringBuffer buf = new StringBuffer(value.length() + 100);
        while (matcher.find()) {
            matcher.appendReplacement(buf, highlightTagPre + matcher.group(0) + highlightTagPost);
        }
        matcher.appendTail(buf);
        return buf.toString();
    }).orElse(value);
}

From source file:org.codelibs.fess.helper.ViewHelper.java

public Object getSitePath(final Map<String, Object> docMap) {
    final FessConfig fessConfig = ComponentUtil.getFessConfig();
    final Object urlLink = docMap.get(fessConfig.getResponseFieldUrlLink());
    if (urlLink != null) {
        final String returnUrl;
        final String url = urlLink.toString();
        if (LOCAL_PATH_PATTERN.matcher(url).find() || SHARED_FOLDER_PATTERN.matcher(url).find()) {
            returnUrl = url.replaceFirst("^file:/+", "");
        } else if (url.startsWith("file:")) {
            returnUrl = url.replaceFirst("^file:/+", "/");
        } else {//from   www.j  av a  2 s.  c  o m
            returnUrl = url.replaceFirst("^[a-zA-Z0-9]*:/+", "");
        }
        final int size = fessConfig.getResponseMaxSitePathLengthAsInteger();
        if (size > -1) {
            return StringUtils.abbreviate(returnUrl, size);
        } else {
            return returnUrl;
        }
    }
    return null;
}

From source file:org.drftpd.commands.tvmaze.TvMazeUtils.java

public static ReplacerEnvironment getShowEnv(TvMazeInfo tvShow) {
    ReplacerEnvironment env = new ReplacerEnvironment(SiteBot.GLOBAL_ENV);
    DateTimeFormatter df = DateTimeFormat.forPattern(TvMazeConfig.getInstance().getDateFormat());
    DateTimeFormatter tf = DateTimeFormat.forPattern(TvMazeConfig.getInstance().getTimeFormat());

    env.add("id", tvShow.getID());
    env.add("url", tvShow.getURL());
    env.add("name", tvShow.getName());
    env.add("type", tvShow.getType());
    env.add("language", tvShow.getLanguage());
    env.add("genres", StringUtils.join(tvShow.getGenres(), " | "));
    env.add("status", tvShow.getStatus());
    env.add("runtime", tvShow.getRuntime());
    DateTimeFormatter dtf = DateTimeFormat.forPattern("yyyy-MM-dd");
    env.add("premiered", df.withZone(TvMazeConfig.getInstance().getTimezone())
            .print(dtf.parseDateTime(tvShow.getPremiered())));
    env.add("network", tvShow.getNetwork());
    env.add("country", tvShow.getCountry());
    env.add("summary", StringUtils.abbreviate(tvShow.getSummary(), 250));

    if (tvShow.getPreviousEP() != null) {
        env.add("prevepid", tvShow.getPreviousEP().getID());
        env.add("prevepurl", tvShow.getPreviousEP().getURL());
        env.add("prevepname", tvShow.getPreviousEP().getName());
        env.add("prevepseason", tvShow.getPreviousEP().getSeason());
        env.add("prevepnumber", String.format("%02d", tvShow.getPreviousEP().getNumber()));
        env.add("prevepairdate", df.withZone(TvMazeConfig.getInstance().getTimezone())
                .print(new DateTime(tvShow.getPreviousEP().getAirDate())));
        env.add("prevepairtime", tf.withZone(TvMazeConfig.getInstance().getTimezone())
                .print(new DateTime(tvShow.getPreviousEP().getAirDate())));
        env.add("prevepruntime", tvShow.getPreviousEP().getRuntime());
        env.add("prevepsummary", StringUtils.abbreviate(tvShow.getPreviousEP().getSummary(), 250));
        env.add("prevepage", calculateAge(new DateTime(tvShow.getPreviousEP().getAirDate())));
    }// ww w  .j  a  va2  s . c  om
    if (tvShow.getNextEP() != null) {
        env.add("nextepid", tvShow.getNextEP().getID());
        env.add("nextepurl", tvShow.getNextEP().getURL());
        env.add("nextepname", tvShow.getNextEP().getName());
        env.add("nextepseason", tvShow.getNextEP().getSeason());
        env.add("nextepnumber", String.format("%02d", tvShow.getNextEP().getNumber()));
        env.add("nextepairdate", df.withZone(TvMazeConfig.getInstance().getTimezone())
                .print(new DateTime(tvShow.getNextEP().getAirDate())));
        env.add("nextepairtime", tf.withZone(TvMazeConfig.getInstance().getTimezone())
                .print(new DateTime(tvShow.getNextEP().getAirDate())));
        env.add("nextepruntime", tvShow.getNextEP().getRuntime());
        env.add("nextepsummary", StringUtils.abbreviate(tvShow.getNextEP().getSummary(), 250));
        env.add("nextepage", calculateAge(new DateTime(tvShow.getNextEP().getAirDate())));
    }

    return env;
}

From source file:org.drftpd.commands.tvmaze.TvMazeUtils.java

public static ReplacerEnvironment getEPEnv(TvMazeInfo tvShow, TvEpisode tvEP) {
    ReplacerEnvironment env = new ReplacerEnvironment(SiteBot.GLOBAL_ENV);
    DateTimeFormatter df = DateTimeFormat.forPattern(TvMazeConfig.getInstance().getDateFormat());
    DateTimeFormatter tf = DateTimeFormat.forPattern(TvMazeConfig.getInstance().getTimeFormat());

    env.add("id", tvShow.getID());
    env.add("url", tvShow.getURL());
    env.add("name", tvShow.getName());
    env.add("type", tvShow.getType());
    env.add("language", tvShow.getLanguage());
    env.add("genres", StringUtils.join(tvShow.getGenres(), " | "));
    env.add("status", tvShow.getStatus());
    env.add("runtime", tvShow.getRuntime());
    DateTimeFormatter dtf = DateTimeFormat.forPattern("yyyy-MM-dd");
    env.add("premiered", df.withZone(TvMazeConfig.getInstance().getTimezone())
            .print(dtf.parseDateTime(tvShow.getPremiered())));
    env.add("network", tvShow.getNetwork());
    env.add("country", tvShow.getCountry());
    env.add("summary", StringUtils.abbreviate(tvShow.getSummary(), 250));

    env.add("epid", tvEP.getID());
    env.add("epurl", tvEP.getURL());
    env.add("epname", tvEP.getName());
    env.add("epseason", tvEP.getSeason());
    env.add("epnumber", String.format("%02d", tvEP.getNumber()));
    env.add("epairdate",
            df.withZone(TvMazeConfig.getInstance().getTimezone()).print(new DateTime(tvEP.getAirDate())));
    env.add("epairtime",
            tf.withZone(TvMazeConfig.getInstance().getTimezone()).print(new DateTime(tvEP.getAirDate())));
    env.add("epruntime", tvEP.getRuntime());
    env.add("epsummary", StringUtils.abbreviate(tvEP.getSummary(), 250));
    env.add("epage", calculateAge(new DateTime(tvEP.getAirDate())));

    return env;//  w ww. j a v a 2 s .com
}