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

The following examples show how to use org.apache.solr.request.SolrQueryRequest#getContentStreams() . 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: MetricsCollectorHandler.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@Override
public void handleRequestBody(SolrQueryRequest req, SolrQueryResponse rsp) throws Exception {
  if (coreContainer == null || coreContainer.isShutDown()) {
    // silently drop request
    return;
  }
  //log.info("#### {}", req);
  if (req.getContentStreams() == null) { // no content
    return;
  }
  for (ContentStream cs : req.getContentStreams()) {
    if (cs.getContentType() == null) {
      log.warn("Missing content type - ignoring");
      continue;
    }
    ContentStreamLoader loader = loaders.get(cs.getContentType());
    if (loader == null) {
      throw new SolrException(SolrException.ErrorCode.UNSUPPORTED_MEDIA_TYPE, "Unsupported content type for stream: " + cs.getSourceInfo() + ", contentType=" + cs.getContentType());
    }
    loader.load(req, rsp, cs, new MetricUpdateProcessor(metricManager));
  }
}
 
Example 2
Source File: DocumentAnalysisRequestHandler.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
/**
 * Extracts the only content stream from the request. {@link org.apache.solr.common.SolrException.ErrorCode#BAD_REQUEST}
 * error is thrown if the request doesn't hold any content stream or holds more than one.
 *
 * @param req The solr request.
 *
 * @return The single content stream which holds the documents to be analyzed.
 */
private ContentStream extractSingleContentStream(SolrQueryRequest req) {
  Iterable<ContentStream> streams = req.getContentStreams();
  String exceptionMsg = "DocumentAnalysisRequestHandler expects a single content stream with documents to analyze";
  if (streams == null) {
    throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, exceptionMsg);
  }
  Iterator<ContentStream> iter = streams.iterator();
  if (!iter.hasNext()) {
    throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, exceptionMsg);
  }
  ContentStream stream = iter.next();
  if (iter.hasNext()) {
    throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, exceptionMsg);
  }
  return stream;
}
 
Example 3
Source File: SchemaHandler.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@Override
public void handleRequestBody(SolrQueryRequest req, SolrQueryResponse rsp) throws Exception {
  RequestHandlerUtils.setWt(req, JSON);
  String httpMethod = (String) req.getContext().get("httpMethod");
  if ("POST".equals(httpMethod)) {
    if (isImmutableConfigSet) {
      throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "ConfigSet is immutable");
    }
    if (req.getContentStreams() == null) {
      throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "no stream");
    }

    try {
      @SuppressWarnings({"rawtypes"})
      List errs = new SchemaManager(req).performOperations();
      if (!errs.isEmpty())
        throw new ApiBag.ExceptionWithErrObject(SolrException.ErrorCode.BAD_REQUEST,"error processing commands", errs);
    } catch (IOException e) {
      throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Error reading input String " + e.getMessage(), e);
    }
  } else {
    handleGET(req, rsp);
  }
}
 
Example 4
Source File: KnowledgeGraphHandler.java    From semantic-knowledge-graph with Apache License 2.0 6 votes vote down vote up
private String getPostString(SolrQueryRequest request) throws IOException {
    Reader inputReader = null;
    Iterable<ContentStream> streams = request.getContentStreams();
    if (streams != null) {
        Iterator<ContentStream> iter = streams.iterator();
        if (iter.hasNext()) {
            inputReader = iter.next().getReader();
        }
        if (iter.hasNext()) {
            throwWithClassName(" does not support multiple ContentStreams");
        }
    }
    if (inputReader == null) {
        throwWithClassName(" requires POST data");
    }
    String inputString;
    inputString = CharStreams.toString(inputReader);
    inputReader.close();
    if(inputString.equals("") || inputString == null) {
       throwWithClassName(" requires POST data");
    }
    return inputString;
}
 
Example 5
Source File: ContentStreamHandlerBase.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 {
  SolrParams params = req.getParams();
  UpdateRequestProcessorChain processorChain =
      req.getCore().getUpdateProcessorChain(params);

  UpdateRequestProcessor processor = processorChain.createProcessor(req, rsp);

  try {
    ContentStreamLoader documentLoader = newLoader(req, processor);


    Iterable<ContentStream> streams = req.getContentStreams();
    if (streams == null) {
      if (!RequestHandlerUtils.handleCommit(req, processor, params, false) && !RequestHandlerUtils.handleRollback(req, processor, params, false)) {
        throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "missing content stream");
      }
    } else {

      for (ContentStream stream : streams) {
        documentLoader.load(req, rsp, stream, processor);
      }

      // Perhaps commit from the parameters
      RequestHandlerUtils.handleCommit(req, processor, params, false);
      RequestHandlerUtils.handleRollback(req, processor, params, false);
    }
  } finally {
    // finish the request
    try {
      processor.finish();
    } finally {
      processor.close();
    }
  }
}
 
Example 6
Source File: SolrRequestParserTest.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public void doAutoDetect(String userAgent, String method, final String body, String expectedContentType, String expectedKey, String expectedValue) throws Exception {
  String uri = "/solr/select";
  String contentType = "application/x-www-form-urlencoded";
  int contentLength = -1;  // does this mean auto-detect?

  HttpServletRequest request = mock(HttpServletRequest.class);
  when(request.getHeader("User-Agent")).thenReturn(userAgent);
  when(request.getRequestURI()).thenReturn(uri);
  when(request.getContentType()).thenReturn(contentType);
  when(request.getContentLength()).thenReturn(contentLength);

  when(request.getMethod()).thenReturn(method);
  // we dont pass a content-length to let the security mechanism limit it:
  when(request.getQueryString()).thenReturn("foo=1&bar=2");
  when(request.getInputStream()).thenReturn(new ByteServletInputStream(body.getBytes(StandardCharsets.US_ASCII)));

  SolrRequestParsers parsers = new SolrRequestParsers(h.getCore().getSolrConfig());
  SolrQueryRequest req = parsers.parse(h.getCore(), "/select", request);
  int num=0;
  if (expectedContentType != null) {
    for (ContentStream cs : req.getContentStreams()) {
      num++;
      assertTrue(cs.getContentType().startsWith(expectedContentType));
      String returnedBody = IOUtils.toString(cs.getReader());
      assertEquals(body, returnedBody);
    }
    assertEquals(1, num);
  }

  assertEquals("1", req.getParams().get("foo"));
  assertEquals("2", req.getParams().get("bar"));

  if (expectedKey != null) {
    assertEquals(expectedValue, req.getParams().get(expectedKey));
  }

  req.close();
  verify(request).getInputStream();
}
 
Example 7
Source File: AbstractIngestionHandler.java    From chronix.server with Apache License 2.0 5 votes vote down vote up
@Override
@SuppressWarnings("PMD.SignatureDeclareThrowsException")
public void handleRequestBody(SolrQueryRequest req, SolrQueryResponse rsp) throws Exception {
    formatResponseAsJson(req);

    if (req.getContentStreams() == null) {
        LOGGER.warn("no content stream");
        rsp.add("error", "No content stream");
        return;
    }

    boolean commit = Boolean.parseBoolean(req.getParams().get("commit", "true"));

    InputStream stream = req.getContentStreams().iterator().next().getStream();
    stream = detectGzip(stream);

    MetricTimeSeriesConverter converter = new MetricTimeSeriesConverter();

    UpdateRequestProcessorChain processorChain = req.getCore().getUpdateProcessorChain(req.getParams());
    UpdateRequestProcessor processor = processorChain.createProcessor(req, rsp);
    try {
        for (MetricTimeSeries series : formatParser.parse(stream)) {
            SolrInputDocument document = new SolrInputDocument();
            converter.to(series).getFields().forEach(document::addField);
            storeDocument(document, processor, req);
        }

        if (commit) {
            LOGGER.debug("Committing transaction...");
            processor.processCommit(new CommitUpdateCommand(req, false));
            LOGGER.debug("Committed transaction");
        } else {
            LOGGER.debug("Only adding documents.");
        }
    } finally {
        processor.finish();
    }
}
 
Example 8
Source File: FeaturesRequestHandler.java    From ltr4l with Apache License 2.0 4 votes vote down vote up
@Override
public void handleRequestBody(SolrQueryRequest req, SolrQueryResponse rsp) throws Exception {
  SimpleOrderedMap<Object> results = new SimpleOrderedMap<Object>();
  String command = req.getParams().required().get("command");
  results.add("command", command);

  if(command.equals("extract")){
    Iterable<ContentStream> ite = req.getContentStreams();
    if(ite == null){
      throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "no queries found");
    }
    else{
      handleExtract(req, ite, results);
    }
  }
  else if(command.equals("progress")){
    handleProgress(req, results);
  }
  else if(command.equals("download")){
    long procId = req.getParams().required().getLong("procId");
    final boolean delete = req.getParams().getBool("delete", false);
    SimpleOrderedMap<Object> data = download(procId, delete);
    results.add("procId", procId);
    if(data == null){
      FeaturesExtractorManager manager = getManager(procId);
      results.add("done?", manager.isDone());
      results.add("progress", manager.getProgress());
      results.add("result", "the process still runs...");
    }
    else{
      if(delete){
        results.add("deleted", "the process has been removed and the procId is no longer valid");
      }
      results.add("result", data);
    }
  }
  else if(command.equals("delete")){
    handleDelete(req, results);
  }
  else{
    throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "unknown command " + command);
  }

  rsp.add("results", results);
}
 
Example 9
Source File: FieldAnalysisRequestHandler.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
/**
 * Resolves the AnalysisRequest based on the parameters in the given SolrParams.
 *
 * @param req the request
 *
 * @return AnalysisRequest containing all the information about what needs to be analyzed, and using what
 *         fields/types
 */
FieldAnalysisRequest resolveAnalysisRequest(SolrQueryRequest req) throws SolrException {
  SolrParams solrParams = req.getParams();
  FieldAnalysisRequest analysisRequest = new FieldAnalysisRequest();

  boolean useDefaultSearchField = true;
  if (solrParams.get(AnalysisParams.FIELD_TYPE) != null) {
    analysisRequest.setFieldTypes(Arrays.asList(solrParams.get(AnalysisParams.FIELD_TYPE).split(",")));
    useDefaultSearchField = false;
  }
  if (solrParams.get(AnalysisParams.FIELD_NAME) != null) {
    analysisRequest.setFieldNames(Arrays.asList(solrParams.get(AnalysisParams.FIELD_NAME).split(",")));
    useDefaultSearchField = false;
  }
  if (useDefaultSearchField) {
    if (solrParams.get(CommonParams.DF) != null) {
      analysisRequest.addFieldName(solrParams.get(CommonParams.DF));
    } else {
      throw new SolrException(SolrException.ErrorCode.BAD_REQUEST,
          "Field analysis request must contain one of analysis.fieldtype, analysis.fieldname or df.");
    }
  }
  analysisRequest.setQuery(solrParams.get(AnalysisParams.QUERY, solrParams.get(CommonParams.Q)));

  String value = solrParams.get(AnalysisParams.FIELD_VALUE);
  if (analysisRequest.getQuery() == null && value == null)  {
    throw new SolrException(SolrException.ErrorCode.BAD_REQUEST,
        "One of analysis.fieldvalue, q, or analysis.query parameters must be specified");
  }

  Iterable<ContentStream> streams = req.getContentStreams();
  if (streams != null) {
    // NOTE: Only the first content stream is currently processed
    for (ContentStream stream : streams) {
      Reader reader = null;
      try {
        reader = stream.getReader();
        value = IOUtils.toString(reader);
      } catch (IOException e) {
        // do nothing, leave value set to the request parameter
      }
      finally {
        IOUtils.closeQuietly(reader);
      }
      break;
    }
  }

  analysisRequest.setFieldValue(value);
  analysisRequest.setShowMatch(solrParams.getBool(AnalysisParams.SHOW_MATCH, false));
  return analysisRequest;
}
 
Example 10
Source File: DumpRequestHandler.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 IOException
{
  // Show params
  rsp.add( "params", req.getParams().toNamedList() );
  String[] parts = req.getParams().getParams("urlTemplateValues");
  if (parts != null && parts.length > 0) {
    @SuppressWarnings({"rawtypes"})
    Map map = new LinkedHashMap<>();
    rsp.getValues().add("urlTemplateValues", map);
    for (String part : parts) {
      map.put(part, req.getPathTemplateValues().get(part));
    }
  }

  String[] returnParams = req.getParams().getParams("param");
  if(returnParams !=null) {
    @SuppressWarnings({"rawtypes"})
    NamedList params = (NamedList) rsp.getValues().get("params");
    for (String returnParam : returnParams) {
      String[] vals = req.getParams().getParams(returnParam);
      if(vals != null){
        if (vals.length == 1) {
          params.add(returnParam, vals[0]);
        } else {
          params.add(returnParam, vals);
        }

      }

    }
  }

  if(req.getParams().getBool("getdefaults", false)){
    @SuppressWarnings({"rawtypes"})
    NamedList def = (NamedList) initArgs.get(PluginInfo.DEFAULTS);
    rsp.add("getdefaults", def);
  }


  if(req.getParams().getBool("initArgs", false)) {
    rsp.add("initArgs", initArgs);
  }
      
  // Write the streams...
  if( req.getContentStreams() != null ) {
    ArrayList<NamedList<Object>> streams = new ArrayList<>();
    // Cycle through each stream
    for( ContentStream content : req.getContentStreams() ) {
      NamedList<Object> stream = new SimpleOrderedMap<>();
      stream.add(NAME, content.getName());
      stream.add( "sourceInfo", content.getSourceInfo() );
      stream.add( "size", content.getSize() );
      stream.add( "contentType", content.getContentType() );
      Reader reader = content.getReader();
      try {
        stream.add( "stream", IOUtils.toString(reader) );
      } finally {
        reader.close();
      }
      streams.add( stream );
    }
    rsp.add( "streams", streams );
  }

  rsp.add("context", req.getContext());
}
 
Example 11
Source File: SecurityConfHandler.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings({"unchecked", "rawtypes"})
private void doEdit(SolrQueryRequest req, SolrQueryResponse rsp, String path, final String key, final Object plugin)
    throws IOException {
  ConfigEditablePlugin configEditablePlugin = null;

  if (plugin == null) {
    throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "No " + key + " plugin configured");
  }
  if (plugin instanceof ConfigEditablePlugin) {
    configEditablePlugin = (ConfigEditablePlugin) plugin;
  } else {
    throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, key + " plugin is not editable");
  }

  if (req.getContentStreams() == null) {
    throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "No contentStream");
  }
  List<CommandOperation> ops = CommandOperation.readCommands(req.getContentStreams(), rsp.getValues());
  if (ops == null) {
    throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "No commands");
  }
  for (int count = 1; count <= 3 ; count++ ) {
    SecurityConfig securityConfig = getSecurityConfig(true);
    Map<String, Object> data = securityConfig.getData();
    Map<String, Object> latestConf = (Map<String, Object>) data.get(key);
    if (latestConf == null) {
      throw new SolrException(SERVER_ERROR, "No configuration present for " + key);
    }
    List<CommandOperation> commandsCopy = CommandOperation.clone(ops);
    Map<String, Object> out = configEditablePlugin.edit(Utils.getDeepCopy(latestConf, 4) , commandsCopy);
    if (out == null) {
      List<Map> errs = CommandOperation.captureErrors(commandsCopy);
      if (!errs.isEmpty()) {
        rsp.add(CommandOperation.ERR_MSGS, errs);
        return;
      }
      log.debug("No edits made");
      return;
    } else {
      if(!Objects.equals(latestConf.get("class") , out.get("class"))){
        throw new SolrException(SERVER_ERROR, "class cannot be modified");
      }
      Map meta = getMapValue(out, "");
      meta.put("v", securityConfig.getVersion()+1);//encode the expected zkversion
      data.put(key, out);
      
      if(persistConf(securityConfig)) {
        securityConfEdited();
        return;
      }
    }
    log.debug("Security edit operation failed {} time(s)", count);
  }
  throw new SolrException(SERVER_ERROR, "Failed to persist security config after 3 attempts. Giving up");
}
 
Example 12
Source File: Sparql11GraphStoreProtocolHandler.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 HttpServletRequest httpRequest = (HttpServletRequest) request.getContext().get(Names.HTTP_REQUEST_KEY);
	final String method = httpRequest.getMethod();

	final SolrParams parameters = request.getParams();
	final String graphUri = parameters.get(Names.DEFAULT_GRAPH_PARAMETER_NAME) != null 
			? null 
			: parameters.get(Names.GRAPH_URI_PARAMETER_NAME);
	
	LOGGER.debug(MessageCatalog._00093_GSP_REQUEST, method, graphUri != null ? graphUri : "default");
	
	// Although a stupid Map could (apparently) avoid the conditional logic, here we have just 4 
	// possible entries (GET, POST, PUT and DELETE), so a set of if statements is almost innocue.
	if ("GET".equals(method)) {
		request.setParams(
				new ModifiableSolrParams(parameters)
					.add(CommonParams.Q, constructQuery(graphUri)));
		
		forward(request, response, SEARCH_HANDLER_PARAMETER_NAME, DEFAULT_SEARCH_HANDLER_NAME);
	} else if ("POST".equals(method)) {
		if (request.getContentStreams() == null || !request.getContentStreams().iterator().hasNext()) {
			throw new SolrException(ErrorCode.BAD_REQUEST, "Empty RDF Payload");
		}
		
		if (graphUri != null) {
			request.setParams(
					new ModifiableSolrParams(parameters).add(
							Names.GRAPH_URI_ATTRIBUTE_NAME, 
							graphUri));
		}			
		forward(request, response, BULK_UPDATE_HANDLER_PARAMETER_NAME, DEFAULT_BULK_UPDATE_HANDLER_NAME);
	} else if ("PUT".equals(method)) {
		// Unfortunately we never fall within this case (see class comments)
		if (request.getContentStreams() == null || !request.getContentStreams().iterator().hasNext()) {
			throw new SolrException(ErrorCode.BAD_REQUEST, "Emtpty RDF Payload");
		}
		
		final String q = new StringBuilder("DROP SILENT ")
			.append(graphUri != null ? "GRAPH <" + graphUri + "> " : "DEFAULT" )
			.toString();
		
		request.setParams(new ModifiableSolrParams(parameters).add(CommonParams.Q, q));

		forward(request, response, UPDATE_HANDLER_PARAMETER_NAME, DEFAULT_UPDATE_HANDLER_NAME);
		forward(request, response, BULK_UPDATE_HANDLER_PARAMETER_NAME, DEFAULT_BULK_UPDATE_HANDLER_NAME);
	} else if ("DELETE".equals(method)) {
		// Unfortunately we never fall within this case (see class comments)
	}
}
 
Example 13
Source File: Sparql11SearchHandler.java    From SolRDF with Apache License 2.0 2 votes vote down vote up
/**
 * Checks if the current (HTTP) request contains a valid body.
 * 
 * @param request the Solr request.
 * @return true if the current (HTTP) request contains a valid body, false otherwise.
 */
boolean isBodyNotEmpty(final SolrQueryRequest request) {
	return request.getContentStreams() != null && request.getContentStreams().iterator().hasNext();
}