List of usage examples for org.apache.solr.client.solrj.util ClientUtils escapeQueryChars
public static String escapeQueryChars(String s)
From source file:org.sakaiproject.nakamura.search.solr.SolrSearchServlet.java
License:Apache License
/** * Load properties from the query node, request and property provider.<br/> * * Overwrite order: query node < request < property provider<br/> * * This ordering allows the query node to set defaults, the request to override those * defaults but the property provider to have the final say in what value is set. * * @param request/*from w w w .j ava 2 s. com*/ * @param propertyProviderName * @return * @throws RepositoryException */ private Map<String, String> loadProperties(SlingHttpServletRequest request, String[] propertyProviderNames, PropertyIterator defaultProps, String queryType) throws RepositoryException { Map<String, String> propertiesMap = new HashMap<String, String>(); // 0. load authorizable (user) information String userId = request.getRemoteUser(); String userPrivatePath = ClientUtils.escapeQueryChars(LitePersonalUtils.getPrivatePath(userId)); propertiesMap.put("_userPrivatePath", userPrivatePath); propertiesMap.put("_userId", ClientUtils.escapeQueryChars(userId)); // 1. load in properties from the query template node so defaults can be set if (defaultProps != null) { while (defaultProps.hasNext()) { javax.jcr.Property prop = defaultProps.nextProperty(); String key = prop.getName(); if (!key.startsWith("jcr:") && !propertiesMap.containsKey(key) && !prop.isMultiple()) { String val = prop.getString(); propertiesMap.put(key, val); } } } // 2. load in properties from the request RequestParameterMap params = request.getRequestParameterMap(); for (Entry<String, RequestParameter[]> entry : params.entrySet()) { RequestParameter[] vals = entry.getValue(); String requestValue = vals[0].getString(); // blank values aren't cool if (StringUtils.isBlank(requestValue)) { continue; } // we're selective with what we escape to make sure we don't hinder // search functionality String key = entry.getKey(); String val = SearchUtil.escapeString(requestValue, queryType); propertiesMap.put(key, val); } // 3. load properties from a property provider if (propertyProviderNames != null) { for (String propertyProviderName : propertyProviderNames) { LOGGER.debug("Trying Provider Name {} ", propertyProviderName); SolrSearchPropertyProvider provider = searchPropertyProviderTracker.getByName(propertyProviderName); if (provider != null) { LOGGER.debug("Trying Provider {} ", provider); provider.loadUserProperties(request, propertiesMap); } else { LOGGER.warn("No properties provider found for {} ", propertyProviderName); } } } else { LOGGER.debug("No Provider "); } return propertiesMap; }
From source file:org.sakaiproject.nakamura.user.counts.ContentCounter.java
License:Apache License
public int countExact(Authorizable au, SolrServerService solrSearchService) { if (au != null && !CountProvider.IGNORE_AUTHIDS.contains(au.getId())) { // find the content where the user has been made either a viewer or a manager String userID = ClientUtils.escapeQueryChars(au.getId()); String readers = userID;/* w w w. ja va 2 s. co m*/ if (!au.isGroup()) { // pooled-content-manager, pooled-content-viewer List<String> principals = Lists.newArrayList(userID); if (au.getPrincipals() != null) { for (String principal : au.getPrincipals()) { principals.add(ClientUtils.escapeQueryChars(principal)); } } principals.remove(Group.EVERYONE); readers = StringUtils.join(principals, " OR "); } String queryString = "resourceType:sakai/pooled-content AND (manager:(" + readers + ") OR viewer:(" + readers + "))"; return getCount(queryString, solrSearchService); } return 0; }
From source file:org.sakaiproject.nakamura.user.search.AuthorizableIndexingHandler.java
License:Apache License
/** * {@inheritDoc}/*w ww. j av a2 s . c om*/ * * @see org.sakaiproject.nakamura.api.solr.IndexingHandler#getDeleteQueries(org.sakaiproject.nakamura.api.solr.RepositorySession, * org.osgi.service.event.Event) */ public Collection<String> getDeleteQueries(RepositorySession repositorySession, Event event) { Collection<String> retval = Collections.emptyList(); String topic = event.getTopic(); String authName = String.valueOf(event.getProperty(FIELD_PATH)); if (topic.endsWith(StoreListener.DELETE_TOPIC)) { logger.debug("GetDelete for {} ", event); retval = ImmutableList.of("id:" + ClientUtils.escapeQueryChars(authName)); } else { // KERN-1822 check if the authorizable is marked to be excluded from searches Authorizable authorizable = getAuthorizable(authName, repositorySession); if (authorizable != null && Boolean .parseBoolean(String.valueOf(authorizable.getProperty(UserConstants.SAKAI_EXCLUDE)))) { retval = ImmutableList.of("id:" + ClientUtils.escapeQueryChars(authName)); } } return retval; }
From source file:org.sakaiproject.nakamura.user.search.AuthorizableIndexingHandlerTest.java
License:Apache License
@Test public void delete() { Hashtable<String, Object> props = new Hashtable<String, Object>(); props.put("path", "user1"); Event event = new Event(StoreListener.DELETE_TOPIC, (Map) props); Collection<String> queries = handler.getDeleteQueries(repoSession, event); assertNotNull(queries);//from w w w. j a v a 2s .c om assertEquals(1, queries.size()); assertEquals("id:" + ClientUtils.escapeQueryChars("user1"), queries.iterator().next()); }
From source file:org.sakaiproject.nakamura.user.search.ProfileIndexingHandler.java
License:Apache License
/** * {@inheritDoc}//w w w . j ava 2 s . co m * * @see org.sakaiproject.nakamura.api.solr.IndexingHandler#getDeleteQueries(org.sakaiproject.nakamura.api.solr.RepositorySession, * org.osgi.service.event.Event) */ @Override public Collection<String> getDeleteQueries(RepositorySession respositorySession, Event event) { // If a Profile section (or Profile section/elements) node has been deleted, then the // index of that section can also be deleted. If an individual element within // the section was deleted, however, the Profile section just needs to be re-indexed. // That should happen when the delete event is passed to getDocuments. Collection<String> deleteQueries; if (event.getTopic().endsWith(StoreListener.DELETE_TOPIC)) { String actualPath = String.valueOf(event.getProperty("path")); String[] pathParts = StringUtils.split(actualPath, "/", 6); if ((pathParts.length == 4) || (pathParts.length == 5)) { String sectionPath = StringUtils.join(pathParts, "/", 0, 4); return ImmutableList.of("id:" + ClientUtils.escapeQueryChars(sectionPath)); } } return ImmutableList.of(); }
From source file:org.sakaiproject.nakamura.user.search.ProfileIndexingHandlerTest.java
License:Apache License
@Test public void deleteSectionIndexWhenSectionDeleted() { Event event = new Event(StoreListener.DELETE_TOPIC, ImmutableMap.of("path", ELEMENTS_PATH)); Collection<String> deleteQueries = handler.getDeleteQueries(repoSession, event); assertEquals(1, deleteQueries.size()); assertEquals("id:" + ClientUtils.escapeQueryChars(SECTION_PATH), deleteQueries.iterator().next()); Collection<SolrInputDocument> documents = handler.getDocuments(repoSession, event); assertEquals(0, documents.size());// w ww .j ava2 s. c o m }
From source file:org.topicquests.solr.Solr4Client.java
License:Apache License
/** * Add data based on fields and values in <code>fields</code> * @param fields//from w ww .j ava 2s.c om * @return <code>null</code> or status code as Integer if error with error string */ public IResult addData(Map<String, Object> fields) { IResult result = new ResultPojo(); if (fields.isEmpty()) return result; System.out.println("Solr3Client.addData " + fields.size()); int status = 0; try { SolrInputDocument document = new SolrInputDocument(); Iterator<String> keys = fields.keySet().iterator(); String key; List<String> lobj; Object o; while (keys.hasNext()) { key = keys.next(); o = fields.get(key); System.out.println("Solr3Client.addData-1 " + key + " " + o); if (o instanceof String) { // if (key.equals(ITopicQuestsOntology.CREATED_DATE_PROPERTY)) document.addField(key, (String) o); // else // document.addField(key, ClientUtils.escapeQueryChars((String)o)); } else if (o instanceof List) { lobj = (List<String>) o; Iterator<String> itr = lobj.iterator(); while (itr.hasNext()) document.addField(key, ClientUtils.escapeQueryChars(itr.next())); //TODO add Booleans and other things } else throw new Exception("Solr4Client.addData fail: " + key + " " + o); } UpdateResponse response = server.add(document); status = response.getStatus(); //TODO full commit or soft commit? server.commit(); } catch (Exception e) { result.addErrorString(e.getMessage()); } result.setResultObject(new Integer(status)); return result; }
From source file:org.triple_brain.module.solr_search.SolrGraphSearch.java
License:Mozilla Public License
private SolrDocumentList autoCompletion(String label, User user, String queryPart, Boolean forPrivateOnly) { try {/*from www . ja v a 2 s .c o m*/ SolrServer solrServer = searchUtils.getServer(); SolrQuery solrQuery = new SolrQuery(); label = label.toLowerCase(); String sentenceMinusLastWord = ClientUtils.escapeQueryChars(sentenceMinusLastWord(label)); String lastWord = ClientUtils.escapeQueryChars(lastWordOfSentence(label)); solrQuery.setQuery(queryPart + " AND " + "(owner_username:" + user.username() + (forPrivateOnly ? ")" : " OR " + "is_public:true)")); String labelQuery = "(" + sentenceMinusLastWord + "*" + lastWord + "*)"; solrQuery.addFilterQuery("label_lower_case:" + labelQuery + " OR " + "property_label:" + labelQuery); QueryResponse queryResponse = solrServer.query(solrQuery); return queryResponse.getResults(); } catch (SolrServerException e) { throw new RuntimeException(e); } }
From source file:org.wso2.carbon.apimgt.impl.AbstractAPIManager.java
License:Open Source License
/** * Search api resources by their content * * @param registry/*from w ww . j a v a 2 s . c o m*/ * @param searchQuery * @param start * @param end * @return * @throws APIManagementException */ public Map<String, Object> searchPaginatedAPIsByContent(Registry registry, int tenantId, String searchQuery, int start, int end, boolean limitAttributes) throws APIManagementException { SortedSet<API> apiSet = new TreeSet<API>(new APINameComparator()); Map<Documentation, API> docMap = new HashMap<Documentation, API>(); Map<String, Object> result = new HashMap<String, Object>(); int totalLength = 0; boolean isMore = false; //SortedSet<Object> compoundResult = new TreeSet<Object>(new ContentSearchResultNameComparator()); ArrayList<Object> compoundResult = new ArrayList<Object>(); try { GenericArtifactManager apiArtifactManager = APIUtil.getArtifactManager(registry, APIConstants.API_KEY); GenericArtifactManager docArtifactManager = APIUtil.getArtifactManager(registry, APIConstants.DOCUMENTATION_KEY); String paginationLimit = getAPIManagerConfiguration() .getFirstProperty(APIConstants.API_STORE_APIS_PER_PAGE); // If the Config exists use it to set the pagination limit final int maxPaginationLimit; if (paginationLimit != null) { // The additional 1 added to the maxPaginationLimit is to help us determine if more // APIs may exist so that we know that we are unable to determine the actual total // API count. We will subtract this 1 later on so that it does not interfere with // the logic of the rest of the application int pagination = Integer.parseInt(paginationLimit); // Because the store jaggery pagination logic is 10 results per a page we need to set pagination // limit to at least 11 or the pagination done at this level will conflict with the store pagination // leading to some of the APIs not being displayed if (pagination < 11) { pagination = 11; log.warn( "Value of '" + APIConstants.API_STORE_APIS_PER_PAGE + "' is too low, defaulting to 11"); } maxPaginationLimit = start + pagination + 1; } // Else if the config is not specified we go with default functionality and load all else { maxPaginationLimit = Integer.MAX_VALUE; } PaginationContext.init(start, end, "ASC", APIConstants.API_OVERVIEW_NAME, maxPaginationLimit); if (tenantId == -1) { tenantId = MultitenantConstants.SUPER_TENANT_ID; } UserRegistry systemUserRegistry = ServiceReferenceHolder.getInstance().getRegistryService() .getRegistry(CarbonConstants.REGISTRY_SYSTEM_USERNAME, tenantId); ContentBasedSearchService contentBasedSearchService = new ContentBasedSearchService(); String newSearchQuery = getSearchQuery(searchQuery); String[] searchQueries = newSearchQuery.split("&"); String apiState = ""; String publisherRoles = ""; Map<String, String> attributes = new HashMap<String, String>(); for (String searchCriterea : searchQueries) { String[] keyVal = searchCriterea.split("="); if (APIConstants.STORE_VIEW_ROLES.equals(keyVal[0])) { attributes.put("propertyName", keyVal[0]); attributes.put("rightPropertyValue", keyVal[1]); attributes.put("rightOp", "eq"); } else if (APIConstants.PUBLISHER_ROLES.equals(keyVal[0])) { publisherRoles = keyVal[1]; } else { if (APIConstants.LCSTATE_SEARCH_KEY.equals(keyVal[0])) { apiState = keyVal[1]; continue; } attributes.put(keyVal[0], keyVal[1]); } } //check whether the new document indexer is engaged RegistryConfigLoader registryConfig = RegistryConfigLoader.getInstance(); Map<String, Indexer> indexerMap = registryConfig.getIndexerMap(); Indexer documentIndexer = indexerMap.get(APIConstants.DOCUMENT_MEDIA_TYPE_KEY); String complexAttribute; if (documentIndexer != null && documentIndexer instanceof DocumentIndexer) { //field check on document_indexed was added to prevent unindexed(by new DocumentIndexer) from coming up as search results //on indexed documents this property is always set to true complexAttribute = ClientUtils.escapeQueryChars(APIConstants.API_RXT_MEDIA_TYPE) + " OR mediaType_s:(" + ClientUtils.escapeQueryChars(APIConstants.DOCUMENT_RXT_MEDIA_TYPE) + " AND document_indexed_s:true)"; //construct query such that publisher roles is checked in properties for api artifacts and in fields for document artifacts //this was designed this way so that content search can be fully functional if registry is re-indexed after engaging DocumentIndexer if (!StringUtils.isEmpty(publisherRoles)) { complexAttribute = "(" + ClientUtils.escapeQueryChars(APIConstants.API_RXT_MEDIA_TYPE) + " AND publisher_roles_ss:" + publisherRoles + ") OR mediaType_s:(" + ClientUtils.escapeQueryChars(APIConstants.DOCUMENT_RXT_MEDIA_TYPE) + " AND publisher_roles_s:" + publisherRoles + ")"; } } else { //document indexer required for document content search is not engaged, therefore carry out the search only for api artifact contents complexAttribute = ClientUtils.escapeQueryChars(APIConstants.API_RXT_MEDIA_TYPE); if (!StringUtils.isEmpty(publisherRoles)) { complexAttribute = "(" + ClientUtils.escapeQueryChars(APIConstants.API_RXT_MEDIA_TYPE) + " AND publisher_roles_ss:" + publisherRoles + ")"; } } attributes.put(APIConstants.DOCUMENTATION_SEARCH_MEDIA_TYPE_FIELD, complexAttribute); attributes.put(APIConstants.API_OVERVIEW_STATUS, apiState); SearchResultsBean resultsBean = contentBasedSearchService.searchByAttribute(attributes, systemUserRegistry); String errorMsg = resultsBean.getErrorMessage(); if (errorMsg != null) { handleException(errorMsg); } ResourceData[] resourceData = resultsBean.getResourceDataList(); if (resourceData == null || resourceData.length == 0) { result.put("apis", compoundResult); result.put("length", 0); result.put("isMore", isMore); } totalLength = PaginationContext.getInstance().getLength(); // Check to see if we can speculate that there are more APIs to be loaded if (maxPaginationLimit == totalLength) { isMore = true; // More APIs exist, cannot determine total API count without incurring perf hit --totalLength; // Remove the additional 1 added earlier when setting max pagination limit } for (ResourceData data : resourceData) { String resourcePath = data.getResourcePath(); int index = resourcePath.indexOf(APIConstants.APIMGT_REGISTRY_LOCATION); resourcePath = resourcePath.substring(index); Resource resource = registry.get(resourcePath); if (APIConstants.DOCUMENT_RXT_MEDIA_TYPE.equals(resource.getMediaType())) { Resource docResource = registry.get(resourcePath); String docArtifactId = docResource.getUUID(); GenericArtifact docArtifact = docArtifactManager.getGenericArtifact(docArtifactId); Documentation doc = APIUtil.getDocumentation(docArtifact); Association[] docAssociations = registry.getAssociations(resourcePath, APIConstants.DOCUMENTATION_ASSOCIATION); API associatedAPI = null; if (docAssociations.length > 0) { // a content can have one api association at most String apiPath = docAssociations[0].getSourcePath(); Resource apiResource = registry.get(apiPath); String apiArtifactId = apiResource.getUUID(); if (apiArtifactId != null) { GenericArtifact apiArtifact = apiArtifactManager.getGenericArtifact(apiArtifactId); associatedAPI = APIUtil.getAPI(apiArtifact, registry); } else { throw new GovernanceException("artifact id is null for " + apiPath); } if (associatedAPI != null && doc != null) { docMap.put(doc, associatedAPI); } } } else { String apiArtifactId = resource.getUUID(); API api; if (apiArtifactId != null) { GenericArtifact apiArtifact = apiArtifactManager.getGenericArtifact(apiArtifactId); api = APIUtil.getAPI(apiArtifact, registry); apiSet.add(api); } else { throw new GovernanceException("artifact id is null for " + resourcePath); } } } compoundResult.addAll(apiSet); compoundResult.addAll(docMap.entrySet()); compoundResult.sort(new ContentSearchResultNameComparator()); } catch (RegistryException e) { handleException("Failed to search APIs by content", e); } catch (IndexerException e) { handleException("Failed to search APIs by content", e); } result.put("apis", compoundResult); result.put("length", totalLength); result.put("isMore", isMore); return result; }
From source file:org.wso2.carbon.apimgt.impl.APIConsumerImpl.java
License:Open Source License
/** * To get the query to retrieve user role list query based on current role list. * * @return the query with user role list. * @throws APIManagementException API Management Exception. *///from w w w . j a va 2 s .c o m private String getUserRoleListQuery() throws APIManagementException { StringBuilder rolesQuery = new StringBuilder(); rolesQuery.append('('); rolesQuery.append(APIConstants.NULL_USER_ROLE_LIST); String[] userRoles = APIUtil .getListOfRoles((userNameWithoutChange != null) ? userNameWithoutChange : username); if (userRoles != null) { for (String userRole : userRoles) { rolesQuery.append(" OR "); rolesQuery.append(ClientUtils.escapeQueryChars(APIUtil.sanitizeUserRole(userRole.toLowerCase()))); } } rolesQuery.append(")"); if (log.isDebugEnabled()) { log.debug("User role list solr query " + APIConstants.STORE_VIEW_ROLES + "=" + rolesQuery.toString()); } return APIConstants.STORE_VIEW_ROLES + "=" + rolesQuery.toString(); }