Search in sources :

Example 36 with Request

use of spark.Request in project searchcode-server by boyter.

the class CodeRouteServiceTest method testHtmlQueryString.

public void testHtmlQueryString() {
    CodeRouteService codeRouteService = new CodeRouteService();
    Request request = Mockito.mock(Request.class);
    Set<String> hashSet = new HashSet<>();
    hashSet.add("q");
    when(request.queryParams()).thenReturn(hashSet);
    when(request.queryParams("q")).thenReturn("test");
    ModelAndView modelAndView = codeRouteService.html(request, null);
    Map<String, Object> model = (Map<String, Object>) modelAndView.getModel();
    String viewName = modelAndView.getViewName();
    assertThat(model.get("searchValue")).isEqualTo("test");
    assertThat(model.get("searchResult")).isNotNull();
    assertThat(model.get("reposQueryString")).isNotNull();
    assertThat(model.get("langsQueryString")).isNotNull();
    assertThat(model.get("ownsQueryString")).isNotNull();
    assertThat(model.get("altQuery")).isNotNull();
    assertThat((int) model.get("totalPages")).isGreaterThanOrEqualTo(0);
    assertThat((boolean) model.get("isHtml")).isTrue();
    assertThat(model.get("logoImage")).isNotNull();
    assertThat(model.get("isCommunity")).isEqualTo(App.ISCOMMUNITY);
    assertThat(viewName).isEqualTo("searchresults.ftl");
}
Also used : CodeRouteService(com.searchcode.app.service.route.CodeRouteService) Request(spark.Request) ModelAndView(spark.ModelAndView) Map(java.util.Map) HashSet(java.util.HashSet)

Example 37 with Request

use of spark.Request in project searchcode-server by boyter.

the class CodeRouteServiceTest method testRootQueryString.

public void testRootQueryString() {
    CodeRouteService codeRouteService = new CodeRouteService();
    Request request = Mockito.mock(Request.class);
    Set<String> hashSet = new HashSet<>();
    hashSet.add("q");
    when(request.queryParams()).thenReturn(hashSet);
    when(request.queryParams("q")).thenReturn("test");
    ModelAndView modelAndView = codeRouteService.root(request, null);
    Map<String, Object> model = (Map<String, Object>) modelAndView.getModel();
    String viewName = modelAndView.getViewName();
    assertThat(model.get("searchValue")).isEqualTo("test");
    assertThat(model.get("searchResultJson")).isNotNull();
    assertThat(model.get("logoImage")).isNotNull();
    assertThat(model.get("isCommunity")).isEqualTo(App.ISCOMMUNITY);
    assertThat(viewName).isEqualTo("search_ajax.ftl");
}
Also used : CodeRouteService(com.searchcode.app.service.route.CodeRouteService) Request(spark.Request) ModelAndView(spark.ModelAndView) Map(java.util.Map) HashSet(java.util.HashSet)

Example 38 with Request

use of spark.Request in project searchcode-server by boyter.

the class CodeRouteService method html.

public ModelAndView html(Request request, Response response) {
    Repo repo = Singleton.getRepo();
    Data data = Singleton.getData();
    SearchcodeLib scl = Singleton.getSearchcodeLib(data);
    CodeSearcher cs = new CodeSearcher();
    CodeMatcher cm = new CodeMatcher(data);
    Map<String, Object> map = new HashMap<>();
    map.put("repoCount", repo.getRepoCount());
    if (request.queryParams().contains("q")) {
        String query = request.queryParams("q").trim();
        String altquery = query.replaceAll("[^A-Za-z0-9 ]", " ").trim().replaceAll(" +", " ");
        int page = 0;
        if (request.queryParams().contains("p")) {
            try {
                page = Integer.parseInt(request.queryParams("p"));
                page = page > 19 ? 19 : page;
            } catch (NumberFormatException ex) {
                page = 0;
            }
        }
        String[] repos = new String[0];
        String[] langs = new String[0];
        String[] owners = new String[0];
        String reposFilter = Values.EMPTYSTRING;
        String langsFilter = Values.EMPTYSTRING;
        String ownersFilter = Values.EMPTYSTRING;
        String reposQueryString = Values.EMPTYSTRING;
        String langsQueryString = Values.EMPTYSTRING;
        String ownsQueryString = Values.EMPTYSTRING;
        if (request.queryParams().contains("repo")) {
            repos = request.queryParamsValues("repo");
            if (repos.length != 0) {
                List<String> reposList = Arrays.asList(repos).stream().map((s) -> "reponame:" + QueryParser.escape(s)).collect(Collectors.toList());
                reposFilter = " && (" + StringUtils.join(reposList, " || ") + ")";
                List<String> reposQueryList = Arrays.asList(repos).stream().map((s) -> "&repo=" + URLEncoder.encode(s)).collect(Collectors.toList());
                reposQueryString = StringUtils.join(reposQueryList, "");
            }
        }
        if (request.queryParams().contains("lan")) {
            langs = request.queryParamsValues("lan");
            if (langs.length != 0) {
                List<String> langsList = Arrays.asList(langs).stream().map((s) -> "languagename:" + QueryParser.escape(s)).collect(Collectors.toList());
                langsFilter = " && (" + StringUtils.join(langsList, " || ") + ")";
                List<String> langsQueryList = Arrays.asList(langs).stream().map((s) -> "&lan=" + URLEncoder.encode(s)).collect(Collectors.toList());
                langsQueryString = StringUtils.join(langsQueryList, "");
            }
        }
        if (request.queryParams().contains("own")) {
            owners = request.queryParamsValues("own");
            if (owners.length != 0) {
                List<String> ownersList = Arrays.asList(owners).stream().map((s) -> "codeowner:" + QueryParser.escape(s)).collect(Collectors.toList());
                ownersFilter = " && (" + StringUtils.join(ownersList, " || ") + ")";
                List<String> ownsQueryList = Arrays.asList(owners).stream().map((s) -> "&own=" + URLEncoder.encode(s)).collect(Collectors.toList());
                ownsQueryString = StringUtils.join(ownsQueryList, "");
            }
        }
        // split the query escape it and and it together
        String cleanQueryString = scl.formatQueryString(query);
        SearchResult searchResult = cs.search(cleanQueryString + reposFilter + langsFilter + ownersFilter, page);
        searchResult.setCodeResultList(cm.formatResults(searchResult.getCodeResultList(), query, true));
        for (CodeFacetRepo f : searchResult.getRepoFacetResults()) {
            if (Arrays.asList(repos).contains(f.getRepoName())) {
                f.setSelected(true);
            }
        }
        for (CodeFacetLanguage f : searchResult.getLanguageFacetResults()) {
            if (Arrays.asList(langs).contains(f.getLanguageName())) {
                f.setSelected(true);
            }
        }
        for (CodeFacetOwner f : searchResult.getOwnerFacetResults()) {
            if (Arrays.asList(owners).contains(f.getOwner())) {
                f.setSelected(true);
            }
        }
        map.put("searchValue", query);
        map.put("searchResult", searchResult);
        map.put("reposQueryString", reposQueryString);
        map.put("langsQueryString", langsQueryString);
        map.put("ownsQueryString", ownsQueryString);
        map.put("altQuery", altquery);
        map.put("totalPages", searchResult.getPages().size());
        map.put("isHtml", true);
        map.put("logoImage", CommonRouteService.getLogo());
        map.put("isCommunity", App.ISCOMMUNITY);
        map.put(Values.EMBED, Singleton.getData().getDataByName(Values.EMBED, Values.EMPTYSTRING));
        return new ModelAndView(map, "searchresults.ftl");
    }
    map.put("photoId", CommonRouteService.getPhotoId());
    map.put("numDocs", cs.getTotalNumberDocumentsIndexed());
    map.put("logoImage", CommonRouteService.getLogo());
    map.put("isCommunity", App.ISCOMMUNITY);
    map.put(Values.EMBED, Singleton.getData().getDataByName(Values.EMBED, Values.EMPTYSTRING));
    return new ModelAndView(map, "index.ftl");
}
Also used : java.util(java.util) Singleton(com.searchcode.app.service.Singleton) Spark.halt(spark.Spark.halt) RepoResult(com.searchcode.app.model.RepoResult) ModelAndView(spark.ModelAndView) Repo(com.searchcode.app.dao.Repo) Collectors(java.util.stream.Collectors) StringUtils(org.apache.commons.lang3.StringUtils) Values(com.searchcode.app.config.Values) CodeMatcher(com.searchcode.app.service.CodeMatcher) com.searchcode.app.util(com.searchcode.app.util) CommonRouteService(com.searchcode.app.service.route.CommonRouteService) URLEncoder(java.net.URLEncoder) com.searchcode.app.dto(com.searchcode.app.dto) CodeSearcher(com.searchcode.app.service.CodeSearcher) QueryParser(org.apache.lucene.queryparser.classic.QueryParser) Gson(com.google.gson.Gson) Request(spark.Request) Data(com.searchcode.app.dao.Data) Response(spark.Response) Properties(com.searchcode.app.util.Properties) StringEscapeUtils(org.apache.commons.lang3.StringEscapeUtils) App(com.searchcode.app.App) ModelAndView(spark.ModelAndView) Data(com.searchcode.app.dao.Data) CodeSearcher(com.searchcode.app.service.CodeSearcher) Repo(com.searchcode.app.dao.Repo) CodeMatcher(com.searchcode.app.service.CodeMatcher)

Example 39 with Request

use of spark.Request in project searchcode-server by boyter.

the class SearchRouteService method codeSearch.

public SearchResult codeSearch(Request request, Response response) {
    CodeSearcher cs = new CodeSearcher();
    CodeMatcher cm = new CodeMatcher(Singleton.getData());
    SearchcodeLib scl = Singleton.getSearchcodeLib(Singleton.getData());
    if (request.queryParams().contains("q") && !request.queryParams("q").trim().equals(Values.EMPTYSTRING)) {
        String query = request.queryParams("q").trim();
        int page = 0;
        if (request.queryParams().contains("p")) {
            try {
                page = Integer.parseInt(request.queryParams("p"));
                page = page > 19 ? 19 : page;
            } catch (NumberFormatException ex) {
                page = 0;
            }
        }
        String[] repos;
        String[] langs;
        String[] owners;
        String reposFilter = Values.EMPTYSTRING;
        String langsFilter = Values.EMPTYSTRING;
        String ownersFilter = Values.EMPTYSTRING;
        if (request.queryParams().contains("repo")) {
            repos = request.queryParamsValues("repo");
            if (repos.length != 0) {
                List<String> reposList = Arrays.asList(repos).stream().map((s) -> "reponame:" + QueryParser.escape(s.replace(" ", "_"))).collect(Collectors.toList());
                reposFilter = " && (" + StringUtils.join(reposList, " || ") + ")";
            }
        }
        if (request.queryParams().contains("lan")) {
            langs = request.queryParamsValues("lan");
            if (langs.length != 0) {
                List<String> langsList = Arrays.asList(langs).stream().map((s) -> "languagename:" + QueryParser.escape(s.replace(" ", "_"))).collect(Collectors.toList());
                langsFilter = " && (" + StringUtils.join(langsList, " || ") + ")";
            }
        }
        if (request.queryParams().contains("own")) {
            owners = request.queryParamsValues("own");
            if (owners.length != 0) {
                List<String> ownersList = Arrays.asList(owners).stream().map((s) -> "codeowner:" + QueryParser.escape(s.replace(" ", "_"))).collect(Collectors.toList());
                ownersFilter = " && (" + StringUtils.join(ownersList, " || ") + ")";
            }
        }
        // split the query escape it and and it together
        String cleanQueryString = scl.formatQueryString(query);
        SearchResult searchResult = cs.search(cleanQueryString + reposFilter + langsFilter + ownersFilter, page);
        searchResult.setCodeResultList(cm.formatResults(searchResult.getCodeResultList(), query, true));
        searchResult.setQuery(query);
        for (String altQuery : scl.generateAltQueries(query)) {
            searchResult.addAltQuery(altQuery);
        }
        // Null out code as it isnt required and there is no point in bloating our ajax requests
        for (CodeResult codeSearchResult : searchResult.getCodeResultList()) {
            codeSearchResult.setCode(null);
        }
        return searchResult;
    }
    return null;
}
Also used : Arrays(java.util.Arrays) SearchResult(com.searchcode.app.dto.SearchResult) Singleton(com.searchcode.app.service.Singleton) StringUtils(org.apache.commons.lang3.StringUtils) Collectors(java.util.stream.Collectors) Values(com.searchcode.app.config.Values) CodeMatcher(com.searchcode.app.service.CodeMatcher) SearchcodeLib(com.searchcode.app.util.SearchcodeLib) List(java.util.List) CodeResult(com.searchcode.app.dto.CodeResult) CodeSearcher(com.searchcode.app.service.CodeSearcher) QueryParser(org.apache.lucene.queryparser.classic.QueryParser) Request(spark.Request) Response(spark.Response) SearchcodeLib(com.searchcode.app.util.SearchcodeLib) CodeResult(com.searchcode.app.dto.CodeResult) CodeMatcher(com.searchcode.app.service.CodeMatcher) SearchResult(com.searchcode.app.dto.SearchResult) CodeSearcher(com.searchcode.app.service.CodeSearcher)

Example 40 with Request

use of spark.Request in project searchcode-server by boyter.

the class AdminRouteServiceTest method testGetStatValuesExpectEmpty.

public void testGetStatValuesExpectEmpty() {
    AdminRouteService adminRouteService = new AdminRouteService();
    Singleton.getLogger().clearAllLogs();
    List<String> statValue = Arrays.asList(null, "", "runningjobs", "alllogs", "infologs", "warninglogs", "severelogs", "searchlogs");
    for (String stat : statValue) {
        Request mockRequest = Mockito.mock(Request.class);
        Set<String> returnSet = new HashSet<>();
        returnSet.add("statname");
        when(mockRequest.queryParams()).thenReturn(returnSet);
        when(mockRequest.queryParams("statname")).thenReturn(stat);
        String result = adminRouteService.getStat(mockRequest, null);
        assertThat(result).as("For value %s", stat).isEmpty();
    }
}
Also used : AdminRouteService(com.searchcode.app.service.route.AdminRouteService) Request(spark.Request) HashSet(java.util.HashSet)

Aggregations

Request (spark.Request)58 ApiRouteService (com.searchcode.app.service.route.ApiRouteService)34 RepoResultApiResponse (com.searchcode.app.dto.api.RepoResultApiResponse)32 Repo (com.searchcode.app.dao.Repo)28 ApiResponse (com.searchcode.app.dto.api.ApiResponse)27 RepoResult (com.searchcode.app.model.RepoResult)10 Response (spark.Response)9 CodeRouteService (com.searchcode.app.service.route.CodeRouteService)8 AdminRouteService (com.searchcode.app.service.route.AdminRouteService)7 HashSet (java.util.HashSet)6 Map (java.util.Map)6 ModelAndView (spark.ModelAndView)5 RouteMatch (spark.routematch.RouteMatch)4 ValidatorResult (com.searchcode.app.model.ValidatorResult)3 FilterImpl (spark.FilterImpl)3 HaltException (spark.HaltException)3 Values (com.searchcode.app.config.Values)2 Data (com.searchcode.app.dao.Data)2 CodeResult (com.searchcode.app.dto.CodeResult)2 CodeMatcher (com.searchcode.app.service.CodeMatcher)2