// get the type of Solr update handler we want to mock, default to xml
final String handler = request.hasParam("handler") ? request.param("handler").toLowerCase() : "xml";
// Requests are typically sent to Solr in batches of documents
// We can copy that by submitting batch requests to Solr
BulkRequest bulkRequest = Requests.bulkRequest();
// parse and handle the content
if (handler.equals("xml")) {
// XML Content
try {
// create parser for the content
XMLStreamReader parser = inputFactory.createXMLStreamReader(new StringReader(request.content().toUtf8()));
// parse the xml
// we only care about doc and delete tags for now
boolean stop = false;
while (!stop) {
// get the xml "event"
int event = parser.next();
switch (event) {
case XMLStreamConstants.END_DOCUMENT :
// this is the end of the document
// close parser and exit while loop
parser.close();
stop = true;
break;
case XMLStreamConstants.START_ELEMENT :
// start of an xml tag
// determine if we need to add or delete a document
String currTag = parser.getLocalName();
if ("doc".equals(currTag)) {
// add a document
Map<String, Object> doc = parseXmlDoc(parser);
if (doc != null) {
bulkRequest.add(getIndexRequest(doc, request));
}
} else if ("delete".equals(currTag)) {
// delete a document
String docid = parseXmlDelete(parser);
if (docid != null) {
bulkRequest.add(getDeleteRequest(docid, request));
}
}
break;
}
}
} catch (Exception e) {
// some sort of error processing the xml input
try {
logger.error("Error processing xml input", e);
channel.sendResponse(new XContentThrowableRestResponse(request, e));
} catch (IOException e1) {
logger.error("Failed to send error response", e1);
}
}
} else if (handler.equals("javabin")) {
// JavaBin Content
try {
// We will use the JavaBin codec from solrj
// unmarshal the input to a SolrUpdate request
JavaBinUpdateRequestCodec codec = new JavaBinUpdateRequestCodec();
UpdateRequest req = codec.unmarshal(new ByteArrayInputStream(request.content().array()), null);
// Get the list of documents to index out of the UpdateRequest
// Add each document to the bulk request
// convert the SolrInputDocument into a map which will be used as the ES source field
List<SolrInputDocument> docs = req.getDocuments();
if (docs != null) {
for (SolrInputDocument doc : docs) {
bulkRequest.add(getIndexRequest(convertToMap(doc), request));
}
}
// See if we have any documents to delete
// if yes, add them to the bulk request
if (req.getDeleteById() != null) {
for (String id : req.getDeleteById()) {
bulkRequest.add(getDeleteRequest(id, request));
}
}
} catch (Exception e) {
// some sort of error processing the javabin input
try {
logger.error("Error processing javabin input", e);
channel.sendResponse(new XContentThrowableRestResponse(request, e));
} catch (IOException e1) {
logger.error("Failed to send error response", e1);
}
}
}
// only submit the bulk request if there are index/delete actions
// it is possible not to have any actions when parsing xml due to the
// commit and optimize messages that will not generate documents
if (bulkRequest.numberOfActions() > 0) {
client.bulk(bulkRequest, new ActionListener<BulkResponse>() {
// successful bulk request
public void onResponse(BulkResponse response) {
logger.info("Bulk request completed");