All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.threatconnect.sdk.client.writer.FileIndicatorWriterAdapter Maven / Gradle / Ivy

Go to download

The ThreatConnect Java SDK. Used to communicate with teh ThreatConnect Threat Intelligence Platform

The newest version!
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.threatconnect.sdk.client.writer;

import com.threatconnect.sdk.client.response.WriteListResponse;
import com.threatconnect.sdk.conn.Connection;
import com.threatconnect.sdk.exception.FailedResponseException;
import com.threatconnect.sdk.server.entity.File;
import com.threatconnect.sdk.server.entity.FileOccurrence;
import com.threatconnect.sdk.server.response.entity.ApiEntitySingleResponse;
import com.threatconnect.sdk.server.response.entity.FileOccurrenceListResponse;
import com.threatconnect.sdk.server.response.entity.FileOccurrenceResponse;
import com.threatconnect.sdk.server.response.entity.FileResponse;

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

/**
 *
 * @author dtineo
 */
public class FileIndicatorWriterAdapter extends AbstractIndicatorWriterAdapter {

    protected FileIndicatorWriterAdapter(Connection conn) {
        super(conn, FileResponse.class);
    }

    @Override
    public String getUrlType() {
        return "files";
    }

    @Override
    public String getId(File file) {
        String hash = file.getMd5();
        if (hash != null) {
            return hash;
        }

        hash = file.getSha1();
        if (hash != null) {
            return hash;
        }

        hash = file.getSha256();
        if (hash != null) {
            return hash;
        }

        return null;
    }
    
    public WriteListResponse updateFileOccurrences(String fileHash, List fileOccurrences)
        throws IOException {
        return updateFileOccurrences(fileHash, fileOccurrences, null);
    }

    public WriteListResponse updateFileOccurrences(String fileHash, List fileOccurrences, String ownerName)
        throws IOException {

        Map map = createParamMap("id", fileHash);
        List idList = new ArrayList<>();
        for(FileOccurrence it : fileOccurrences)    idList.add( it.getId() );
        WriteListResponse data = updateListWithParam(getUrlBasePrefix() + ".byId.fileOccurrences.byFileOccurrenceId", FileOccurrenceListResponse.class, ownerName, map, "fileOccurrenceId", idList, fileOccurrences);

        return data;
    }

    public FileOccurrence updateFileOccurrence(String fileHash, FileOccurrence fileOccurrence) throws IOException, FailedResponseException {
        return updateFileOccurrence(fileHash, fileOccurrence, null);
    }

    public FileOccurrence updateFileOccurrence(String fileHash, FileOccurrence fileOccurrence, String ownerName)
        throws IOException, FailedResponseException {

        Map map = createParamMap("id", fileHash, "fileOccurrenceId", fileOccurrence.getId());
        FileOccurrenceResponse item = updateItem(getUrlBasePrefix() + ".byId.fileOccurrences.byFileOccurrenceId"
                                            , FileOccurrenceResponse.class, ownerName, map, fileOccurrence);

        return (FileOccurrence) item.getData().getData();
    }

    public FileOccurrence createFileOccurrence(String fileHash, FileOccurrence fileOccurrence) throws IOException, FailedResponseException {
        return createFileOccurrence(fileHash, fileOccurrence, null);
    }

    public FileOccurrence createFileOccurrence(String fileHash, FileOccurrence fileOccurrence, String ownerName)
            throws IOException, FailedResponseException {

        Map map = createParamMap("id", fileHash);
        FileOccurrenceResponse item = createItem(getUrlBasePrefix() + ".byId.fileOccurrences"
                , FileOccurrenceResponse.class, ownerName, map, fileOccurrence);

        return (FileOccurrence) item.getData().getData();
    }

    public ApiEntitySingleResponse deleteFileOccurrence(String fileHash, FileOccurrence fileOccurrence) throws IOException, FailedResponseException {
        return deleteFileOccurrence(fileHash, fileOccurrence, null);
    }

    public ApiEntitySingleResponse deleteFileOccurrence(String fileHash, FileOccurrence fileOccurrence, String ownerName)
            throws IOException, FailedResponseException {

        Map map = createParamMap("id", fileHash, "fileOccurrenceId", fileOccurrence.getId());
        ApiEntitySingleResponse item = deleteItem(getUrlBasePrefix() + ".byId.fileOccurrences.byFileOccurrenceId"
                , FileOccurrenceResponse.class, ownerName, map);

        return item;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy