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));
}
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, "/");
}
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);
}
}
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);
}
}
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);
}
}
Aggregations