Java Code Examples for org.apache.solr.request.SolrQueryRequest#setParams()

The following examples show how to use org.apache.solr.request.SolrQueryRequest#setParams() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TermRecognitionRequestHandler.java    From jate with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * This request handler supports configuration options defined at the top
 * level as well as those in typical Solr 'defaults', 'appends', and
 * 'invariants'. The top level ones are treated as invariants.
 */
private void setTopInitArgsAsInvariants(SolrQueryRequest req) {
    // First convert top level initArgs to SolrParams
    HashMap<String, String> map = new HashMap<String, String>(initArgs.size());
    for (int i = 0; i < initArgs.size(); i++) {
        Object val = initArgs.getVal(i);
        if (val != null && !(val instanceof NamedList))
            map.put(initArgs.getName(i), val.toString());
    }
    if (map.isEmpty())
        return;// short circuit; nothing to do
    SolrParams topInvariants = new MapSolrParams(map);
    // By putting the top level into the 1st arg, it overrides
    // request params in 2nd arg.
    req.setParams(SolrParams.wrapDefaults(topInvariants, req.getParams()));
}
 
Example 2
Source File: SolrTestCaseJ4.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
/** Makes a query request and returns the JSON string response */
public static String JQ(SolrQueryRequest req) throws Exception {
  SolrParams params = req.getParams();
  if (!"json".equals(params.get("wt","xml")) || params.get("indent")==null) {
    ModifiableSolrParams newParams = new ModifiableSolrParams(params);
    newParams.set("wt","json");
    if (params.get("indent")==null) newParams.set("indent","true");
    req.setParams(newParams);
  }

  String response;
  boolean failed=true;
  try {
    response = h.query(req);
    failed = false;
  } finally {
    if (failed) {
      log.error("REQUEST FAILED: {}", req.getParamString());
    }
  }

  return response;
}
 
Example 3
Source File: Cloud.java    From SearchServices with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * @param requestHandler the handler that handles the request
 * @param request the request object to put the params on
 * @param params Solr parameters
 * @return the response
 */
SolrQueryResponse getResponse(SolrRequestHandler requestHandler, SolrQueryRequest request, SolrParams params)
{
    request.setParams(params);
    log.info("Running query " + params.get("q"));
    SolrQueryResponse solrRsp = new SolrQueryResponse();
    requestHandler.handleRequest(request, solrRsp);
    return solrRsp;
}
 
Example 4
Source File: UpdateRequestHandler.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
private void setDefaultWT(SolrQueryRequest req, ContentStreamLoader loader) {
  SolrParams params = req.getParams();
  if( params.get(CommonParams.WT) == null ) {
    String wt = loader.getDefaultWT();
    // Make sure it is a valid writer
    if(req.getCore().getQueryResponseWriter(wt)!=null) {
      Map<String,String> map = new HashMap<>(1);
      map.put(CommonParams.WT, wt);
      req.setParams(SolrParams.wrapDefaults(params,
          new MapSolrParams(map)));
    }
  }
}
 
Example 5
Source File: JavabinLoader.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
private void handleMultiStream(SolrQueryRequest req, SolrQueryResponse rsp, InputStream stream, UpdateRequestProcessor processor)
    throws IOException {
  FastInputStream in = FastInputStream.wrap(stream);
  SolrParams old = req.getParams();
  try (JavaBinCodec jbc = new JavaBinCodec() {
    SolrParams params;
    AddUpdateCommand addCmd = null;

    @Override
    public List<Object> readIterator(DataInputInputStream fis) throws IOException {
      while (true) {
        Object o = readVal(fis);
        if (o == END_OBJ) break;
        if (o instanceof NamedList) {
          params = ((NamedList) o).toSolrParams();
        } else {
          try {
            if (o instanceof byte[]) {
              if (params != null) req.setParams(params);
              byte[] buf = (byte[]) o;
              contentStreamLoader.load(req, rsp, new ContentStreamBase.ByteArrayStream(buf, null), processor);
            } else {
              throw new RuntimeException("unsupported type ");
            }
          } catch (Exception e) {
            throw new RuntimeException(e);
          } finally {
            params = null;
            req.setParams(old);
          }
        }
      }
      return Collections.emptyList();
    }

  }) {
    jbc.unmarshal(in);
  }
}
 
Example 6
Source File: ExportHandler.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Override
public void handleRequestBody(SolrQueryRequest req, SolrQueryResponse rsp) throws Exception {
  try {
    super.handleRequestBody(req, rsp);
  } catch (Exception e) {
    rsp.setException(e);
  }
  String wt = req.getParams().get(CommonParams.WT, JSON);
  if("xsort".equals(wt)) wt = JSON;
  Map<String, String> map = new HashMap<>(1);
  map.put(CommonParams.WT, ReplicationHandler.FILE_STREAM);
  req.setParams(SolrParams.wrapDefaults(new MapSolrParams(map),req.getParams()));
  rsp.add(ReplicationHandler.FILE_STREAM, new ExportWriter(req, rsp, wt, initialStreamContext));
}
 
Example 7
Source File: TaggerRequestHandler.java    From SolrTextTagger with Apache License 2.0 5 votes vote down vote up
/**
 * This request handler supports configuration options defined at the top level as well as
 * those in typical Solr 'defaults', 'appends', and 'invariants'.  The top level ones are treated
 * as invariants.
 */
private void setTopInitArgsAsInvariants(SolrQueryRequest req) {
  // First convert top level initArgs to SolrParams
  HashMap<String,String> map = new HashMap<>(initArgs.size());
  for (int i=0; i<initArgs.size(); i++) {
    Object val = initArgs.getVal(i);
    if (val != null && !(val instanceof NamedList))
      map.put(initArgs.getName(i), val.toString());
  }
  if (map.isEmpty())
    return;//short circuit; nothing to do
  SolrParams topInvariants = new MapSolrParams(map);
  // By putting putting the top level into the 1st arg, it overrides request params in 2nd arg.
  req.setParams(SolrParams.wrapDefaults(topInvariants, req.getParams()));
}
 
Example 8
Source File: TestZKPropertiesWriter.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
/**
 * Code copied from {@link org.apache.solr.SolrTestCaseJ4#assertQ(String, SolrQueryRequest, String...)} in order not to
 * use the instance of the {@link org.apache.solr.util.TestHarness}.
 */
private static void localAssertQ(String message, SolrQueryRequest req, String... tests) {
  try {
    String m = (null == message) ? "" : message + " "; // TODO log 'm' !!!
    //since the default (standard) response format is now JSON
    //need to explicitly request XML since this class uses XPath
    ModifiableSolrParams xmlWriterTypeParams = new ModifiableSolrParams(req.getParams());
    xmlWriterTypeParams.set(CommonParams.WT,"xml");
    //for tests, let's turn indention off so we don't have to handle extraneous spaces
    xmlWriterTypeParams.set("indent", xmlWriterTypeParams.get("indent", "off"));
    req.setParams(xmlWriterTypeParams);
    String response = localQuery(req.getParams().get(CommonParams.QT), req);

    if (req.getParams().getBool("facet", false)) {
      // add a test to ensure that faceting did not throw an exception
      // internally, where it would be added to facet_counts/exception
      String[] allTests = new String[tests.length+1];
      System.arraycopy(tests,0,allTests,1,tests.length);
      allTests[0] = "*[count(//lst[@name='facet_counts']/*[@name='exception'])=0]";
      tests = allTests;
    }

    String results = BaseTestHarness.validateXPath(response, tests);

    if (null != results) {
      String msg = "REQUEST FAILED: xpath=" + results
          + "\n\txml response was: " + response
          + "\n\trequest was:" + req.getParamString();

      log.error(msg);
      throw new RuntimeException(msg);
    }

  } catch (XPathExpressionException e1) {
    throw new RuntimeException("XPath is invalid", e1);
  } catch (Exception e2) {
    SolrException.log(log,"REQUEST FAILED: " + req.getParamString(), e2);
    throw new RuntimeException("Exception during query", e2);
  }
}
 
Example 9
Source File: SolrTestCaseJ4.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
/** Validates a query matches some XPath test expressions and closes the query */
public static void assertQ(String message, SolrQueryRequest req, String... tests) {
  try {
    String m = (null == message) ? "" : message + " "; // TODO log 'm' !!!
    //since the default (standard) response format is now JSON
    //need to explicitly request XML since this class uses XPath
    ModifiableSolrParams xmlWriterTypeParams = new ModifiableSolrParams(req.getParams());
    xmlWriterTypeParams.set(CommonParams.WT,"xml");
    //for tests, let's turn indention off so we don't have to handle extraneous spaces
    xmlWriterTypeParams.set("indent", xmlWriterTypeParams.get("indent", "off"));
    req.setParams(xmlWriterTypeParams);
    String response = h.query(req);

    if (req.getParams().getBool("facet", false)) {
      // add a test to ensure that faceting did not throw an exception
      // internally, where it would be added to facet_counts/exception
      String[] allTests = new String[tests.length+1];
      System.arraycopy(tests,0,allTests,1,tests.length);
      allTests[0] = "*[count(//lst[@name='facet_counts']/*[@name='exception'])=0]";
      tests = allTests;
    }

    String results = BaseTestHarness.validateXPath(response, tests);

    if (null != results) {
      String msg = "REQUEST FAILED: xpath=" + results
          + "\n\txml response was: " + response
          + "\n\trequest was:" + req.getParamString();

      log.error(msg);
      throw new RuntimeException(msg);
    }

  } catch (XPathExpressionException e1) {
    throw new RuntimeException("XPath is invalid", e1);
  } catch (Exception e2) {
    SolrException.log(log,"REQUEST FAILED: " + req.getParamString(), e2);
    throw new RuntimeException("Exception during query", e2);
  }
}
 
Example 10
Source File: SolrTestCaseJ4.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
/**
 * Validates a query matches some JSON test expressions and closes the
 * query. The text expression is of the form path:JSON.  The Noggit JSON
 * parser used accepts single quoted strings and bare strings to allow
 * easy embedding in Java Strings.
 * <p>
 * Please use this with care: this makes it easy to match complete
 * structures, but doing so can result in fragile tests if you are
 * matching more than what you want to test.
 * </p>
 * @param req Solr request to execute
 * @param delta tolerance allowed in comparing float/double values
 * @param tests JSON path expression + '==' + expected value
 * @return The request response as a JSON String if all test patterns pass
 */
public static String assertJQ(SolrQueryRequest req, double delta, String... tests) throws Exception {
  SolrParams params =  null;
  try {
    params = req.getParams();
    if (!"json".equals(params.get("wt","xml")) || params.get("indent")==null) {
      ModifiableSolrParams newParams = new ModifiableSolrParams(params);
      newParams.set("wt","json");
      if (params.get("indent")==null) newParams.set("indent","true");
      req.setParams(newParams);
    }

    String response;
    boolean failed=true;
    try {
      response = h.query(req);
      failed = false;
    } finally {
      if (failed) {
        log.error("REQUEST FAILED: {}", req.getParamString());
      }
    }

    for (String test : tests) {
      if (test == null || test.length()==0) continue;
      String testJSON = json(test);

      try {
        failed = true;
        String err = JSONTestUtil.match(response, testJSON, delta);
        failed = false;
        if (err != null) {
          log.error("query failed JSON validation. error={}\n expected ={}\n response = {}\n request = {}"
              , err, testJSON, response, req.getParamString()
          );
          throw new RuntimeException(err);
        }
      } finally {
        if (failed) {
          log.error("JSON query validation threw an exception.\n expected ={} \n response = {}\n request = {}"
              , testJSON, response, req.getParamString()
          );
        }
      }
    }
    return response;
  } finally {
    // restore the params
    if (params != null && params != req.getParams()) req.setParams(params);
  }
}
 
Example 11
Source File: AlfrescoSolrHighlighter.java    From SearchServices with GNU Lesser General Public License v3.0 4 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public NamedList<Object> doHighlighting(DocList docs, Query query, SolrQueryRequest request, String[] defaultFields) throws IOException
{
	final String idFieldName = request.getSchema().getUniqueKeyField().getName();
	final Set<String> idFields = Set.of(idFieldName, "DBID");
	final SolrParams originalRequestParameters = request.getParams();

	// raw fields in the hl.fl parameter (e.g. hl.fl=content, name, title)
	List<String> highlightFields = stream(super.getHighlightFields(query, request, defaultFields)).collect(toList());

	/*
		For each field name collected in the previous step, we need to query the AlfrescoSolrDataModel in order to
		retrieve the corresponding field that will be used for the current highlighting request.

		e.g.
	 	{
	 		name 	=>	text@s_stored_lt@{http://www.alfresco.org/model/content/1.0}name,
	 		title	=>	mltext@m__stored_lt@{http://www.alfresco.org/model/content/1.0}title,
	 		content	=>	content@s_stored_lt@{http://www.alfresco.org/model/content/1.0}content
	 	}

	 	Since at the end we need to restore (in the response) the original request(ed) fields names (e.g. content, name) used on client side
	 	we need to maintain a map which associates each schema field (e.g. text@s_stored_lt@{http://www.alfresco.org/model/content/1.0}name)
	 	with the corresponding request(ed) field (e.g. name).
	*/
	Map<String, String> mappings = withDebug(createInitialFieldMappings(request, highlightFields));

	// The identifiers map collects two documents identifiers for each document (Solr "id" and "DBID").
	// Keys of the identifiers map are Solr "id", while values are simple value objects encapsulating all those two identifiers (for a specific document).
	Iterable<Integer> iterable = docs::iterator;
	Map<String, DocumentIdentifiers> identifiers =
			StreamSupport.stream(iterable.spliterator(), false)
				.map(docid -> identifiersEntry(request.getSearcher(), docid, idFields, idFieldName))
				.filter(Objects::nonNull)
				.collect(toMap(AbstractMap.SimpleEntry::getKey, AbstractMap.SimpleEntry::getValue));

	// First round: call the Solr highlighting procedure using the current fields mappings.
	request.setParams(rewrite(originalRequestParameters, mappings, join(",", mappings.keySet())));
	NamedList<Object> highlightingResponse = super.doHighlighting(docs, query, request, defaultFields);

       // Final step: under each document section, highlight snippets are associated with Solr field names,
	// so we need to replace them with fields actually requested
	// In addition, beside the snippets we want to have the document DBID as well.
	NamedList<Object> response = new SimpleOrderedMap<>();
	highlightingResponse.forEach( entry -> {
				String id = entry.getKey();
				NamedList<Object> documentHighlighting = (NamedList<Object>) entry.getValue();
				NamedList<Object> renamedDocumentHighlighting = new SimpleOrderedMap<>();
				if (notNullAndNotEmpty.test(documentHighlighting))
				{
					ofNullable(identifiers.get(id))
							.map(DocumentIdentifiers::dbid)
							.ifPresent(dbid -> renamedDocumentHighlighting.add("DBID", dbid));
				}

				documentHighlighting.forEach(fieldEntry -> {
					detectAndRemoveLocalePrefix(fieldEntry);

					String solrFieldName = fieldEntry.getKey();
					String requestFieldName = mappings.get(solrFieldName);
					renamedDocumentHighlighting.add(requestFieldName, fieldEntry.getValue());
				});

				response.add(id, renamedDocumentHighlighting);
			});

	return response;
}
 
Example 12
Source File: SQLHandler.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
public void handleRequestBody(SolrQueryRequest req, SolrQueryResponse rsp) throws Exception {
  ModifiableSolrParams params = new ModifiableSolrParams(req.getParams());
  params = adjustParams(params);
  req.setParams(params);

  String sql = params.get("stmt");
  // Set defaults for parameters
  params.set("numWorkers", params.getInt("numWorkers", 1));
  params.set("workerCollection", params.get("workerCollection", defaultWorkerCollection));
  params.set("workerZkhost", params.get("workerZkhost", defaultZkhost));
  params.set("aggregationMode", params.get("aggregationMode", "facet"));

  TupleStream tupleStream = null;
  try {

    if(!isCloud) {
      throw new IllegalStateException(sqlNonCloudErrorMsg);
    }

    if(sql == null) {
      throw new Exception("stmt parameter cannot be null");
    }

    String url = CalciteSolrDriver.CONNECT_STRING_PREFIX;

    Properties properties = new Properties();
    // Add all query parameters
    Iterator<String> parameterNamesIterator = params.getParameterNamesIterator();
    while(parameterNamesIterator.hasNext()) {
      String param = parameterNamesIterator.next();
      properties.setProperty(param, params.get(param));
    }

    // Set these last to ensure that they are set properly
    properties.setProperty("lex", Lex.MYSQL.toString());
    properties.setProperty("zk", defaultZkhost);

    String driverClass = CalciteSolrDriver.class.getCanonicalName();

    // JDBC driver requires metadata from the SQLHandler. Default to false since this adds a new Metadata stream.
    boolean includeMetadata = params.getBool("includeMetadata", false);
    tupleStream = new SqlHandlerStream(url, sql, null, properties, driverClass, includeMetadata);

    tupleStream = new StreamHandler.TimerStream(new ExceptionStream(tupleStream));

    rsp.add("result-set", tupleStream);
  } catch(Exception e) {
    //Catch the SQL parsing and query transformation exceptions.
    if(tupleStream != null) {
      tupleStream.close();
    }
    SolrException.log(log, e);
    rsp.add("result-set", new StreamHandler.DummyErrorStream(e));
  }
}
 
Example 13
Source File: ZookeeperInfoHandler.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
@Override
@SuppressWarnings({"unchecked"})
public void handleRequestBody(SolrQueryRequest req, SolrQueryResponse rsp) throws Exception {
  final SolrParams params = req.getParams();
  Map<String, String> map = new HashMap<>(1);
  map.put(WT, "raw");
  map.put(OMIT_HEADER, "true");
  req.setParams(SolrParams.wrapDefaults(new MapSolrParams(map), params));
  synchronized (this) {
    if (pagingSupport == null) {
      pagingSupport = new PagedCollectionSupport();
      ZkController zkController = cores.getZkController();
      if (zkController != null) {
        // get notified when the ZK session expires (so we can clear the cached collections and rebuild)
        zkController.addOnReconnectListener(pagingSupport);
      }
    }
  }

  String path = params.get(PATH);

  if (params.get("addr") != null) {
    throw new SolrException(ErrorCode.BAD_REQUEST, "Illegal parameter \"addr\"");
  }

  String detailS = params.get("detail");
  boolean detail = detailS != null && detailS.equals("true");

  String dumpS = params.get("dump");
  boolean dump = dumpS != null && dumpS.equals("true");

  int start = params.getInt("start", 0); // Note start ignored if rows not specified
  int rows = params.getInt("rows", -1);

  String filterType = params.get("filterType");
  if (filterType != null) {
    filterType = filterType.trim().toLowerCase(Locale.ROOT);
    if (filterType.length() == 0)
      filterType = null;
  }
  FilterType type = (filterType != null) ? FilterType.valueOf(filterType) : FilterType.none;

  String filter = (type != FilterType.none) ? params.get("filter") : null;
  if (filter != null) {
    filter = filter.trim();
    if (filter.length() == 0)
      filter = null;
  }

  ZKPrinter printer = new ZKPrinter(cores.getZkController());
  printer.detail = detail;
  printer.dump = dump;
  boolean isGraphView = "graph".equals(params.get("view"));
  // There is no znode /clusterstate.json (removed in Solr 9), but we do as if there's one and return collection listing
  // Need to change services.js if cleaning up here, collection list is used from Admin UI Cloud - Graph
  boolean paginateCollections = (isGraphView && "/clusterstate.json".equals(path));
  printer.page = paginateCollections ? new PageOfCollections(start, rows, type, filter) : null;
  printer.pagingSupport = pagingSupport;

  try {
    if (paginateCollections) {
      // List collections and allow pagination, but no specific znode info like when looking at a normal ZK path
      printer.printPaginatedCollections();
    } else {
      printer.print(path);
    }
  } finally {
    printer.close();
  }
  rsp.getValues().add(RawResponseWriter.CONTENT,printer);
}
 
Example 14
Source File: Sparql11SearchHandler.java    From SolRDF with Apache License 2.0 4 votes vote down vote up
@Override
public void handleRequestBody(final SolrQueryRequest request, final SolrQueryResponse response) throws Exception {
	final SolrParams parameters = request.getParams();
	if (isUsingGET(request)) {
		if (containsQueryParameter(parameters)) {
			requestHandler(
					request,
					parameters.get(SEARCH_HANDLER_PARAMETER_NAME, DEFAULT_SEARCH_HANDLER_NAME))
				.handleRequest(request, response);	
		} else {
			throw new SolrException(
					ErrorCode.BAD_REQUEST, 
					MISSING_QUERY_IN_GET_MESSAGE);
		}
	} else if (isUsingPOST(request)) {
		if (isUsingURLEncodedParameters(request)) {
			if (containsUpdateParameter(parameters)) {
				requestHandler(
						request,
						parameters.get(UPDATE_HANDLER_PARAMETER_NAME, DEFAULT_UPDATE_HANDLER_NAME))
					.handleRequest(new SparqlUpdateSolrQueryRequest(request), response);	
			} else if (containsQueryParameter(parameters)) {
				requestHandler(
						request,
						parameters.get(SEARCH_HANDLER_PARAMETER_NAME, DEFAULT_SEARCH_HANDLER_NAME))
					.handleRequest(request, response);	
			} else {    
				throw new SolrException(
						ErrorCode.BAD_REQUEST, 
						MISSING_QUERY_OR_UPDATE_IN_POST_MESSAGE);					
			}
		} else if (isSparqlQueryContentType(request)) {
			if (isBodyNotEmpty(request)) {
				request.setParams(new ModifiableSolrParams(parameters).set(Names.QUERY, readCommandFromIncomingStream(request.getContentStreams().iterator().next())));
				requestHandler(
						request,
						parameters.get(SEARCH_HANDLER_PARAMETER_NAME, DEFAULT_SEARCH_HANDLER_NAME))
					.handleRequest(new SparqlQuerySolrQueryRequest(request), response);	
			} else {
				throw new SolrException(
						ErrorCode.BAD_REQUEST, 
						MISSING_QUERY_IN_POST_BODY);
			} 
		} else if (isSparqlUpdateContentType(request)){ 
			if (isBodyNotEmpty(request)) {
				requestHandler(
						request,
						parameters.get(UPDATE_HANDLER_PARAMETER_NAME, DEFAULT_UPDATE_HANDLER_NAME))
					.handleRequest(request, response);	
			} else {
				throw new SolrException(
						ErrorCode.BAD_REQUEST, 
						MISSING_UPDATE_IN_POST_BODY);
			}
		} else {
			throw new SolrException(
					ErrorCode.BAD_REQUEST, 
					BAD_POST_REQUEST);
		}
	} else {
		throw new SolrException(
				ErrorCode.BAD_REQUEST,
				INVALID_HTTP_METHOD);
	}
}
 
Example 15
Source File: ShowFileRequestHandler.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
private void showFromFileSystem(SolrQueryRequest req, SolrQueryResponse rsp) {
  File adminFile = getAdminFileFromFileSystem(req, rsp, hiddenFiles);

  if (adminFile == null) { // exception already recorded
    return;
  }

  // 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);
}
 
Example 16
Source File: RewriteFieldListComponent.java    From SearchServices with GNU Lesser General Public License v3.0 4 votes vote down vote up
private void transformFieldList(SolrQueryRequest req)
{
    Set<String> fieldListSet = new HashSet<>();

    Set<String> defaultNonCachedFields = Set.of("id","DBID", "_version_");
    Set<String> allowedNonCachedFields = new HashSet<>(defaultNonCachedFields);
    allowedNonCachedFields.add("score");

    SolrReturnFields solrReturnFields = new SolrReturnFields(req);
    String originalFieldList = req.getParams().get("fl");

    boolean cacheTransformer = ofNullable(solrReturnFields.getTransformer())
            .map(DocTransformer::getName)
            .map(name -> name.contains("fmap"))
            .orElse(false);

    ModifiableSolrParams params = new ModifiableSolrParams(req.getParams());


    // In case cache transformer is no set, we need to modify the field list in order return
    // only id, DBID and _version_ fields
    if (!cacheTransformer){
        if (!solrReturnFields.wantsAllFields())
        {
            fieldListSet.addAll(solrReturnFields.getLuceneFieldNames()
                    .stream()
                    .filter(allowedNonCachedFields::contains)
                    .collect(Collectors.toSet()));
        }

        if (fieldListSet.isEmpty())
        {
            fieldListSet.addAll(defaultNonCachedFields);
        }

        params.set("fl", String.join(",", fieldListSet));
    }
    else
    {
        if (solrReturnFields.wantsAllFields() || solrReturnFields.hasPatternMatching())
        {
            fieldListSet.add("*");
        }
        else
        {
            fieldListSet.addAll(solrReturnFields.getLuceneFieldNames().stream()
                    .map( field -> AlfrescoSolrDataModel.getInstance()
                                            .mapStoredProperty(field, req))
                    .filter(Objects::nonNull)
                    .map(schemaFieldName -> schemaFieldName.chars()
                            .mapToObj(c -> (char) c)
                            .map(c -> Character.isJavaIdentifierPart(c)? c : '?')
                            .map(Object::toString)
                            .collect(Collectors.joining()))
                    .collect(Collectors.toSet()));
        }

        params.add("fl", String.join(",", fieldListSet));
    }

    // This is added for filtering the fields in the cached transformer.
    params.set("originalFl", originalFieldList);
    req.setParams(params);
}
 
Example 17
Source File: ChronixQueryHandler.java    From chronix.server with Apache License 2.0 4 votes vote down vote up
@Override
@SuppressWarnings("PMD.SignatureDeclareThrowsException")
public void handleRequestBody(SolrQueryRequest req, SolrQueryResponse rsp) throws Exception {
    LOGGER.debug("Handling request {}", req);
    final ModifiableSolrParams modifiableSolrParams = new ModifiableSolrParams(req.getParams());

    final String originQuery = modifiableSolrParams.get(CommonParams.Q);

    final long[] startAndEnd = dateRangeParser.getNumericQueryTerms(originQuery);
    final long queryStart = or(startAndEnd[0], -1L, 0L);
    final long queryEnd = or(startAndEnd[1], -1L, Long.MAX_VALUE);

    modifiableSolrParams.set(ChronixQueryParams.QUERY_START_LONG, String.valueOf(queryStart));
    modifiableSolrParams.set(ChronixQueryParams.QUERY_END_LONG, String.valueOf(queryEnd));
    final String query = dateRangeParser.replaceRangeQueryTerms(originQuery);
    modifiableSolrParams.set(CommonParams.Q, query);

    //Set the min required fields if the user define a sub set of fields
    final String fields = modifiableSolrParams.get(CommonParams.FL);
    modifiableSolrParams.set(CommonParams.FL, requestedFields(fields, req.getSchema().getFields().keySet()));
    //Set the updated query
    req.setParams(modifiableSolrParams);

    //check the filter queries
    final String[] chronixFunctions = modifiableSolrParams.getParams(ChronixQueryParams.CHRONIX_FUNCTION);
    final String chronixJoin = modifiableSolrParams.get(ChronixQueryParams.CHRONIX_JOIN);


    //if we have an function query or someone wants the data as json or a join query
    if (arrayIsNotEmpty(chronixFunctions) || contains(ChronixQueryParams.DATA_AS_JSON, fields) || !StringUtils.isEmpty(chronixJoin)) {
        LOGGER.debug("Request is an analysis request.");
        analysisHandler.handleRequestBody(req, rsp);
    } else {
        //let the default search handler do its work
        LOGGER.debug("Request is a default request");
        searchHandler.handleRequestBody(req, rsp);
    }

    //add the converted start and end to the response
    rsp.getResponseHeader().add(ChronixQueryParams.QUERY_START_LONG, queryStart);
    rsp.getResponseHeader().add(ChronixQueryParams.QUERY_END_LONG, queryEnd);
}
 
Example 18
Source File: SchemaHandler.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
private void handleGET(SolrQueryRequest req, SolrQueryResponse rsp) {
  try {
    String path = (String) req.getContext().get("path");
    switch (path) {
      case "/schema":
        rsp.add(IndexSchema.SCHEMA, req.getSchema().getNamedPropertyValues());
        break;
      case "/schema/version":
        rsp.add(IndexSchema.VERSION, req.getSchema().getVersion());
        break;
      case "/schema/uniquekey":
        rsp.add(IndexSchema.UNIQUE_KEY, req.getSchema().getUniqueKeyField().getName());
        break;
      case "/schema/similarity":
        rsp.add(IndexSchema.SIMILARITY, req.getSchema().getSimilarityFactory().getNamedPropertyValues());
        break;
      case "/schema/name": {
        final String schemaName = req.getSchema().getSchemaName();
        if (null == schemaName) {
          String message = "Schema has no name";
          throw new SolrException(SolrException.ErrorCode.NOT_FOUND, message);
        }
        rsp.add(IndexSchema.NAME, schemaName);
        break;
      }
      case "/schema/zkversion": {
        int refreshIfBelowVersion = req.getParams().getInt("refreshIfBelowVersion");
        int zkVersion = -1;
        IndexSchema schema = req.getSchema();
        if (schema instanceof ManagedIndexSchema) {
          ManagedIndexSchema managed = (ManagedIndexSchema) schema;
          zkVersion = managed.getSchemaZkVersion();
          if (refreshIfBelowVersion != -1 && zkVersion < refreshIfBelowVersion) {
            log.info("REFRESHING SCHEMA (refreshIfBelowVersion={}, currentVersion={}) before returning version!"
                , refreshIfBelowVersion, zkVersion);
            ZkSolrResourceLoader zkSolrResourceLoader = (ZkSolrResourceLoader) req.getCore().getResourceLoader();
            ZkIndexSchemaReader zkIndexSchemaReader = zkSolrResourceLoader.getZkIndexSchemaReader();
            managed = zkIndexSchemaReader.refreshSchemaFromZk(refreshIfBelowVersion);
            zkVersion = managed.getSchemaZkVersion();
          }
        }
        rsp.add("zkversion", zkVersion);
        break;
      }
      default: {
        List<String> parts = StrUtils.splitSmart(path, '/', true);
        if (parts.size() > 1 && level2.containsKey(parts.get(1))) {
          String realName = parts.get(1);
          String fieldName = IndexSchema.nameMapping.get(realName);

          String pathParam = level2.get(realName);
          if (parts.size() > 2) {
            req.setParams(SolrParams.wrapDefaults(new MapSolrParams(singletonMap(pathParam, parts.get(2))), req.getParams()));
          }
          @SuppressWarnings({"rawtypes"})
          Map propertyValues = req.getSchema().getNamedPropertyValues(realName, req.getParams());
          Object o = propertyValues.get(fieldName);
          if(parts.size()> 2) {
            String name = parts.get(2);
            if (o instanceof List) {
              @SuppressWarnings({"rawtypes"})
              List list = (List) o;
              for (Object obj : list) {
                if (obj instanceof SimpleOrderedMap) {
                  @SuppressWarnings({"rawtypes"})
                  SimpleOrderedMap simpleOrderedMap = (SimpleOrderedMap) obj;
                  if(name.equals(simpleOrderedMap.get("name"))) {
                    rsp.add(fieldName.substring(0, realName.length() - 1), simpleOrderedMap);
                    return;
                  }
                }
              }
            }
            throw new SolrException(SolrException.ErrorCode.NOT_FOUND, "No such path " + path);
          } else {
            rsp.add(fieldName, o);
          }
          return;
        }

        throw new SolrException(SolrException.ErrorCode.NOT_FOUND, "No such path " + path);
      }
    }

  } catch (Exception e) {
    rsp.setException(e);
  }
}
 
Example 19
Source File: QueryAutoFilteringComponent.java    From query-autofiltering-component with Apache License 2.0 4 votes vote down vote up
@Override
public void prepare( ResponseBuilder rb ) throws IOException
{
  SolrQueryRequest req = rb.req;
  SolrParams params = req.getParams( );
    
  // Only build the field map and do the processing if we are the main event
  String isShard = params.get( "isShard" );
  if (isShard != null && isShard.equals( "true" )) {
    Log.debug( "A shard query: don't process!" );
    return;
  }
    
  Log.debug( "prepare ..." );
  if (initFieldMap) {
    synchronized( this ) {
      buildFieldMap( rb );
      initFieldMap = false;
    }
  }
    
  int mintok = 1;
  String mt = params.get( MINIMUM_TOKENS );
  if ( mt != null ) {
    try {
      mintok = Integer.parseInt( mt );
    }
    catch ( NumberFormatException nfe ) {
      // ???
      mintok = 1;
    }
  }
      
  String qStr = params.get( CommonParams.Q );
  Log.info( "query is: " + qStr );
  if (qStr.equals( "*" ) || qStr.indexOf( ":" ) > 0) {
    Log.debug( "Complex query - do not process" );
    return;
  }
    
  // tokenize the query string, if any part of it matches, remove the token from the list and
  // add a filter query with <categoryField>:value:
  ArrayList<char[]> queryTokens = tokenize( qStr );
    
  if (queryTokens.size( ) >= mintok) {
    ModifiableSolrParams modParams = new ModifiableSolrParams( params );
    if (findPattern( queryTokens, rb, modParams )) {
      req.setParams( modParams );
    }
  }
}
 
Example 20
Source File: QueryAutoFilteringComponent.java    From query-autofiltering-component with Apache License 2.0 4 votes vote down vote up
@Override
public void prepare( ResponseBuilder rb ) throws IOException
{
  SolrQueryRequest req = rb.req;
  SolrParams params = req.getParams( );
    
  // Only build the field map and do the processing if we are the main event
  String isShard = params.get( "isShard" );
  if (isShard != null && isShard.equals( "true" )) {
    Log.debug( "A shard query: don't process!" );
    return;
  }
    
  Log.info( "prepare ..." );
  if (initFieldMap) {
    synchronized( this ) {
      buildFieldMap( rb );
      initFieldMap = false;
    }
  }
    
  int mintok = 1;
  String mt = params.get( MINIMUM_TOKENS );
  if ( mt != null ) {
    try {
      mintok = Integer.parseInt( mt );
    }
    catch ( NumberFormatException nfe ) {
      // ???
      mintok = 1;
    }
  }
      
  String qStr = params.get( CommonParams.Q );
  Log.debug( "query is: " + qStr );
  if (qStr.equals( "*" ) || qStr.indexOf( ":" ) > 0) {
    Log.debug( "Complex query - do not process" );
    return;
  }
    
  // tokenize the query string, if any part of it matches, remove the token from the list and
  // add a filter query with <categoryField>:value:
  ArrayList<char[]> queryTokens = tokenize( qStr );
    
  if (queryTokens.size( ) >= mintok) {
    ModifiableSolrParams modParams = new ModifiableSolrParams( params );
    if (findPattern( queryTokens, rb, modParams )) {
      req.setParams( modParams );
    }
  }
}