JAVA-SDK
Overview
This document mainly introduces the installation and use of the Java SDK for the certificate management module.
Install SDK Toolkit
Operating environment
The Java SDK toolkit can run under the environment of jdk1.8 or higher.
Method 1: Install with Maven
Add the bce-java-sdk dependency in Maven's pom.xml file:
<dependency>
<groupId>com.baidubce</groupId>
<artifactId>bce-java-sdk</artifactId>
<version>{version}</version>
</dependency>
Where, {version}
is version number which can be found in SDK Download Page
Method 2: Install directly using JAR package
- Download the Java SDK compression toolkit at Official Website.
- Unzip the downloaded
bce-java-sdk-version.zip
and copy it to the project folder. - Right-click "Project > Properties > Java Build Path > Add JARs" in Eclipse.
- Add SDK toolkit
lib/bce-java-sdk-version.jar
and third-party dependency toolkitthird-party/*. Jar
. Among them,version
is the version number.
SDK's directory structure
com.baidubce
├── auth // BCE's signature related class
├── http / / BCE's Http communication related class
├── internal // SDK's internal class
├── model // BCE's public model class
├── services
│ └── cert // Certificate Management Services Related Class
│ ├── model // Certificate management service internal model, such as Request or Response
│ └── CertClient.class // Certificate Management Service Client Entry Class
├── util // BCE's utility tool class
├── BceClientConfiguration.class // Configuration of BCE's HttpClient
├── BceClientException.class // Exception class of BCE's client
├── BceServiceException.class // The exception class after interacting with the BCE's server
├── ErrorCode.class // BCE's general error code
└── Region.class // BCE's service region
Create CertClient
The user can refer to the following code to create a new CertClient, which needs to pass in AKSK and endpoint (url of the certificate service)
String endpoint = "https://certificate.baidubce.com"; // Service url
String accessKey = "your-access-key-id"; // User ak
String secretKey = "your-secret-access-key"; // User sk
CertClient certClient = CertClient.createCertClient(accessKey, secretKey, endpoint);
Method List
1. Create Certificate
Request parameter
Parameter name | Parameter Type | Required or not | Description |
---|---|---|---|
certName | String | Required | Name of the certificate. The length is limited to 1-65 characters. It starts with a letter and only contains letters, numbers, ’-‘, ‘/‘, ‘.‘, '', Java regular expressions ^ [a-zA-Z]a-zA-Z0-9 \-/ \.]{2,64} $ |
certServerData | String | Required | Data content of the server certificate (Base64 encoded) |
certPrivateData | String | Maybe require | The private key data content of the certificate (Base64 encoding). Required when the certificate type is 1. |
certLinkData | String | Optional | Certificate chain data content (Base64 encoded) |
certType | Integer | Optional | Certificate type, 1 for server certificate, 2 for client certificate, the default is 1 |
Return value: CertCreateResponse
Attribute name | Attribute type | Description |
---|---|---|
certId | String | Certificate id |
certName | String | Certificate name |
Reference Code
// Prepare parameters
String certName = "Your-certificate-name";
String certServerData = "Your-certificate-server-data";
String certPrivateData = "Your-certificate-private-data";
String certLinkData = "Your-certificate ";
// Construct request
CertCreateRequest request = new CertCreateRequest();
request.setCertName(certName);
request.setCertPrivateData(certPrivateData);
request.setCertServerData(certServerData);
request.setCertLinkData(certLinkData);
// Make a request to get the results
CertCreateResponse createResponse = certClient.createCert(request);
Possible exception
Exception code | Description |
---|---|
CertExceedLimit (409) | Exceed the maximum number of users |
UnmatchedPairParameterInvalidException (400) | Certificate validity time does not include current time |
PrivateKeyParameterInvalid (400) | Private key parsing exception |
CertificateParameterInvalid (400) | Certificate parsing exception |
CertChainParameterInvalid (400) | Certificate chain parsing exception |
UnmatchedPairParameterInvalid (400) | Public key and private key do not match |
2. Get the Certificate List
Request parameter: None
Return value: CertCreateResponse
Attribute name | Attribute type | Description |
---|---|---|
certs | List<CertificateMeta> | List of certificate information |
CertificateMeta
Attribute name | Attribute type | Description |
---|---|---|
certId | String | Certificate ID |
certName | String | Certificate name |
certCommonName | String | Certificate Common Name |
certStartTime | DateTime | Certificate effective time |
certStopTime | DateTime | Certificate expiration time |
certCreateTime | DateTime | Certificate creation time |
certUpdateTime | DateTime | Certificate renewal time |
Reference Code
// Make a request to get the results
CertListResponse listResponse = certClient.listUserCerts();
3. Get the Information of a Single Certificate (not including the certificate public key and private key information)
Request parameter
Parameter name | Parameter Type | Required or not | Description |
---|---|---|---|
certId | String | Required | Certificate id |
Return value: CertificateMeta
Attribute name | Attribute type | Description |
---|---|---|
certId | String | Certificate ID |
certName | String | Certificate name |
certCommonName | String | Certificate Common Name |
certStartTime | DateTime | Certificate effective time |
certStopTime | DateTime | Certificate expiration time |
certCreateTime | DateTime | Certificate creation time |
certUpdateTime | DateTime | Certificate renewal time |
Reference Code
// Prepare parameters
String certId = "Your-cert-id";
// Make a request to get the results
CertificateMeta certificateMeta = certClient.getCertInfo(certId);
4. Rename the Certificate
Request parameter
Parameter name | Parameter Type | Required or not | Description |
---|---|---|---|
certName | String | Required | Name of the certificate. The length is limited to 1-65 characters. It starts with a letter and only contains letters, numbers, ‘-‘, ‘/‘, ‘.‘, '', Java regular expressions ^ [a-zA-Z]a-zA-Z0-9 \-/ \.]{2,64} $ |
certId | String | Required | Certificate id |
Return value: CertInServiceListResponse
Attribute name | Attribute type | Description |
---|---|---|
certId | String | Certificate id |
certName | String | Certificate name |
Reference Code
// Prepare parameters
String certId = "Your-cert-id";
String newName = "Cert-new-name";
// Make a request
certClient.updateCertName (certId, newName);
Possible exception
Exception code | Description |
---|---|
AccessDeniedException (403) | No access |
ResourceNotFoundException (404) | Certificate does not exist |
5. Delete Certificate
Request parameter
Parameter name | Parameter Type | Required or not | Description |
---|---|---|---|
certId | String | Required | Certificate id |
Return value: None
Reference Code
// Prepare parameters
String certId = "Your-cert-id";
// Make a request
certClient.delete (certId);
Possible exception
Exception code | Description |
---|---|
OperationNotAllowedException (409) | Certificate in use |
AccessDeniedException (403) | No access |
ResourceNotFoundException (404) | Certificate does not exist |
6. Replace Certificate
Request parameter
Parameter name | Parameter Type | Required or not | Description |
---|---|---|---|
certId | String | Required | Certificate id |
certName | String | Required | Name of the certificate. The length is limited to 1-65 characters. It starts with a letter and only contains letters, numbers, ‘-‘, ‘/‘, ‘.‘, '', Java regular expressions ^ [a-zA-Z]a-zA-Z0-9 \-/ \.]{2,64} $ |
certServerData | String | Required | Data content of the server certificate (Base64 encoded) |
certPrivateData | String | Maybe require | The private key data content of the certificate (Base64 encoding). Required when the certificate type is 1. |
certLinkData | String | Optional | Certificate chain data content (Base64 encoded) |
certType | Integer | Optional | Certificate type, 1 for server certificate, 2 for client certificate, the default is 1 |
Return value: None
Reference Code
// Prepare parameters
String certId = "Your-certificate-id";
String certName = "Your-certificate-name";
String certServerData = "Your-certificate-server-data";
String certPrivateData = "Your-certificate-private-data";
String certLinkData = "Your-certificate ";
// Construct request
CertCreateRequest request = new CertCreateRequest();
request.setCertName(certName);
request.setCertPrivateData(certPrivateData);
request.setCertServerData(certServerData);
request.setCertLinkData(certLinkData);
// Make a request
certClient.replaceCertData(certId, request);
Possible exception
Exception code | Description |
---|---|
OperationNotAllowedException (409) | Certificate in use |
AccessDeniedException (403) | No access |
ResourceNotFoundException (404) | Certificate does not exist |
UnmatchedPairParameterInvalidException (400) | Certificate validity time does not include current time |
PrivateKeyParameterInvalid (400) | Private key parsing exception |
CertificateParameterInvalid (400) | Certificate parsing exception |
CertChainParameterInvalid (400) | Certificate chain parsing exception |
UnmatchedPairParameterInvalid (400) | Public key and private key do not match |
Version Description
v1.0.0
First release.