List of usage examples for org.apache.solr.common SolrException SolrException
public SolrException(ErrorCode code, Throwable th)
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;/* w ww . jav a 2 s .co m*/ } // 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 boolean isHiddenFile(SolrQueryRequest req, SolrQueryResponse rsp, String fnameIn, boolean reportError, Set<String> hiddenFiles) { String fname = fnameIn.toUpperCase(Locale.ROOT); if (hiddenFiles.contains(fname) || hiddenFiles.contains("*")) { if (reportError) { log.error("Cannot access " + fname); rsp.setException(/* www.j a v a 2s. co m*/ new SolrException(SolrException.ErrorCode.FORBIDDEN, "Can not access: " + fnameIn)); } return true; } // This is slightly off, a valid path is something like ./schema.xml. I don't think it's worth the effort though // to fix it to handle all possibilities though. if (fname.indexOf("..") >= 0 || fname.startsWith(".")) { if (reportError) { log.error("Invalid path: " + fname); rsp.setException(new SolrException(SolrException.ErrorCode.FORBIDDEN, "Invalid path: " + fnameIn)); } return true; } // Make sure that if the schema is managed, we don't allow editing. Don't really want to put // this in the init since we're not entirely sure when the managed schema will get initialized relative to this // handler. SolrCore core = req.getCore(); IndexSchema schema = core.getLatestSchema(); if (schema instanceof ManagedIndexSchema) { String managed = schema.getResourceName(); if (fname.equalsIgnoreCase(managed)) { return true; } } return 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;/* w ww . j a v a 2s . c om*/ 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:at.newmedialab.lmf.util.solr.suggestion.service.SuggestionService.java
License:Apache License
private SolrQueryResponse query(String query, String df, String[] fields, String[] fqs) { SolrQueryResponse rsp = new SolrQueryResponse(); //append *//w w w . j a v a2 s . co m if (!query.endsWith("*")) { query = query.trim() + "*"; } //Prepare query ModifiableSolrParams params = new ModifiableSolrParams(); SolrQueryRequest req = new LocalSolrQueryRequest(solrCore, params); params.add(CommonParams.Q, query.toLowerCase()); params.add(CommonParams.DF, df); params.add("q.op", "AND"); params.add(FacetParams.FACET, "true"); params.add(FacetParams.FACET_LIMIT, internalFacetLimit); params.add(FacetParams.FACET_MINCOUNT, "1"); for (String field : fields) { params.add(FacetParams.FACET_FIELD, field); } if (fqs != null) { for (String fq : fqs) { params.add(CommonParams.FQ, fq); } } if (spellcheck_enabled) { params.add("spellcheck", "true"); params.add("spellcheck.collate", "true"); } try { //execute query and return searchHandler.handleRequestBody(req, rsp); return rsp; } catch (SolrException se) { throw se; } catch (Exception e) { e.printStackTrace(); throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, "internal server error"); } finally { req.close(); } }
From source file:cn.howardliu.lucene.extension.ManagedSynonymFilterFactory.java
License:Apache License
/** * Called once, during core initialization, to initialize any analysis components * that depend on the data managed by this resource. It is important that the * analysis component is only initialized once during core initialization so that * text analysis is consistent, especially in a distributed environment, as we * don't want one server applying a different set of stop words than other servers. *//*from w w w . j a va2 s . c o m*/ @SuppressWarnings("unchecked") @Override public void onManagedResourceInitialized(NamedList<?> initArgs, final ManagedResource res) throws SolrException { NamedList<Object> args = (NamedList<Object>) initArgs; args.add("synonyms", getResourceId()); args.add("expand", "false"); args.add("format", "solr"); Map<String, String> filtArgs = new HashMap<>(); for (Map.Entry<String, ?> entry : args) { filtArgs.put(entry.getKey(), entry.getValue().toString()); } // create the actual filter factory that pulls the synonym mappings // from synonymMappings using a custom parser implementation delegate = new SynonymFilterFactory(filtArgs) { @Override protected SynonymMap loadSynonyms(ResourceLoader loader, String cname, boolean dedup, Analyzer analyzer) throws IOException, ParseException { CharsetDecoder decoder = StandardCharsets.UTF_8.newDecoder() .onMalformedInput(CodingErrorAction.REPORT).onUnmappableCharacter(CodingErrorAction.REPORT); ManagedSynonymParser parser = new ManagedSynonymParser((SynonymManager) res, dedup, analyzer); // ??? InputStreamReader in = null; if (StringUtils.isNotBlank(synonymFile)) { in = new InputStreamReader(loader.openResource(synonymFile), decoder); } parser.parse(in); return parser.build(); } }; try { delegate.inform(res.getResourceLoader()); } catch (IOException e) { throw new SolrException(ErrorCode.SERVER_ERROR, e); } }
From source file:com.billiger.solr.handler.component.QLTBComponent.java
License:Apache License
/** * Inform component of core reload.//from w ww .ja va2s .com * * This will both set the analyzer according to the configured * queryFieldType, and load the QLTB data. Data source can be (in this * order) ZooKeeper, the conf/ directory or the data/ directory. */ @Override public final void inform(final SolrCore core) { // load analyzer String queryFieldType = initArgs.get(FIELD_TYPE); if (queryFieldType != null) { FieldType ft = core.getLatestSchema().getFieldTypes().get(queryFieldType); if (ft == null) { throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, "unknown FieldType \"" + queryFieldType + "\" used in QLTBComponent"); } analyzer = ft.getQueryAnalyzer(); } else { analyzer = null; } synchronized (qltbCache) { qltbCache.clear(); try { // retrieve QLTB data filename String qltbFile = initArgs.get(QLTB_FILE); if (qltbFile == null) { throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, "QLTBComponent must specify argument: \"" + QLTB_FILE + "\" - path to QLTB data"); } boolean exists = false; // check ZooKeeper ZkController zkController = core.getCoreDescriptor().getCoreContainer().getZkController(); if (zkController != null) { exists = zkController.configFileExists(zkController.readConfigName( core.getCoreDescriptor().getCloudDescriptor().getCollectionName()), qltbFile); } else { // no ZooKeeper, check conf/ and data/ directories File fConf = new File(core.getResourceLoader().getConfigDir(), qltbFile); File fData = new File(core.getDataDir(), qltbFile); if (fConf.exists() == fData.exists()) { // both or neither exist throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, "QLTBComponent missing config file: \"" + qltbFile + "\": either " + fConf.getAbsolutePath() + " or " + fData.getAbsolutePath() + " must exist, but not both"); } if (fConf.exists()) { // conf/ found, load it exists = true; log.info("QLTB source conf/: " + fConf.getAbsolutePath()); Config cfg = new Config(core.getResourceLoader(), qltbFile); qltbCache.put(null, loadQLTBMap(cfg, core)); } } if (!exists) { // Neither ZooKeeper nor conf/, so must be in data/ // We need an IndexReader and the normal RefCounted<SolrIndexSearcher> searcher = null; try { searcher = core.getNewestSearcher(false); IndexReader reader = searcher.get().getIndexReader(); getQLTBMap(reader, core); } finally { if (searcher != null) { searcher.decref(); } } } } catch (Exception ex) { throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, "Error initializing QltbComponent.", ex); } } }
From source file:com.billiger.solr.handler.component.QLTBComponent.java
License:Apache License
/** * Get QLTB map for the given IndexReader. * * If the QLTB map is located in the conf/ directory, it is independent * of the IndexReader and reloaded only during a core reload. * If, however, QLTB data is read from ZooKeeper or the data/ directory, * it is reloaded for each new IndexReader via the core's resource loader. * * @return QLTB map for the given IndexReader. *///from w w w .j a v a 2 s.c om private Map<String, List<Query>> getQLTBMap(final IndexReader reader, final SolrCore core) throws Exception { Map<String, List<Query>> map = null; synchronized (qltbCache) { map = qltbCache.get(null); // Magic "null" key for data from conf/ if (map != null) { // QLTB data from the conf/ directory, reader-independent. return map; } map = qltbCache.get(reader); if (map == null) { // No QLTB map for this reader yet, load it from ZooKeeper or // the data/ directory. log.info("load QLTB map for new IndexReader"); String qltbFile = initArgs.get(QLTB_FILE); if (qltbFile == null) { throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, "QLTBComponent must specify argument: " + QLTB_FILE); } Config cfg; ZkController zkController = core.getCoreDescriptor().getCoreContainer().getZkController(); if (zkController != null) { // We're running under ZooKeeper control... cfg = new Config(core.getResourceLoader(), qltbFile, null, null); } else { // No ZooKeeper, use data/ directory InputStream is = VersionedFile.getLatestFile(core.getDataDir(), qltbFile); cfg = new Config(core.getResourceLoader(), qltbFile, new InputSource(is), null); } map = loadQLTBMap(cfg, core); qltbCache.put(reader, map); } return map; } }
From source file:com.cominvent.solr.RequestSanitizerComponent.java
License:Apache License
/** * Parses the mapping parameters//ww w.j av a 2 s .co m * @param replacements a list of all sanitize http params to parse * @return a Map of parsed mappings that can be looked up */ protected Map<String, Map<String, String>> parseMappings(List<String> replacements) { Map<String, Map<String, String>> parsedMappings = new HashMap<>(); if (replacements == null || replacements.size() == 0) { return parsedMappings; } for (String replacement : replacements) { int offset = replacement.indexOf("="); if (offset == -1) { log.error("Parameter " + SANITIZE_PARAM + " must be on the format " + SANITIZE_PARAM + "=param=<value>"); throw new SolrException(ErrorCode.BAD_REQUEST, "Parameter " + SANITIZE_PARAM + " must be on the format " + SANITIZE_PARAM + "=param=<value>"); } String param = replacement.substring(0, offset); String val = replacement.substring(offset + 1); if (!val.contains(":")) { parsedMappings.put(param, Collections.singletonMap("invariant", val)); continue; } else { Map<String, String> keyVal = new HashMap<>(); String[] rules = val.split(" "); for (String rule : rules) { String[] kv = rule.split(":"); if (kv.length != 2) { throw new SolrException(ErrorCode.BAD_REQUEST, "Parameter " + SANITIZE_PARAM + " must be on the format " + SANITIZE_PARAM + "=param=from:to from:to default:val >num:num2"); } keyVal.put(kv[0], kv[1]); } parsedMappings.put(param, keyVal); } } return parsedMappings; }
From source file:com.cominvent.solr.RequestSanitizerComponent.java
License:Apache License
/** * Modify input params if mappings exist * @param origParams the set of input parameters for the request * @param mappings the mappings parsed from the config * @return a Map of override values which can be applied on top of the original params *///from w w w. ja v a 2s . c o m protected Map<String, String> getModifications(SolrParams origParams, Map<String, Map<String, String>> mappings) { Map<String, String> params = new HashMap<String, String>(); if (mappings == null) { throw new SolrException(ErrorCode.BAD_REQUEST, "mappings cannot be null"); } for (String parToReplace : mappings.keySet()) { Map<String, String> keyVal = mappings.get(parToReplace); if (keyVal.get("invariant") != null) { params.put(parToReplace, keyVal.get("invariant")); log.debug("Param " + parToReplace + " not given in request, setting to invariant: " + keyVal.get("invariant")); continue; } if (origParams.get(parToReplace) == null) { if (keyVal.get("default") != null) { params.put(parToReplace, keyVal.get("default")); log.debug("Param " + parToReplace + " not given in request, setting to default: " + keyVal.get("default")); } continue; } String origVal = origParams.get(parToReplace); for (String k : keyVal.keySet()) { if (k.startsWith(">") || k.startsWith("<")) { int trueCondition = k.startsWith(">") ? 1 : -1; Long cmp; try { cmp = Long.parseLong(k.substring(1)); } catch (NumberFormatException e) { log.error("Wrong format of replace rule for param " + parToReplace + ":" + k + ":" + keyVal.get(k)); throw new SolrException(ErrorCode.BAD_REQUEST, "Wrong format of replace rule for param " + parToReplace + ":" + k + ":" + keyVal.get(k)); } try { Long orig = Long.parseLong(origVal); if (orig.compareTo(cmp) == trueCondition || orig.compareTo(cmp) == 0) { log.debug("Param " + parToReplace + " hit rule " + k); params.put(parToReplace, keyVal.get(k)); break; } } catch (NumberFormatException e) { log.debug("Target value is not a number, ignoring max test"); } } else if (k.equals(origVal)) { log.debug("Replacing param " + parToReplace + "=" + origVal + "=>" + keyVal.get(k)); params.put(parToReplace, keyVal.get(k)); break; } } } return params; }
From source file:com.cominvent.solr.update.processor.MappingUpdateProcessor.java
License:Apache License
/** * The constructor initializes the processor by reading configuration * and loading a HashMap from the specified file name. *//*from w ww .java 2s . c o m*/ public MappingUpdateProcessor(SolrCore core, SolrParams params, SolrQueryRequest req, SolrQueryResponse rsp, UpdateRequestProcessor next) { super(next); this.core = core; // TODO: Add support for caseSensitive and expand parameters, support full synonyms.txt format if (params != null) { setEnabled(params.getBool("enabled", true)); inputField = params.get(INPUT_FIELD_PARAM, "").trim(); outputField = params.get(OUTPUT_FIELD_PARAM, "").trim(); docIdField = params.get(DOCID_PARAM, DOCID_FIELD_DEFAULT); fallbackValue = params.get(FALLBACK_VALUE_PARAM, null); mappingFile = params.get(MAPPING_FILE_PARAM, "").trim(); } if (inputField.length() == 0) { log.error("Missing or faulty configuration of MappingUpdateProcessor. Input field must be specified"); throw new SolrException(ErrorCode.NOT_FOUND, "Missing or faulty configuration of MappingUpdateProcessor. Input field must be specified"); } try { log.info("Attempting to initialize mapping file " + mappingFile); InputStream is = core.getResourceLoader().openResource(mappingFile); BufferedReader br = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8"))); String line; while ((line = br.readLine()) != null) { if (line.startsWith("#")) continue; String[] kv = line.split("=>"); if (kv.length < 2) continue; map.put(kv[0].trim(), kv[1].trim()); } log.info("Map initialized from " + mappingFile + ": " + map); } catch (Exception e) { throw new SolrException(ErrorCode.NOT_FOUND, "Error when reading mapping file " + mappingFile + "."); } }