Ejemplo n.º 1
0
  public boolean isAllow(RestRequest request) {
    RestRequest.Method method = request.method();
    if (log.isTraceEnabled()) {
      log.trace(String.format("Checking rules for %s request [%s]...", method, request.path()));
    }

    Set<String> allowRules = allowRulesByMethod.get(request.method());
    String path = request.path();
    if (allowRules != null) {
      for (String allowRule : allowRules) {
        if (path.startsWith(allowRule)) {
          if (log.isTraceEnabled()) {
            log.trace(
                String.format(
                    "Find matching rule [%s] for %s request [%s]: allow", allowRule, method, path));
          }
          return true;
        }
      }
    }

    log.trace(String.format("No matching rules for %s request [%s]: reject", method, path));
    return false;
  }
Ejemplo n.º 2
0
  public static SearchRequest parseSearchRequest(RestRequest request) {
    String[] indices = Strings.splitStringByCommaToArray(request.param("index"));
    SearchRequest searchRequest = new SearchRequest(indices);
    // get the content, and put it in the body
    // add content/source as template if template flag is set
    boolean isTemplateRequest = request.path().endsWith("/template");
    if (RestActions.hasBodyContent(request)) {
      if (isTemplateRequest) {
        searchRequest.templateSource(RestActions.getRestContent(request));
      } else {
        searchRequest.source(RestActions.getRestContent(request));
      }
    }

    // do not allow 'query_and_fetch' or 'dfs_query_and_fetch' search types
    // from the REST layer. these modes are an internal optimization and should
    // not be specified explicitly by the user.
    String searchType = request.param("search_type");
    if (SearchType.fromString(searchType).equals(SearchType.QUERY_AND_FETCH)
        || SearchType.fromString(searchType).equals(SearchType.DFS_QUERY_AND_FETCH)) {
      throw new IllegalArgumentException("Unsupported search type [" + searchType + "]");
    } else {
      searchRequest.searchType(searchType);
    }

    searchRequest.extraSource(parseSearchSource(request));
    searchRequest.queryCache(request.paramAsBoolean("query_cache", null));

    String scroll = request.param("scroll");
    if (scroll != null) {
      searchRequest.scroll(new Scroll(parseTimeValue(scroll, null, "scroll")));
    }

    searchRequest.types(Strings.splitStringByCommaToArray(request.param("type")));
    searchRequest.routing(request.param("routing"));
    searchRequest.preference(request.param("preference"));
    searchRequest.indicesOptions(
        IndicesOptions.fromRequest(request, searchRequest.indicesOptions()));

    return searchRequest;
  }