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();
}
}
}
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());
}
});
}
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");
});
}
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());
}
});
}
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());
}
}
Aggregations