This topic describes how to use Content Moderation SDK for Java to query existing and custom business scenarios. This helps you manage business scenarios.

Before you begin

Before you call operations, make the following preparations:
  • Create an AccessKey pair for your Alibaba Cloud account. For more information, see Create an AccessKey pair.
  • Install Java dependencies. For more information, see Installation.
  • Optional:Download and import the Extension.Uploader utility class into your project if you submit an on-premises image or a binary image stream for image moderation.

Query business scenarios

Operation Description Supported region
DescribeUserBizTypes Queries business scenarios.
  • cn-shanghai: China (Shanghai)
  • cn-beijing: China (Beijing)
  • cn-shenzhen: China (Shenzhen)
  • ap-southeast-1: Singapore (Singapore)

Sample code

// Import the required classes. 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.green.model.v20170823.DescribeUserBizTypesRequest;
import com.aliyuncs.green.model.v20170823.DescribeUserBizTypesResponse;
import com.aliyuncs.green.model.v20170823.DescribeUserBizTypesResponse.Item;
import com.aliyuncs.http.FormatType;
import com.aliyuncs.http.HttpResponse;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import org.apache.commons.collections.CollectionUtils;

/**
 * Use the following code to query business scenarios. 
 */
public class DescribeBizTypesRequestSample extends BaseSample {

    public static void main(String[] args) throws Exception {
        // Use the AccessKey ID and AccessKey secret of your Alibaba Cloud account. 
        IClientProfile profile = DefaultProfile
            .getProfile("cn-shanghai", "Your AccessKey ID", "Your AccessKey secret");
        DefaultProfile
            .addEndpoint("cn-shanghai", "cn-shanghai", "Green", "green.cn-shanghai.aliyuncs.com");
        IAcsClient client = new DefaultAcsClient(profile);

        DescribeUserBizTypesRequest describeUserBizTypesRequest = new DescribeUserBizTypesRequest();
        // Specify the response format of the operation. 
        describeUserBizTypesRequest.setAcceptFormat(FormatType.JSON); 
        // Specify the request method. 
        describeUserBizTypesRequest.setSysMethod(MethodType.GET); 
        describeUserBizTypesRequest.setSysEncoding("utf-8");
        // Specify the connection timeout. You can modify the timeout period as needed. Unit: milliseconds. 
        describeUserBizTypesRequest.setSysConnectTimeout(3000);
        // Specify the read timeout. You can modify the timeout period as needed. Unit: milliseconds. 
        describeUserBizTypesRequest.setSysReadTimeout(6000);

        try {
            HttpResponse httpResponse = client.doAction(describeUserBizTypesRequest);
            // Check whether the HTTP request is successful. 
            if (httpResponse.isSuccess()) {

                DescribeUserBizTypesResponse describeUserBizTypesResponse = JSON.parseObject(new String(httpResponse.getHttpContent(), "UTF-8"), DescribeUserBizTypesResponse.class);
                // The list of all business scenarios. 
                System.out.println("query success. bizTypes size :" + describeUserBizTypesResponse.getBizTypeList().size());
                if (CollectionUtils.isNotEmpty(describeUserBizTypesResponse.getBizTypeList())) {
                    for (Item bizTypeItem: describeUserBizTypesResponse.getBizTypeList()) {
                        // The name of the business scenario. 
                        System.out.println(bizTypeItem.getBizType());
                        // Indicates whether an industry template was imported. 
                        System.out.println(bizTypeItem.getCiteTemplate());
                        // The industry information. 
                        System.out.println(bizTypeItem.getIndustryInfo());
                        // The source of the business scenario. A value of custom indicates a custom business scenario. A value of system indicates the default business scenario of Content Moderation. 
                        System.out.println(bizTypeItem.getSource());                
                    }
                }
                // When you create a business scenario, you can import the configuration of an existing business scenario in the list of queried business scenarios. 
                System.out.println("query success. import bizTypes size :" + describeUserBizTypesResponse.getBizTypeList().size());
            } else {
                JSONObject scrResponse = JSON.parseObject(new String(httpResponse.getHttpContent(), "utf-8"));
                // Check the error message. 
                System.out.println("query fail. detail msg: " + JSON.toJSONString(scrResponse.get("Message"), true));
            }
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}