/*
 * 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
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * 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.opensoc.enrichment.adapters.threat;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.HConnection;
import org.apache.hadoop.hbase.client.HConnectionManager;
import org.apache.hadoop.hbase.client.HTableInterface;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.hadoop.hbase.KeyValue;
import org.apache.log4j.Logger;

@SuppressWarnings("unchecked")
public class ThreatHbaseAdapter extends AbstractThreatAdapter {

	private static final long serialVersionUID = 1L;
	private String _tableName;
	private HTableInterface table;
	private String _quorum;
	private String _port;

	public ThreatHbaseAdapter(String quorum, String port, String tableName) {
		_quorum = quorum;
		_port = port;
		_tableName = tableName;
	}

	/** The LOGGER. */
	private static final Logger LOGGER = Logger
			.getLogger(ThreatHbaseAdapter.class);

	public JSONObject enrich(String metadata) {

		JSONObject output = new JSONObject();
		LOGGER.debug("=======Looking Up For:" + metadata);
		output.putAll(getThreatObject(metadata));

		return output;
	}

	@SuppressWarnings({ "rawtypes", "deprecation" })
	protected Map getThreatObject(String key) {

		LOGGER.debug("=======Pinging HBase For:" + key);

		Get get = new Get(key.getBytes());
		Result rs;
		Map output = new HashMap();

		try {
			rs = table.get(get);

			
			byte[] source_family = Bytes.toBytes("source");
			JSONParser parser = new JSONParser();
			
			Map<byte[], byte[]> sourceFamilyMap = rs.getFamilyMap(source_family);
			
			for (Map.Entry<byte[], byte[]> entry  : sourceFamilyMap.entrySet()) {
				String k = Bytes.toString(entry.getKey());
				
				output.put(k,parser.parse(Bytes.toString(entry.getValue())));
            }
			

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return output;
	}

	@Override
	public boolean initializeAdapter() {

		// Initialize HBase Table
		Configuration conf = null;
		conf = HBaseConfiguration.create();
		conf.set("hbase.zookeeper.quorum", _quorum);
		conf.set("hbase.zookeeper.property.clientPort", _port);

		try {
			LOGGER.debug("=======Connecting to HBASE===========");
			LOGGER.debug("=======ZOOKEEPER = "
					+ conf.get("hbase.zookeeper.quorum"));
			HConnection connection = HConnectionManager.createConnection(conf);
			table = connection.getTable(_tableName);
			return true;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			LOGGER.debug("=======Unable to Connect to HBASE===========");
			e.printStackTrace();
		}

		return false;
	}

	@Override
	public String enrichByIP(String metadata) {
		return null;
	}

	@Override
	public String enrichByDomain(String metadata) {
		return null;
	}

	@Override
	public String enrichByEmail(String metadata) {
		return null;
	}

}

@@ -2417,16 +2417,18 @@
 + key);%0A
+%09%09
 %0A%09%09Get g
@@ -2444,21 +2444,25 @@
 Get(
-key.get
+Bytes.to
 Bytes(
+key
 ));%0A
@@ -2541,17 +2541,38 @@
 t);%0A%0A%09%09%09
-%0A
+if (!rs.isEmpty()) %7B%0A%09
 %09%09%09byte%5B
@@ -2614,16 +2614,17 @@
 urce%22);%0A
+%09
 %09%09%09JSONP
@@ -2659,17 +2659,19 @@
 r();%0A%09%09%09
-%0A
+%09%0A%09
 %09%09%09Map%3Cb
@@ -2738,20 +2738,22 @@
 ly);%0A%09%09%09
+%09
 %0A%09%09%09
+%09
 for (Map
@@ -2818,16 +2818,17 @@
 ) %7B%0A%09%09%09%09
+%09
 String k
@@ -2862,25 +2862,80 @@
 Key());%0A%09%09%09%09
+%09
-%0A
+LOGGER.debug(%22=======Found intel from source: %22 + k);%0A%09
 %09%09%09%09output.p
@@ -2988,16 +2988,17 @@
 e())));%0A
+%09
         
@@ -3006,17 +3006,17 @@
    %7D%0A%09%09%09
-%0A
+%7D
 %0A%09%09%7D cat

check for non-empty results before iterating over value map