Skip to content

OTX-Java-SDK retrieve pulse and other information on periodic basic. #4

@manojsharma20

Description

@manojsharma20

I want to run OTX-Java-SDK infinitely in background , like using deoman thread. For this i am using Future and ExecuteService. It is working fine for first time when it runs, but when i resubmit the task after each one minute it does not retrieve it. I have also increase the time from 1 minute to 1 hour, but still it does work for me.

I am submitting my code please check and suggest me the solution.

package com.alienvault.otx;

import com.alienvault.otx.connect.ConnectionUtil;
import com.alienvault.otx.connect.OTXConnection;
import com.alienvault.otx.model.indicator.Indicator;
import com.alienvault.otx.model.indicator.IndicatorType;
import com.alienvault.otx.model.pulse.Pulse;
import com.fasterxml.jackson.databind.deser.std.DateDeserializers.CalendarDeserializer;

import org.apache.commons.cli.*;
import org.joda.time.DateTime;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.util.StringUtils;
import org.springframework.web.client.HttpClientErrorException;

import java.io.;
import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.util.
;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

@SpringBootApplication
public class CommandlineRunner {
private static final ExecutorService threadpool = Executors.newFixedThreadPool(30);
private static Options getOptions() {
Options opts = new Options();
opts.addOption("k", "key", true, "API Key from OTX Settings Page (https://otx.alienvault.com/settings/).");
opts.addOption("o", "output-file", true, "File to save indicators");
opts.addOption("d", "date", true, "Only pulses modified since the date provided will be downloaded");
opts.addOption("i", "indicators", true, "Indicator types to save to the file. Provide a comma separated string of indicators (" + IndicatorType.toTypeList() + ")");
return opts;

}

public static void main(String[] args) throws InterruptedException {
    ConfigurableApplicationContext run = SpringApplication.run(CommandlineRunner.class,args);

    List<String> filteredArgs = new ArrayList<>();
    for (String arg : args) {
        if (!"--spring.output.ansi.enabled=always".equals(arg))
            filteredArgs.add(arg);
        System.out.println(arg);
    }
    GetOXTData otx = new GetOXTData(filteredArgs, run);
    System.out.println("Submitting Task ..."); 
    Future future = threadpool.submit(otx); 

    System.out.println("Task is submitted");
    int counter = 0;
    while (true) {
        if(future.isDone()){
            if(counter == 0){
                otx.setDate(new DateTime(Calendar.getInstance().getTime()));
                counter++;
            } else{

            }
            DateTime gdate = otx.getDate();
            Calendar calendar = gdate.toCalendar(Locale.getDefault());
            Date d1 = calendar.getTime();
            Date d2 = Calendar.getInstance().getTime();

            int diffInMins = (int)((d2.getTime() - d1.getTime())/(60*60*1000));
            if(diffInMins < 59){
                diffInMins = 60;
            } else {
                diffInMins = 60;
            }
            calendar.add(Calendar.MINUTE, diffInMins);
            DateTime d = new DateTime(calendar.getTime());
            if(Calendar.getInstance().compareTo(calendar) >= 0){
                otx.setDate(d);
                System.out.println("running for next " + diffInMins+ " Minutes"); 
                Future submit = threadpool.submit(otx);
                future = submit;
            } else{
                Thread.sleep(36000000);
            }
        } 
    }

// while(!future.isDone()){
// System.out.println("Not completed yet, continue for next 1 seconds");
// Thread.sleep(1000);
// }

// threadpool.shutdown();
}

private static Set<IndicatorType> parseTypes(String types) throws ParseException {
    Set<String> strings = StringUtils.commaDelimitedListToSet(types);
    Set<IndicatorType> ret = new HashSet<>();
    for (String string : strings) {
        try {
            ret.add(IndicatorType.valueOf(string.toUpperCase()));
        } catch (IllegalArgumentException e) {
            throw new ParseException("Error parsing enum type: " +string);
        }
    }
    return ret;
}

private static PrintWriter getPrintStream(File dest) throws FileNotFoundException {
    if (dest == null)
        return new PrintWriter(System.out);
    else
        return new PrintWriter(new FileOutputStream(dest));
}

private static void printUsage() {
    HelpFormatter formatter = new HelpFormatter();
    formatter.setWidth(80);
    formatter.printHelp("otx", getOptions());
}

private static class GetOXTData implements Callable { 

    List<String> filteredArgs = null;
    CommandLineParser parser = null;
    CommandLine cmd = null;
    ConfigurableApplicationContext run = null;
    PrintWriter outs = null;
    Set<IndicatorType> types = new HashSet<>(Arrays.asList(IndicatorType.values()));

    String apiKey;
    DateTime date = null;
    File dest = null;



    public DateTime getDate() {
        return date;
    }

    public void setDate(DateTime date) {
        this.date = date;
    }

    public GetOXTData(List<String> filteredArgs, ConfigurableApplicationContext run) {

        this.filteredArgs = filteredArgs;
        this.run = run;

        parser = new BasicParser();
        try {
            cmd = parser.parse(getOptions(), filteredArgs.toArray(new String[filteredArgs.size()]));
            if (cmd.hasOption('k'))
                apiKey = cmd.getOptionValue('k');
            else
                throw new ParseException("-key is a required option");
            if (cmd.hasOption('d'))
                date = DateTime.parse(cmd.getOptionValue('d'));
            if (cmd.hasOption('o'))
                dest = new File(cmd.getOptionValue('o'));

            outs = getPrintStream(dest);

            if (cmd.hasOption('i'))
                types = parseTypes(cmd.getOptionValue('i'));

        } catch (ParseException e) {
            System.out.println("Error parsing commandline options: " + e.getMessage());
            printUsage();
        } catch (FileNotFoundException e) {
            System.out.println("Error writing to the output file: " + e.getMessage());
        }
    }

    @Override
    public Boolean call() {

        boolean isDone = false;
        try {

            OTXConnection connection = ConnectionUtil.getOtxConnection(run.getEnvironment(), apiKey);
            System.out.println("--------------------------- Connection Established -----------------------");
            List<Pulse> pulses;
            if (date != null){
                pulses = connection.getPulsesSinceDate(date);
                System.out.println("--------------------------- all pulse getting from date -----------------------");
            } else{
                pulses = connection.getAllPulses();
                System.out.println("--------------------------- All pulse getting, no date specify -----------------------");
            }
            System.out.println("--------------------------- Looping pulse -----------------------");
            for (Pulse pulse : pulses) {
                List<Indicator> indicators = pulse.getIndicators();
                for (Indicator indicator : indicators) {
                    if (types.contains(indicator.getType())) {
                        outs.println(indicator.getIndicator());
                        outs.flush();
                    }
                }
            }
            isDone = true;
        } catch (HttpClientErrorException ex) {
            System.out.println("Error retrieving data: " + ex.getMessage());
        } catch (URISyntaxException | MalformedURLException e) {
            isDone = false;
            System.out.println("Error configuring OTX connection: " + e.getMessage());
        }
        return isDone;
    }
}

}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions