use of org.apache.solr.common.util.ContentStream in project lucene-solr by apache.
the class ConfigSetsHandler method handleConfigUploadRequest.
private void handleConfigUploadRequest(SolrQueryRequest req, SolrQueryResponse rsp) throws Exception {
String configSetName = req.getParams().get(NAME);
if (StringUtils.isBlank(configSetName)) {
throw new SolrException(ErrorCode.BAD_REQUEST, "The configuration name should be provided in the \"name\" parameter");
}
SolrZkClient zkClient = coreContainer.getZkController().getZkClient();
String configPathInZk = ZkConfigManager.CONFIGS_ZKNODE + Path.SEPARATOR + configSetName;
if (zkClient.exists(configPathInZk, true)) {
throw new SolrException(ErrorCode.BAD_REQUEST, "The configuration " + configSetName + " already exists in zookeeper");
}
Iterator<ContentStream> contentStreamsIterator = req.getContentStreams().iterator();
if (!contentStreamsIterator.hasNext()) {
throw new SolrException(ErrorCode.BAD_REQUEST, "No stream found for the config data to be uploaded");
}
InputStream inputStream = contentStreamsIterator.next().getStream();
// Create a node for the configuration in zookeeper
boolean trusted = getTrusted(req);
zkClient.makePath(configPathInZk, ("{\"trusted\": " + Boolean.toString(trusted) + "}").getBytes(StandardCharsets.UTF_8), true);
ZipInputStream zis = new ZipInputStream(inputStream, StandardCharsets.UTF_8);
ZipEntry zipEntry = null;
while ((zipEntry = zis.getNextEntry()) != null) {
String filePathInZk = configPathInZk + "/" + zipEntry.getName();
if (zipEntry.isDirectory()) {
zkClient.makePath(filePathInZk, true);
} else {
createZkNodeIfNotExistsAndSetData(zkClient, filePathInZk, IOUtils.toByteArray(zis));
}
}
zis.close();
}
use of org.apache.solr.common.util.ContentStream in project lucene-solr by apache.
the class DataImportHandler method handleRequestBody.
@Override
@SuppressWarnings("unchecked")
public void handleRequestBody(SolrQueryRequest req, SolrQueryResponse rsp) throws Exception {
rsp.setHttpCaching(false);
//TODO: figure out why just the first one is OK...
ContentStream contentStream = null;
Iterable<ContentStream> streams = req.getContentStreams();
if (streams != null) {
for (ContentStream stream : streams) {
contentStream = stream;
break;
}
}
SolrParams params = req.getParams();
NamedList defaultParams = (NamedList) initArgs.get("defaults");
RequestInfo requestParams = new RequestInfo(req, getParamsMap(params), contentStream);
String command = requestParams.getCommand();
if (DataImporter.SHOW_CONF_CMD.equals(command)) {
String dataConfigFile = params.get("config");
String dataConfig = params.get("dataConfig");
if (dataConfigFile != null) {
dataConfig = SolrWriter.getResourceAsString(req.getCore().getResourceLoader().openResource(dataConfigFile));
}
if (dataConfig == null) {
rsp.add("status", DataImporter.MSG.NO_CONFIG_FOUND);
} else {
// Modify incoming request params to add wt=raw
ModifiableSolrParams rawParams = new ModifiableSolrParams(req.getParams());
rawParams.set(CommonParams.WT, "raw");
req.setParams(rawParams);
ContentStreamBase content = new ContentStreamBase.StringStream(dataConfig);
rsp.add(RawResponseWriter.CONTENT, content);
}
return;
}
rsp.add("initArgs", initArgs);
String message = "";
if (command != null) {
rsp.add("command", command);
}
// If importer is still null
if (importer == null) {
rsp.add("status", DataImporter.MSG.NO_INIT);
return;
}
if (command != null && DataImporter.ABORT_CMD.equals(command)) {
importer.runCmd(requestParams, null);
} else if (importer.isBusy()) {
message = DataImporter.MSG.CMD_RUNNING;
} else if (command != null) {
if (DataImporter.FULL_IMPORT_CMD.equals(command) || DataImporter.DELTA_IMPORT_CMD.equals(command) || IMPORT_CMD.equals(command)) {
importer.maybeReloadConfiguration(requestParams, defaultParams);
UpdateRequestProcessorChain processorChain = req.getCore().getUpdateProcessorChain(params);
UpdateRequestProcessor processor = processorChain.createProcessor(req, rsp);
SolrResourceLoader loader = req.getCore().getResourceLoader();
DIHWriter sw = getSolrWriter(processor, loader, requestParams, req);
if (requestParams.isDebug()) {
if (debugEnabled) {
// Synchronous request for the debug mode
importer.runCmd(requestParams, sw);
rsp.add("mode", "debug");
rsp.add("documents", requestParams.getDebugInfo().debugDocuments);
if (requestParams.getDebugInfo().debugVerboseOutput != null) {
rsp.add("verbose-output", requestParams.getDebugInfo().debugVerboseOutput);
}
} else {
message = DataImporter.MSG.DEBUG_NOT_ENABLED;
}
} else {
// Asynchronous request for normal mode
if (requestParams.getContentStream() == null && !requestParams.isSyncMode()) {
importer.runAsync(requestParams, sw);
} else {
importer.runCmd(requestParams, sw);
}
}
} else if (DataImporter.RELOAD_CONF_CMD.equals(command)) {
if (importer.maybeReloadConfiguration(requestParams, defaultParams)) {
message = DataImporter.MSG.CONFIG_RELOADED;
} else {
message = DataImporter.MSG.CONFIG_NOT_RELOADED;
}
}
}
rsp.add("status", importer.isBusy() ? "busy" : "idle");
rsp.add("importResponse", message);
rsp.add("statusMessages", importer.getStatusMessages());
}
use of org.apache.solr.common.util.ContentStream in project lucene-solr by apache.
the class FieldAnalysisRequestHandler method resolveAnalysisRequest.
// ================================================= Helper methods ================================================
/**
* 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;
}
use of org.apache.solr.common.util.ContentStream in project lucene-solr by apache.
the class DocumentAnalysisRequestHandler method resolveAnalysisRequest.
//================================================ Helper Methods ==================================================
/**
* Resolves the {@link DocumentAnalysisRequest} from the given solr request.
*
* @param req The solr request.
*
* @return The resolved document analysis request.
*
* @throws IOException Thrown when reading/parsing the content stream of the request fails.
* @throws XMLStreamException Thrown when reading/parsing the content stream of the request fails.
*/
DocumentAnalysisRequest resolveAnalysisRequest(SolrQueryRequest req) throws IOException, XMLStreamException {
DocumentAnalysisRequest request = new DocumentAnalysisRequest();
SolrParams params = req.getParams();
String query = params.get(AnalysisParams.QUERY, params.get(CommonParams.Q, null));
request.setQuery(query);
boolean showMatch = params.getBool(AnalysisParams.SHOW_MATCH, false);
request.setShowMatch(showMatch);
ContentStream stream = extractSingleContentStream(req);
InputStream is = null;
XMLStreamReader parser = null;
try {
is = stream.getStream();
final String charset = ContentStreamBase.getCharsetFromContentType(stream.getContentType());
parser = (charset == null) ? inputFactory.createXMLStreamReader(is) : inputFactory.createXMLStreamReader(is, charset);
while (true) {
int event = parser.next();
switch(event) {
case XMLStreamConstants.END_DOCUMENT:
{
parser.close();
return request;
}
case XMLStreamConstants.START_ELEMENT:
{
String currTag = parser.getLocalName();
if ("doc".equals(currTag)) {
log.trace("Reading doc...");
SolrInputDocument document = readDocument(parser, req.getSchema());
request.addDocument(document);
}
break;
}
}
}
} finally {
if (parser != null)
parser.close();
IOUtils.closeQuietly(is);
}
}
use of org.apache.solr.common.util.ContentStream in project lucene-solr by apache.
the class DocumentAnalysisRequestHandler method extractSingleContentStream.
/**
* 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;
}
Aggregations