Search in sources :

Example 1 with ScanFileRequest

use of ai.nightfall.scan.model.ScanFileRequest in project nightfall-java-sdk by nightfallai.

the class FileScannerExample method main.

/**
 * Submit the provided files for scanning and print the result.
 */
public static void main(String[] args) {
    if (args.length != 2) {
        throw new RuntimeException(usage);
    }
    // File scans are conducted asynchronously, so provide a webhook route to an HTTPS server to send results to.
    String webhookResponseListenerURL = args[0];
    String file = args[1];
    try (NightfallClient client = NightfallClient.Builder.defaultClient()) {
        // Define some detectors to use to scan your data
        Detector creditCard = new Detector("CREDIT_CARD_NUMBER");
        creditCard.setMinConfidence(Confidence.LIKELY);
        creditCard.setMinNumFindings(1);
        Detector ssn = new Detector("US_SOCIAL_SECURITY_NUMBER");
        ssn.setMinConfidence(Confidence.POSSIBLE);
        ssn.setMinNumFindings(1);
        // A rule contains a set of detectors to scan with
        DetectionRule rule = new DetectionRule(Arrays.asList(creditCard, ssn), LogicalOp.ANY);
        ScanPolicy policy = ScanPolicy.fromDetectionRules(Arrays.asList(rule), webhookResponseListenerURL);
        ScanFileRequest req = new ScanFileRequest(policy, "my request metadata");
        // Upload the data to the API, then trigger the async scan
        File file1 = new File(file);
        try (InputStream stream = new FileInputStream(file1)) {
            ScanFileResponse response = client.scanFile(req, stream, file1.length());
            System.out.println("started scan: " + response.toString());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
Also used : NightfallClient(ai.nightfall.scan.NightfallClient) ScanFileResponse(ai.nightfall.scan.model.ScanFileResponse) Detector(ai.nightfall.scan.model.Detector) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ScanPolicy(ai.nightfall.scan.model.ScanPolicy) DetectionRule(ai.nightfall.scan.model.DetectionRule) IOException(java.io.IOException) File(java.io.File) ScanFileRequest(ai.nightfall.scan.model.ScanFileRequest) FileInputStream(java.io.FileInputStream)

Example 2 with ScanFileRequest

use of ai.nightfall.scan.model.ScanFileRequest in project nightfall-java-sdk by nightfallai.

the class NightfallClientTest method testScanFile_Timeout.

@Test
public void testScanFile_Timeout() {
    assertThrows(NightfallRequestTimeoutException.class, () -> {
        try (MockWebServer server = new MockWebServer()) {
            server.enqueue(new MockResponse().setBody("{\"id\": \"2eda1019-f991-4535-be9f-cecbe6b6c2eb\"," + "\"fileSizeBytes\": 1738, \"mimeType\": \"text/plain\", \"chunkSize\": 10485760}"));
            server.enqueue(new MockResponse().setResponseCode(204));
            server.enqueue(new MockResponse().setBody("{\"id\": \"2eda1019-f991-4535-be9f-cecbe6b6c2eb\"," + "\"fileSizeBytes\": 1738, \"mimeType\": \"text/plain\", \"chunkSize\": 10485760}"));
            server.enqueue(new MockResponse().setBody("{\"id\": \"2eda1019-f991-4535-be9f-cecbe6b6c2eb\"," + "\"message\": \"scan initiated\"}"));
            NightfallClient c = new NightfallClient(getRequestURL(server), "key", 1, getHttpClient());
            ScanFileRequest req = new ScanFileRequest(new ScanPolicy("foo", null, null), "foo");
            Duration timeout = Duration.ofMillis(1);
            c.scanFile(req, new ByteArrayInputStream(new byte[1738]), 1738, timeout);
            fail("did not expect scan to succeed");
        } catch (IOException e) {
            fail("IOException during test: " + e.getMessage());
        }
    });
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) ByteArrayInputStream(java.io.ByteArrayInputStream) ScanPolicy(ai.nightfall.scan.model.ScanPolicy) MockWebServer(okhttp3.mockwebserver.MockWebServer) Duration(java.time.Duration) IOException(java.io.IOException) ScanFileRequest(ai.nightfall.scan.model.ScanFileRequest) Test(org.junit.jupiter.api.Test)

Example 3 with ScanFileRequest

use of ai.nightfall.scan.model.ScanFileRequest in project nightfall-java-sdk by nightfallai.

the class NightfallClientTest method testScanFile_InvalidContent.

@Test
public void testScanFile_InvalidContent() {
    assertThrows(IllegalArgumentException.class, () -> {
        NightfallClient c = new NightfallClient("url", "key", 1, getHttpClient());
        ScanFileRequest req = new ScanFileRequest(UUID.randomUUID(), "");
        c.scanFile(req, null, 0, null);
        fail("should not have completed call");
    });
}
Also used : ScanFileRequest(ai.nightfall.scan.model.ScanFileRequest) Test(org.junit.jupiter.api.Test)

Example 4 with ScanFileRequest

use of ai.nightfall.scan.model.ScanFileRequest in project nightfall-java-sdk by nightfallai.

the class NightfallClientTest method testScanFile_APIException.

@Test
public void testScanFile_APIException() {
    assertThrows(NightfallAPIException.class, () -> {
        // use reference type to work around Java inner class limitations
        final int[] reqCount = { 0 };
        try (MockWebServer server = new MockWebServer()) {
            final String successBody = "{\"id\": \"2eda1019-f991-4535-be9f-cecbe6b6c2eb\"," + "\"fileSizeBytes\": 1738, \"mimeType\": \"text/plain\", \"chunkSize\": 10485760}";
            final String errBody = "{\"code\": 400, \"message\": \"failure\", \"description\": \"failure\"}";
            server.setDispatcher(new Dispatcher() {

                @NotNull
                @Override
                public MockResponse dispatch(@NotNull RecordedRequest r) {
                    if (++reqCount[0] == 1) {
                        return new MockResponse().setBody(successBody);
                    }
                    // fail chunk upload attempts
                    return new MockResponse().setResponseCode(400).setBody(errBody);
                }
            });
            NightfallClient c = new NightfallClient(getRequestURL(server), "key", 1, getHttpClient());
            ScanFileRequest req = new ScanFileRequest(new ScanPolicy("foo", null, null), "foo");
            c.scanFile(req, new ByteArrayInputStream(new byte[1738]), 1738, null);
            fail("did not expect scan to succeed");
        } catch (IOException e) {
            fail("IOException during test: " + e.getMessage());
        }
    });
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) IOException(java.io.IOException) Dispatcher(okhttp3.mockwebserver.Dispatcher) NotNull(org.jetbrains.annotations.NotNull) ByteArrayInputStream(java.io.ByteArrayInputStream) ScanPolicy(ai.nightfall.scan.model.ScanPolicy) MockWebServer(okhttp3.mockwebserver.MockWebServer) ScanFileRequest(ai.nightfall.scan.model.ScanFileRequest) Test(org.junit.jupiter.api.Test)

Example 5 with ScanFileRequest

use of ai.nightfall.scan.model.ScanFileRequest in project nightfall-java-sdk by nightfallai.

the class NightfallClientTest method testScanFile_HappyPath.

@Test
public void testScanFile_HappyPath() {
    try (MockWebServer server = new MockWebServer()) {
        server.enqueue(new MockResponse().setBody("{\"id\": \"2eda1019-f991-4535-be9f-cecbe6b6c2eb\"," + "\"fileSizeBytes\": 1738, \"mimeType\": \"text/plain\", \"chunkSize\": 10485760}"));
        server.enqueue(new MockResponse().setResponseCode(204));
        server.enqueue(new MockResponse().setBody("{\"id\": \"2eda1019-f991-4535-be9f-cecbe6b6c2eb\"," + "\"fileSizeBytes\": 1738, \"mimeType\": \"text/plain\", \"chunkSize\": 10485760}"));
        server.enqueue(new MockResponse().setBody("{\"id\": \"2eda1019-f991-4535-be9f-cecbe6b6c2eb\"," + "\"message\": \"scan initiated\"}"));
        ScanFileResponse expectedResponse = new ScanFileResponse(UUID.fromString("2eda1019-f991-4535-be9f-cecbe6b6c2eb"), "scan initiated");
        NightfallClient c = new NightfallClient(getRequestURL(server), "key", 1, getHttpClient());
        ScanFileRequest req = new ScanFileRequest(new ScanPolicy("foo", null, null), "foo");
        ScanFileResponse resp = c.scanFile(req, new ByteArrayInputStream(new byte[1738]), 1738, null);
        assertEquals(expectedResponse.getId(), resp.getId());
        assertEquals(expectedResponse.getMessage(), resp.getMessage());
    } catch (IOException e) {
        fail("IOException during test: " + e.getMessage());
    }
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) ScanFileResponse(ai.nightfall.scan.model.ScanFileResponse) ByteArrayInputStream(java.io.ByteArrayInputStream) ScanPolicy(ai.nightfall.scan.model.ScanPolicy) MockWebServer(okhttp3.mockwebserver.MockWebServer) IOException(java.io.IOException) ScanFileRequest(ai.nightfall.scan.model.ScanFileRequest) Test(org.junit.jupiter.api.Test)

Aggregations

ScanFileRequest (ai.nightfall.scan.model.ScanFileRequest)6 Test (org.junit.jupiter.api.Test)5 ScanPolicy (ai.nightfall.scan.model.ScanPolicy)4 ByteArrayInputStream (java.io.ByteArrayInputStream)4 IOException (java.io.IOException)4 MockResponse (okhttp3.mockwebserver.MockResponse)3 MockWebServer (okhttp3.mockwebserver.MockWebServer)3 ScanFileResponse (ai.nightfall.scan.model.ScanFileResponse)2 NightfallClient (ai.nightfall.scan.NightfallClient)1 DetectionRule (ai.nightfall.scan.model.DetectionRule)1 Detector (ai.nightfall.scan.model.Detector)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 InputStream (java.io.InputStream)1 Duration (java.time.Duration)1 Dispatcher (okhttp3.mockwebserver.Dispatcher)1 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)1 NotNull (org.jetbrains.annotations.NotNull)1