Search in sources :

Example 1 with PopularWordHelper

use of org.codelibs.fess.helper.PopularWordHelper in project fess by codelibs.

the class JsonApiManager method processPopularWordRequest.

protected void processPopularWordRequest(final HttpServletRequest request, final HttpServletResponse response, final FilterChain chain) {
    if (!ComponentUtil.getFessConfig().isWebApiPopularWord()) {
        writeJsonResponse(9, null, "Unsupported operation.");
        return;
    }
    final String seed = request.getParameter("seed");
    final List<String> tagList = new ArrayList<>();
    final String[] tags = request.getParameterValues("labels");
    if (tags != null) {
        tagList.addAll(Arrays.asList(tags));
    }
    final String key = ComponentUtil.getVirtualHostHelper().getVirtualHostKey();
    if (StringUtil.isNotBlank(key)) {
        tagList.add(key);
    }
    final String[] fields = request.getParameterValues("fields");
    // TODO
    final String[] excludes = StringUtil.EMPTY_STRINGS;
    final PopularWordHelper popularWordHelper = ComponentUtil.getPopularWordHelper();
    int status = 0;
    Exception err = null;
    // TODO replace response stream
    final StringBuilder buf = new StringBuilder(255);
    try {
        final List<String> popularWordList = popularWordHelper.getWordList(SearchRequestType.JSON, seed, tagList.toArray(new String[tagList.size()]), null, fields, excludes);
        buf.append("\"result\":[");
        boolean first1 = true;
        for (final String word : popularWordList) {
            if (!first1) {
                buf.append(',');
            } else {
                first1 = false;
            }
            buf.append(escapeJson(word));
        }
        buf.append(']');
    } catch (final Exception e) {
        if (e instanceof WebApiException) {
            status = ((WebApiException) e).getStatusCode();
        } else {
            status = 1;
        }
        err = e;
        if (logger.isDebugEnabled()) {
            logger.debug("Failed to process a popularWord request.", e);
        }
    }
    writeJsonResponse(status, buf.toString(), err);
}
Also used : PopularWordHelper(org.codelibs.fess.helper.PopularWordHelper) ArrayList(java.util.ArrayList) WebApiException(org.codelibs.fess.exception.WebApiException) IORuntimeException(org.codelibs.core.exception.IORuntimeException) ServletException(javax.servlet.ServletException) WebApiException(org.codelibs.fess.exception.WebApiException) IOException(java.io.IOException)

Aggregations

IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 ServletException (javax.servlet.ServletException)1 IORuntimeException (org.codelibs.core.exception.IORuntimeException)1 WebApiException (org.codelibs.fess.exception.WebApiException)1 PopularWordHelper (org.codelibs.fess.helper.PopularWordHelper)1