org.apache.solr.common.util.ContentStream Java Examples

The following examples show how to use org.apache.solr.common.util.ContentStream. 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: Sparql11SearchHandlerTestCase.java    From SolRDF with Apache License 2.0 6 votes vote down vote up
@Test 
public void queryUsingPOSTDirectly() throws Exception {
	when(httpRequest.getContentType()).thenReturn(WebContent.contentTypeSPARQLQuery);
	when(httpRequest.getMethod()).thenReturn("POST");
	
	final List<ContentStream> dummyStream = new ArrayList<ContentStream>(1);
	dummyStream.add(new ContentStreamBase.ByteArrayStream(new byte[0], "dummy") {
		@Override
		public String getContentType() {
			return WebContent.contentTypeSPARQLUpdate;
		}
	});
	when(request.getContentStreams()).thenReturn(dummyStream);
	when(request.getParams()).thenReturn(new ModifiableSolrParams());
	
	cut.handleRequestBody(request, response);
	
	verify(cut).requestHandler(request, Sparql11SearchHandler.DEFAULT_SEARCH_HANDLER_NAME);
}
 
Example #2
Source File: UniqFieldsUpdateProcessorFactoryTest.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
private void addDoc(String doc) throws Exception {
  Map<String, String[]> params = new HashMap<>();
  MultiMapSolrParams mmparams = new MultiMapSolrParams(params);
  params.put(UpdateParams.UPDATE_CHAIN, new String[] { "uniq-fields" });
  SolrQueryRequestBase req = new SolrQueryRequestBase(h.getCore(),
      (SolrParams) mmparams) {
  };

  UpdateRequestHandler handler = new UpdateRequestHandler();
  handler.init(null);
  ArrayList<ContentStream> streams = new ArrayList<>(2);
  streams.add(new ContentStreamBase.StringStream(doc));
  req.setContentStreams(streams);
  handler.handleRequestBody(req, new SolrQueryResponse());
  req.close();
}
 
Example #3
Source File: AtlasJanusGraphIndexClient.java    From atlas with Apache License 2.0 6 votes vote down vote up
private SolrResponse performRequestHandlerAction(String collectionName,
                                               SolrClient solrClient,
                                               String actionPayLoad,
                                               Solr6Index.Mode mode) throws IOException, SolrServerException, AtlasBaseException {
    switch (mode) {
        case CLOUD:
            V2Request v2request = new V2Request.Builder(String.format("/collections/%s/config", collectionName))
                                                        .withMethod(SolrRequest.METHOD.POST)
                                                        .withPayload(actionPayLoad)
                                                        .build();

            return validateResponseForSuccess(v2request.process(solrClient));

        case HTTP:
            Collection<ContentStream> contentStreams = ClientUtils.toContentStreams(actionPayLoad, "application/json; charset=UTF-8");
            GenericSolrRequest        request        = new GenericSolrRequest(SolrRequest.METHOD.POST, String.format("/%s/config", collectionName), null);

            request.setContentStreams(contentStreams);
            request.setUseV2(false);

            return validateResponseForSuccess(request.process(solrClient));

        default:
            throw new IllegalArgumentException("Unsupported Solr operation mode: " + mode);
    }
}
 
Example #4
Source File: SolrDao.java    From DataHubSystem with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * Unset the given properties that have been previously set with {@link #setProperties(Map)}.
 * @param props properties to unset.
 * @throws IOException network error.
 * @throws SolrServerException solr error.
 */
public void unsetProperties(Set<String> props) throws SolrServerException, IOException
{
   // Solrj does not support the config API yet.
   StringBuilder command = new StringBuilder("{\"unset-property\": [");
   for (String prop: props)
   {
      command.append('"').append(prop).append('"').append(',');
   }
   command.setLength(command.length()-1); // remove last comma
   command.append("]}");

   GenericSolrRequest rq = new GenericSolrRequest(SolrRequest.METHOD.POST, "/config", null);
   ContentStream content = new ContentStreamBase.StringStream(command.toString());
   rq.setContentStreams(Collections.singleton(content));
   rq.process(solrClient);
}
 
Example #5
Source File: SolrRequestParserTest.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@Test
@SuppressWarnings({"try"})
public void testStreamFile() throws Exception
{
  File file = getFile("README");
  
  byte[] bytes = FileUtils.readFileToByteArray(file);

  SolrCore core = h.getCore();
  
  Map<String,String[]> args = new HashMap<>();
  args.put( CommonParams.STREAM_FILE, new String[] { file.getAbsolutePath() } );
  
  // Make sure it got a single stream in and out ok
  List<ContentStream> streams = new ArrayList<>();
  try (SolrQueryRequest req = parser.buildRequestFrom( core, new MultiMapSolrParams( args ), streams )) {
    assertEquals( 1, streams.size() );
    try (InputStream in = streams.get(0).getStream()) {
      assertArrayEquals( bytes, IOUtils.toByteArray( in ) );
    }
  }
}
 
Example #6
Source File: AbstractAlfrescoSolrIT.java    From SearchServices with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * Creates a solr request.
 * @param params
 * @param json
 * @return
 */
public SolrServletRequest areq(ModifiableSolrParams params, String json) 
{
    if(params.get("wt" ) == null)
    {
        params.add("wt","xml");
    }
    SolrServletRequest req =  new SolrServletRequest(getCore(), null);
    req.setParams(params);
    if(json != null) 
    {
        ContentStream stream = new ContentStreamBase.StringStream(json);
        ArrayList<ContentStream> streams = new ArrayList<ContentStream>();
        streams.add(stream);
        req.setContentStreams(streams);
    }
    return req;
}
 
Example #7
Source File: TestUtils.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
public void testBinaryCommands() throws IOException {
  ByteArrayOutputStream baos = new ByteArrayOutputStream();
  try (final JavaBinCodec jbc = new JavaBinCodec()) {
    jbc.marshal((MapWriter) ew -> {
      ew.put("set-user", fromJSONString("{x:y}"));
      ew.put("set-user", fromJSONString("{x:y,x1:y1}"));
      ew.put("single", asList(fromJSONString("[{x:y,x1:y1},{x2:y2}]"), fromJSONString( "{x2:y2}")));
      ew.put("multi", asList(fromJSONString("{x:y,x1:y1}"), fromJSONString( "{x2:y2}")));
    }, baos);
  }

  ContentStream stream = new ContentStreamBase.ByteArrayStream(baos.toByteArray(),null, "application/javabin");
  @SuppressWarnings({"rawtypes"})
  List<CommandOperation> commands = CommandOperation.readCommands(Collections.singletonList(stream), new NamedList(), Collections.singleton("single"));

  assertEquals(5, commands.size());
}
 
Example #8
Source File: ContentStreamUpdateRequest.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@Override
public RequestWriter.ContentWriter getContentWriter(String expectedType) {
  if (contentStreams == null || contentStreams.isEmpty() || contentStreams.size() > 1) return null;
  ContentStream stream = contentStreams.get(0);
  return new RequestWriter.ContentWriter() {
    @Override
    public void write(OutputStream os) throws IOException {
      try(var inStream = stream.getStream()) {
        IOUtils.copy(inStream, os);
      }
    }

    @Override
    public String getContentType() {
      return stream.getContentType();
    }
  };
}
 
Example #9
Source File: HttpSolrClient.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
private void fillSingleContentStream(Collection<ContentStream> streams, HttpEntityEnclosingRequestBase postOrPut) throws IOException {
  // Single stream as body
  // Using a loop just to get the first one
  final ContentStream[] contentStream = new ContentStream[1];
  for (ContentStream content : streams) {
    contentStream[0] = content;
    break;
  }
  Long size = contentStream[0].getSize();
  postOrPut.setEntity(new InputStreamEntity(contentStream[0].getStream(), size == null ? -1 : size) {
    @Override
    public Header getContentType() {
      return new BasicHeader("Content-Type", contentStream[0].getContentType());
    }

    @Override
    public boolean isRepeatable() {
      return false;
    }
  });

}
 
Example #10
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 #11
Source File: EmbeddedSolrServerTestBase.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
private void writeTo(final Path base, final ContentStream... contents) {
  try {
    if (!Files.exists(base)) {
      Files.createDirectories(base);
    }

    for (final ContentStream content : contents) {
      final File file = new File(base.toFile(), content.getName());
      file.getParentFile().mkdirs();

      try (OutputStream os = new FileOutputStream(file)) {
        ByteStreams.copy(content.getStream(), os);
      }
    }
  } catch (final IOException e) {
    throw new RuntimeException(e);
  }
}
 
Example #12
Source File: SolrTestCaseJ4.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
public static void addDoc(String doc, String updateRequestProcessorChain) throws Exception {
  Map<String, String[]> params = new HashMap<>();
  MultiMapSolrParams mmparams = new MultiMapSolrParams(params);
  params.put(UpdateParams.UPDATE_CHAIN, new String[]{updateRequestProcessorChain});
  SolrQueryRequestBase req = new SolrQueryRequestBase(h.getCore(),
      (SolrParams) mmparams) {
  };

  UpdateRequestHandler handler = new UpdateRequestHandler();
  handler.init(null);
  ArrayList<ContentStream> streams = new ArrayList<>(2);
  streams.add(new ContentStreamBase.StringStream(doc));
  req.setContentStreams(streams);
  handler.handleRequestBody(req, new SolrQueryResponse());
  req.close();
}
 
Example #13
Source File: Sparql11SearchHandlerTestCase.java    From SolRDF with Apache License 2.0 6 votes vote down vote up
@Test 
public void updateUsingPOSTDirectly() throws Exception {
	when(httpRequest.getContentType()).thenReturn(WebContent.contentTypeSPARQLUpdate);
	when(httpRequest.getMethod()).thenReturn("POST");
	
	final List<ContentStream> dummyStream = new ArrayList<ContentStream>(1);
	dummyStream.add(new ContentStreamBase.ByteArrayStream(new byte[0], "dummy") {
		@Override
		public String getContentType() {
			return WebContent.contentTypeSPARQLUpdate;
		}
	});
	when(request.getContentStreams()).thenReturn(dummyStream);
	when(request.getParams()).thenReturn(new ModifiableSolrParams());
	
	cut.handleRequestBody(request, response);
	
	verify(cut).requestHandler(request, Sparql11SearchHandler.DEFAULT_UPDATE_HANDLER_NAME);
}
 
Example #14
Source File: TestRerankBase.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
protected static void bulkIndex(String filePath) throws Exception {
  final SolrQueryRequestBase req = lrf.makeRequest(
      CommonParams.STREAM_CONTENTTYPE, "application/xml");

  final List<ContentStream> streams = new ArrayList<ContentStream>();
  final File file = new File(filePath);
  streams.add(new ContentStreamBase.FileStream(file));
  req.setContentStreams(streams);

  try {
    final SolrQueryResponse res = new SolrQueryResponse();
    h.updater.handleRequest(req, res);
  } catch (final Throwable ex) {
    // Ignore. Just log the exception and go to the next file
    log.error(ex.getMessage(), ex);
  }
  assertU(commit());

}
 
Example #15
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 #16
Source File: SolrDao.java    From DataHubSystem with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * Set the given properties and values using the config API.
 * @param props properties to set.
 * @throws IOException network error.
 * @throws SolrServerException solr error.
 */
public void setProperties(Map<String, String> props) throws SolrServerException, IOException
{
   // Solrj does not support the config API yet.
   StringBuilder command = new StringBuilder("{\"set-property\": {");
   for (Map.Entry<String, String> entry: props.entrySet())
   {
      command.append('"').append(entry.getKey()).append('"').append(':');
      command.append(entry.getValue()).append(',');
   }
   command.setLength(command.length()-1); // remove last comma
   command.append("}}");

   GenericSolrRequest rq = new GenericSolrRequest(SolrRequest.METHOD.POST, "/config", null);
   ContentStream content = new ContentStreamBase.StringStream(command.toString());
   rq.setContentStreams(Collections.singleton(content));
   rq.process(solrClient);
}
 
Example #17
Source File: SolrRequestParsers.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
public SolrQueryRequest parse( SolrCore core, String path, HttpServletRequest req ) throws Exception
{
  SolrRequestParser parser = standard;
  
  // TODO -- in the future, we could pick a different parser based on the request
  
  // Pick the parser from the request...
  ArrayList<ContentStream> streams = new ArrayList<>(1);
  SolrParams params = parser.parseParamsAndFillStreams( req, streams );
  if (GlobalTracer.get().tracing()) {
    GlobalTracer.getTracer().activeSpan().setTag("params", params.toString());
  }
  SolrQueryRequest sreq = buildRequestFrom(core, params, streams, getRequestTimer(req), req);

  // Handlers and login will want to know the path. If it contains a ':'
  // the handler could use it for RESTful URLs
  sreq.getContext().put(PATH, RequestHandlers.normalize(path));
  sreq.getContext().put("httpMethod", req.getMethod());

  if(addHttpRequestToContext) {
    sreq.getContext().put("httpRequest", req);
  }
  return sreq;
}
 
Example #18
Source File: FeaturesRequestHandler.java    From ltr4l with Apache License 2.0 6 votes vote down vote up
public void handleExtract(SolrQueryRequest req, Iterable<ContentStream> ite, SimpleOrderedMap<Object> results)
        throws Exception {
  FeaturesConfigReader fcReader = new FeaturesConfigReader(req.getCore().getResourceLoader(),
          req.getParams().required().get("conf"));
  FeaturesConfigReader.FeatureDesc[] featureDescs = fcReader.getFeatureDescs();
  List<FieldFeatureExtractorFactory> featuresSpec = new ArrayList<FieldFeatureExtractorFactory>();
  for(FeaturesConfigReader.FeatureDesc featureDesc: featureDescs){
    FieldFeatureExtractorFactory dfeFactory = FeaturesConfigReader.loadFactory(featureDesc);
    featuresSpec.add(dfeFactory);
  }
  StringBuilder queries = new StringBuilder();
  for(ContentStream cs: ite){
    Reader reader = cs.getReader();
    try{
      queries.append(IOUtils.toString(reader));
    }
    finally{
      IOUtils.closeQuietly(reader);
    }
  }
  long procId = startExtractor(req, featuresSpec, queries.toString());
  FeaturesExtractorManager manager = getManager(procId);
  results.add("procId", procId);
  results.add("progress", manager.getProgress());
}
 
Example #19
Source File: F.java    From SolRDF with Apache License 2.0 5 votes vote down vote up
/**
 * Reads the incoming stream in order to build the wrapped operation.
 * 
 * @param stream the incoming content stream.
 * @return the incoming stream in order to build the wrapped operation.
 * @throws IOException in case of I/O failure.
 */
public static String readCommandFromIncomingStream(final ContentStream stream) throws IOException {
	final BufferedReader reader = new BufferedReader(stream.getReader());
	final StringBuilder builder = new StringBuilder();
	String actLine = null;
	try {
		while ( (actLine = reader.readLine()) != null) {
			builder.append(actLine).append(" ");
		}
		return builder.toString();
	} finally {
		IOUtils.closeQuietly(reader);
	}
}
 
Example #20
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 #21
Source File: RdfBulkUpdateRequestHandler.java    From SolRDF with Apache License 2.0 5 votes vote down vote up
@Override
public void load( 
		final SolrQueryRequest request, 
		final SolrQueryResponse response,
		final ContentStream stream, 
		final UpdateRequestProcessor processor) throws Exception {
	
	final PipedRDFIterator<Quad> iterator = new PipedRDFIterator<Quad>();
	final StreamRDF inputStream = new PipedQuadsStream(iterator);
	
	executor.submit(new Runnable() {
		@Override
		public void run() {
			try {
				RDFDataMgr.parse(
						inputStream, 
						stream.getStream(), 
						RDFLanguages.contentTypeToLang(stream.getContentType()));
			} catch (final IOException exception) {
				throw new SolrException(ErrorCode.SERVER_ERROR, exception);
			}					
		}
	});
	
	final DatasetGraph dataset = new LocalDatasetGraph(request, response);
	while (iterator.hasNext()) {
		dataset.add(iterator.next());
	}									
}
 
Example #22
Source File: UpdateRequestHandler.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Override
public void load(SolrQueryRequest req, SolrQueryResponse rsp,
    ContentStream stream, UpdateRequestProcessor processor) throws Exception {

  ContentStreamLoader loader = pathVsLoaders.get(req.getContext().get(PATH));
  if(loader == null) {
    String type = req.getParams().get(UpdateParams.ASSUME_CONTENT_TYPE);
    if (type == null) {
      type = stream.getContentType();
    }
    if (type == null) { // Normal requests will not get here.
      throw new SolrException(ErrorCode.UNSUPPORTED_MEDIA_TYPE, "Missing ContentType");
    }
    int idx = type.indexOf(';');
    if (idx > 0) {
      type = type.substring(0, idx);
    }
    loader = loaders.get(type);
    if (loader == null) {
      throw new SolrException(ErrorCode.UNSUPPORTED_MEDIA_TYPE, "Unsupported ContentType: "
          + type + "  Not in: " + loaders.keySet());
    }
  }

  if(loader.getDefaultWT()!=null) {
    setDefaultWT(req,loader);
  }
  loader.load(req, rsp, stream, processor);
}
 
Example #23
Source File: TaggerTestCase.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
/** REMEMBER to close() the result req object. */
protected SolrQueryRequest reqDoc(String doc, SolrParams moreParams) {
  log.debug("Test doc: {}", doc);
  SolrParams params = SolrParams.wrapDefaults(moreParams, baseParams);
  SolrQueryRequestBase req = new SolrQueryRequestBase(h.getCore(), params) {};
  Iterable<ContentStream> stream = Collections.singleton((ContentStream)new ContentStreamBase.StringStream(doc));
  req.setContentStreams(stream);
  return req;
}
 
Example #24
Source File: MoreLikeThisHandlerTest.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Test
public void testMultifieldSimilarity() throws Exception
{
  SolrCore core = h.getCore();
  ModifiableSolrParams params = new ModifiableSolrParams();

  assertU(adoc("id", "1", "name", "aaa bbb ccc", "subword", "        zzz"));
  assertU(adoc("id", "2", "name", "    bbb ccc", "subword", "    bbb zzz"));
  assertU(adoc("id", "3", "name", "        ccc", "subword", "aaa bbb zzz"));
  assertU(adoc("id", "4", "name", "        ccc", "subword", "    bbb    "));
  assertU(commit());

  params.set(CommonParams.QT, "/mlt");
  params.set(MoreLikeThisParams.MLT, "true");
  params.set(MoreLikeThisParams.SIMILARITY_FIELDS, "name,subword");
  params.set(MoreLikeThisParams.INTERESTING_TERMS, "details");
  params.set(MoreLikeThisParams.MIN_TERM_FREQ, "1");
  params.set(MoreLikeThisParams.MIN_DOC_FREQ, "2");
  params.set(MoreLikeThisParams.BOOST, true);
  params.set("indent", "true");

  try (SolrQueryRequestBase req = new SolrQueryRequestBase(core, params) {}) {
    ArrayList<ContentStream> streams = new ArrayList<>(2);
    streams.add(new ContentStreamBase.StringStream("bbb", "zzz"));
    req.setContentStreams(streams);

    // Make sure we have terms from both fields in the interestingTerms array and all documents have been
    // retrieved as matching.
    assertQ(req,
        "//lst[@name = 'interestingTerms']/float[@name = 'subword:bbb']",
        "//lst[@name = 'interestingTerms']/float[@name = 'name:bbb']",
        "//result[@name = 'response' and @numFound = '4']");
  }
}
 
Example #25
Source File: RawResponseWriter.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Override
public void write(OutputStream out, SolrQueryRequest request, SolrQueryResponse response) throws IOException {
  Object obj = response.getValues().get( CONTENT );
  if( obj != null && (obj instanceof ContentStream ) ) {
    // copy the contents to the writer...
    ContentStream content = (ContentStream)obj;
    try(InputStream in = content.getStream()) {
      IOUtils.copy( in, out );
    }
  } else {
    QueryResponseWriterUtil.writeQueryResponse(out, 
        getBaseWriter(request), request, response, getContentType(request, response));
  }
}
 
Example #26
Source File: RawResponseWriter.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Override
public String getContentType(SolrQueryRequest request, SolrQueryResponse response) {
  Object obj = response.getValues().get( CONTENT );
  if( obj != null && (obj instanceof ContentStream ) ) {
    return ((ContentStream)obj).getContentType();
  }
  return getBaseWriter( request ).getContentType( request, response );
}
 
Example #27
Source File: SolrRequestParsers.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Override
public SolrParams parseParamsAndFillStreams(HttpServletRequest req, ArrayList<ContentStream> streams ) throws Exception {
  if (!isFormData(req)) {
    throw new SolrException(ErrorCode.BAD_REQUEST, "Not application/x-www-form-urlencoded content: " + req.getContentType());
  }

  return parseParamsAndFillStreams(req, streams, null);
}
 
Example #28
Source File: SolrRequestParsers.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public SolrParams parseParamsAndFillStreams(HttpServletRequest req, ArrayList<ContentStream> streams, InputStream in) throws Exception {
  final Map<String,String[]> map = new HashMap<>();

  // also add possible URL parameters and include into the map (parsed using UTF-8):
  final String qs = req.getQueryString();
  if (qs != null) {
    parseQueryString(qs, map);
  }

  // may be -1, so we check again later. But if it's already greater we can stop processing!
  final long totalLength = req.getContentLength();
  final long maxLength = ((long) uploadLimitKB) * 1024L;
  if (totalLength > maxLength) {
    throw new SolrException(ErrorCode.BAD_REQUEST, "application/x-www-form-urlencoded content length (" +
        totalLength + " bytes) exceeds upload limit of " + uploadLimitKB + " KB");
  }

  // get query String from request body, using the charset given in content-type:
  final String cs = ContentStreamBase.getCharsetFromContentType(req.getContentType());
  final Charset charset = (cs == null) ? StandardCharsets.UTF_8 : Charset.forName(cs);

  try {
    // Protect container owned streams from being closed by us, see SOLR-8933
    in = FastInputStream.wrap( in == null ? new CloseShieldInputStream(req.getInputStream()) : in );

    final long bytesRead = parseFormDataContent(in, maxLength, charset, map, false);
    if (bytesRead == 0L && totalLength > 0L) {
      throw getParameterIncompatibilityException();
    }
  } catch (IOException ioe) {
    throw new SolrException(ErrorCode.BAD_REQUEST, ioe);
  } catch (IllegalStateException ise) {
    throw (SolrException) getParameterIncompatibilityException().initCause(ise);
  } finally {
    IOUtils.closeWhileHandlingException(in);
  }

  return new MultiMapSolrParams(map);
}
 
Example #29
Source File: SolrRequestParsers.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Override
public SolrParams parseParamsAndFillStreams( 
    final HttpServletRequest req, ArrayList<ContentStream> streams ) throws Exception
{
  streams.add( new HttpRequestContentStream( req ) );
  return parseQueryString( req.getQueryString() );
}
 
Example #30
Source File: DocumentAnalysisRequestHandlerTest.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Test
public void testCharsetOutsideDocument() throws Exception {
  final byte[] xmlBytes = (
    "<docs>\r\n" +
    " <doc>\r\n" +
    "  <field name=\"id\">Müller</field>\r\n" +
    " </doc>" +
    "</docs>"
  ).getBytes(StandardCharsets.ISO_8859_1);
  
  // we declare a content stream with charset:
  final ContentStream cs = new ByteStream(xmlBytes, "application/xml; charset=ISO-8859-1");
  
  ModifiableSolrParams params = new ModifiableSolrParams();
  SolrQueryRequest req = new SolrQueryRequestBase(h.getCore(), params) {
    @Override
    public Iterable<ContentStream> getContentStreams() {
      return Collections.singleton(cs);
    }
  };

  DocumentAnalysisRequest request = handler.resolveAnalysisRequest(req);
  assertNotNull(request);
  final List<SolrInputDocument> documents = request.getDocuments();
  assertNotNull(documents);
  assertEquals(1, documents.size());
  SolrInputDocument doc = documents.get(0);
  assertEquals("Müller", doc.getField("id").getValue());
}