Java tutorial
/* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package cn.hbu.cs.esearch.controller; import org.apache.commons.lang.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import com.google.common.base.Strings; import cn.hbu.cs.esearch.core.EsearchException; import cn.hbu.cs.esearch.model.SearchRequest; import cn.hbu.cs.esearch.model.SearchResult; import cn.hbu.cs.esearch.service.SearchService; @Controller @RequestMapping("/") public class EsearchController { private static final Logger LOGGER = LoggerFactory.getLogger(EsearchController.class); @Autowired private SearchService searchService; @RequestMapping(value = { "index", "/" }, method = { RequestMethod.GET, RequestMethod.HEAD }) public String indexPage(ModelMap modelMap) { modelMap.addAttribute("isSearchRequest", false); return "index"; } @RequestMapping(value = "how", method = { RequestMethod.GET, RequestMethod.HEAD }) public String howPage(ModelMap modelMap) { modelMap.addAttribute("isSearchRequest", false); return "how"; } @RequestMapping(value = "api", method = { RequestMethod.GET, RequestMethod.HEAD }) public String apiPage(ModelMap modelMap) { modelMap.addAttribute("isSearchRequest", false); return "api"; } @RequestMapping(value = "admin", method = { RequestMethod.GET, RequestMethod.HEAD }) public String adminPage(ModelMap modelMap) { modelMap.addAttribute("isSearchRequest", false); return "admin"; } @RequestMapping("_search") public String search(@RequestParam String query, ModelMap modelMap) { modelMap.addAttribute("isSearchRequest", true); if (StringUtils.isEmpty(query)) { LOGGER.info("Query string is empty."); return "index"; } if (Strings.isNullOrEmpty(query)) { return "index"; } SearchRequest searchRequest = new SearchRequest(query); LOGGER.info("Query :{}", query); SearchResult searchResult = null; try { searchResult = searchService.search(searchRequest); } catch (EsearchException e) { LOGGER.error("Query failure, the error is: \n {}", e); } if (searchResult != null) { modelMap.addAttribute("totalTime", searchResult.getTime()); modelMap.addAttribute("totalHits", searchResult.getTotalHits()); modelMap.addAttribute("totalDocs", searchResult.getTotalDocs()); modelMap.addAttribute("searchHits", searchResult.getHits()); } else { modelMap.addAttribute("totalTime", 0); modelMap.addAttribute("totalHits", 0); } modelMap.addAttribute("query", query); return "index"; } }