Skip to content

Commit 32025b6

Browse files
authored
samples: create Java sample for Search API and Submit API in Webrisk (#306)
Fixes #153 ☕️
1 parent 3c5a4c0 commit 32025b6

File tree

4 files changed

+181
-0
lines changed

4 files changed

+181
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* Copyright 2020 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package webrisk;
18+
19+
import com.google.cloud.webrisk.v1.WebRiskServiceClient;
20+
import com.google.webrisk.v1.SearchUrisRequest;
21+
import com.google.webrisk.v1.SearchUrisResponse;
22+
import com.google.webrisk.v1.ThreatType;
23+
import java.io.IOException;
24+
25+
public class SearchUriExample {
26+
27+
public static void searchUriExample() throws IOException {
28+
//The URL to be searched
29+
String uri = "http://testsafebrowsing.appspot.com/s/malware.html";
30+
SearchUrisResponse response = searchUriExample(uri);
31+
}
32+
33+
// [START webrisk_search_uri]
34+
public static SearchUrisResponse searchUriExample(String uri) throws IOException {
35+
//create-webrisk-client
36+
try (WebRiskServiceClient webRiskServiceClient = WebRiskServiceClient.create()) {
37+
//Query the url for a specific threat type
38+
SearchUrisRequest searchUrisRequest = SearchUrisRequest.newBuilder()
39+
.addThreatTypes(ThreatType.MALWARE)
40+
.setUri(uri).build();
41+
SearchUrisResponse searchUrisResponse = webRiskServiceClient.searchUris(searchUrisRequest);
42+
webRiskServiceClient.shutdownNow();
43+
if (!searchUrisResponse.getThreat().getThreatTypesList().isEmpty()) {
44+
System.out.println("The URL has the following threat : ");
45+
System.out.println(searchUrisResponse);
46+
} else {
47+
System.out.println("The URL is safe!");
48+
}
49+
50+
return searchUrisResponse;
51+
}
52+
}
53+
// [END webrisk_search_uri]
54+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Copyright 2020 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package webrisk;
18+
19+
import com.google.cloud.webrisk.v1.WebRiskServiceClient;
20+
import com.google.webrisk.v1.CreateSubmissionRequest;
21+
import com.google.webrisk.v1.Submission;
22+
import java.io.IOException;
23+
24+
public class SubmitUriExample {
25+
26+
public static void submitUriExample() throws IOException {
27+
//The URL to be submitted
28+
String uri = "http://testsafebrowsing.appspot.com/s/malware.html";
29+
Submission response = submitUriExample(uri);
30+
}
31+
32+
// [START webrisk_submit_uri]
33+
public static Submission submitUriExample(String uri) throws IOException {
34+
//create-webrisk-client
35+
try (WebRiskServiceClient webRiskServiceClient = WebRiskServiceClient.create()) {
36+
Submission submission = Submission.newBuilder()
37+
.setUri(uri).build();
38+
CreateSubmissionRequest submissionRequest = CreateSubmissionRequest.newBuilder()
39+
.setParent("projects/your-project-id").setSubmission(submission).build();
40+
Submission submissionResponse = webRiskServiceClient.createSubmission(submissionRequest);
41+
webRiskServiceClient.shutdownNow();
42+
System.out.println("The submitted " + submissionResponse);
43+
return submissionResponse;
44+
}
45+
46+
}
47+
// [END webrisk_submit_uri]
48+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Copyright 2020 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package webrisk;
18+
19+
import com.google.common.truth.Truth;
20+
import com.google.webrisk.v1.SearchUrisResponse;
21+
import com.google.webrisk.v1.ThreatType;
22+
import java.io.IOException;
23+
import java.util.List;
24+
import org.junit.Test;
25+
import org.junit.runner.RunWith;
26+
import org.junit.runners.JUnit4;
27+
28+
@RunWith(JUnit4.class)
29+
public class SearchUriExampleTest {
30+
@Test
31+
public void testSearchWithThreat() throws IOException {
32+
//The URL to be searched
33+
String uri = "http://testsafebrowsing.appspot.com/s/malware.html";
34+
SearchUrisResponse actualResponse = SearchUriExample.searchUriExample(uri);
35+
List<ThreatType> type = actualResponse.getThreat().getThreatTypesList();
36+
Truth.assertThat(type).contains(ThreatType.MALWARE);
37+
}
38+
39+
@Test
40+
public void testSearchWithoutThreat() throws IOException {
41+
//The URL to be searched
42+
String uri = "http://testsafebrowsing.appspot.com/malware.html";
43+
SearchUrisResponse actualResponse = SearchUriExample.searchUriExample(uri);
44+
List<ThreatType> type = actualResponse.getThreat().getThreatTypesList();
45+
Truth.assertThat(type).isEmpty();
46+
}
47+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* Copyright 2020 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
18+
package webrisk;
19+
20+
import com.google.common.truth.Truth;
21+
import com.google.webrisk.v1.Submission;
22+
import java.io.IOException;
23+
import org.junit.Test;
24+
25+
public class SubmitUriExampleTest {
26+
@Test
27+
public void testSumbitUriExample() throws IOException {
28+
String testUri = "http://testsafebrowsing.appspot.com/s/malware.html";
29+
Submission actualSubmission = SubmitUriExample.submitUriExample(testUri);
30+
Truth.assertThat(actualSubmission.getUri()).isEqualTo(testUri);
31+
}
32+
}

0 commit comments

Comments
 (0)