Search in sources :

Example 11 with URLCodec

use of org.apache.commons.codec.net.URLCodec in project alfresco-repository by Alfresco.

the class SOLRAdminClient method executeCommand.

/* (non-Javadoc)
     * @see org.alfresco.repo.search.impl.solr.SolrAdminClient#executeCommand(java.lang.String, org.alfresco.repo.search.impl.solr.SolrAdminClient.HANDLER, org.alfresco.repo.search.impl.solr.SolrAdminClient.COMMAND, java.util.Map)
     */
@Override
public JSONAPIResult executeCommand(String core, JSONAPIResultFactory.HANDLER handler, JSONAPIResultFactory.COMMAND command, Map<String, String> parameters) {
    StoreRef store = StoreRef.STORE_REF_WORKSPACE_SPACESSTORE;
    SolrStoreMappingWrapper mapping = SolrClientUtil.extractMapping(store, mappingLookup, shardRegistry, useDynamicShardRegistration, beanFactory);
    HttpClient httpClient = mapping.getHttpClientAndBaseUrl().getFirst();
    StringBuilder url = new StringBuilder();
    url.append(baseUrl);
    if (!url.toString().endsWith("/")) {
        url.append("/");
    }
    url.append(core + "/" + handler.toString().toLowerCase());
    URLCodec encoder = new URLCodec();
    url.append("?command=" + command.toString().toLowerCase());
    parameters.forEach((key, value) -> {
        try {
            url.append("&" + key + "=" + encoder.encode(value));
        } catch (EncoderException e) {
            throw new RuntimeException(e);
        }
    });
    url.append("&alfresco.shards=");
    if (mapping.isSharded()) {
        url.append(mapping.getShards());
    } else {
        String solrurl = httpClient.getHostConfiguration().getHostURL() + mapping.getHttpClientAndBaseUrl().getSecond();
        url.append(solrurl);
    }
    try {
        JSONAPIResult response = new SolrCommandBackupResult(getOperation(httpClient, url.toString()));
        if (response.getStatus() != 0) {
            solrTracker.setSolrActive(false);
        }
        return response;
    } catch (IOException e) {
        throw new LuceneQueryParserException("action", e);
    }
}
Also used : URLCodec(org.apache.commons.codec.net.URLCodec) StoreRef(org.alfresco.service.cmr.repository.StoreRef) EncoderException(org.apache.commons.codec.EncoderException) AlfrescoRuntimeException(org.alfresco.error.AlfrescoRuntimeException) SolrCommandBackupResult(org.alfresco.repo.search.impl.lucene.SolrCommandBackupResult) ExplicitSolrStoreMappingWrapper(org.alfresco.repo.search.impl.solr.ExplicitSolrStoreMappingWrapper) SolrStoreMappingWrapper(org.alfresco.repo.search.impl.solr.SolrStoreMappingWrapper) LuceneQueryParserException(org.alfresco.repo.search.impl.lucene.LuceneQueryParserException) HttpClient(org.apache.commons.httpclient.HttpClient) IOException(java.io.IOException) JSONAPIResult(org.alfresco.repo.search.impl.lucene.JSONAPIResult)

Example 12 with URLCodec

use of org.apache.commons.codec.net.URLCodec in project alfresco-repository by Alfresco.

the class SolrQueryHTTPClient method buildPivotParameters.

protected void buildPivotParameters(SearchParameters searchParameters, URLCodec encoder, StringBuilder url) throws UnsupportedEncodingException {
    if (searchParameters.getPivots() != null && !searchParameters.getPivots().isEmpty()) {
        url.append("&facet=").append(encoder.encode("true", "UTF-8"));
        for (List<String> pivotKeys : searchParameters.getPivots()) {
            List<String> pivotsList = new ArrayList<>();
            pivotsList.addAll(pivotKeys);
            url.append("&facet.pivot=");
            StringBuilder prefix = new StringBuilder("{! ");
            if (searchParameters.getStats() != null && !searchParameters.getStats().isEmpty()) {
                for (StatsRequestParameters aStat : searchParameters.getStats()) {
                    if (pivotKeys.contains(aStat.getLabel())) {
                        prefix.append("stats=" + aStat.getLabel() + " ");
                        pivotsList.remove(aStat.getLabel());
                        // only do it once
                        break;
                    }
                }
            }
            if (searchParameters.getRanges() != null && !searchParameters.getRanges().isEmpty()) {
                for (RangeParameters aRange : searchParameters.getRanges()) {
                    Optional<String> found = pivotKeys.stream().filter(aKey -> aKey.equals(aRange.getLabel())).findFirst();
                    if (found.isPresent()) {
                        prefix.append("range=" + found.get() + " ");
                        pivotsList.remove(found.get());
                        // only do it once
                        break;
                    }
                }
            }
            if (// We have add something
            prefix.length() > 3) {
                url.append(encoder.encode(prefix.toString().trim(), "UTF-8"));
                url.append(encoder.encode("}", "UTF-8"));
            }
            url.append(encoder.encode(String.join(",", pivotsList), "UTF-8"));
        }
    }
}
Also used : SolrJsonProcessor(org.alfresco.repo.search.impl.lucene.SolrJsonProcessor) RangeParameters(org.alfresco.service.cmr.search.RangeParameters) URIException(org.apache.commons.httpclient.URIException) StringUtils(org.apache.commons.lang3.StringUtils) Header(org.apache.commons.httpclient.Header) DefaultTypeConverter(org.alfresco.service.cmr.repository.datatype.DefaultTypeConverter) JSONResult(org.alfresco.repo.search.impl.lucene.JSONResult) JSONException(org.json.JSONException) JSONObject(org.json.JSONObject) PermissionService(org.alfresco.service.cmr.security.PermissionService) RepositoryState(org.alfresco.repo.admin.RepositoryState) NodeDAO(org.alfresco.repo.domain.node.NodeDAO) Locale(java.util.Locale) Map(java.util.Map) NodeService(org.alfresco.service.cmr.repository.NodeService) HttpStatus(org.apache.commons.httpclient.HttpStatus) ResultSet(org.alfresco.service.cmr.search.ResultSet) StatsParameters(org.alfresco.service.cmr.search.StatsParameters) QueryParserUtils(org.alfresco.repo.search.impl.QueryParserUtils) Set(java.util.Set) ShardRegistry(org.alfresco.repo.index.shard.ShardRegistry) Reader(java.io.Reader) CMISStrictDictionaryService(org.alfresco.opencmis.dictionary.CMISStrictDictionaryService) GetMethod(org.apache.commons.httpclient.methods.GetMethod) List(java.util.List) IntervalSet(org.alfresco.service.cmr.search.IntervalSet) I18NUtil(org.springframework.extensions.surf.util.I18NUtil) HttpClient(org.apache.commons.httpclient.HttpClient) Entry(java.util.Map.Entry) Optional(java.util.Optional) LogFactory(org.apache.commons.logging.LogFactory) URI(org.apache.commons.httpclient.URI) UnsupportedEncodingException(java.io.UnsupportedEncodingException) LimitBy(org.alfresco.service.cmr.search.LimitBy) LuceneQueryParserException(org.alfresco.repo.search.impl.lucene.LuceneQueryParserException) Interval(org.alfresco.service.cmr.search.Interval) FieldFacet(org.alfresco.service.cmr.search.SearchParameters.FieldFacet) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) FieldFacetSort(org.alfresco.service.cmr.search.SearchParameters.FieldFacetSort) NamespaceDAO(org.alfresco.repo.dictionary.NamespaceDAO) ParameterCheck(org.alfresco.util.ParameterCheck) StatsRequestParameters(org.alfresco.service.cmr.search.StatsRequestParameters) HttpException(org.apache.commons.httpclient.HttpException) AuthorityType(org.alfresco.service.cmr.security.AuthorityType) StoreRef(org.alfresco.service.cmr.repository.StoreRef) Iterator(java.util.Iterator) FieldFacetMethod(org.alfresco.service.cmr.search.SearchParameters.FieldFacetMethod) SearchDateConversion.parseDateInterval(org.alfresco.util.SearchDateConversion.parseDateInterval) FieldHighlightParameters(org.alfresco.service.cmr.search.FieldHighlightParameters) HttpServletResponse(javax.servlet.http.HttpServletResponse) JSONTokener(org.json.JSONTokener) Pair(org.alfresco.util.Pair) IOException(java.io.IOException) AlfrescoRuntimeException(org.alfresco.error.AlfrescoRuntimeException) BeansException(org.springframework.beans.BeansException) InputStreamReader(java.io.InputStreamReader) TenantService(org.alfresco.repo.tenant.TenantService) BasicSearchParameters(org.alfresco.service.cmr.search.BasicSearchParameters) PermissionEvaluationMode(org.alfresco.service.cmr.search.PermissionEvaluationMode) DictionaryService(org.alfresco.service.cmr.dictionary.DictionaryService) SortDefinition(org.alfresco.service.cmr.search.SearchParameters.SortDefinition) DataTypeDefinition(org.alfresco.service.cmr.dictionary.DataTypeDefinition) SolrStatsResult(org.alfresco.repo.search.impl.lucene.SolrStatsResult) URLCodec(org.apache.commons.codec.net.URLCodec) StringJoiner(java.util.StringJoiner) BeanFactory(org.springframework.beans.factory.BeanFactory) PropertyCheck(org.alfresco.util.PropertyCheck) Log(org.apache.commons.logging.Log) BufferedReader(java.io.BufferedReader) Floc(org.alfresco.repo.index.shard.Floc) SearchParameters(org.alfresco.service.cmr.search.SearchParameters) PropertyDefinition(org.alfresco.service.cmr.dictionary.PropertyDefinition) SolrJSONResultSet(org.alfresco.repo.search.impl.lucene.SolrJSONResultSet) JSONArray(org.json.JSONArray) ArrayList(java.util.ArrayList) StatsRequestParameters(org.alfresco.service.cmr.search.StatsRequestParameters) RangeParameters(org.alfresco.service.cmr.search.RangeParameters)

Example 13 with URLCodec

use of org.apache.commons.codec.net.URLCodec in project alfresco-repository by Alfresco.

the class SolrQueryHTTPClient method buildStatsUrl.

protected String buildStatsUrl(StatsParameters searchParameters, String baseUrl, Locale locale, SolrStoreMappingWrapper mapping) throws UnsupportedEncodingException {
    URLCodec encoder = new URLCodec();
    StringBuilder url = new StringBuilder();
    String languageUrlFragment = SolrClientUtil.extractLanguageFragment(languageMappings, searchParameters.getLanguage());
    url.append(baseUrl);
    url.append("/").append(languageUrlFragment);
    url.append("?wt=").append(encoder.encode("json", "UTF-8"));
    url.append("&locale=").append(encoder.encode(locale.toString(), "UTF-8"));
    url.append(buildSortParameters(searchParameters, encoder));
    url.append("&stats=true");
    url.append("&rows=0");
    if (!StringUtils.isBlank(searchParameters.getFilterQuery())) {
        url.append("?fq=").append(encoder.encode(searchParameters.getFilterQuery(), "UTF-8"));
    }
    for (Entry<String, String> entry : searchParameters.getStatsParameters().entrySet()) {
        url.append("&stats.").append(entry.getKey()).append("=").append(encoder.encode(entry.getValue(), "UTF-8"));
    }
    if ((mapping != null) && ((searchParameters.getStores().size() > 1) || (mapping.isSharded()))) {
        url.append("&shards=");
        buildShards(url, searchParameters.getStores());
    }
    return url.toString();
}
Also used : URLCodec(org.apache.commons.codec.net.URLCodec)

Example 14 with URLCodec

use of org.apache.commons.codec.net.URLCodec in project alfresco-repository by Alfresco.

the class ExplicitSolrStoreMappingWrapper method getShards2.

private String getShards2() {
    try {
        URLCodec encoder = new URLCodec();
        StringBuilder builder = new StringBuilder();
        for (int shard = 0; shard < wrapped.getNumShards(); shard++) {
            int position = random.nextInt(wrapped.getReplicationFactor());
            List<Integer> nodeInstances = policy.getNodeInstancesForShardId(shard);
            Integer nodeId = nodeInstances.get(position);
            if (builder.length() > 0) {
                builder.append(',');
            }
            HttpClientAndBaseUrl httpClientAndBaseUrl = httpClientsAndBaseURLs.toArray(new HttpClientAndBaseUrl[0])[nodeId - 1];
            builder.append(encoder.encode(httpClientAndBaseUrl.getProtocol() + "://", "UTF-8"));
            builder.append(encoder.encode(httpClientAndBaseUrl.getHost(), "UTF-8"));
            builder.append(':');
            builder.append(encoder.encode("" + httpClientAndBaseUrl.getPort(), "UTF-8"));
            if (httpClientAndBaseUrl.getBaseUrl().startsWith("/")) {
                builder.append(encoder.encode(httpClientAndBaseUrl.getBaseUrl(), "UTF-8"));
            } else {
                builder.append(encoder.encode("/" + httpClientAndBaseUrl.getBaseUrl(), "UTF-8"));
            }
            if (isSharded())
                builder.append('-').append(shard);
        }
        return builder.toString();
    } catch (UnsupportedEncodingException e) {
        throw new LuceneQueryParserException("", e);
    }
}
Also used : URLCodec(org.apache.commons.codec.net.URLCodec) LuceneQueryParserException(org.alfresco.repo.search.impl.lucene.LuceneQueryParserException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Aggregations

URLCodec (org.apache.commons.codec.net.URLCodec)14 IOException (java.io.IOException)9 LuceneQueryParserException (org.alfresco.repo.search.impl.lucene.LuceneQueryParserException)8 HttpClient (org.apache.commons.httpclient.HttpClient)6 UnsupportedEncodingException (java.io.UnsupportedEncodingException)5 AlfrescoRuntimeException (org.alfresco.error.AlfrescoRuntimeException)5 StoreRef (org.alfresco.service.cmr.repository.StoreRef)5 EncoderException (org.apache.commons.codec.EncoderException)5 BufferedReader (java.io.BufferedReader)4 InputStreamReader (java.io.InputStreamReader)4 Reader (java.io.Reader)4 JSONException (org.json.JSONException)4 JSONObject (org.json.JSONObject)4 ArrayList (java.util.ArrayList)3 Locale (java.util.Locale)3 Map (java.util.Map)3 Header (org.apache.commons.httpclient.Header)3 HttpException (org.apache.commons.httpclient.HttpException)3 URI (org.apache.commons.httpclient.URI)3 GetMethod (org.apache.commons.httpclient.methods.GetMethod)3