/**
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.apache.kafka.connect.solr.util;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.concurrent.LinkedBlockingQueue;

import org.apache.http.HttpHeaders;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.EntityBuilder;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.apache.kafka.connect.solr.config.SolrConnectorConfig;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;

public class SolrServerClient {
	
	private static final Logger logger = LoggerFactory.getLogger(SolrServerClient.class);
	
	private final SolrConnectorConfig _config;
	private LinkedBlockingQueue<HashMap<String,Object>> _batchQueue = null;
	private TypeReference<HashMap<String,Object>> typeRef = new TypeReference<HashMap<String,Object>>() {};
	private  ObjectMapper mapper;
	public SolrServerClient (final SolrConnectorConfig _config) {
		this._config = _config;
		this._batchQueue = new LinkedBlockingQueue<HashMap<String,Object>>(_config.getBulkSize());
		mapper = new ObjectMapper();
	}
	
	public void write(byte[] data) {
		
		try {
			this._batchQueue.add((HashMap<String,Object>)mapper.readValue(data, typeRef));
		} catch (JsonParseException e) {
			logger.error("Json processing EXception" + e);
		} catch (JsonMappingException e) {
			logger.error("Json processing EXception" + e);
		} catch (IOException e) {
			
			logger.error("Json IO EXception" + e);
		}
		 if(this._batchQueue.remainingCapacity() == 0) {
			 ArrayList<HashMap<String,Object>>  drainToArray = new ArrayList<HashMap<String,Object>>();
			 this._batchQueue.drainTo(drainToArray);
			 try {
				writeToSolr(mapper.writeValueAsBytes(drainToArray));
			} catch (JsonProcessingException e) {
				logger.error("Json processing EXception" + e);
			}
		 }
	}
	
	private void writeToSolr(byte[] data) {
		
		CloseableHttpClient client = HttpClients.createDefault();
		HttpPost post = new HttpPost(this._config.getClusterUrl());
		post.setHeader(HttpHeaders.CONTENT_TYPE, "application/json;charset=UTF-8");
		
		try {
			EntityBuilder eb = EntityBuilder.create();
			eb.setBinary(data);
			post.setEntity(eb.build());
			HttpResponse response = client.execute(post);
			//if( logger.isInfoEnabled() )
			logger.info( "[Response] :: " +EntityUtils.toString(response.getEntity()));
			
			
		} catch (ClientProtocolException clientException) {
			logger.error(clientException.getMessage());
			
		} catch (IOException ioException) {
			logger.error(ioException.getMessage());
		} finally {
			try {
				client.close();
			} catch (IOException e) {
				logger.error("Exception occured when closing connection."+e)
			}
		}
	}
	
}

@@ -1790,18 +1790,17 @@
 lient %7B%0A
-%09
 %0A
+
 %09private
@@ -1878,18 +1878,17 @@
 class);%0A
-%09
 %0A
+
 %09private
@@ -2348,18 +2348,17 @@
 r();%0A%09%7D%0A
-%09
 %0A
+
 %09public 
@@ -2383,19 +2383,17 @@
 data) %7B%0A
-%09%09
 %0A
+
 %09%09try %7B%0A
@@ -2674,20 +2674,17 @@
 on e) %7B%0A
-%09%09%09
 %0A
+
 %09%09%09logge
@@ -3083,18 +3083,17 @@
 %09%09 %7D%0A%09%7D%0A
-%09
 %0A
+
 %09private
@@ -3125,19 +3125,17 @@
 data) %7B%0A
-%09%09
 %0A
+
 %09%09Closea
@@ -3326,19 +3326,17 @@
 TF-8%22);%0A
-%09%09
 %0A
+
 %09%09try %7B%0A
@@ -3601,15 +3601,9 @@
 ));%0A
-%09%09%09%0A%09%09%09
+%0A
 %0A%09%09%7D
@@ -3704,12 +3704,9 @@
 ));%0A
-%09%09%09
 %0A
+
 %09%09%7D 
@@ -3918,16 +3918,17 @@
 ion.%22+e)
+;
 %0A%09%09%09%7D%0A%09%09
@@ -3932,12 +3932,11 @@
 %0A%09%09%7D%0A%09%7D%0A
-%09
 %0A%7D%0A

Fix missing semicolon