From 32025b6076482252afc4f88d5db48374f5e37e8d Mon Sep 17 00:00:00 2001 From: Neenu Shaji Date: Tue, 15 Dec 2020 17:10:04 -0500 Subject: [PATCH] samples: create Java sample for Search API and Submit API in Webrisk (#306) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixes #153 ☕️ --- .../main/java/webrisk/SearchUriExample.java | 54 +++++++++++++++++++ .../main/java/webrisk/SubmitUriExample.java | 48 +++++++++++++++++ .../java/webrisk/SearchUriExampleTest.java | 47 ++++++++++++++++ .../java/webrisk/SubmitUriExampleTest.java | 32 +++++++++++ 4 files changed, 181 insertions(+) create mode 100644 webrisk/src/main/java/webrisk/SearchUriExample.java create mode 100644 webrisk/src/main/java/webrisk/SubmitUriExample.java create mode 100644 webrisk/src/test/java/webrisk/SearchUriExampleTest.java create mode 100644 webrisk/src/test/java/webrisk/SubmitUriExampleTest.java diff --git a/webrisk/src/main/java/webrisk/SearchUriExample.java b/webrisk/src/main/java/webrisk/SearchUriExample.java new file mode 100644 index 00000000000..2f052480743 --- /dev/null +++ b/webrisk/src/main/java/webrisk/SearchUriExample.java @@ -0,0 +1,54 @@ +/* + * Copyright 2020 Google LLC + * + * Licensed 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 webrisk; + +import com.google.cloud.webrisk.v1.WebRiskServiceClient; +import com.google.webrisk.v1.SearchUrisRequest; +import com.google.webrisk.v1.SearchUrisResponse; +import com.google.webrisk.v1.ThreatType; +import java.io.IOException; + +public class SearchUriExample { + + public static void searchUriExample() throws IOException { + //The URL to be searched + String uri = "http://testsafebrowsing.appspot.com/s/malware.html"; + SearchUrisResponse response = searchUriExample(uri); + } + + // [START webrisk_search_uri] + public static SearchUrisResponse searchUriExample(String uri) throws IOException { + //create-webrisk-client + try (WebRiskServiceClient webRiskServiceClient = WebRiskServiceClient.create()) { + //Query the url for a specific threat type + SearchUrisRequest searchUrisRequest = SearchUrisRequest.newBuilder() + .addThreatTypes(ThreatType.MALWARE) + .setUri(uri).build(); + SearchUrisResponse searchUrisResponse = webRiskServiceClient.searchUris(searchUrisRequest); + webRiskServiceClient.shutdownNow(); + if (!searchUrisResponse.getThreat().getThreatTypesList().isEmpty()) { + System.out.println("The URL has the following threat : "); + System.out.println(searchUrisResponse); + } else { + System.out.println("The URL is safe!"); + } + + return searchUrisResponse; + } + } + // [END webrisk_search_uri] +} diff --git a/webrisk/src/main/java/webrisk/SubmitUriExample.java b/webrisk/src/main/java/webrisk/SubmitUriExample.java new file mode 100644 index 00000000000..b389cd34868 --- /dev/null +++ b/webrisk/src/main/java/webrisk/SubmitUriExample.java @@ -0,0 +1,48 @@ +/* + * Copyright 2020 Google LLC + * + * Licensed 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 webrisk; + +import com.google.cloud.webrisk.v1.WebRiskServiceClient; +import com.google.webrisk.v1.CreateSubmissionRequest; +import com.google.webrisk.v1.Submission; +import java.io.IOException; + +public class SubmitUriExample { + + public static void submitUriExample() throws IOException { + //The URL to be submitted + String uri = "http://testsafebrowsing.appspot.com/s/malware.html"; + Submission response = submitUriExample(uri); + } + + // [START webrisk_submit_uri] + public static Submission submitUriExample(String uri) throws IOException { + //create-webrisk-client + try (WebRiskServiceClient webRiskServiceClient = WebRiskServiceClient.create()) { + Submission submission = Submission.newBuilder() + .setUri(uri).build(); + CreateSubmissionRequest submissionRequest = CreateSubmissionRequest.newBuilder() + .setParent("projects/your-project-id").setSubmission(submission).build(); + Submission submissionResponse = webRiskServiceClient.createSubmission(submissionRequest); + webRiskServiceClient.shutdownNow(); + System.out.println("The submitted " + submissionResponse); + return submissionResponse; + } + + } + // [END webrisk_submit_uri] +} diff --git a/webrisk/src/test/java/webrisk/SearchUriExampleTest.java b/webrisk/src/test/java/webrisk/SearchUriExampleTest.java new file mode 100644 index 00000000000..30237f53b8a --- /dev/null +++ b/webrisk/src/test/java/webrisk/SearchUriExampleTest.java @@ -0,0 +1,47 @@ +/* + * Copyright 2020 Google LLC + * + * Licensed 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 webrisk; + +import com.google.common.truth.Truth; +import com.google.webrisk.v1.SearchUrisResponse; +import com.google.webrisk.v1.ThreatType; +import java.io.IOException; +import java.util.List; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +public class SearchUriExampleTest { + @Test + public void testSearchWithThreat() throws IOException { + //The URL to be searched + String uri = "http://testsafebrowsing.appspot.com/s/malware.html"; + SearchUrisResponse actualResponse = SearchUriExample.searchUriExample(uri); + List type = actualResponse.getThreat().getThreatTypesList(); + Truth.assertThat(type).contains(ThreatType.MALWARE); + } + + @Test + public void testSearchWithoutThreat() throws IOException { + //The URL to be searched + String uri = "http://testsafebrowsing.appspot.com/malware.html"; + SearchUrisResponse actualResponse = SearchUriExample.searchUriExample(uri); + List type = actualResponse.getThreat().getThreatTypesList(); + Truth.assertThat(type).isEmpty(); + } +} diff --git a/webrisk/src/test/java/webrisk/SubmitUriExampleTest.java b/webrisk/src/test/java/webrisk/SubmitUriExampleTest.java new file mode 100644 index 00000000000..1836e12fd9a --- /dev/null +++ b/webrisk/src/test/java/webrisk/SubmitUriExampleTest.java @@ -0,0 +1,32 @@ +/* + * Copyright 2020 Google LLC + * + * Licensed 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 webrisk; + +import com.google.common.truth.Truth; +import com.google.webrisk.v1.Submission; +import java.io.IOException; +import org.junit.Test; + +public class SubmitUriExampleTest { + @Test + public void testSumbitUriExample() throws IOException { + String testUri = "http://testsafebrowsing.appspot.com/s/malware.html"; + Submission actualSubmission = SubmitUriExample.submitUriExample(testUri); + Truth.assertThat(actualSubmission.getUri()).isEqualTo(testUri); + } +}