Search in sources :

Example 1 with URLCodec

use of org.apache.commons.codec.net.URLCodec in project incubator-gobblin by apache.

the class AzkabanAjaxAPIClient method changeProjectDescription.

private static void changeProjectDescription(String sessionId, String azkabanServerUrl, String azkabanProjectName, String projectDescription) throws IOException {
    String encodedProjectDescription;
    try {
        encodedProjectDescription = new URLCodec().encode(projectDescription);
    } catch (EncoderException e) {
        throw new IOException("Could not encode Azkaban project description", e);
    }
    Map<String, String> params = Maps.newHashMap();
    params.put("ajax", "changeDescription");
    params.put("project", azkabanProjectName);
    params.put("description", encodedProjectDescription);
    executeGetRequest(prepareGetRequest(azkabanServerUrl + "/manager", sessionId, params));
}
Also used : URLCodec(org.apache.commons.codec.net.URLCodec) EncoderException(org.apache.commons.codec.EncoderException) IOException(java.io.IOException)

Example 2 with URLCodec

use of org.apache.commons.codec.net.URLCodec in project build-info by JFrogDev.

the class ArtifactoryHttpClient method encodePath.

public static String encodePath(String unescaped) {
    URLCodec codec = new URLCodec();
    String[] split = StringUtils.split(unescaped, "/");
    for (int i = 0; i < split.length; i++) {
        split[i] = newStringUsAscii(codec.encode(getBytesUtf8(split[i])));
        // codec.encode replaces spaces with '+', but we want to escape them to %20.
        split[i] = split[i].replaceAll("\\+", "%20");
    }
    return StringUtils.join(split, "/");
}
Also used : URLCodec(org.apache.commons.codec.net.URLCodec)

Example 3 with URLCodec

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

the class SolrAdminHTTPClient method execute.

public JSONObject execute(String relativeHandlerPath, HashMap<String, String> args) {
    ParameterCheck.mandatory("relativeHandlerPath", relativeHandlerPath);
    ParameterCheck.mandatory("args", args);
    String path = getPath(relativeHandlerPath);
    try {
        URLCodec encoder = new URLCodec();
        StringBuilder url = new StringBuilder();
        for (String key : args.keySet()) {
            String value = args.get(key);
            if (url.length() == 0) {
                url.append(path);
                url.append("?");
                url.append(encoder.encode(key, "UTF-8"));
                url.append("=");
                url.append(encoder.encode(value, "UTF-8"));
            } else {
                url.append("&");
                url.append(encoder.encode(key, "UTF-8"));
                url.append("=");
                url.append(encoder.encode(value, "UTF-8"));
            }
        }
        // PostMethod post = new PostMethod(url.toString());
        GetMethod get = new GetMethod(url.toString());
        try {
            httpClient.executeMethod(get);
            if (get.getStatusCode() == HttpStatus.SC_MOVED_PERMANENTLY || get.getStatusCode() == HttpStatus.SC_MOVED_TEMPORARILY) {
                Header locationHeader = get.getResponseHeader("location");
                if (locationHeader != null) {
                    String redirectLocation = locationHeader.getValue();
                    get.setURI(new URI(redirectLocation, true));
                    httpClient.executeMethod(get);
                }
            }
            if (get.getStatusCode() != HttpServletResponse.SC_OK) {
                throw new LuceneQueryParserException("Request failed " + get.getStatusCode() + " " + url.toString());
            }
            Reader reader = new BufferedReader(new InputStreamReader(get.getResponseBodyAsStream()));
            // TODO - replace with streaming-based solution e.g. SimpleJSON ContentHandler
            JSONObject json = new JSONObject(new JSONTokener(reader));
            return json;
        } finally {
            get.releaseConnection();
        }
    } 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);
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader) UnsupportedEncodingException(java.io.UnsupportedEncodingException) JSONException(org.json.JSONException) IOException(java.io.IOException) URI(org.apache.commons.httpclient.URI) URLCodec(org.apache.commons.codec.net.URLCodec) JSONTokener(org.json.JSONTokener) Header(org.apache.commons.httpclient.Header) JSONObject(org.json.JSONObject) LuceneQueryParserException(org.alfresco.repo.search.impl.lucene.LuceneQueryParserException) GetMethod(org.apache.commons.httpclient.methods.GetMethod) BufferedReader(java.io.BufferedReader) HttpException(org.apache.commons.httpclient.HttpException)

Example 4 with URLCodec

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

the class SolrQueryHTTPClient method execute.

/**
 * @param storeRef
 * @param handler
 * @param params
 * @return
 */
public JSONObject execute(StoreRef storeRef, String handler, HashMap<String, String> params) {
    try {
        SolrStoreMappingWrapper mapping = SolrClientUtil.extractMapping(storeRef, mappingLookup, shardRegistry, useDynamicShardRegistration, beanFactory);
        URLCodec encoder = new URLCodec();
        StringBuilder url = new StringBuilder();
        Pair<HttpClient, String> httpClientAndBaseUrl = mapping.getHttpClientAndBaseUrl();
        HttpClient httpClient = httpClientAndBaseUrl.getFirst();
        for (String key : params.keySet()) {
            String value = params.get(key);
            if (url.length() == 0) {
                url.append(httpClientAndBaseUrl.getSecond());
                if (!handler.startsWith("/")) {
                    url.append("/");
                }
                url.append(handler);
                url.append("?");
                url.append(encoder.encode(key, "UTF-8"));
                url.append("=");
                url.append(encoder.encode(value, "UTF-8"));
            } else {
                url.append("&");
                url.append(encoder.encode(key, "UTF-8"));
                url.append("=");
                url.append(encoder.encode(value, "UTF-8"));
            }
        }
        if (mapping.isSharded()) {
            url.append("&shards=");
            url.append(mapping.getShards());
        }
        // PostMethod post = new PostMethod(url.toString());
        GetMethod get = new GetMethod(url.toString());
        try {
            httpClient.executeMethod(get);
            if (get.getStatusCode() == HttpStatus.SC_MOVED_PERMANENTLY || get.getStatusCode() == HttpStatus.SC_MOVED_TEMPORARILY) {
                Header locationHeader = get.getResponseHeader("location");
                if (locationHeader != null) {
                    String redirectLocation = locationHeader.getValue();
                    get.setURI(new URI(redirectLocation, true));
                    httpClient.executeMethod(get);
                }
            }
            if (get.getStatusCode() != HttpServletResponse.SC_OK) {
                throw new LuceneQueryParserException("Request failed " + get.getStatusCode() + " " + url.toString());
            }
            Reader reader = new BufferedReader(new InputStreamReader(get.getResponseBodyAsStream()));
            // TODO - replace with streaming-based solution e.g. SimpleJSON ContentHandler
            JSONObject json = new JSONObject(new JSONTokener(reader));
            return json;
        } finally {
            get.releaseConnection();
        }
    } 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);
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader) UnsupportedEncodingException(java.io.UnsupportedEncodingException) JSONException(org.json.JSONException) IOException(java.io.IOException) URI(org.apache.commons.httpclient.URI) URLCodec(org.apache.commons.codec.net.URLCodec) JSONTokener(org.json.JSONTokener) Header(org.apache.commons.httpclient.Header) JSONObject(org.json.JSONObject) LuceneQueryParserException(org.alfresco.repo.search.impl.lucene.LuceneQueryParserException) HttpClient(org.apache.commons.httpclient.HttpClient) GetMethod(org.apache.commons.httpclient.methods.GetMethod) BufferedReader(java.io.BufferedReader) HttpException(org.apache.commons.httpclient.HttpException)

Example 5 with URLCodec

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

the class SolrQueryHTTPClient method executeQuery.

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 = SolrClientUtil.extractStoreRef(searchParameters);
        SolrStoreMappingWrapper mapping = SolrClientUtil.extractMapping(store, mappingLookup, shardRegistry, useDynamicShardRegistration, beanFactory);
        Pair<HttpClient, String> httpClientAndBaseUrl = mapping.getHttpClientAndBaseUrl();
        HttpClient httpClient = httpClientAndBaseUrl.getFirst();
        URLCodec encoder = new URLCodec();
        StringBuilder url = new StringBuilder();
        url.append(httpClientAndBaseUrl.getSecond());
        String languageUrlFragment = SolrClientUtil.extractLanguageFragment(languageMappings, language);
        if (!url.toString().endsWith("/")) {
            url.append("/");
        }
        url.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"));
        // 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));
        if ((searchParameters.getStores().size() > 1) || (mapping.isSharded())) {
            boolean requiresSeparator = false;
            url.append("&shards=");
            for (StoreRef storeRef : searchParameters.getStores()) {
                SolrStoreMappingWrapper storeMapping = SolrClientUtil.extractMapping(storeRef, mappingLookup, shardRegistry, useDynamicShardRegistration, beanFactory);
                if (requiresSeparator) {
                    url.append(',');
                } else {
                    requiresSeparator = true;
                }
                url.append(storeMapping.getShards());
            }
        }
        buildUrlParameters(searchParameters, mapping.isSharded(), encoder, url);
        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);
        // just needed for the final parameter
        final int maximumResults = maxResults;
        return (ResultSet) postSolrQuery(httpClient, url.toString(), body, 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);
    }
}
Also used : Locale(java.util.Locale) 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) StoreRef(org.alfresco.service.cmr.repository.StoreRef) LimitBy(org.alfresco.service.cmr.search.LimitBy) JSONArray(org.json.JSONArray) UnsupportedEncodingException(java.io.UnsupportedEncodingException) JSONException(org.json.JSONException) SolrJSONResultSet(org.alfresco.repo.search.impl.lucene.SolrJSONResultSet) IOException(java.io.IOException) URLCodec(org.apache.commons.codec.net.URLCodec) JSONObject(org.json.JSONObject) LuceneQueryParserException(org.alfresco.repo.search.impl.lucene.LuceneQueryParserException) HttpClient(org.apache.commons.httpclient.HttpClient) ResultSet(org.alfresco.service.cmr.search.ResultSet) SolrJSONResultSet(org.alfresco.repo.search.impl.lucene.SolrJSONResultSet) AlfrescoRuntimeException(org.alfresco.error.AlfrescoRuntimeException) HttpException(org.apache.commons.httpclient.HttpException)

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