List of usage examples for org.apache.commons.codec.net URLCodec encode
public String encode(String pString, String charset) throws UnsupportedEncodingException
From source file:bin.httpclient3.uri.EncodingUtil.java
/** * Form-urlencoding routine.//from ww w.j av a 2 s . c o m * * The default encoding for all forms is `application/x-www-form-urlencoded'. * A form data set is represented in this media type as follows: * * The form field names and values are escaped: space characters are replaced * by `+', and then reserved characters are escaped as per [URL]; that is, * non-alphanumeric characters are replaced by `%HH', a percent sign and two * hexadecimal digits representing the ASCII code of the character. Line breaks, * as in multi-line text field values, are represented as CR LF pairs, i.e. `%0D%0A'. * * @param pairs the values to be encoded * @param charset the character set of pairs to be encoded * * @return the urlencoded pairs * @throws UnsupportedEncodingException if charset is not supported * * @since 2.0 final */ private static String doFormUrlEncode(NameValuePair[] pairs, String charset) throws UnsupportedEncodingException { StringBuffer buf = new StringBuffer(); for (int i = 0; i < pairs.length; i++) { URLCodec codec = new URLCodec(); NameValuePair pair = pairs[i]; if (pair.getName() != null) { if (i > 0) { buf.append("&"); } buf.append(codec.encode(pair.getName(), charset)); buf.append("="); if (pair.getValue() != null) { buf.append(codec.encode(pair.getValue(), charset)); } } } return buf.toString(); }
From source file:com.microsoft.tfs.core.httpclient.util.EncodingUtil.java
/** * Form-urlencoding routine./* w w w .j av a 2 s . co m*/ * * The default encoding for all forms is * `application/x-www-form-urlencoded'. A form data set is represented in * this media type as follows: * * The form field names and values are escaped: space characters are * replaced by `+', and then reserved characters are escaped as per [URL]; * that is, non-alphanumeric characters are replaced by `%HH', a percent * sign and two hexadecimal digits representing the ASCII code of the * character. Line breaks, as in multi-line text field values, are * represented as CR LF pairs, i.e. `%0D%0A'. * * @param pairs * the values to be encoded * @param charset * the character set of pairs to be encoded * * @return the urlencoded pairs * @throws UnsupportedEncodingException * if charset is not supported * * @since 2.0 final */ private static String doFormUrlEncode(final NameValuePair[] pairs, final String charset) throws UnsupportedEncodingException { final StringBuffer buf = new StringBuffer(); for (int i = 0; i < pairs.length; i++) { final URLCodec codec = new URLCodec(); final NameValuePair pair = pairs[i]; if (pair.getName() != null) { if (i > 0) { buf.append("&"); } buf.append(codec.encode(pair.getName(), charset)); buf.append("="); if (pair.getValue() != null) { buf.append(codec.encode(pair.getValue(), charset)); } } } return buf.toString(); }
From source file:com.eucalyptus.auth.login.Hmacv2LoginModule.java
private String makeSubjectString(String httpMethod, String host, String path, final Map<String, String> parameters) throws UnsupportedEncodingException { URLCodec codec = new URLCodec(); parameters.remove(""); StringBuilder sb = new StringBuilder(); sb.append(httpMethod);// w w w .j a v a 2 s . co m sb.append("\n"); sb.append(host); sb.append("\n"); sb.append(path); sb.append("\n"); String prefix = sb.toString(); sb = new StringBuilder(); NavigableSet<String> sortedKeys = new TreeSet<String>(); sortedKeys.addAll(parameters.keySet()); String firstKey = sortedKeys.pollFirst(); if (firstKey != null) { sb.append(codec.encode(firstKey, "UTF-8")).append("=").append( codec.encode(Strings.nullToEmpty(parameters.get(firstKey)), "UTF-8").replaceAll("\\+", "%20")); } while ((firstKey = sortedKeys.pollFirst()) != null) { sb.append("&").append(codec.encode(firstKey, "UTF-8")).append("=").append( codec.encode(Strings.nullToEmpty(parameters.get(firstKey)), "UTF-8").replaceAll("\\+", "%20")); } String subject = prefix + sb.toString(); LOG.trace("VERSION2: " + subject); return subject; }
From source file:org.alfresco.repo.search.impl.solr.ExplicitSolrStoreMappingWrapper.java
private String getShards1() { try {/*from w w w . j a v a 2s . c o m*/ URLCodec encoder = new URLCodec(); StringBuilder builder = new StringBuilder(); Set<Integer> shards = new HashSet<Integer>(); for (int i = 0; i < httpClientsAndBaseURLs.size(); i += wrapped.getReplicationFactor()) { for (Integer shardId : policy.getShardIdsForNode(i + 1)) { if (!shards.contains(shardId % wrapped.getNumShards())) { if (shards.size() > 0) { builder.append(','); } HttpClientAndBaseUrl httpClientAndBaseUrl = httpClientsAndBaseURLs .toArray(new HttpClientAndBaseUrl[0])[i]; 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")); } builder.append('-').append(shardId); shards.add(shardId % wrapped.getNumShards()); } } } return builder.toString(); } catch (UnsupportedEncodingException e) { throw new LuceneQueryParserException("", e); } }
From source file:org.alfresco.repo.search.impl.solr.ExplicitSolrStoreMappingWrapper.java
private String getShards2() { try {/* ww w. j a v a 2 s . c o m*/ 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.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")); } builder.append('-').append(shard); } return builder.toString(); } catch (UnsupportedEncodingException e) { throw new LuceneQueryParserException("", e); } }
From source file:org.alfresco.repo.search.impl.solr.SolrAdminHTTPClient.java
public JSONObject execute(String relativeHandlerPath, HashMap<String, String> args) { ParameterCheck.mandatory("relativeHandlerPath", relativeHandlerPath); ParameterCheck.mandatory("args", args); String path = getPath(relativeHandlerPath); try {/*from w ww .j a v a 2 s . com*/ 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); } }
From source file:org.alfresco.repo.search.impl.solr.SolrQueryHTTPClient.java
protected String buildStatsUrl(StatsParameters searchParameters, String baseUrl, Locale locale, SolrStoreMappingWrapper mapping) throws UnsupportedEncodingException { URLCodec encoder = new URLCodec(); StringBuilder url = new StringBuilder(); String languageUrlFragment = extractLanguageFragment(searchParameters.getLanguage()); url.append(baseUrl);/* www .j a v a2 s . co m*/ 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()))) { 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()); } } return url.toString(); }
From source file:org.alfresco.repo.search.impl.solr.SolrQueryHTTPClient.java
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"); }/* ww w . j a v a2 s. c o m*/ 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); 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")); 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 || searchParameters.getFacetQueries().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")); } int facetLimit; if (facet.getLimitOrNull() == null) { if (mapping.isSharded()) { facetLimit = defaultShardedFacetLimit; } else { facetLimit = defaultUnshardedFacetLimit; } } else { facetLimit = facet.getLimitOrNull().intValue(); } url.append("&").append(encoder.encode("f." + facet.getField() + ".facet.limit", "UTF-8")) .append("=").append(encoder.encode("" + facetLimit, "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()) { if (!facetQuery.startsWith("{!afts")) { facetQuery = "{!afts}" + facetQuery; } url.append("&facet.query=").append(encoder.encode(facetQuery, "UTF-8")); } } // filter queries for (String filterQuery : searchParameters.getFilterQueries()) { if (!filterQuery.startsWith("{!afts")) { filterQuery = "{!afts}" + filterQuery; } url.append("&fq=").append(encoder.encode(filterQuery, "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); } }
From source file:org.alfresco.repo.search.impl.solr.SolrQueryHTTPClient.java
private StringBuffer buildSortParameters(BasicSearchParameters searchParameters, URLCodec encoder) throws UnsupportedEncodingException { StringBuffer sortBuffer = new StringBuffer(); for (SortDefinition sortDefinition : searchParameters.getSortDefinitions()) { if (sortBuffer.length() == 0) { sortBuffer.append("&sort="); } else {/* w w w . ja v a2 s . co m*/ sortBuffer.append(encoder.encode(", ", "UTF-8")); } // MNT-8557 fix, manually replace ' ' with '%20' // The sort can be different, see MNT-13742 switch (sortDefinition.getSortType()) { case DOCUMENT: sortBuffer.append(encoder.encode("_docid_", "UTF-8")).append(encoder.encode(" ", "UTF-8")); break; case SCORE: sortBuffer.append(encoder.encode("score", "UTF-8")).append(encoder.encode(" ", "UTF-8")); break; case FIELD: default: sortBuffer.append(encoder.encode(sortDefinition.getField().replaceAll(" ", "%20"), "UTF-8")) .append(encoder.encode(" ", "UTF-8")); break; } if (sortDefinition.isAscending()) { sortBuffer.append(encoder.encode("asc", "UTF-8")); } else { sortBuffer.append(encoder.encode("desc", "UTF-8")); } } return sortBuffer; }
From source file:org.alfresco.repo.search.impl.solr.SolrQueryHTTPClient.java
/** * @param storeRef//from ww w .ja v a 2s .c o m * @param handler * @param params * @return */ public JSONObject execute(StoreRef storeRef, String handler, HashMap<String, String> params) { try { SolrStoreMappingWrapper mapping = extractMapping(storeRef); 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); } }