Search in sources :

Example 6 with URLCodec

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);
    }
}
Also used : Locale(java.util.Locale) StoreRef(org.alfresco.service.cmr.repository.StoreRef) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) IOException(java.io.IOException) URLCodec(org.apache.commons.codec.net.URLCodec) SearchParameters(org.alfresco.service.cmr.search.SearchParameters) EncoderException(org.apache.commons.codec.EncoderException) JSONObject(org.json.JSONObject) LuceneQueryParserException(org.alfresco.repo.search.impl.lucene.LuceneQueryParserException) HttpClient(org.apache.commons.httpclient.HttpClient) AlfrescoRuntimeException(org.alfresco.error.AlfrescoRuntimeException) ConnectException(java.net.ConnectException)

Example 7 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 8 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 9 with URLCodec

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;
}
Also used : URLCodec(org.apache.commons.codec.net.URLCodec) EncoderException(org.apache.commons.codec.EncoderException) ArrayList(java.util.ArrayList)

Example 10 with URLCodec

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;
}
Also used : ClusterNode(com.openmeap.model.dto.ClusterNode) ApplicationVersion(com.openmeap.model.dto.ApplicationVersion) BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) FileNotFoundException(java.io.FileNotFoundException) Error(com.openmeap.protocol.dto.Error) GlobalSettings(com.openmeap.model.dto.GlobalSettings) IOException(java.io.IOException) GenericRuntimeException(com.openmeap.util.GenericRuntimeException) FileNameMap(java.net.FileNameMap) FileInputStream(java.io.FileInputStream) Result(com.openmeap.protocol.dto.Result) URLCodec(org.apache.commons.codec.net.URLCodec) BufferedInputStream(java.io.BufferedInputStream) DigestException(com.openmeap.digest.DigestException) FileNameMap(java.net.FileNameMap) Map(java.util.Map) File(java.io.File)

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