Cloud Firewall SDK for Java lets you manage firewall policies programmatically. This guide covers installing the SDK, initializing a client, and making your first API call using the AddAddressBook operation as an example.
Prerequisites
Before you begin, ensure that you have:
An AccessKey pair for a RAM user (not the Alibaba Cloud root account — a leaked root AccessKey exposes all your resources). For instructions, see Create an AccessKey pair.
One of the following policies granted to the RAM user: To grant custom permissions instead, see Create custom policies and RAM authorization.
Policy Access level AliyunYundunCloudFirewallFullAccessFull management permissions on Cloud Firewall AliyunYundunCloudFirewallReadOnlyAccessRead-only permissions on Cloud Firewall The AccessKey pair stored in environment variables: For setup instructions, see Configure environment variables in Linux, macOS, and Windows.
Environment variable Description ALIBABA_CLOUD_ACCESS_KEY_IDYour RAM user's AccessKey ID ALIBABA_CLOUD_ACCESS_KEY_SECRETYour RAM user's AccessKey secret
Install the SDK
Add the Maven repository to your Maven configuration file:
<repositories> <repository> <id>sonatype-nexus-staging</id> <name>Sonatype Nexus Staging</name> <url>https://oss.sonatype.org/service/local/staging/deploy/maven2/</url> <releases> <enabled>true</enabled> </releases> <snapshots> <enabled>true</enabled> </snapshots> </repository> </repositories>Add the following dependency to the
<dependencies>section of yourpom.xml, then refresh your Maven configuration:<dependency> <groupId>com.aliyun</groupId> <artifactId>domain20180129</artifactId> <version>3.16.0</version> </dependency>
Use the SDK
Step 1: Initialize the client
The SDK supports multiple credential types, including AccessKey pairs and Security Token Service (STS) tokens. This example uses an AccessKey pair. For other credential options, see Manage access credentials.
package com.aliyun.sample;
public class Sample {
public static com.aliyun.cloudfw20171207.Client createClient() throws Exception {
com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
// Read credentials from environment variables to avoid hardcoding sensitive values.
.setAccessKeyId(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"))
.setAccessKeySecret(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"));
// The Cloud Firewall endpoint. For a full list of endpoints, see https://api.aliyun.com/product/Cloudfw.
config.endpoint = "cloudfw.aliyuncs.com";
return new com.aliyun.cloudfw20171207.Client(config);
}
}Step 2: Construct a request object
Request objects follow the naming pattern {OperationName}Request. For AddAddressBook, the request object is AddAddressBookRequest.
Before constructing a request, check the operation's parameters in List of operations by function.
com.aliyun.cloudfw20171207.models.AddAddressBookRequest addAddressBookRequest =
new com.aliyun.cloudfw20171207.models.AddAddressBookRequest()
.setLang("your_value")
.setAddressList("your_value")
.setDescription("your_value");Step 3: Call the operation
Response objects follow the naming pattern {OperationName}Response. For AddAddressBook, the response object is AddAddressBookResponse.
// Pass runtime parameters to control retry behavior and timeouts.
com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
com.aliyun.cloudfw20171207.models.AddAddressBookResponse resp =
client.addAddressBookWithOptions(addAddressBookRequest, runtime);Step 4: Handle exceptions
The SDK raises two exception types:
| Exception type | Cause | When it occurs |
|---|---|---|
TeaUnretryableException | Network error | After the maximum number of retries is reached |
TeaException | Business error | When the API returns an error response |
try {
com.aliyun.cloudfw20171207.Client client = Sample.createClient();
com.aliyun.cloudfw20171207.models.AddAddressBookRequest addAddressBookRequest =
new com.aliyun.cloudfw20171207.models.AddAddressBookRequest()
.setLang("your_value")
.setAddressList("your_value")
.setDescription("your_value");
com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
com.aliyun.cloudfw20171207.models.AddAddressBookResponse resp =
client.addAddressBookWithOptions(addAddressBookRequest, runtime);
} catch (TeaException error) {
// Log the error details and troubleshooting link for diagnosis.
System.out.println(error.getMessage());
System.out.println(error.getData().get("Recommend"));
com.aliyun.teautil.Common.assertAsString(error.message);
} catch (Exception _error) {
TeaException error = new TeaException(_error.getMessage(), _error);
System.out.println(error.getMessage());
System.out.println(error.getData().get("Recommend"));
com.aliyun.teautil.Common.assertAsString(error.message);
}In production, always handle exceptions explicitly — log the error, report it to your monitoring system, or implement retry logic. Do not silently ignore exceptions.
Complete sample code
package com.aliyun.sample;
import com.aliyun.tea.*;
public class Sample {
public static com.aliyun.cloudfw20171207.Client createClient() throws Exception {
com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
// Read credentials from environment variables to avoid hardcoding sensitive values.
.setAccessKeyId(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"))
.setAccessKeySecret(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"));
// The Cloud Firewall endpoint. For a full list of endpoints, see https://api.aliyun.com/product/Cloudfw.
config.endpoint = "cloudfw.aliyuncs.com";
return new com.aliyun.cloudfw20171207.Client(config);
}
public static void main(String[] args_) throws Exception {
try {
com.aliyun.cloudfw20171207.Client client = Sample.createClient();
com.aliyun.cloudfw20171207.models.AddAddressBookRequest addAddressBookRequest =
new com.aliyun.cloudfw20171207.models.AddAddressBookRequest()
.setLang("your_value")
.setAddressList("your_value")
.setDescription("your_value");
com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
com.aliyun.cloudfw20171207.models.AddAddressBookResponse resp =
client.addAddressBookWithOptions(addAddressBookRequest, runtime);
com.aliyun.teaconsole.Client.log(com.aliyun.teautil.Common.toJSONString(resp));
} catch (TeaException error) {
System.out.println(error.getMessage());
System.out.println(error.getData().get("Recommend"));
com.aliyun.teautil.Common.assertAsString(error.message);
} catch (Exception _error) {
TeaException error = new TeaException(_error.getMessage(), _error);
System.out.println(error.getMessage());
System.out.println(error.getData().get("Recommend"));
com.aliyun.teautil.Common.assertAsString(error.message);
}
}
}What's next
Browse all available operations: List of operations by function
Explore other credential options: Manage access credentials
Manage RAM permissions: RAM authorization