List of usage examples for org.apache.solr.request SolrQueryRequest getParams
SolrParams getParams();
From source file:alba.components.FilteredShowFileRequestHandler.java
License:Apache License
private void showFromZooKeeper(SolrQueryRequest req, SolrQueryResponse rsp, CoreContainer coreContainer) throws KeeperException, InterruptedException, UnsupportedEncodingException { SolrZkClient zkClient = coreContainer.getZkController().getZkClient(); String adminFile = getAdminFileFromZooKeeper(req, rsp, zkClient, hiddenFiles); if (adminFile == null) { return;//from w w w. ja v a 2 s .c om } // Show a directory listing List<String> children = zkClient.getChildren(adminFile, null, true); if (children.size() > 0) { NamedList<SimpleOrderedMap<Object>> files = new SimpleOrderedMap<>(); for (String f : children) { if (isHiddenFile(req, rsp, f, false, hiddenFiles)) { continue; } SimpleOrderedMap<Object> fileInfo = new SimpleOrderedMap<>(); files.add(f, fileInfo); List<String> fchildren = zkClient.getChildren(adminFile + "/" + f, null, true); if (fchildren.size() > 0) { fileInfo.add("directory", true); } else { // TODO? content type fileInfo.add("size", f.length()); } // TODO: ? // fileInfo.add( "modified", new Date( f.lastModified() ) ); } rsp.add("files", files); } else { // Include the file contents // The file logic depends on RawResponseWriter, so force its use. ModifiableSolrParams params = new ModifiableSolrParams(req.getParams()); params.set(CommonParams.WT, "raw"); req.setParams(params); ContentStreamBase content = new ContentStreamBase.ByteArrayStream( zkClient.getData(adminFile, null, null, true), adminFile); content.setContentType(req.getParams().get(USE_CONTENT_TYPE)); // Velocity parsing here! // http://velocity.apache.org/engine/devel/developer-guide.html#The_Context Velocity.init(); VelocityContext context = new VelocityContext(); //add some vars?? //context.put( "context", new String("Velocity") ); for (int i = 0; i < rsp.getValues().size(); i++) { context.put(rsp.getValues().getName(i), rsp.getValues().getVal(i)); } Template template = null; String fname = req.getParams().get("file", null); try { //TODO what if fname is null? template = Velocity.getTemplate(fname); } catch (ResourceNotFoundException rnfe) { // couldn't find the template, try to load it // TODO it should be fired only for SOME mimetypes (..through an annotation??) StringBuilder sb = this.getTemplate(content); RuntimeServices runtimeServices = RuntimeSingleton.getRuntimeServices(); StringReader reader = new StringReader(sb.toString()); SimpleNode node = null; try { node = runtimeServices.parse(reader, fname); } catch (ParseException e) { // TODO Auto-generated catch block logger.error("error while parsing new template", e); } template = new Template(); template.setRuntimeServices(runtimeServices); if (node != null) { template.setData(node); } else { logger.error("node null, can't set on template"); } template.initDocument(); } catch (ParseErrorException pee) { // syntax error: problem parsing the template logger.error("error while parsing template: ", pee); } catch (MethodInvocationException mie) { // something invoked in the template // threw an exception logger.error("error while parsing temaplate: ", mie); } catch (Exception e) { logger.error("error while parsing temaplate: ", e); } StringWriter sw = new StringWriter(); template.merge(context, sw); // http://stackoverflow.com/questions/18571223/how-to-convert-java-string-into-byte content = new ContentStreamBase.ByteArrayStream( sw.getBuffer().toString().getBytes(Charset.forName("UTF-8")), adminFile); content.setContentType(req.getParams().get(USE_CONTENT_TYPE)); rsp.add(RawResponseWriter.CONTENT, content); } rsp.setHttpCaching(false); }
From source file:alba.components.FilteredShowFileRequestHandler.java
License:Apache License
private void showFromFileSystem(SolrQueryRequest req, SolrQueryResponse rsp) { File adminFile = getAdminFileFromFileSystem(req, rsp, hiddenFiles); if (adminFile == null) { // exception already recorded return;//from w w w . j a v a 2s.c om } // Make sure the file exists, is readable and is not a hidden file if (!adminFile.exists()) { log.error("Can not find: " + adminFile.getName() + " [" + adminFile.getAbsolutePath() + "]"); rsp.setException(new SolrException(ErrorCode.NOT_FOUND, "Can not find: " + adminFile.getName() + " [" + adminFile.getAbsolutePath() + "]")); return; } if (!adminFile.canRead() || adminFile.isHidden()) { log.error("Can not show: " + adminFile.getName() + " [" + adminFile.getAbsolutePath() + "]"); rsp.setException(new SolrException(ErrorCode.NOT_FOUND, "Can not show: " + adminFile.getName() + " [" + adminFile.getAbsolutePath() + "]")); return; } // Show a directory listing if (adminFile.isDirectory()) { // it's really a directory, just go for it. int basePath = adminFile.getAbsolutePath().length() + 1; NamedList<SimpleOrderedMap<Object>> files = new SimpleOrderedMap<>(); for (File f : adminFile.listFiles()) { String path = f.getAbsolutePath().substring(basePath); path = path.replace('\\', '/'); // normalize slashes if (isHiddenFile(req, rsp, f.getName().replace('\\', '/'), false, hiddenFiles)) { continue; } SimpleOrderedMap<Object> fileInfo = new SimpleOrderedMap<>(); files.add(path, fileInfo); if (f.isDirectory()) { fileInfo.add("directory", true); } else { // TODO? content type fileInfo.add("size", f.length()); } fileInfo.add("modified", new Date(f.lastModified())); } rsp.add("files", files); } else { // Include the file contents //The file logic depends on RawResponseWriter, so force its use. ModifiableSolrParams params = new ModifiableSolrParams(req.getParams()); params.set(CommonParams.WT, "raw"); req.setParams(params); ContentStreamBase content = new ContentStreamBase.FileStream(adminFile); content.setContentType(req.getParams().get(USE_CONTENT_TYPE)); rsp.add(RawResponseWriter.CONTENT, content); } rsp.setHttpCaching(false); }
From source file:alba.components.FilteredShowFileRequestHandler.java
License:Apache License
public static String getAdminFileFromZooKeeper(SolrQueryRequest req, SolrQueryResponse rsp, SolrZkClient zkClient, Set<String> hiddenFiles) throws KeeperException, InterruptedException { String adminFile = null;/*from ww w. ja va2 s .c o m*/ SolrCore core = req.getCore(); final ZkSolrResourceLoader loader = (ZkSolrResourceLoader) core.getResourceLoader(); String confPath = loader.getConfigSetZkPath(); String fname = req.getParams().get("file", null); if (fname == null) { adminFile = confPath; } else { fname = fname.replace('\\', '/'); // normalize slashes if (isHiddenFile(req, rsp, fname, true, hiddenFiles)) { return null; } if (fname.startsWith("/")) { // Only files relative to conf are valid fname = fname.substring(1); } adminFile = confPath + "/" + fname; } // Make sure the file exists, is readable and is not a hidden file if (!zkClient.exists(adminFile, true)) { log.error("Can not find: " + adminFile); rsp.setException(new SolrException(SolrException.ErrorCode.NOT_FOUND, "Can not find: " + adminFile)); return null; } return adminFile; }
From source file:alba.components.FilteredShowFileRequestHandler.java
License:Apache License
public static File getAdminFileFromFileSystem(SolrQueryRequest req, SolrQueryResponse rsp, Set<String> hiddenFiles) { File adminFile = null;//from w w w .j a v a2 s.co m final SolrResourceLoader loader = req.getCore().getResourceLoader(); File configdir = new File(loader.getConfigDir()); if (!configdir.exists()) { // TODO: maybe we should just open it this way to start with? try { configdir = new File(loader.getClassLoader().getResource(loader.getConfigDir()).toURI()); } catch (URISyntaxException e) { log.error("Can not access configuration directory!"); rsp.setException(new SolrException(SolrException.ErrorCode.FORBIDDEN, "Can not access configuration directory!", e)); return null; } } String fname = req.getParams().get("file", null); if (fname == null) { adminFile = configdir; } else { fname = fname.replace('\\', '/'); // normalize slashes if (hiddenFiles.contains(fname.toUpperCase(Locale.ROOT))) { log.error("Can not access: " + fname); rsp.setException(new SolrException(SolrException.ErrorCode.FORBIDDEN, "Can not access: " + fname)); return null; } if (fname.indexOf("..") >= 0) { log.error("Invalid path: " + fname); rsp.setException(new SolrException(SolrException.ErrorCode.FORBIDDEN, "Invalid path: " + fname)); return null; } adminFile = new File(configdir, fname); } return adminFile; }
From source file:alba.solr.searchcomponents.AlbaRequestHandler.java
License:Apache License
public void handleRequestBody(SolrQueryRequest req, SolrQueryResponse rsp) throws Exception { // TODO Auto-generated method stub /* List<SearchComponent> components = new ArrayList<SearchComponent>(); /*from ww w . j a v a 2 s .c o m*/ MySearchComponent msc = new MySearchComponent(); ResponseBuilder rb = new ResponseBuilder(req, rsp, components); msc.process(rb);*/ //rsp.add("hello", rb.rsp.getValues()); req.getContext().put(Loader.FUNCTIONS, functions); Object params[] = new Object[2]; params[0] = req; params[1] = rsp; // what if this method calls rsp.add( .. ) ???? Object result = this.function.getMethod().invoke(this.function.getInstance(), params); if (Map.class.isAssignableFrom(result.getClass())) { // if we got a Map, just return it as-is rsp.add(this.sectionName, result); } else // if we got anything else, try to serialize it! if (List.class.isAssignableFrom(result.getClass())) { for (Object o : (List) result) { DocumentObjectBinder dob = new DocumentObjectBinder(); SolrInputDocument sd = dob.toSolrInputDocument(o); SolrDocument dest = ClientUtils.toSolrDocument(sd); HashMap<Object, Object> nl = (HashMap<Object, Object>) dest.get("functionDescriptor"); //rsp.add(nl.get("name").toString(), dest2); rsp.add(null, dest); } } if (StaticResource.class.isAssignableFrom(result.getClass())) { FilteredShowFileRequestHandler file = new FilteredShowFileRequestHandler(); file.init(new NamedList()); //to initialize internal variables - but in this way it will NOT get the proper configuration from SolrConfig! ModifiableSolrParams solrParams = new ModifiableSolrParams(req.getParams()); StaticResource resource = ((StaticResource) result); solrParams.set("file", resource.getName()); //TODO Proper mapping here!! //solrParams.set("contentType", "text/xml;charset=utf-8"); solrParams.set("contentType", resource.getContentType()); req.setParams(solrParams); file.handleRequest(req, rsp); // logger.error("returning the content of " + ); } else { // unable to do any kind of serialization.. just add the result and let the ResponseWriter handle it rsp.add(null, result); } }
From source file:at.newmedialab.lmf.util.solr.SuggestionRequestHandler.java
License:Apache License
@Override public void handleRequestBody(SolrQueryRequest req, SolrQueryResponse rsp) throws Exception { SolrParams params = req.getParams(); if (params.getBool(SuggestionRequestParams.SUGGESTION, SUGGESTION)) { String q = params.get(CommonParams.Q); if (q == null) { rsp.add("error", error(400, "SuggestionRequest needs to have a 'q' parameter")); return; }// w w w . ja va 2 s . com String[] fields = params.getParams(SuggestionRequestParams.SUGGESTION_FIELD) != null ? params.getParams(SuggestionRequestParams.SUGGESTION_FIELD) : FIELDS; if (fields == null) { rsp.add("error", error(400, "SuggestionRequest needs to have at least one 'suggestion.field' parameter")); return; } String df = params.get(SuggestionRequestParams.SUGGESTION_DF, DF); if (df == null) { rsp.add("error", error(400, "SuggestionRequest needs to have a 'df' parameter")); return; } int limit = params.getInt(SuggestionRequestParams.SUGGESTION_LIMIT, LIMIT); if (limit < 1) { rsp.add("error", error(400, "SuggestionRequest needs to have a 'suggestion.limit' greater than 0")); return; } String[] fqs = params.getParams(CommonParams.FQ) != null ? params.getParams(CommonParams.FQ) : FQS; Boolean multivalue = params.getBool(SuggestionRequestParams.SUGGESTION_MULTIVALUE, MULTIVALUE); //TODO replace if (multivalue) { rsp.add("error", error(500, "Multivalue suggestions are not yet supported!")); return; } suggestionService.run(rsp, q, df, fields, fqs, limit, multivalue); } else { super.handleRequestBody(req, rsp); } }
From source file:com.adr.bigdata.search.product.cm.CmBackendQuery.java
public CmBackendQuery(SolrQueryRequest req) { SolrParams param = req.getParams(); this.keyword = param.get(Params.KEYWORD); this.start = param.get(Params.START, "0"); this.rows = param.get(Params.ROWS, "10"); this.sort = param.get(Params.SORT); this.dateFromTo = param.get(Params.DATE_FROM_TO); this.categoryIds = param.getParams(Params.CATEGORY_ID); this.warehouseIds = param.getParams(Params.WAREHOUSE_ID); this.warehouseProductItemMappingId = param.getParams(Params.WAREHOUSE_PRODUCT_ITEM_MAPPING_ID); this.merchantIds = param.getParams(Params.MERCHANT_ID); this.brandIds = param.getParams(Params.BRAND_ID); this.productItemIds = param.getParams(Params.PRODUCT_ITEM_ID); this.visible = param.get(Params.VISIBLE); this.productItemStatuses = param.getParams(Params.PRODUCT_ITEM_STATUS); this.merchantProductItemStatuses = param.getParams(Params.MERCHANT_PRODUCT_ITEM_STATUS); this.productItemTypes = param.getParams(Params.PRODUCT_ITEM_TYPE); this.inStock = param.get(Params.IN_STOCK); this.approved = param.get(Params.APPROVED); this.safetyStock = param.get(Params.SAFETY_STOCK); this.isoriginal = param.get(Params.IS_ORIGINAL); }
From source file:com.adr.bigdata.search.product.fe.BaseSuggestionHandler.java
@Override public void handleRequest(SolrQueryRequest req, SolrQueryResponse rsp) { String cacheKey = SolrParamUtils.transform(req.getParams()); try {//w w w . java 2 s.c o m String cacheResponse = this.rdModel.get(cacheKey); if (cacheResponse == null) { //cacheMiss try { final Object[] terms = new Object[1]; suggestionLogic.execute(req, rsp, new Callable() { @Override public void call(Object... args) { terms[0] = args[0]; } }); SolrQueryResponse _rsp = new SolrQueryResponse(); super.handleRequest(req, _rsp); suggestionLogic.writeRsp(req, _rsp, terms[0]); String result = outerString(req, _rsp); if (!zeroResult(_rsp)) { this.rdModel.put(cacheKey, result); } ModifiableSolrParams params = new ModifiableSolrParams(req.getParams()); params.set("vincache", true); SolrParams _params = SolrParams.wrapDefaults(params, defaults); _params = SolrParams.wrapAppended(_params, appends); req.setParams(_params); rsp.add("vincache", result); } catch (Exception e) { error(req, rsp); getLogger().error("", e); } } else { //cache hit ModifiableSolrParams solrParam = new ModifiableSolrParams(req.getParams()); solrParam.set("vincache", true); SolrParams params = SolrParams.wrapAppended(solrParam, appends); params = SolrParams.wrapDefaults(params, defaults); req.setParams(params); rsp.add("vincache", cacheResponse); } } catch (Exception cacheEx) { getLogger().error("fail to get from redis cache.....{}", cacheEx.getMessage()); try { final Object[] terms = new Object[1]; suggestionLogic.execute(req, rsp, new Callable() { @Override public void call(Object... args) { terms[0] = args[0]; } }); super.handleRequest(req, rsp); suggestionLogic.writeRsp(req, rsp, terms[0]); } catch (Exception e) { error(req, rsp); getLogger().error("", e); } } }
From source file:com.basho.yokozuna.handler.EntropyData.java
License:Open Source License
@Override public void handleRequestBody(SolrQueryRequest req, SolrQueryResponse rsp) throws Exception, InstantiationException, IllegalAccessException { String contParam = req.getParams().get("continue"); BytesRef cont = contParam != null ? decodeCont(contParam) : DEFAULT_CONT; // TODO: Make before required in handler config String before = req.getParams().get("before"); if (before == null) { throw new Exception("Parameter 'before' is required"); }/*from ww w . ja v a 2 s.c o m*/ int n = req.getParams().getInt("n", DEFAULT_N); SolrDocumentList docs = new SolrDocumentList(); // Add docs here and modify object inline in code rsp.add("response", docs); try { SolrIndexSearcher searcher = req.getSearcher(); AtomicReader rdr = searcher.getAtomicReader(); BytesRef tmp = null; Terms terms = rdr.terms(ENTROPY_DATA_FIELD); TermsEnum te = terms.iterator(null); if (isContinue(cont)) { log.debug("continue from " + cont); TermsEnum.SeekStatus status = te.seekCeil(cont, true); if (status == TermsEnum.SeekStatus.END) { rsp.add("more", false); return; } else if (status == TermsEnum.SeekStatus.FOUND) { // If this term has already been seen then skip it. tmp = te.next(); if (endOfItr(tmp)) { rsp.add("more", false); return; } } else if (status == TermsEnum.SeekStatus.NOT_FOUND) { tmp = te.next(); } } else { tmp = te.next(); } String text = null; String[] vals = null; String ts = null; String docId = null; String vectorClock = null; int count = 0; BytesRef current = null; while (!endOfItr(tmp) && count < n) { current = BytesRef.deepCopyOf(tmp); text = tmp.utf8ToString(); log.debug("text: " + text); vals = text.split(" "); ts = vals[0]; // TODO: what if null? if (!(ts.compareTo(before) < 0)) { rsp.add("more", false); docs.setNumFound(count); return; } docId = vals[1]; vectorClock = vals[2]; SolrDocument tmpDoc = new SolrDocument(); tmpDoc.addField("doc_id", docId); tmpDoc.addField("base64_vclock", Base64.encodeBase64String(sha(vectorClock))); docs.add(tmpDoc); count++; tmp = te.next(); } if (count < n) { rsp.add("more", false); } else { rsp.add("more", true); String newCont = Base64.encodeBase64URLSafeString(current.bytes); // The continue context for next req to start where // this one finished. rsp.add("continuation", newCont); } docs.setNumFound(count); } catch (Exception e) { e.printStackTrace(); } }
From source file:com.doculibre.constellio.solr.handler.component.ConstellioAuthorizationComponent.java
License:Open Source License
@SuppressWarnings("unchecked") @Override// w w w. jav a 2 s .co m public void prepare(ResponseBuilder rb) throws IOException { SolrQueryRequest req = rb.req; SolrIndexSearcher searcher = req.getSearcher(); //IndexReader reader = req.getSearcher().getReader(); SolrParams params = req.getParams(); // A runtime param can skip if (!params.getBool(ENABLE, true)) { return; } Query query = rb.getQuery(); String qstr = rb.getQueryString(); if (query == null || qstr == null) { return; } ConstellioUser user; String userIdStr = params.get(ConstellioSolrQueryParams.USER_ID); if (userIdStr != null) { UserServices userServices = ConstellioSpringUtils.getUserServices(); try { user = userServices.get(new Long(userIdStr)); } catch (NumberFormatException e) { user = null; } } else { user = null; } String collectionName = params.get(ConstellioSolrQueryParams.COLLECTION_NAME); RecordCollectionServices collectionServices = ConstellioSpringUtils.getRecordCollectionServices(); FederationServices federationServices = ConstellioSpringUtils.getFederationServices(); RecordCollection collection = collectionServices.get(collectionName); List<TermQuery> restrictedCollectionQueries = new ArrayList<TermQuery>(); if (collection.isFederationOwner()) { List<RecordCollection> includedCollections = federationServices.listIncludedCollections(collection); for (RecordCollection includedCollection : includedCollections) { if (includedCollection.hasSearchPermission() && (user == null || !user.hasSearchPermission(includedCollection))) { restrictedCollectionQueries.add(new TermQuery( new Term(IndexField.COLLECTION_ID_FIELD, "" + includedCollection.getId()))); } } } // User must be logged in to see private records if (user != null) { String luceneQueryStr = params.get(ConstellioSolrQueryParams.LUCENE_QUERY); if (StringUtils.isBlank(luceneQueryStr)) { return; } IndexSchema schema = req.getSchema(); SolrQueryParser queryParser = new SolrQueryParser(rb.getQparser(), IndexField.DEFAULT_SEARCH_FIELD); Query luceneQuery; try { luceneQuery = queryParser.parse(luceneQueryStr); } catch (SyntaxError e) { log.error("Error parsing lucene query " + luceneQueryStr, e); return; } // Create a new query which will only include private records BooleanQuery privateRecordQuery = new BooleanQuery(true); privateRecordQuery.add(luceneQuery, BooleanClause.Occur.MUST); for (TermQuery restrictionCollectionQuery : restrictedCollectionQueries) { privateRecordQuery.add(restrictionCollectionQuery, BooleanClause.Occur.MUST_NOT); } TermQuery privateRecordTQ = new TermQuery(new Term(IndexField.PUBLIC_RECORD_FIELD, "F")); privateRecordQuery.add(privateRecordTQ, BooleanClause.Occur.MUST); DocSet privateRecordIdDocSet = searcher.getDocSet(privateRecordQuery); if (privateRecordIdDocSet.size() > 0) { RecordServices recordServices = ConstellioSpringUtils.getRecordServices(); ACLServices aclServices = ConstellioSpringUtils.getACLServices(); ConnectorManagerServices connectorManagerServices = ConstellioSpringUtils .getConnectorManagerServices(); List<Record> privateRecords = new ArrayList<Record>(); DocIterator docIt = privateRecordIdDocSet.iterator(); while (docIt.hasNext()) { int docId = docIt.nextDoc(); Document luceneDoc = searcher.doc(docId); Long recordId = new Long(luceneDoc.get(IndexField.RECORD_ID_FIELD)); Record record = recordServices.get(recordId, collection); privateRecords.add(record); } // First pass : Remove ACL authorized records List<Record> unevaluatedPrivateRecords = aclServices.removeAuthorizedRecords(privateRecords, user); if (!unevaluatedPrivateRecords.isEmpty()) { Set<UserCredentials> userCredentials = user.getUserCredentials(); // Second pass : Ask the connector manager ConnectorManager connectorManager = connectorManagerServices.getDefaultConnectorManager(); List<Record> authorizedRecords = connectorManagerServices .authorizeByConnector(unevaluatedPrivateRecords, userCredentials, connectorManager); List<Record> unauthorizedRecords = ListUtils.removeAll(unevaluatedPrivateRecords, authorizedRecords); if (!unauthorizedRecords.isEmpty()) { // Create a new query which will exclude unauthorized records BooleanQuery authorizedRecordQuery = new BooleanQuery(true); authorizedRecordQuery.add(query, BooleanClause.Occur.MUST); for (Record unauthorizedRecord : unauthorizedRecords) { TermQuery unauthorizedRecordTQ = new TermQuery( new Term(IndexField.RECORD_ID_FIELD, "" + unauthorizedRecord.getId())); authorizedRecordQuery.add(unauthorizedRecordTQ, BooleanClause.Occur.MUST_NOT); } rb.setQuery(authorizedRecordQuery); } } } } else { BooleanQuery publicRecordQuery = new BooleanQuery(true); publicRecordQuery.add(query, BooleanClause.Occur.MUST); TermQuery publicRecordTQ = new TermQuery(new Term(IndexField.PUBLIC_RECORD_FIELD, "T")); publicRecordQuery.add(publicRecordTQ, BooleanClause.Occur.MUST); for (TermQuery restrictionCollectionQuery : restrictedCollectionQueries) { publicRecordQuery.add(restrictionCollectionQuery, BooleanClause.Occur.MUST_NOT); } rb.setQuery(publicRecordQuery); } }