Search in sources :

Example 6 with LuceneQueryParserException

use of org.alfresco.repo.search.impl.lucene.LuceneQueryParserException 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 LuceneQueryParserException

use of org.alfresco.repo.search.impl.lucene.LuceneQueryParserException in project alfresco-repository by Alfresco.

the class AbstractSolrQueryHTTPClient method postQuery.

protected JSONObject postQuery(HttpClient httpClient, String url, JSONObject body) throws UnsupportedEncodingException, IOException, HttpException, URIException, JSONException {
    PostMethod post = new PostMethod(url);
    if (body.toString().length() > DEFAULT_SAVEPOST_BUFFER) {
        post.getParams().setBooleanParameter(HttpMethodParams.USE_EXPECT_CONTINUE, true);
    }
    StringRequestEntity requestEntity = new StringRequestEntity(body.toString(), "application/json", "UTF-8");
    post.setRequestEntity(requestEntity);
    try {
        httpClient.executeMethod(post);
        if (post.getStatusCode() == HttpStatus.SC_MOVED_PERMANENTLY || post.getStatusCode() == HttpStatus.SC_MOVED_TEMPORARILY) {
            Header locationHeader = post.getResponseHeader("location");
            if (locationHeader != null) {
                String redirectLocation = locationHeader.getValue();
                post.setURI(new URI(redirectLocation, true));
                httpClient.executeMethod(post);
            }
        }
        if (post.getStatusCode() != HttpServletResponse.SC_OK) {
            throw new LuceneQueryParserException("Request failed " + post.getStatusCode() + " " + url.toString());
        }
        Reader reader = new BufferedReader(new InputStreamReader(post.getResponseBodyAsStream(), post.getResponseCharSet()));
        // TODO - replace with streaming-based solution e.g. SimpleJSON ContentHandler
        JSONObject json = new JSONObject(new JSONTokener(reader));
        return json;
    } finally {
        post.releaseConnection();
    }
}
Also used : JSONTokener(org.json.JSONTokener) StringRequestEntity(org.apache.commons.httpclient.methods.StringRequestEntity) Header(org.apache.commons.httpclient.Header) InputStreamReader(java.io.InputStreamReader) JSONObject(org.json.JSONObject) PostMethod(org.apache.commons.httpclient.methods.PostMethod) LuceneQueryParserException(org.alfresco.repo.search.impl.lucene.LuceneQueryParserException) BufferedReader(java.io.BufferedReader) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader) URI(org.apache.commons.httpclient.URI)

Example 8 with LuceneQueryParserException

use of org.alfresco.repo.search.impl.lucene.LuceneQueryParserException 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 9 with LuceneQueryParserException

use of org.alfresco.repo.search.impl.lucene.LuceneQueryParserException in project alfresco-repository by Alfresco.

the class SolrChildApplicationContextFactory method getProperty.

@Override
public String getProperty(String name) {
    // MNT-9254 fix, use search.solrAdminHTTPCLient bean to retrive property value only if sorl subsystem is active and started (application context in state should be not null)
    if (false == isUpdateable(name) && ((ApplicationContextState) getState(false)).getApplicationContext(false) != null) {
        try {
            ApplicationContext ctx = getApplicationContext();
            SolrAdminHTTPClient adminClient = (SolrAdminHTTPClient) ctx.getBean("search.solrAdminHTTPCLient");
            HashMap<String, String> args = new HashMap<String, String>();
            args.put("action", "SUMMARY");
            args.put("wt", "json");
            JSONObject json = adminClient.execute(args);
            JSONObject summary = json.getJSONObject("Summary");
            JSONObject alfresco = null;
            try {
                alfresco = summary.getJSONObject("alfresco");
            } catch (JSONException e) {
            // The core might be absent.
            }
            if (alfresco != null) {
                if (name.equals(SolrChildApplicationContextFactory.ALFRESCO_ACTIVE)) {
                    String alfrescoActive = alfresco.getString("Active");
                    if (alfrescoActive == null || alfrescoActive.isEmpty()) {
                        // Admin Console is expecting a true/false value, not blank
                        return "false";
                    }
                    return alfrescoActive;
                } else if (name.equals(SolrChildApplicationContextFactory.ALFRESCO_LAG)) {
                    return alfresco.getString("TX Lag");
                } else if (name.equals(SolrChildApplicationContextFactory.ALFRESCO_LAG_DURATION)) {
                    return alfresco.getString("TX Duration");
                } else if (name.equals(SolrChildApplicationContextFactory.ALFRESCO_LAST_INDEXED_TXN)) {
                    return alfresco.getString("Id for last TX in index");
                } else if (name.equals(SolrChildApplicationContextFactory.ALFRESCO_APPROX_TXNS_REMAINING)) {
                    return alfresco.getString("Approx transactions remaining");
                } else if (name.equals(SolrChildApplicationContextFactory.ALFRESCO_APPROX_INDEXING_TIME_REMAINING)) {
                    return alfresco.getString("Approx transaction indexing time remaining");
                } else if (name.equals(SolrChildApplicationContextFactory.ALFRESCO_DISK)) {
                    return alfresco.getString("On disk (GB)");
                } else if (name.equals(SolrChildApplicationContextFactory.ALFRESCO_MEMORY)) {
                    return alfresco.getString("Total Searcher Cache (GB)");
                }
            }
            JSONObject archive = null;
            try {
                archive = summary.getJSONObject("archive");
            } catch (JSONException e) {
            // The core might be absent.
            }
            if (archive != null) {
                if (name.equals(SolrChildApplicationContextFactory.ARCHIVE_ACTIVE)) {
                    return archive.getString("Active");
                } else if (name.equals(SolrChildApplicationContextFactory.ARCHIVE_LAG)) {
                    return archive.getString("TX Lag");
                } else if (name.equals(SolrChildApplicationContextFactory.ARCHIVE_LAG_DURATION)) {
                    return archive.getString("TX Duration");
                } else if (name.equals(SolrChildApplicationContextFactory.ARCHIVE_LAST_INDEXED_TXN)) {
                    return archive.getString("Id for last TX in index");
                } else if (name.equals(SolrChildApplicationContextFactory.ARCHIVE_APPROX_TXNS_REMAINING)) {
                    return archive.getString("Approx transactions remaining");
                } else if (name.equals(SolrChildApplicationContextFactory.ARCHIVE_APPROX_INDEXING_TIME_REMAINING)) {
                    return archive.getString("Approx transaction indexing time remaining");
                } else if (name.equals(SolrChildApplicationContextFactory.ARCHIVE_DISK)) {
                    return archive.getString("On disk (GB)");
                } else if (name.equals(SolrChildApplicationContextFactory.ARCHIVE_MEMORY)) {
                    return archive.getString("Total Searcher Cache (GB)");
                }
            }
            // Did not find the property in JSON or the core is turned off
            return "Unavailable";
        } catch (LuceneQueryParserException lqe) {
            return "Unavailable: " + lqe.getMessage();
        } catch (JSONException e) {
            return "Unavailable: " + e.getMessage();
        } catch (IllegalArgumentException iae) {
            return "Unavailable: " + iae.getMessage();
        }
    } else {
        // solr subsystem is not started or not active
        if (name.equals(SolrChildApplicationContextFactory.ALFRESCO_ACTIVE)) {
            return "";
        } else if (name.equals(SolrChildApplicationContextFactory.ARCHIVE_ACTIVE)) {
            return "";
        } else if (name.equals(SolrChildApplicationContextFactory.ALFRESCO_LAG)) {
            return "Unavailable: solr subsystem not started";
        } else if (name.equals(SolrChildApplicationContextFactory.ALFRESCO_LAG_DURATION)) {
            return "Unavailable: solr subsystem not started";
        } else if (name.equals(SolrChildApplicationContextFactory.ALFRESCO_LAST_INDEXED_TXN)) {
            return "Unavailable: solr subsystem not started";
        } else if (name.equals(SolrChildApplicationContextFactory.ALFRESCO_APPROX_TXNS_REMAINING)) {
            return "Unavailable: solr subsystem not started";
        } else if (name.equals(SolrChildApplicationContextFactory.ALFRESCO_APPROX_INDEXING_TIME_REMAINING)) {
            return "Unavailable: solr subsystem not started";
        } else if (name.equals(SolrChildApplicationContextFactory.ALFRESCO_DISK)) {
            return "Unavailable: solr subsystem not started";
        } else if (name.equals(SolrChildApplicationContextFactory.ALFRESCO_MEMORY)) {
            return "Unavailable: solr subsystem not started";
        } else if (name.equals(SolrChildApplicationContextFactory.ARCHIVE_LAG)) {
            return "Unavailable: solr subsystem not started";
        } else if (name.equals(SolrChildApplicationContextFactory.ARCHIVE_LAG_DURATION)) {
            return "Unavailable: solr subsystem not started";
        } else if (name.equals(SolrChildApplicationContextFactory.ARCHIVE_LAST_INDEXED_TXN)) {
            return "Unavailable: solr subsystem not started";
        } else if (name.equals(SolrChildApplicationContextFactory.ARCHIVE_APPROX_TXNS_REMAINING)) {
            return "Unavailable: solr subsystem not started";
        } else if (name.equals(SolrChildApplicationContextFactory.ARCHIVE_APPROX_INDEXING_TIME_REMAINING)) {
            return "Unavailable: solr subsystem not started";
        } else if (name.equals(SolrChildApplicationContextFactory.ARCHIVE_DISK)) {
            return "Unavailable: solr subsystem not started";
        } else if (name.equals(SolrChildApplicationContextFactory.ARCHIVE_MEMORY)) {
            return "Unavailable: solr subsystem not started";
        } else {
            return super.getProperty(name);
        }
    }
}
Also used : ApplicationContext(org.springframework.context.ApplicationContext) JSONObject(org.json.JSONObject) HashMap(java.util.HashMap) LuceneQueryParserException(org.alfresco.repo.search.impl.lucene.LuceneQueryParserException) JSONException(org.json.JSONException)

Example 10 with LuceneQueryParserException

use of org.alfresco.repo.search.impl.lucene.LuceneQueryParserException in project alfresco-remote-api by Alfresco.

the class SearchSQLApiWebscript method execute.

@Override
public void execute(WebScriptRequest webScriptRequest, WebScriptResponse res) throws IOException {
    try {
        // Turn JSON into a Java object representation
        SearchSQLQuery searchQuery = extractJsonContent(webScriptRequest, assistant.getJsonHelper(), SearchSQLQuery.class);
        SearchParameters sparams = buildSearchParameters(searchQuery);
        ResultSet results = searchService.query(sparams);
        FilteringResultSet frs = (FilteringResultSet) results;
        SolrSQLJSONResultSet ssjr = (SolrSQLJSONResultSet) frs.getUnFilteredResultSet();
        // When solr format is requested pass the solr output directly.
        if (searchQuery.getFormat().equalsIgnoreCase("solr")) {
            res.getWriter().write(ssjr.getSolrResponse());
        } else {
            CollectionWithPagingInfo<TupleList> nodes = resultMapper.toCollectionWithPagingInfo(ssjr.getDocs(), searchQuery);
            renderJsonResponse(res, nodes, assistant.getJsonHelper());
        }
        setResponse(res, DEFAULT_SUCCESS);
    } catch (Exception exception) {
        if (exception instanceof LuceneQueryParserException) {
            renderException(exception, res, assistant);
        } else {
            renderException(new WebScriptException(400, exception.getMessage()), res, assistant);
        }
    }
}
Also used : FilteringResultSet(org.alfresco.repo.security.permissions.impl.acegi.FilteringResultSet) TupleList(org.alfresco.rest.api.search.model.TupleList) SearchParameters(org.alfresco.service.cmr.search.SearchParameters) SolrSQLJSONResultSet(org.alfresco.repo.search.impl.solr.SolrSQLJSONResultSet) WebScriptException(org.springframework.extensions.webscripts.WebScriptException) LuceneQueryParserException(org.alfresco.repo.search.impl.lucene.LuceneQueryParserException) ResultSet(org.alfresco.service.cmr.search.ResultSet) FilteringResultSet(org.alfresco.repo.security.permissions.impl.acegi.FilteringResultSet) SolrSQLJSONResultSet(org.alfresco.repo.search.impl.solr.SolrSQLJSONResultSet) SearchSQLQuery(org.alfresco.rest.api.search.model.SearchSQLQuery) LuceneQueryParserException(org.alfresco.repo.search.impl.lucene.LuceneQueryParserException) IOException(java.io.IOException) AlfrescoRuntimeException(org.alfresco.error.AlfrescoRuntimeException) WebScriptException(org.springframework.extensions.webscripts.WebScriptException)

Aggregations

LuceneQueryParserException (org.alfresco.repo.search.impl.lucene.LuceneQueryParserException)15 IOException (java.io.IOException)9 JSONObject (org.json.JSONObject)9 URLCodec (org.apache.commons.codec.net.URLCodec)8 AlfrescoRuntimeException (org.alfresco.error.AlfrescoRuntimeException)7 HttpClient (org.apache.commons.httpclient.HttpClient)7 JSONException (org.json.JSONException)7 BufferedReader (java.io.BufferedReader)6 InputStreamReader (java.io.InputStreamReader)6 Reader (java.io.Reader)6 SearchParameters (org.alfresco.service.cmr.search.SearchParameters)6 Header (org.apache.commons.httpclient.Header)6 URI (org.apache.commons.httpclient.URI)6 JSONTokener (org.json.JSONTokener)6 UnsupportedEncodingException (java.io.UnsupportedEncodingException)5 StoreRef (org.alfresco.service.cmr.repository.StoreRef)5 GetMethod (org.apache.commons.httpclient.methods.GetMethod)5 HttpException (org.apache.commons.httpclient.HttpException)4 HashMap (java.util.HashMap)3 Locale (java.util.Locale)3