001package org.xbib.elasticsearch.action.websocket.bulk; 002 003import org.elasticsearch.action.index.IndexRequest; 004import org.elasticsearch.client.Client; 005import org.elasticsearch.client.Requests; 006import org.elasticsearch.common.inject.Inject; 007import org.elasticsearch.common.settings.Settings; 008import org.xbib.elasticsearch.websocket.InteractiveChannel; 009import org.xbib.elasticsearch.websocket.InteractiveController; 010import org.xbib.elasticsearch.websocket.InteractiveRequest; 011 012import java.io.IOException; 013import java.util.Map; 014 015public class BulkIndexAction extends BulkHandler { 016 017 private final static String TYPE = "index"; 018 019 @Inject 020 public BulkIndexAction(Settings settings, Client client, InteractiveController controller) { 021 super(settings, client); 022 controller.registerHandler(TYPE, this); 023 } 024 025 @Override 026 public void handleRequest(final InteractiveRequest request, final InteractiveChannel channel) { 027 String index = request.paramAsString("index"); 028 String type = request.paramAsString("type"); 029 String id = request.paramAsString("id"); 030 try { 031 if (index == null) { 032 channel.sendResponse(TYPE, new IllegalArgumentException("index is null")); 033 return; 034 } 035 if (type == null) { 036 channel.sendResponse(TYPE, new IllegalArgumentException("type is null")); 037 return; 038 } 039 if (id == null) { 040 channel.sendResponse(TYPE, new IllegalArgumentException("id is null")); 041 return; 042 } 043 IndexRequest indexRequest = Requests.indexRequest(index).type(type).id(id) 044 .source((Map<String, Object>) request.asMap().get("data")); 045 add(indexRequest, channel); 046 } catch (IOException ex) { 047 try { 048 channel.sendResponse(TYPE, ex); 049 } catch (IOException ex1) { 050 logger.error("error while sending exception"); 051 } 052 } 053 } 054}