use of org.apache.commons.codec.net.URLCodec in project alfresco-repository by Alfresco.
the class SolrSQLHttpClient method executeQuery.
public ResultSet executeQuery(SearchParameters searchParameters, String language) {
if (repositoryState.isBootstrapping()) {
throw new AlfrescoRuntimeException("SOLR queries can not be executed while the repository is bootstrapping");
}
if (StringUtils.isEmpty(searchParameters.getQuery())) {
throw new AlfrescoRuntimeException("SOLR query statement is missing");
}
try {
StoreRef store = SolrClientUtil.extractStoreRef(searchParameters);
SolrStoreMappingWrapper mapping = SolrClientUtil.extractMapping(store, mappingLookup, shardRegistry, useDynamicShardRegistration, beanFactory);
// Extract collection name from stmt.
Pair<HttpClient, String> httpClientAndBaseUrl = mapping.getHttpClientAndBaseUrl();
HttpClient httpClient = httpClientAndBaseUrl.getFirst();
URLCodec encoder = new URLCodec();
StringBuilder url = new StringBuilder();
url.append(httpClientAndBaseUrl.getSecond());
if (!url.toString().endsWith("/")) {
url.append("/");
}
url.append("sql?stmt=" + encoder.encode(searchParameters.getQuery()));
SearchParameters sp = (SearchParameters) searchParameters;
url.append("&includeMetadata=" + sp.isIncludeMetadata());
url.append("&aggregationMode=facet");
if (searchParameters.getTimezone() != null && !searchParameters.getTimezone().isEmpty()) {
url.append("&TZ=").append(encoder.encode(searchParameters.getTimezone(), "UTF-8"));
}
url.append("&alfresco.shards=");
/*
* When sharded we pass array of shard instances otherwise we pass the local instance url which
* is http://url:port/solr/collection_name
*/
if (mapping.isSharded()) {
url.append(mapping.getShards());
} else {
String solrurl = httpClient.getHostConfiguration().getHostURL() + httpClientAndBaseUrl.getSecond();
url.append(solrurl);
}
JSONObject body = new JSONObject();
// 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 filterQueries = new JSONArray();
for (String filterQuery : searchParameters.getFilterQueries()) {
filterQueries.put(filterQuery);
}
body.put("filterQueries", filterQueries);
return postSolrQuery(httpClient, url.toString(), body, json -> new SolrSQLJSONResultSet(json, searchParameters));
} catch (ConnectException ce) {
throw new LuceneQueryParserException("Unable to reach InsightEngine", ce);
} catch (JSONException | IOException | EncoderException e) {
throw new LuceneQueryParserException("Unable to parse the solr response ", e);
}
}
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 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 OpenAttestation by OpenAttestation.
the class ApiClient method querystring.
private String querystring(MultivaluedMap<String, String> query) {
URLCodec urlsafe = new URLCodec("UTF-8");
String queryString = "";
ArrayList<String> params = new ArrayList<String>();
for (String key : query.keySet()) {
if (query.get(key) == null) {
params.add(key + "=");
} else {
for (String value : query.get(key)) {
try {
// XXX assumes that the keys don't have any special characters
params.add(key + "=" + urlsafe.encode(value));
} catch (EncoderException ex) {
log.error("Cannot encode query parameter: {}", value, ex);
}
}
}
queryString = StringUtils.join(params, "&");
}
return queryString;
}
use of org.apache.commons.codec.net.URLCodec in project OpenMEAP by OpenMEAP.
the class ApplicationManagementServlet method handleArchiveDownload.
private Result handleArchiveDownload(HttpServletRequest request, HttpServletResponse response) {
Result res = new Result();
Error err = new Error();
res.setError(err);
GlobalSettings settings = modelManager.getGlobalSettings();
Map properties = this.getServicesWebProperties();
String nodeKey = (String) properties.get("clusterNodeUrlPrefix");
ClusterNode clusterNode = settings.getClusterNode(nodeKey);
if (nodeKey == null || clusterNode == null) {
// TODO: create a configuration error code
err.setCode(ErrorCode.UNDEFINED);
err.setMessage("A configuration is missing. Please consult the error logs.");
logger.error("For each node in the cluster, the property or environment variable OPENMEAP_CLUSTER_NODE_URL_PREFIX must match the \"Service Url Prefix\" value configured in the administrative interface. This value is currently " + nodeKey + ".");
return res;
}
String pathValidation = clusterNode.validateFileSystemStoragePathPrefix();
if (pathValidation != null) {
err.setCode(ErrorCode.UNDEFINED);
err.setMessage("A configuration is missing. Please consult the error logs.");
logger.error("There is an issue with the location at \"File-system Storage Prefix\". " + pathValidation);
return res;
}
String hash = request.getParameter(UrlParamConstants.APPARCH_HASH);
String hashAlg = request.getParameter(UrlParamConstants.APPARCH_HASH_ALG);
String fileName = null;
if (hash == null || hashAlg == null) {
// look in the apps directory for the archive specified
String appName = request.getParameter(UrlParamConstants.APP_NAME);
String versionId = request.getParameter(UrlParamConstants.APP_VERSION);
ApplicationVersion appVersion = modelManager.getModelService().findAppVersionByNameAndId(appName, versionId);
if (appVersion == null) {
String mesg = "The application version " + versionId + " was not found for application " + appName;
err.setCode(ErrorCode.APPLICATION_VERSION_NOTFOUND);
err.setMessage(mesg);
logger.warn(mesg);
return res;
}
String auth = request.getParameter(UrlParamConstants.AUTH_TOKEN);
com.openmeap.model.dto.Application app = appVersion.getApplication();
try {
if (auth == null || !AuthTokenProvider.validateAuthToken(app.getProxyAuthSalt(), auth)) {
err.setCode(ErrorCode.AUTHENTICATION_FAILURE);
err.setMessage("The \"auth\" token presented is not recognized, missing, or empty.");
return res;
}
} catch (DigestException e) {
throw new GenericRuntimeException(e);
}
hash = appVersion.getArchive().getHash();
hashAlg = appVersion.getArchive().getHashAlgorithm();
fileName = app.getName() + " - " + appVersion.getIdentifier();
} else {
fileName = hashAlg + "-" + hash;
}
File file = ApplicationArchive.getFile(clusterNode.getFileSystemStoragePathPrefix(), hashAlg, hash);
if (!file.exists()) {
String mesg = "The application archive with " + hashAlg + " hash " + hash + " was not found.";
// TODO: create an enumeration for this error
err.setCode(ErrorCode.UNDEFINED);
err.setMessage(mesg);
logger.warn(mesg);
return res;
}
try {
FileNameMap fileNameMap = URLConnection.getFileNameMap();
String mimeType = fileNameMap.getContentTypeFor(file.toURL().toString());
response.setContentType(mimeType);
response.setContentLength(Long.valueOf(file.length()).intValue());
URLCodec codec = new URLCodec();
response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + ".zip\";");
InputStream inputStream = null;
OutputStream outputStream = null;
try {
inputStream = new BufferedInputStream(new FileInputStream(file));
outputStream = response.getOutputStream();
Utils.pipeInputStreamIntoOutputStream(inputStream, outputStream);
} finally {
if (inputStream != null) {
inputStream.close();
}
// if(outputStream!=null) {outputStream.close();}
}
response.flushBuffer();
} catch (FileNotFoundException e) {
logger.error("Exception {}", e);
} catch (IOException ioe) {
logger.error("Exception {}", ioe);
}
return null;
}
Aggregations