SearchExplainerController.java 2.76 KB
package com.yoho.search.restapi.tools;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yoho.search.base.utils.HttpServletRequestUtils;
import com.yoho.search.service.searchexplainer.SearchExplainerService;

/**
 * Created by ginozhang on 2016/11/16.
 */
@Controller
public class SearchExplainerController {

    private static final Logger logger = LoggerFactory.getLogger(SearchExplainerController.class);

    @Autowired
    private SearchExplainerService searchExplainerService;

    @RequestMapping(value = "/tools/explain")
    @ResponseBody
    public Map<String, Object> explain(HttpServletRequest request) {
        Map<String, String> paramMap = HttpServletRequestUtils.transParamType(request);
        try {
            return searchExplainerService.explain(paramMap);
        } catch (Throwable t) {
            logger.error(t.getMessage(), t);
            return errorResult(t.getMessage());
        }
    }

    @RequestMapping(value = "/tools/show")
    @ResponseBody
    public Map<String, Object> show(HttpServletRequest request) {
        Map<String, String> paramMap = HttpServletRequestUtils.transParamType(request);
        try {
            return searchExplainerService.show(paramMap);
        } catch (Throwable t) {
            logger.error(t.getMessage(), t);
            return errorResult(t.getMessage());
        }
    }

    @RequestMapping(value = "/tools/clearExplainCachedData")
    @ResponseBody
    public Map<String, Object> clearExplainCachedData() {
        try {
            searchExplainerService.clearCachedData();
            Map<String, Object> map = new HashMap<>();
            map.put("code", "200");
            map.put("message", "succeed");
            return map;
        } catch (Throwable t) {
            logger.error(t.getMessage(), t);
            return errorResult(t.getMessage());
        }
    }

    @RequestMapping(value = "/tools/tokens")
    @ResponseBody
    public Map<String, Object> getTokens(@RequestParam String skn) {
        try {
            return searchExplainerService.getTokens(skn);
        } catch (Throwable t) {
            logger.error(t.getMessage(), t);
            return errorResult(t.getMessage());
        }
    }

    private Map<String, Object> errorResult(String message) {
        Map<String, Object> map = new HashMap<>();
        map.put("code", "400");
        map.put("message", message);
        return map;
    }

}