Java Code Examples for org.apache.solr.common.util.ContentStream#getContentType()

The following examples show how to use org.apache.solr.common.util.ContentStream#getContentType() . 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: 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 2
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 3
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 4
Source File: RdfBulkUpdateRequestHandler.java    From SolRDF with Apache License 2.0 4 votes vote down vote up
@Override
public void load(
		final SolrQueryRequest request, 
		final SolrQueryResponse response,
		final ContentStream stream, 
		final UpdateRequestProcessor processor) throws Exception {
	
	// Default ContentStream implementation starts reading the stream and
	// if it starts with '<' then it assumes a content type of "application/xml", 
	// if it starts with '{' then it assumes a content type of "application/json" 			
	// This behaviour is wrong is SolRDF and maybe we need a custom ContentStream here
	// At the moment this is just a workaround:
	final String contentType = stream.getContentType() != null 
			&& !"application/xml".equals(stream.getContentType())
			&& !"application/json".equals(stream.getContentType()) 
				? stream.getContentType() 
				: request.getParams().get(UpdateParams.ASSUME_CONTENT_TYPE);
	
	log.debug(MessageCatalog._00094_BULK_LOADER_CT, contentType);			
				
	final Lang lang = RDFLanguages.contentTypeToLang(contentType);
	if (lang == null) {
		final String message = MessageFactory.createMessage(MessageCatalog._00095_INVALID_CT, contentType);
		log.error(message);							
		throw new SolrException(ErrorCode.BAD_REQUEST, message);
	}
	
	final ContentStreamLoader delegate = 
			(lang == Lang.NQ || lang == Lang.NQUADS || lang == Lang.TRIG)
				? quadsLoader
				: triplesLoader;
	
	log.debug(MessageCatalog._00096_SELECTED_BULK_LOADER, contentType, delegate);
	
	delegate.load(
			request, 
			response, 
			new ContentStream() {	
				@Override
				public InputStream getStream() throws IOException {
					return stream.getStream();
				}
				
				@Override
				public String getSourceInfo() {
					return stream.getSourceInfo();
				}
				
				@Override
				public Long getSize() {
					return stream.getSize();
				}
				
				@Override
				public Reader getReader() throws IOException {
					return stream.getReader();
				}
				
				@Override
				public String getName() {
					return stream.getName();
				}
				
				@Override
				public String getContentType() {
					return contentType;
				}
			}, 
		processor);
}