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

The following examples show how to use org.apache.solr.request.SolrQueryRequest#getParamString() . 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: AbstractAlfrescoSolrIT.java    From SearchServices with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Validates a query matches some XPath test expressions and closes the query
 * @param message
 * @param req
 * @param tests
 */
public static void assertQ(String message, SolrQueryRequest req, String... tests)
{
    try
    {
        String response = 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)
    {
        e2.printStackTrace();
        throw new RuntimeException("Exception during query", e2);
    }
}
 
Example 2
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 3
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 4
Source File: SentryIndexAuthorizationSingleton.java    From incubator-sentry with Apache License 2.0 4 votes vote down vote up
/**
 * Attempt to authorize a collection action.
 *
 * @param req request to check
 * @param actions set of actions to check
 * @param collectionName the collection to check.  If null, the collection
 *   name is pulled from the request
 * @param errorIfNoCollection is true, throw an exception if collection
 *   cannot be located
 */
public void authorizeCollectionAction(SolrQueryRequest req,
    Set<SearchModelAction> actions, String operation, String collectionName,
    boolean errorIfNoCollection)
    throws SolrException {

  Subject superUser = new Subject(System.getProperty("solr.authorization.superuser", "solr"));
  Subject userName = new Subject(getUserName(req));
  long eventTime = req.getStartTime();
  String paramString = req.getParamString();
  String impersonator = getImpersonatorName(req);

  String ipAddress = null;
  HttpServletRequest sreq = (HttpServletRequest) req.getContext().get("httpRequest");
  if (sreq != null) {
    try {
      ipAddress = sreq.getRemoteAddr();
    } catch (AssertionError e) {
      // ignore
      // This is a work-around for "Unexpected method call getRemoteAddr()"
      // exception during unit test mocking at
      // com.sun.proxy.$Proxy28.getRemoteAddr(Unknown Source)
    }
  }

  if (collectionName == null) {
    SolrCore solrCore = req.getCore();
    if (solrCore == null) {
      String msg = "Unable to locate collection for sentry to authorize because "
        + "no SolrCore attached to request";
      if (errorIfNoCollection) {
        auditLogger.log(userName.getName(), impersonator, ipAddress,
            operation, paramString, eventTime, AuditLogger.UNAUTHORIZED, "");
        throw new SolrException(SolrException.ErrorCode.UNAUTHORIZED, msg);
      } else { // just warn
        log.warn(msg);
        auditLogger.log(userName.getName(), impersonator, ipAddress,
            operation, paramString, eventTime, AuditLogger.ALLOWED, "");
        return;
      }
    }
    collectionName = solrCore.getCoreDescriptor().getCloudDescriptor().getCollectionName();
  }

  Collection collection = new Collection(collectionName);
  try {
    if (!superUser.getName().equals(userName.getName())) {
      binding.authorizeCollection(userName, collection, actions);
    }
  } catch (SentrySolrAuthorizationException ex) {
    auditLogger.log(userName.getName(), impersonator, ipAddress,
        operation, paramString, eventTime, AuditLogger.UNAUTHORIZED, collectionName);
    throw new SolrException(SolrException.ErrorCode.UNAUTHORIZED, ex);
  }

  auditLogger.log(userName.getName(), impersonator, ipAddress,
      operation, paramString, eventTime, AuditLogger.ALLOWED, collectionName);
}