コード例 #1
0
  public ResultSet query(SearchParameters searchParameters) {
    if (searchParameters.getStores().size() != 1) {
      throw new IllegalStateException("Only one store can be searched at present");
    }

    ArrayList<StoreRef> stores = searchParameters.getStores();
    stores.set(0, tenantService.getName(searchParameters.getStores().get(0)));

    String parameterisedQueryString;
    if (searchParameters.getQueryParameterDefinitions().size() > 0) {
      Map<QName, QueryParameterDefinition> map = new HashMap<QName, QueryParameterDefinition>();

      for (QueryParameterDefinition qpd : searchParameters.getQueryParameterDefinitions()) {
        map.put(qpd.getQName(), qpd);
      }

      parameterisedQueryString =
          parameterise(searchParameters.getQuery(), map, null, namespacePrefixResolver);
    } else {
      parameterisedQueryString = searchParameters.getQuery();
    }
    // TODO: add another property so the set query is not changed ...
    // May be good to return the query as run ??
    searchParameters.setQuery(parameterisedQueryString);

    LuceneQueryLanguageSPI language =
        queryLanguages.get(searchParameters.getLanguage().toLowerCase());
    if (language != null) {
      return language.executeQuery(searchParameters, this);
    } else {
      throw new SearcherException("Unknown query language: " + searchParameters.getLanguage());
    }
  }
コード例 #2
0
  public static CMISQueryOptions create(SearchParameters searchParameters) {
    String sql = searchParameters.getQuery();

    CMISQueryOptions options = new CMISQueryOptions(sql, searchParameters.getStores().get(0));
    options.setIncludeInTransactionData(!searchParameters.excludeDataInTheCurrentTransaction());
    options.setDefaultFTSConnective(
        searchParameters.getDefaultOperator() == SearchParameters.Operator.OR
            ? Connective.OR
            : Connective.AND);
    options.setDefaultFTSFieldConnective(
        searchParameters.getDefaultOperator() == SearchParameters.Operator.OR
            ? Connective.OR
            : Connective.AND);
    options.setSkipCount(searchParameters.getSkipCount());
    options.setMaxPermissionChecks(searchParameters.getMaxPermissionChecks());
    options.setMaxPermissionCheckTimeMillis(searchParameters.getMaxPermissionCheckTimeMillis());
    if (searchParameters.getLimitBy() == LimitBy.FINAL_SIZE) {
      options.setMaxItems(searchParameters.getLimit());
    } else {
      options.setMaxItems(searchParameters.getMaxItems());
    }
    options.setMlAnalaysisMode(searchParameters.getMlAnalaysisMode());
    options.setLocales(searchParameters.getLocales());
    options.setStores(searchParameters.getStores());
    options.setUseInMemorySort(searchParameters.getUseInMemorySort());
    options.setMaxRawResultSetSizeForInMemorySort(
        searchParameters.getMaxRawResultSetSizeForInMemorySort());
    // options.setQuery(); Done on conbstruction
    // options.setQueryMode(); Should set afterwards
    options.setQueryParameterDefinitions(searchParameters.getQueryParameterDefinitions());
    options.setDefaultFieldName(searchParameters.getDefaultFieldName());
    options.setBulkFetchEnabled(searchParameters.isBulkFetchEnabled());
    options.setExcludeTenantFilter(searchParameters.getExcludeTenantFilter());
    options.setSinceTxId(searchParameters.getSinceTxId());
    return options;
  }
コード例 #3
0
  public ResultSet executeQuery(final SearchParameters searchParameters, String language) {
    if (repositoryState.isBootstrapping()) {
      throw new AlfrescoRuntimeException(
          "SOLR queries can not be executed while the repository is bootstrapping");
    }

    try {
      StoreRef store = extractStoreRef(searchParameters);
      SolrStoreMappingWrapper mapping = extractMapping(store);
      Locale locale = extractLocale(searchParameters);

      URLCodec encoder = new URLCodec();
      StringBuilder url = new StringBuilder();

      Pair<HttpClient, String> httpClientAndBaseUrl = mapping.getHttpClientAndBaseUrl();
      HttpClient httpClient = httpClientAndBaseUrl.getFirst();

      url.append(httpClientAndBaseUrl.getSecond());

      String languageUrlFragment = extractLanguageFragment(language);
      url.append("/").append(languageUrlFragment);

      // Send the query in JSON only
      // url.append("?q=");
      // url.append(encoder.encode(searchParameters.getQuery(), "UTF-8"));
      url.append("?wt=").append(encoder.encode("json", "UTF-8"));
      url.append("&fl=").append(encoder.encode("DBID,score", "UTF-8"));

      if ((searchParameters.getStores().size() > 1) || (mapping.isSharded())) {
        boolean requiresSeparator = false;
        url.append("&shards=");
        for (StoreRef storeRef : searchParameters.getStores()) {
          SolrStoreMappingWrapper storeMapping = extractMapping(storeRef);

          if (requiresSeparator) {
            url.append(',');
          } else {
            requiresSeparator = true;
          }

          url.append(storeMapping.getShards());
        }
      }

      // Emulate old limiting behaviour and metadata
      final LimitBy limitBy;
      int maxResults = -1;
      if (searchParameters.getMaxItems() >= 0) {
        maxResults = searchParameters.getMaxItems();
        limitBy = LimitBy.FINAL_SIZE;
      } else if (searchParameters.getLimitBy() == LimitBy.FINAL_SIZE
          && searchParameters.getLimit() >= 0) {
        maxResults = searchParameters.getLimit();
        limitBy = LimitBy.FINAL_SIZE;
      } else {
        maxResults = searchParameters.getMaxPermissionChecks();
        if (maxResults < 0) {
          maxResults = maximumResultsFromUnlimitedQuery;
        }
        limitBy = LimitBy.NUMBER_OF_PERMISSION_EVALUATIONS;
      }
      url.append("&rows=").append(String.valueOf(maxResults));

      url.append("&df=").append(encoder.encode(searchParameters.getDefaultFieldName(), "UTF-8"));
      url.append("&start=").append(encoder.encode("" + searchParameters.getSkipCount(), "UTF-8"));

      url.append("&locale=");
      url.append(encoder.encode(locale.toString(), "UTF-8"));
      url.append("&")
          .append(SearchParameters.ALTERNATIVE_DICTIONARY)
          .append("=")
          .append(alternativeDictionary);
      for (String paramName : searchParameters.getExtraParameters().keySet()) {
        url.append("&")
            .append(paramName)
            .append("=")
            .append(searchParameters.getExtraParameters().get(paramName));
      }
      StringBuffer sortBuffer = buildSortParameters(searchParameters, encoder);
      url.append(sortBuffer);

      if (searchParameters.getPermissionEvaluation() != PermissionEvaluationMode.NONE) {
        url.append("&fq=").append(encoder.encode("{!afts}AUTHORITY_FILTER_FROM_JSON", "UTF-8"));
      }

      if (searchParameters.getExcludeTenantFilter() == false) {
        url.append("&fq=").append(encoder.encode("{!afts}TENANT_FILTER_FROM_JSON", "UTF-8"));
      }

      if (searchParameters.getFieldFacets().size() > 0) {
        url.append("&facet=").append(encoder.encode("true", "UTF-8"));
        for (FieldFacet facet : searchParameters.getFieldFacets()) {
          url.append("&facet.field=").append(encoder.encode(facet.getField(), "UTF-8"));
          if (facet.getEnumMethodCacheMinDF() != 0) {
            url.append("&")
                .append(
                    encoder.encode("f." + facet.getField() + ".facet.enum.cache.minDf", "UTF-8"))
                .append("=")
                .append(encoder.encode("" + facet.getEnumMethodCacheMinDF(), "UTF-8"));
          }
          url.append("&")
              .append(encoder.encode("f." + facet.getField() + ".facet.limit", "UTF-8"))
              .append("=")
              .append(encoder.encode("" + facet.getLimit(), "UTF-8"));
          if (facet.getMethod() != null) {
            url.append("&")
                .append(encoder.encode("f." + facet.getField() + ".facet.method", "UTF-8"))
                .append("=")
                .append(
                    encoder.encode(
                        facet.getMethod() == FieldFacetMethod.ENUM ? "enum" : "fc", "UTF-8"));
          }
          if (facet.getMinCount() != 0) {
            url.append("&")
                .append(encoder.encode("f." + facet.getField() + ".facet.mincount", "UTF-8"))
                .append("=")
                .append(encoder.encode("" + facet.getMinCount(), "UTF-8"));
          }
          if (facet.getOffset() != 0) {
            url.append("&")
                .append(encoder.encode("f." + facet.getField() + ".facet.offset", "UTF-8"))
                .append("=")
                .append(encoder.encode("" + facet.getOffset(), "UTF-8"));
          }
          if (facet.getPrefix() != null) {
            url.append("&")
                .append(encoder.encode("f." + facet.getField() + ".facet.prefix", "UTF-8"))
                .append("=")
                .append(encoder.encode("" + facet.getPrefix(), "UTF-8"));
          }
          if (facet.getSort() != null) {
            url.append("&")
                .append(encoder.encode("f." + facet.getField() + ".facet.sort", "UTF-8"))
                .append("=")
                .append(
                    encoder.encode(
                        facet.getSort() == FieldFacetSort.COUNT ? "count" : "index", "UTF-8"));
          }
        }
        for (String facetQuery : searchParameters.getFacetQueries()) {
          url.append("&facet.query=").append(encoder.encode("{!afts}" + facetQuery, "UTF-8"));
        }
      }
      // end of field facets

      final String searchTerm = searchParameters.getSearchTerm();
      String spellCheckQueryStr = null;
      if (searchTerm != null && searchParameters.isSpellCheck()) {
        StringBuilder builder = new StringBuilder();
        builder.append("&spellcheck.q=").append(encoder.encode(searchTerm, "UTF-8"));
        builder.append("&spellcheck=").append(encoder.encode("true", "UTF-8"));
        spellCheckQueryStr = builder.toString();
        url.append(spellCheckQueryStr);
      }

      JSONObject body = new JSONObject();
      body.put("query", searchParameters.getQuery());

      // Authorities go over as is - and tenant mangling and query building takes place on the SOLR
      // side

      Set<String> allAuthorisations = permissionService.getAuthorisations();
      boolean includeGroups =
          includeGroupsForRoleAdmin
              ? true
              : !allAuthorisations.contains(PermissionService.ADMINISTRATOR_AUTHORITY);

      JSONArray authorities = new JSONArray();
      for (String authority : allAuthorisations) {
        if (includeGroups) {
          authorities.put(authority);
        } else {
          if (AuthorityType.getAuthorityType(authority) != AuthorityType.GROUP) {
            authorities.put(authority);
          }
        }
      }
      body.put("authorities", authorities);
      body.put("anyDenyDenies", anyDenyDenies);

      JSONArray tenants = new JSONArray();
      tenants.put(tenantService.getCurrentUserDomain());
      body.put("tenants", tenants);

      JSONArray locales = new JSONArray();
      for (Locale currentLocale : searchParameters.getLocales()) {
        locales.put(DefaultTypeConverter.INSTANCE.convert(String.class, currentLocale));
      }
      if (locales.length() == 0) {
        locales.put(I18NUtil.getLocale());
      }
      body.put("locales", locales);

      JSONArray templates = new JSONArray();
      for (String templateName : searchParameters.getQueryTemplates().keySet()) {
        JSONObject template = new JSONObject();
        template.put("name", templateName);
        template.put("template", searchParameters.getQueryTemplates().get(templateName));
        templates.put(template);
      }
      body.put("templates", templates);

      JSONArray allAttributes = new JSONArray();
      for (String attribute : searchParameters.getAllAttributes()) {
        allAttributes.put(attribute);
      }
      body.put("allAttributes", allAttributes);

      body.put("defaultFTSOperator", searchParameters.getDefaultFTSOperator());
      body.put("defaultFTSFieldOperator", searchParameters.getDefaultFTSFieldOperator());
      body.put("queryConsistency", searchParameters.getQueryConsistency());
      if (searchParameters.getMlAnalaysisMode() != null) {
        body.put("mlAnalaysisMode", searchParameters.getMlAnalaysisMode().toString());
      }
      body.put("defaultNamespace", searchParameters.getNamespace());

      JSONArray textAttributes = new JSONArray();
      for (String attribute : searchParameters.getTextAttributes()) {
        textAttributes.put(attribute);
      }
      body.put("textAttributes", textAttributes);

      final int maximumResults = maxResults; // just needed for the final parameter

      return (ResultSet)
          postSolrQuery(
              httpClient,
              url.toString(),
              body,
              new SolrJsonProcessor<SolrJSONResultSet>() {

                @Override
                public SolrJSONResultSet getResult(JSONObject json) {
                  return new SolrJSONResultSet(
                      json, searchParameters, nodeService, nodeDAO, limitBy, maximumResults);
                }
              },
              spellCheckQueryStr);
    } catch (UnsupportedEncodingException e) {
      throw new LuceneQueryParserException("", e);
    } catch (HttpException e) {
      throw new LuceneQueryParserException("", e);
    } catch (IOException e) {
      throw new LuceneQueryParserException("", e);
    } catch (JSONException e) {
      throw new LuceneQueryParserException("", e);
    }
  }
コード例 #4
0
  /**
   * Set auditable information and determine if auditing is required before method invocation. This
   * would normally be based on the method arguments.
   *
   * @param auditMode
   * @param auditInfo
   * @param mi
   * @return - the audit mode.
   */
  private AuditMode beforeInvocation(
      AuditMode auditMode, AuditState auditInfo, MethodInvocation mi) {
    AuditMode effectiveAuditMode = auditModel.beforeExecution(auditMode, mi);

    if (auditMode != AuditMode.NONE) {
      String methodName = mi.getMethod().getName();
      String serviceName = publicServiceIdentifier.getPublicServiceName(mi);
      auditInfo.setAuditApplication(SYSTEM_APPLICATION);
      auditInfo.setAuditConfiguration(auditConfiguration);
      auditInfo.setAuditMethod(methodName);
      auditInfo.setAuditService(serviceName);
      auditInfo.setClientAddress(null);
      auditInfo.setDate(new Date());
      auditInfo.setFail(false);
      auditInfo.setFiltered(false);
      auditInfo.setHostAddress(auditHost);

      auditInfo.setPath(null);

      Auditable auditable = mi.getMethod().getAnnotation(Auditable.class);
      Object key = null;
      switch (auditable.key()) {
        case ARG_0:
          checkArgLength(mi, methodName, serviceName, 0);
          key = mi.getArguments()[0];
          break;
        case ARG_1:
          checkArgLength(mi, methodName, serviceName, 1);
          key = mi.getArguments()[1];
          break;
        case ARG_2:
          checkArgLength(mi, methodName, serviceName, 2);
          key = mi.getArguments()[2];
          break;
        case ARG_3:
          checkArgLength(mi, methodName, serviceName, 3);
          key = mi.getArguments()[3];
          break;
        case ARG_4:
          checkArgLength(mi, methodName, serviceName, 4);
          key = mi.getArguments()[4];
          break;
        case ARG_5:
          checkArgLength(mi, methodName, serviceName, 5);
          key = mi.getArguments()[5];
          break;
        case ARG_6:
          checkArgLength(mi, methodName, serviceName, 6);
          key = mi.getArguments()[6];
          break;
        case ARG_7:
          checkArgLength(mi, methodName, serviceName, 7);
          key = mi.getArguments()[7];
          break;
        case ARG_8:
          checkArgLength(mi, methodName, serviceName, 8);
          key = mi.getArguments()[8];
          break;
        case ARG_9:
          checkArgLength(mi, methodName, serviceName, 9);
          key = mi.getArguments()[9];
          break;
        case NO_KEY:
        default:
          break;
      }
      if (key != null) {
        RecordOptions recordOptions = auditModel.getAuditRecordOptions(mi);
        if (key instanceof NodeRef) {
          auditInfo.setKeyStore(((NodeRef) key).getStoreRef());
          auditInfo.setKeyGUID(((NodeRef) key).getId());
          if (recordOptions != null && recordOptions.getRecordPath() == TrueFalseUnset.TRUE) {
            auditInfo.setPath(getNodePath((NodeRef) key));
          }
        } else if (key instanceof StoreRef) {
          auditInfo.setKeyStore((StoreRef) key);
        } else if (key instanceof ChildAssociationRef) {
          ChildAssociationRef car = (ChildAssociationRef) key;
          auditInfo.setKeyStore(car.getParentRef().getStoreRef());
          auditInfo.setKeyGUID(car.getParentRef().getId());
          if (recordOptions != null && recordOptions.getRecordPath() == TrueFalseUnset.TRUE) {
            auditInfo.setPath(getNodePath(car.getParentRef()));
          }
        } else if (key instanceof SearchParameters) {
          SearchParameters sp = (SearchParameters) key;
          if (sp.getStores().size() > 0) {
            auditInfo.setKeyStore(sp.getStores().get(0));
          }
        } else {
          logger.warn(
              "Key argument is not a node, store or child assoc reference or search parameters on "
                  + serviceName
                  + "."
                  + methodName
                  + " it is "
                  + key.getClass().getName());
        }
      }
      auditInfo.setKeyPropertiesAfter(null);
      auditInfo.setKeyPropertiesBefore(null);
      auditInfo.setMessage(null);
      if (mi.getArguments() != null) {
        Serializable[] serArgs = new Serializable[mi.getArguments().length];
        for (int i = 0; i < mi.getArguments().length; i++) {
          if ((auditable.recordable() == null)
              || (auditable.recordable().length <= i)
              || auditable.recordable()[i]) {
            if (mi.getArguments()[i] == null) {
              serArgs[i] = null;
            } else if (mi.getArguments()[i] instanceof Serializable) {
              serArgs[i] = (Serializable) mi.getArguments()[i];
            } else {
              serArgs[i] = mi.getArguments()[i].toString();
            }
          } else {
            serArgs[i] = "********";
          }
        }
        auditInfo.setMethodArguments(serArgs);
      }
      auditInfo.setReturnObject(null);
      auditInfo.setSessionId(null);
      auditInfo.setThrowable(null);
      auditInfo.setTxId(AlfrescoTransactionSupport.getTransactionId());
      auditInfo.setUserIdentifier(AuthenticationUtil.getFullyAuthenticatedUser());
    }

    return effectiveAuditMode;
  }